How to shrink a dynamically-expanding guest virtualbox image
Sometimes bigger isn’t always better. If your dynamically-expanding virtual machine images are growing out of control, then here’s how to trim them back…
I’m a big fan of VirtualBox, and use separate virtual machines (VMs) for the various separate bits and pieces I’ve got on the go (as I invariably end up messing something up, and can just trash the image and start again, without taking down whatever else it is I’m playing with at the time).
All my VMs use a dynamically expanding image for their hard drive, where you set the maximum size of the disk, but the system will only grow to fill that space if required. By setting this nice and high, I can be sure that the hard drive space is there if I need it, without taking space away unnecessarily from the rest of the system.
Unfortunately, whilst VirtualBox will dynamically expand the hard drive as it’s required, it won’t dynamically shrink it again if you free up space in the VM. This can be a problem, especially if, like me, the sum total of all those theoretical maximums exceeds the actual maximum capacity of the hard drive hosting all these VMs.
The good news is that you can shrink those images back down again. The bad news is that a lot of the guides on the internet are out-of-date, and woefully misleading. Here’s what I did to get the job done…
1. Free up space in the client machine
It’s a bit of an obvious first step, but you can only shrink down the client VM by the size of the available free space therein, so delete the files and uninstall the programs that you no longer need but are hogging your resources.
2. Zero out the free space
VirtualBox only knows that the space is really free if it’s been set to zero, and standard deletion won’t do this.
If it’s an Ubuntu VM
You’ll want to use zerofree:
- install with sudo aptitude install zerofree
- (if you don’t have aptitude, you can either use apt-get to install zerofree (sudo apt-get install zerofree) or use apt-get to install aptitude (sudo apt-get install aptitude). I’d recommend getting hold of aptitude, as it does a great job of managing packages in Ubuntu)
- Reboot the machine (sudo shutdown -r now). During boot, hold down the left shift key. A menu will appear, you need to select “recovery mode”; this should be the second item in the list.
- You’ll get another menu, towards the bottom there should be the option to “Drop to root shell prompt”
- Run df and look for the mount point that’s that the biggest – this is where all your files are, and is the one we’ll need to run zerofree against. For the rest of this guide, we’ll assume it’s /dev/sda1
- The following three commands (thanks, VirtualBox forum!) stop background services that we can’t have running:
- service rsyslog stop
- service network-manager stop
- killall dhclient
- Once they’ve stopped, you can re-mount the partition as readonly (zerofree needs this)
- mount -n -o remount,ro -t ext3 /dev/sda1 /
- You can now run zerofree
- zerofree -v /dev/sda1
- Finally, shut down the VM
- shutdown -h now
If it’s a Windows VM
You’ll need to run sdelete; I’ve never done this, but there are instructions on that here:
3. Shrink the VM
Quite a lot of the online guides say that you’ll have to clone the hard drive image to shrink it, as VirtualBox 2.2 and above dropped support for compacting the image. This isn’t true, certainly not for version 4.0.4, and you can shrink the image in-place with the following command:
- VBoxManage modifyhd my.vdi –compact
With any luck, you’ll now have plenty of disk space to fill will equally useless tat…