December 20, 2008 – 2:24 pm
If you’re like me and have several VirtualBox Windows virtual machines, you’ll probably find they gradually take up more and more space as time goes on. This is at least partly because the filesystem gets fragmented and old deleted files still take up space in the VirtualBox VDI file. If you’ve created the VDI as a dynamically expanding image, it will only take up as much space in your filesystem as the files inside it take up – but when those files are deleted it won’t shrink again.
A VirtualBox VDI can be made to shrink, however, but first you need to do something about what’s inside it. A useful application called ‘VBoxManage’ can shrink those virtual disk images – but only if the empty space is zeroed first. I run VirtualBox on a Linux system and i use the method i’ve described here – but i can’t guarantee that all of what i’ve written applies to other operating systems.
The first step in reducing the amount of hard disk space a VDI takes up is to delete as much junk from inside it as you can. You can start by deleting the hidden folder of backup files which is created when you install a service pack. This is called \Windows\$NTServicePackUninstall. If you do this, you won’t be able to roll back that service pack upgrade. Personally, i’m not bothered by this because i keep a backup copy of the VDI which i can use to roll back and start again, in the unlikely event that i need to.
Depending on how the installation was done, there may also be a folder C:\Windows\SoftwareDistribution\Download. You can delete that too.
The service pack installation creates a temporary folder as well. This doesn’t always get properly removed. It will be on the drive you downloaded the setup files to, which is probably C:, and it’ll have a name made up of random characters – e.g., C:\be35a76f21ed. If it’s still there, you can delete it.
Don’t delete C:\Windows\ServicePackFiles though. This will be used in future. However, if you want, you can compress it and save a fair bit of space. Right click on it, select “Properties”, click on the “Advanced” button, and select “Compress”.
The next step is to defragment the Windows file system. I always do this two or three times in succession, as it seems to keep improving the way the files are scattered around a bit. That probably doesn’t matter much (or at all), but it seems tidier to me!
After that, you need to zero all the empty space in the filesystem. Download ‘sdelete’ from http://www.microsoft.com/technet/sysinternals/Security/SDelete.mspx to do that job. You should probably download sdelete before you defrag, as when you unzip it a small amount of extra fragmentation is likely to occur.
Open up a DOS window and run sdelete with
sdelete -c c:
(assuming C: is your filesystem). That will zero all the free space.
Then shut down Windows and you’re ready to shrink the VDI file by running the following command on it:
VBoxManage modifyvdi WinXP.vdi compact
(replace “WinXP.vdi” with the name of your vdi file).
One of my VDI files had grown to 5.2GB recently – from a couple of months of Windows updates, etc. Using the method above, i managed to shrink it back to 2.7GB.