Thursday, June 26, 2008

How to compact a VirtualBox virtual disk image (VDI)

After having used VirtualBox for a while, the actual size of your virtual disk image (VDI) may have grown to become very close to the virtual size. That is, even if your guest machine system reports that it only uses a small amount of disk space, its VDI image filesize is considerably larger. In general, the following three steps will compact your VDI file:
  1. Defragment hard drive (in guest system)
  2. Write zeros to hard drive blocks of free space (in guest system)
  3. Compact the VDI virtual disk image (in host system)
After this process, your VDI file size should be approximately equal to what the guest system reports being in use.

Consider the following example: I run Ubuntu 8.04 as the host system and have Windows XP installed as a guest machine. The corresponding VDI file, winxp.vdi, has a virtual size of 20 GB and an actual size of ~18 GB. However, logging in to the virtual Windows XP system, it tells me that only 3 GB of disk space is in use, while 17 GB is free. How can I shrink, or compact, the VDI file to only use about 3 GB as needed?
  1. Windows XP (guest): Run Start -> Accessories -> System Tools -> Disk Defragmenter
  2. Windows XP (guest): Download and run nullfile-1.02.exe, or run
    sdelete -c

  3. Ubuntu 8.04 (host): Run
    VBoxManage modifyvdi winxp.vdi compact

Afterwards, the actual size of winxp.vdi is only ~3GB, that is, about 15 GB of free space has become available to Ubuntu.

No comments: