Introduction
Working with Virtual Machines (VMs) is an essential skill for anyone in the IT industry, from software development and system testing to operating services on servers.
During this process, one of the challenges we frequently face is the need to convert virtual machine disk formats. For example, you might need to migrate a VM from VMware to Proxmox, or from VirtualBox to a more flexible KVM/QEMU platform.
Personally, I operate a homelab with Proxmox VE, managing over 12 VMs and containers. This is an ideal playground for me to test everything before deploying to a production environment. During this process, disk format conversion occurs frequently.
Sometimes it’s to optimize performance with QCOW2, other times it’s to ensure compatibility with different hypervisors. Many tools can do this, but qemu-img stands out due to its power, flexibility, and widespread use in the Linux community.
So, how can you efficiently and safely convert popular formats like VMDK, QCOW2, VDI, or VHD? Don’t worry, this guide will help you, especially beginners, master the use of qemu-img to thoroughly solve this problem.
Core Concepts
Common Virtual Machine Disk Formats
Before diving into practice, let’s review the main virtual machine disk formats that will be covered:
- VMDK (VMware Virtual Disk Format): VMware’s proprietary format, a pioneer in virtualization.
.vmdkfiles are very familiar to anyone working with VMware Workstation, ESXi, or vSphere. - QCOW2 (QEMU Copy-On-Write Version 2): This is the default and recommended format when using QEMU/KVM. It possesses many useful features such as snapshots, thin provisioning (allocating disk space only when needed), and data compression. I highly favor this format in my homelab for its flexibility on Proxmox.
- VDI (VirtualBox Disk Image): The proprietary disk format of Oracle VirtualBox. If you frequently use VirtualBox on your personal computer to run guest operating systems, you will be familiar with
.vdifiles. - VHD (Virtual Hard Disk) / VHDX: Microsoft’s format, primarily used on Hyper-V in Windows Server and Windows clients. VHDX is an upgraded version of VHD, supporting larger capacities (up to 64TB) and better fault recovery capabilities.
What is the qemu-img tool?
qemu-img is a command-line utility that comes with QEMU, an open-source emulator and virtualizer. Although QEMU is known for its ability to run guest operating systems on Linux hosts, the qemu-img utility specializes in managing virtual disk image files. It can create, check, modify, and most importantly, convert between different virtual disk formats.
The strength of qemu-img lies in its multi-format support and its ability to operate independently of your hypervisor. This makes it an ideal tool for easily migrating virtual machines between different virtualization environments.
Detailed Practice
Preparation before conversion
For a smooth and safe conversion, you need to perform some important preparatory steps:
- Install QEMU Utils: Ensure that you have installed the
qemu-utilspackage (on Debian/Ubuntu) orqemu-img(on RHEL/CentOS) on your Linux system. This package contains theqemu-imgtool we need.# For Debian/Ubuntu sudo apt update sudo apt install qemu-utils # For CentOS/RHEL sudo yum install qemu-img - Backup the original disk: This is the most important step! You absolutely must back up the original virtual machine disk file before performing any conversion operations. If an error occurs, you can always revert to a safe original copy.
cp original.vmdk original.vmdk.bak - Ensure sufficient disk space: The conversion process creates a new disk file. Make sure you have enough free space on your hard drive to accommodate it. Note that you may need twice the size of the original file if converting from thin provisioning to thick provisioning.
- Shut down the virtual machine: Absolutely do not perform the conversion while the virtual machine is running or in a suspended state. Completely shut down the virtual machine before starting the operation.
Basic command syntax of qemu-img convert
The qemu-img convert command has the following basic syntax:
qemu-img convert -f <source_format> -O <output_format> <source_file> <output_file>
-f <source_format>: Specifies the format of the original disk file (input format). Examples:vmdk,qcow2,vdi,vhd.-O <output_format>: Specifies the format you want to convert to (output format). Examples:qcow2,vmdk,vdi,vhd. The ‘O’ here stands for Output.<source_file>: Path to the original virtual machine disk file.<output_file>: Path and filename of the virtual machine disk after conversion.
Common conversion scenarios
Converting VMDK to QCOW2
This is one of the most common scenarios for me, especially when needing to migrate a virtual machine from VMware to Proxmox VE or KVM. QCOW2 offers good performance and supports many essential features for me.
qemu-img convert -f vmdk -O qcow2 my_vm_disk.vmdk my_vm_disk.qcow2
Converting QCOW2 to VMDK
If you need to move in the opposite direction, for example, preparing a QEMU/KVM disk to run on VMware Workstation, this command will help you:
qemu-img convert -f qcow2 -O vmdk my_vm_disk.qcow2 my_vm_disk.vmdk
Converting VDI to QCOW2
When you want to move a virtual machine from VirtualBox to Proxmox or a KVM host, converting from VDI to QCOW2 is necessary and common.
qemu-img convert -f vdi -O qcow2 virtualbox_vm.vdi virtualbox_vm.qcow2
Converting QCOW2 to VDI
Sometimes you might want to create a copy of a QEMU/KVM VM for temporary use on VirtualBox; this command will easily assist you.
qemu-img convert -f qcow2 -O vdi my_vm_disk.qcow2 my_vm_disk.vdi
Converting VHD to QCOW2
If you have a virtual machine from a Hyper-V environment and want to run it on KVM/Proxmox, convert it to QCOW2 to ensure compatibility.
qemu-img convert -f vhd -O qcow2 hyperv_vm.vhd hyperv_vm.qcow2
Converting QCOW2 to VHD
Similarly, to create a Hyper-V compatible disk from QCOW2, you can use the following command:
qemu-img convert -f qcow2 -O vhd my_vm_disk.qcow2 my_vm_disk.vhd
Some useful advanced options
qemu-img convert also has several other options to help you better control the conversion process:
- Show progress (
-p): For large disk files (e.g., several hundred GBs), monitoring progress is crucial to know how the process is unfolding and to estimate completion time.
qemu-img convert -p -f vmdk -O qcow2 my_vm_disk.vmdk my_vm_disk.qcow2
-c): This option allows compressing the destination disk file using the zlib algorithm, which significantly saves storage space. However, compression will reduce the I/O performance of the virtual machine when running, so it is generally not recommended for primary disks. It is more suitable for backups or archiving.qemu-img convert -c -f qcow2 -O qcow2 my_vm_disk.qcow2 compressed_my_vm_disk.qcow2
-o <option>=<value>): Some formats, especially QCOW2, can be created with specific options for optimization. For example, to create a QCOW2 file with a more optimal cluster_size or to set a backing_file for snapshots:
# Create QCOW2 with thin provisioning and cluster_size 128K
qemu-img convert -f vmdk -O qcow2 -o cluster_size=128K my_vm_disk.vmdk my_vm_disk_optimized.qcow2
Conclusion
qemu-img is an extremely useful and flexible tool that helps us effectively solve the problem of virtual machine disk format conversion.
I want to emphasize once again the importance of data backup before performing any operations on virtual machine disks. Safety is always the top priority!
Try out the commands above in your environment (remember to do it on a backup!). Familiarizing yourself with command-line tools like qemu-img will help you better control virtualization systems and become a more proficient IT Pro.
