Compacting a virtualbox volume

One nice feature about virtualbox is that volume files only grow up to their size as you use them. However, after a certain time, it can be that a volume containing 1GB of data occupies 8GB on disk.

This is not really related to virtualbox but rather to the way the filesystem is allocating the blocks on the disk : it not necessarilly reuses blocks, so when new ones are allocated, the volume’s size increases.

Decreasing the size of a volume file (compacting) is not that easy. Virtualbox will only “forget” sectors that contain all zero’s. Doing this on linux is straight forward (dd), but there is no Windows equivalent.

In fact there is ! run sdelete -c C:\ to zero all available blocks on the disk. It will write to all free blocks, therefore the size of the volume will reach the maximum. It will also take some time.

Then, the volume file can be compacted :

VBoxManage modifyvdi <path to VDI file> compact