HowTo: Compact Virtualbox VDI images

I just ran into an issue while trying to compact my Virtualbox hard drive images. On virtual NTFS filesystems I usually run defrag twice, then rely on a tool called sdelete (download page) to zero the free space within the image. Afterwards I use vboxmanage to reduce the image size.

This time either zeroing the free space or the shrinking process seemed to fail. The supposedly compressed images needed even more space than before. It took me a while to figure out what happened – the parameters of sdelete had changed:

C:\>sdelete

SDelete - Secure Delete v1.6
Copyright (C) 1999-2010 Mark Russinovich
Sysinternals - www.sysinternals.com

usage: sdelete [-p passes] [-s] [-q] <file or directory> ...
       sdelete [-p passes] [-z|-c] [drive letter] ...
   -a         Remove Read-Only attribute
   -c         Clean free space
   -p passes  Specifies number of overwrite passes (default is 1)
   -q         Don't print errors (Quiet)
   -s or -r   Recurse subdirectories
   -z         Zero free space (good for virtual disk optimization)

It feels like 99% of the tips concerning zeroing the free space (by calling sdelete -c, the old option for zeroing) seem to be outdated. Even worse: the “clean free space” feature shows the same output as “zero free space”.

So this should (currently) be a working procedure:

  1. On the guest system: Run the Windows “defrag” tool twice.
  2. On the guest system: (Download and) run “sdelete -z” to zero the free space.
  3. On the host system: Use vboxmanage “modifyvdi IMAGE.vdi –compact” to reduce the image size.

5 thoughts on “HowTo: Compact Virtualbox VDI images”

  1. For me the image grown bigger after filling it with zeros.. Maybe Virtualbox automatically only saves the real files?

    1. This observation is correct: zeroing out the free space with sdelete initially leads to an increase in the size of the image. The second step (vboxmanage … –compact) identifies empty space (filled with zeroes) and removes it.

Leave a Reply

Your email address will not be published. Required fields are marked *