HOWTO compact a Virtualbox disk
In this entry I explain how to compact the Virtualbox hard disk of a virtualized MS Windows 7 running inside a Linux host.
1. Run the MS Windows virtual machine and execute the Disk Defragmentation tool. Wait for it to finish.
2. Also from inside Windows, download a command-line program named
sdelete from Microsoft (search for it). Run the following command from a console:
sdelete c: -z
This tool supposedly fills with zeroes the full filesystem. It takes a while. Once it finishes, close Windows and exit the Virtualbox GUI.
3. Open a Linux console and move to the directory where your virtual machine files are stored.
4a. If your virtual disk is already in vdi format, run the following command:
vboxmanage modifyhd windows_7-disk001.vdi --compact
And you'll be done.
4b. Instead, if you need to convert the virtual disk from vmdk to vdi, run the following command:
vboxmanage clonehd --format vdi windows_7-disk001.vmdk windows_7-disk001.vdi
Wait for it to finish, open the Virtualbox GUI, select the virtual machine, click on setup, go to storage, select the current vmdk file, click on a little icon resembling a hard disk to the right of the file, click on the virtual hard disk change option and select the new vdi file. This operation also compacts the virtual disk on its way.