Keeping VirtualBox hard disk images (VDI) nice and small

  • Posted on: 23 March 2016
  • By: hapebe

While moving some legacy web apps from a real Windows server (headed for retirement, to be somewhat euphemestic about it) to a virtual machine, I encountered the need to make back-ups of this whole VM - for the sake of simplicity, copying / compressing the whole disk images and the virtual machine definition file. Now here comes the issue: Churn on the VDI hard disk causes it to become bigger than necessary, and now I'm trying to reduce it back to the max. Like most of the time, this has been done before - this time, my thanks go to the authors of: http://acidx.net/wordpress/2014/03/how-to-reduce-the-size-of-virtualbox-vms/ .

The very short synopsis:

  • run sdelete -z (sysinternals suite) inside the VM to zero free hard disk space
  • run VBoxManage modifyhd somedisk.vdi --compact to reduce the VDI file size

Worked well for me (VirtualBox 4.3.20)!


Additions on the next day: Now I tried the same for an Ubuntu Desktop 15.10 machine - and it was a bit more complicated than expected (LVM!), but still turned out well in the end. What a boost of confidence for me! ;-)

These were the winning moves:

lvmdiskscan
# this gave me /dev/mapper/ubuntu--vg-root as the most likely partition I was looking for...
#
# let's do a basic check whether this could actually be a file system:
# fsck /dev/mapper/ubuntu--vg-root
#
# the following 3 lines is pure luxury - but can give confirmation that my assumptions about 
# file system etc. are correct:
mkdir /mnt/part
mount -n -o ro -t ext4 /dev/mapper/ubuntu--vg-root /mnt/part
du -h -s /mnt/part
#
# next line is the "payload":
zerofree /dev/mapper/ubuntu--vg-root
  • Shut down the VM, and trim the VDI file as in the Windows chapter:
VBoxManage modifyhd somedisk.vdi --compact

Worked well for me, VirtualBox 5.0.16 this time!