February 19, 2012

Truncate KVM/QEMU Raw Disk Image

kvmbanner-logo2

Most modern filesystems allow you to create virtual disk files without pre-allocating disk space. These virtual disks grow in size as the space is needed. The default virtual disk image type in KVM/QEMU is a sparse “raw” disk image which consumes real disk space as files are added. Interestingly, when files are deleted the virtual disk does not shrink. This makes sense as deleting files from a filesystem doesn’t typically erase the data but only the reference to where the data is stored on the disk. What follows is a recipe to reclaim this disk space.


Summary

Recently I needed to move a KVM virtual machine from one hosting company to another. The virtual machine was created with a 8 GB virtual disk this can be verified using a “long listing.”

ls -lh ./Virtual_Disk.img

The actual disk usage was 4.6 GB which was verified with the disk usage command.

du -sh ./Virtual_Disk.img

When within the running virtual machine the actual disk usage as seen by the virtual machine is 1.2 GB which is demonstrated by checking the space using the “df” command.

df -h

The reality is that as files (such as updates, logs, etc) have been created and deleted information was written to the virtual disk. The data is still there so the space cannot be reclaimed. The solution therefore is (from within the Virtual Machine) to overwrite the free space and file slack space with zeros. Once complete we convert the virtual disk image and through that process reclaim the free disk space. Finally, we compress the image and send it across the network.

Overwrite Free Space & File Slack Space

The best way I have found to write the free disk space to zero is using bcwipe. From within the guest you can erase free space on a mounted filesystem as the super user using:

./bcwipe -mz -F -S -v /Mount_Pointk

Converting The Disk Image To Reclaim Space

After the free space has been written to zeros I suggest powering off the virtual machine. From the host we can then convert the virtual disk using qemu-img.

qemu-img convert -O raw ./Virtual_Disk.img ./New_Virtual_Disk.img

Using the disk usage tool we can now see that we have reclaimed the freespace:

du -sh ./Virtual_Disk.img
 

Share your thoughts, post a comment.

You must be logged in to post a comment.