Back up a VirtualBox image

What are we trying to achive: A backup of a VirtualBox image with a size as small as possible.

‘Zero’ out the harddisk

As you keep using your virtual machine and writing and deleting things to the virtual harddisk, you’re filling it up with 0’s and 1’s. As you probably know, deleting a file doesn’t delete its binary data, just the file system reference to the data on the disk. Furthermore data chunks with random 0’s and 1’s can’t be compressed very well. So in order to achive a good compression rate, we need to fill up the ‘free’ space with uniform data, e. g. 0’s:

Boot into the virtual machine and run as root:

dd if=/dev/zero of=/tmp/zeros bs=10M
rm /tmp/zeros

This command will write zeros into the file /tmp/zeros until there is not disk space left. Then we delete this file again.

Shutdown the virtual machine.

Compress the virtual harddisk

On the host machine:

Find out where the virtual harddisk (usually [VM_NAME].vdi) file is stored.


# On Linux:
vboxmanage modifymedium disk /path/to/disk.vdi --compact

# on Windows:
VBoxManage.exe modifymedium disk "C:\path\to\disk.vdi" --compact

Export the virtual machine

Finally in the VirtualBox GUI: ‘Export Appliance’