Compacting the virtual hard disk of a Linux virtual machine

If you have installed Linux (or a *BSD variant) inside of a virtual machine under Virtual PC or Virtual Server - you will probably want to compress the virtual hard disk after a while, to reclaim any unused space on the virtual hard disk. In order to do this you will need to 'zero-out' the virtual hard disk first. Now - we provide a handy tool to do this under Windows - but under Linux all you need to do is to run the following command:

cat /dev/zero > zero.dat ; sync ; sleep 1 ; sync ; rm zero.dat

This will create a very large temporary file that has nothing but binary zeros in it, synchronize the hard disk and delete the temporary. Once this has completed you can shutdown the virtual machine and compact the virtual hard disk by editing it with the virtual hard disk wizard. Using this command I was able to reduce my SuSE virtual machine from 6.9GB to 4.12GB in size.

Cheers,
Ben