Is Your virtual machine sluggish? Have Your disk images grown in size? Are Disk space errors like the one below popping up?
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
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)
- Stop ALL non-essential software, especially any database software!
- Cleanup your temp files
- Defragment all hard drives attached to the guest.
- 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)
- Stop ALL non-essential software, especially any database software!
- 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 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.
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.
*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.