bookmark_borderConvert IMG (raw) to QCOW2

Most of you will know the Kernel-based virtual machine. It’s already included with the latest Linux kernels and it gives you full virtualization under Linux which provides the capability to run almost every x86 OS you want inside a virtual machine.
Some versions ago, if you created a new virtual machine in KVM, the virtual hard disk was a RAW .img container. The new container type is QCOW2 and one of it’s main features is to enable the snapshot functionality of KVM.
So this means, if you have virtual machines which have a IMG HDD attached, than you will not be able to create snapshots of this virtual machine. Luckily the KVM developers are providing tools, which helps you to convert existing IMG HDDs to QCOW2 HDDs.

The convert process

First of all, this will take some time and it depends of course on the size of the HDD. Also, you should shutdown the virtual machine so that the convert process has the standalone access on the HDD while converting. The following example would convert a .img HDD to a .qcow2 HDD:

qemu-img convert -f raw -O qcow2 /path/to/your/hdd/vm01.img /path/to/your/hdd/vm01.qcow2

To explain the command a litte bit more:

  • qemu-img is the command which should be executed
  • convert says qemu-img that we want to convert an existing HDD
  • the switch -f raw lets qemu-img know, that the existing format of the HDD is RAW (in this case with .img filename ending)
  • the -O qcow2 switch tells the qemu-img command that the destination HDD should be QCOW2
  • the first file is the exisiting raw HDD, the second one is the filename of the new QCOW2 type HDD

So, let us say we want to convert a raw HDD which is located in /var/lib/libvirt/images (standard path for new KVM machines) to a QCOW2 HDD:

qemu-img convert -f raw -O qcow2 /var/lib/libvirt/images/machine01.img /var/lib/libvirt/images/machine01.qcow2

After you have done this, you just have to change the path from your HDD in your virtual machine from the raw .img to the .qcow2 file. NOTE: The .img file is not deleted after the successful convert process. You have to do this on your own.
At the end, you should be able to create snapshots for your virtual machine. One of the best features while using virtual machines at all 😉

Further links