How to shrink Ubuntu 12.04 guest VirtualBox image

If you have done a lot of adding and removing of files on your Ubuntu 12.04 guest virtual machine, either by installing and uninstalling software or building and rebuilding source code or any other means you will eventually find that the dynamic virtual disk image size has increased and is much larger (on the host) than the actual data contained inside the guest operating system. You can reclaim this space by shrinking the VM image:

  1. Install zerofree in the guest Ubuntu virtual machine:

    sudo apt-get install zerofree

  2. Restart to reovery mode:
    1. Restart the computer and hold the shift key when you see the BIOS screen.
    2. A recovery menu will appear.

      Ubuntu 12.04 Reovery Menu

      Ubuntu 12.04 Reovery Menu

    3. Select the root – Drop to root shell prompt option and click enter.
  3. Mount the partition as read-only. In my case on 12.04, I found this to already be the case, so nothing to do.
  4. Zero the unused disk clusters:

    zerofree /dev/sda1

  5. Power off the virtual machine and in the host shrink the virtual disk file:

    VBoxManage modifyhd –compact myvirtualboximage.vdi

You can leave a response, or trackback from your own site.

Leave a Reply