Optimize KVM / Qemu Disk Images the Easy Way!

Is Your virtual machine sluggish?  Have Your disk images grown in size?  Are Disk space errors like the one below popping up?

Low Disk Space Warning.

Low Disk Space Warning.

Alright, Lets take a look there must be something wrong with the  free space reporting.  That’s the only thing that makes sense..  Right?

Lets run a quick check using the following command to make sure  “df -h” without the quotes


Output of df -h, on a full disk.

Output of df -h, on a full disk. Using Ubuntu 12.02

WTF, the disk is full?!  How did this happen?  You only have a couple of virtual machines, and they are no where near that big.

So after checking the file sizes on the host, and the disk usage inside the virtual machines, You find that the the the disk file sizes on the host are maxed out, but the disk images are nowhere near full, when viewed from inside the guest.  what gives?

  • Page Files
  • Hibernation files
  • Database Temp files
  • and other such files that are elastic in nature.

KVM like most other Virtualization technologies is great at expanding dynamic disk images when needed. However Removing the free space left over by the previously mentioned files expanding and contracting, is still a manual process.

Now that You know,  The How, and Why of the problem lets take a look at the solution.

Step 1.  This differs depending on the os. 

For a Windows Guest : (These actions are performed inside the guest)

  1. Stop ALL non-essential software, especially any database software!
  2. Cleanup your temp files
  3. Defragment all hard drives attached to the guest.
  4. Now use a little app from Microsoft called SDelete to write zeros to the free space of each drive attached to the guest. For a quick and easy How-To click HERE.


For a Linux Guest : (These actions are performed inside the guest)

  1. Stop ALL non-essential software, especially any database software!
  2. Then create a file filled with zeros on each disk attached to the guest, and then delete said file. The following command is an example.

“cat /dev/zero > zero.file; rm -f zero.file”

cat /dev/zero > zero.file; rm -f zero.file

cat /dev/zero > zero.file; rm -f zero.file Using Centos 6.2

Note: this will produce the error “no space left on device” , as shown in the picture above. Fear not this is normal and expected!  The second part of the command will delete the file.


Step 2. Turn off the guest virtual machines, and backup everything up right now.

Step 3. Find, and Rename each disk image file with “bak” prefix.

Bash mv Command

Bash mv Command being used to rename a file in Ubuntu 12.02


Step 4. The most important step!!!  Use the following command to make a “freshly Compacted” copy of each disk image. (Look below for an explanation of the command, and the switches.)


qemu-img convert -p -O qcow2 /vms/bak.ltsp_ubuntux64.qcow2 /temp_mount/ltsp_ubuntux64.qcow2  Using Ubuntu 12.02 & qemu-img 1.0

Notice How I renamed the image in the command?  This will also leave a backup copy called “bak.ltsp_ubuntux64.qcow2”.  I usually keep these copies until I successfully boot the new disk image.


Qemu-img progress indicator

Qemu-img progress indicator.  Using Ubuntu 12.02 & qemu-img 1.0

*Using -p enables a basic progress indicator.

*The -O is REQUIRED,  this specifies the output format that qemu-img will use.  The qemu-img command is very good at determining the Source format of the image, but it needs to be told what the output format should be.  Otherwise You will get an image with whatever Your systems default is.


qemu-img convert -p -O qcow2 /vms/bak.ltsp_ubuntux64.qcow2 /temp_mount/ltsp_ubuntux64.qcow2




Leave a Reply

Your email address will not be published. Required fields are marked *