Perşembe, Nisan 25, 2013

How to Compact a VirtualBox Linux Guest’s VDI file

Steps to compact a VDI file:

  1. Zero-fill the virtual machine’s disk using the dd command
  2. Shut down the machine
  3. Use Oracle’s VBoxManage program to compact the VDI
  4. Restart the virtual machine

To zero-fill a linux disk, the most efficient way is to use the dd command, which can write a stream to a file. In this case, we will be writing from the linux pseudo-device /dev/zero to a fill file. Use the following command to zero-fill the disk:

$ dd if=/dev/zero of=fillfile bs=1M

if= specifies the input file, in this case, a stream of zeroes. of= specifies an output file. If you were to specify a drive here, like /dev/sda, it would zero fill the entire drive, so be careful. bs= specifies the block size. In this case, it will write a megabyte of zeroes at a time into fillfile.

Once dd has completed, delete your zero-fill file

This is a minor consideration, done for speed. For future reference, it can be much faster to specify a 512k-1M block size when random filling a drive with dd for data destruction purposes.


Once dd has completed, delete your fill file (which should be taking up all of the free space on the drive at this point) and shut the virtual machine down. Once it’s shut down, run VBoxModify to complete the process. VBoxModify is a very powerful tool, and occupies an entire chapter of the VirtualBox Manual. Compact the VDI using the following command:

$ VBoxManage modifyhd /path/to/your.vdi --compact




When the process is complete, you should have a smaller vdi. If not, something went wrong with the zero-filling process or there was no room to compact your VDI.