How to convert a virtual disk between VMDK, VHD/VHDX, VDI and qcow2

Virtual Disk Trim product page & download

Moving a virtual machine to another hypervisor means converting its disk: VMDK for VMware, VHD or VHDX for Hyper-V, VDI for VirtualBox, qcow2 for QEMU, KVM and Proxmox. Other tools can do the conversion, but most of them don't reclaim the space that deleted files still occupy in the disk image. This guide shows how to convert and trim your virtual disks in one pass with Virtual Disk Trim.

Usual tools

qemu-img converts between all the common formats:

qemu-img convert -f vmdk -O vhdx server.vmdk server.vhdx

VirtualBox converts between VDI, VMDK and VHD:

VBoxManage clonemedium disk server.vmdk server.vdi --format VDI

Hyper-V converts between VHD and VHDX only:

Convert-VHD -Path server.vhd -DestinationPath server.vhdx

Disadvantage of the usual tools

These tools copy the disk as it is. They can skip blocks that are all zeros, but they do not read the guest file system, so deleted but untrimmed data inside the guest is copied into the new image. To avoid that, the free space has to be zeroed inside the running guest first. On a Linux host, virt-sparsify can zero and convert in one run, but it may require up to twice the disk’s virtual size in free space for the process.

Convert and trim in one pass with Virtual Disk Trim

The command line tool of Virtual Disk Trim reads the guest file system from the image, writes only the in-use blocks in the new format, and then verifies the result against the source. The VM stays off and nothing needs to be zeroed.

vdtrim trim server.vmdk server.vhdx

The output file extension determines the format: .vhdx, .vhd, .vmdk, .vdi, .qcow2, or .img for raw. A few options are useful when migrating:

:: leave out page, swap and hibernation files as well
vdtrim trim --discard all desktop.vdi desktop.qcow2

:: convert a Hyper-V disk into a VMware disk split into 2 GB pieces
vdtrim trim --layout split server.vhdx server.vmdk

The source is left untouched. The new disk gets a new identity, as it would with any converter, and has to be attached to the virtual machine on the new hypervisor.

Which to choose

qemu-img is free and reads more formats, so it is a good tool for a plain copy.

Virtual Disk Trim is the better choice when you want the new disk to take only as much space as its data occupies, without starting the guest, for example when archiving an old VM, migrating over a slow link, or preparing a template or an appliance for distribution.

Caveats:

  • Conversion is done by the vdtrim command line tool. The Virtual Disk Trim graphical interface only trims and keeps each image in its original format.
  • Compressed qcow2 images, the stream-optimised disks inside an OVA, and flat ESXi disks are not read. Convert those with qemu-img first.
  • Delete snapshots before converting, and do not convert a base disk that snapshots or linked clones still depend on.
  • A converted disk still holds the guest’s old drivers. A Windows guest moved to KVM, for example, needs VirtIO drivers or a SATA controller to start.

Further reading