Oracle Blog

Virtual Thoughts

Saturday Jun 13, 2009

In the previous article, we've discussed the reasons that cause your virtual disk images to become quite large over time. It might seem to be a pretty hopeless situation, but let me assure you: there is a way out. This article will show you how to reclaim your disk space. A couple of steps are presented and while each of them make sense, it is important that you execute them in the exact order given if you want to achieve the best results.

The first step (don't shoot me for that) is to delete files in your VM you don't really need. Clean up your trashcan, the temp directories, download stuff, uninstall applications you never use, etc. There are a lot of tools out there to delete unused files from Windows, I'm sure you will find a tool that suits your needs.

Now when you have just the files on the disk that you need, it's time to defragment the hard disk. After defragmentation, the files will be nicely aligned and there will be little free space between files. Windows comes with (not so good) defragmentation software, you will find it in the properties of your disk drive in My Computer under the "Tools" tab. Just give it a run and hope it will improve fragmentation. There are better tools out there, some of them cost money.

The data is now nicely aligned but we still got all those unused blocks that contain garbage (the contents of the files that used to live there). Therefore we need a tool that can find these blocks and overwrite them with zeros. Windows does not come with such a compact tool but it's available for download from Microsoft: http://technet.microsoft.com/en-us/sysinternals/bb897443.aspx

Close all your programs and do a

sdelete.exe -c

This will take a while, you should let it do its job without interfering. The tool will go through all parts of your virtual disk and look for things it can wipe out. It's known to be a very safe process, so don't worry.

Next, you should shutdown your virtual machine (power off, not save state) and let VirtualBox optimize the disk image and cut out all parts that SDelete zeroed out. There are two ways to do this: first you could compact the image (this will just operate on the disk image and make it smaller) or you could clone the disk image to a new image. The former needs more disk space but the latter has the advantage of being more secure (you still got the orginal bloated file after all) and it even allows you to switch from one virtual disk format (e.g. VDI) to another (e.g. VMDK). Let look at both options.

VBoxManage modifyhd XP.vdi --compact

This will compact your disk image and it will take some time. The cloning from VDI to VMDK works as follows:

VBoxManage clonehd XP.vdi NewXP.vmdk --format VMDK

There are a lot more options to clonehd and modifyhd, have a look at the VirtualBox user manual.

This concludes our article and I hope I've given you some useful information that allows you to reclaim some of your disk space. I'm about to go on a 11h intercontinental flight and the virtual machine I want to work with was too big for my small notebook so I've used these techniques to shrink the image to less than half of its previous size.