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

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. Free tools do this well. What they do not do is leave out the space that deleted files still occupy in the image. This guide shows the usual tools and how Virtual Disk Trim converts and trims in one pass.

The 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

What they carry along

These tools copy the disk as it is. They skip blocks that are all zeros, but they do not read the guest file system, so everything that was deleted inside the guest is copied into the new image as if it were data. To avoid that, the free space has to be zeroed inside the running guest first. On Linux, virt-sparsify can sparsify and convert in one run, though it may need up to twice the disk’s full size in temporary space.

Convert and trim in one pass

The command line tool of Virtual Disk Trim reads the guest file system from the image, writes only the blocks in use in the new format, and compares the result with the source. The VM stays off and nothing is zeroed.

vdtrim trim server.vmdk server.vhdx

The name of the output decides 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

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

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

Which to use

qemu-img is free and reads more variants, so it is the tool for a plain copy. Virtual Disk Trim is the better tool when the result should also be as small as the data in it, without starting the guest to prepare it: an old VM being archived, a template, an appliance to hand out, a migration over a slow link.

  • Conversion is done by the command line tool; the application window keeps each image in its own 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 leave alone 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