My development Ubuntu image inside VirtualBox was using too much space to store empty but non-zero disk blocks on its virtual drive. This sucked space from my laptop’s SSD (which is already not big enough!). Shrinking it by zeroing the blocks took a little bit of effort.
Inside VirtualBox if I boot my Ubuntu 11.04 instance ‘df -h’ reports 14GB used of a 27GB virtual drive (where the .vdi image is 22GB). Back on the host laptop I see that the .vdi file is 22GB which suggests that 8GB could be freed up. The problem is that the unused blocks have been used and then deleted but not reset to 0, so VirtualBox keeps them allocated.
The solution is simple using these instructions:
- Delete any snapshots in VirtualBox so you’re just dealing with 1 .vdi file (not sure if this helps but it seemed sensible)
- Get the PartImage ISO (about 300MB), it includes zerofree (unlike my usual favourite SysRescueCD)
- Mount the PartImage ISO as a CD drive in VirtualBox, boot the virtual machine, it’ll start PartImage
- Run “$ fsck /dev/sda1” to confirm that the Ubuntu hd (/dev/sda1) is clean
- Make a temporary mount point “$ mkdir /mnt/ss”
- Mount the drive read-only and without modifying /etc as our host image is read-only “$ mount -n -o ro -t ext4 /dev/sda1 /mnt/ss”
- Check the space that’s used “$ du -h -s /mnt/ss” – for me this reported 14GB of a 27GB drive
- Let zerofree reset all the unused blocks “$ zerofree /dev/sda1” – took about 5 minutes for a 22GB .vdi on my SSD
- Close the virtual machine
- Back on the host laptop ask vboxmanage to compact all the zero’d blocks “$ vboxmanage modifyhd –compact /home/ian/<path>…/myimage.vdi” and remember to use a fully-qualified path (not ./myimage.vdi) else it reports an error
- Wait 2 minutes
- Check the .vdi image size – mine shrank from 22GB to 17GB
- Reboot the virtual image to confirm that the disk is unaffected (in the virtual machine it still reports 14GB used on a 27GB partition)
This solution looks easier but requires the dynamically allocated virtual image to expand to its fullest size which won’t work for me – right now my laptop has less free space than the virtual image is allowed to use!
Ian applies Data Science as an AI/Data Scientist for companies in ModelInsight and in his Mor Consulting, sign-up for Data Science tutorials in London. He also founded the image and text annotation API Annotate.io, lives in London and is a consumer of fine coffees.