Ian Ozsvald picture

This is Ian Ozsvald's blog (@IanOzsvald), I'm an entrepreneurial geek, a Data Science/ML/NLP/AI consultant, author of O'Reilly's High Performance Python book, co-organiser of PyDataLondon, a Pythonista, co-founder of ShowMeDo and also a Londoner. Here's a little more about me.

High Performance Python book with O'Reilly

View Ian Ozsvald's profile on LinkedIn

ModelInsight Data Science Consultancy London Protecting your bits. Open Rights Group

26 July 2012 - 17:32Using ZeroFree to shrink a VirtualBox Linux Image

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, sign-up for Data Science tutorials in London. Historically Ian ran Mor Consulting. He also founded the image and text annotation API Annotate.io, co-authored SocialTies, programs Python, authored The Screencasting Handbook, lives in London and is a consumer of fine coffees.

1 Comment | Tags: Life, Ubuntu