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:
- Install zerofree in the guest Ubuntu virtual machine:
sudo apt-get install zerofree
- Restart to reovery mode:
- Restart the computer and hold the shift key when you see the BIOS screen.
- A recovery menu will appear.
- Select the root – Drop to root shell prompt option and click enter.
- Mount the partition as read-only. In my case on 12.04, I found this to already be the case, so nothing to do.
- Zero the unused disk clusters:
zerofree /dev/sda1
- Power off the virtual machine and in the host shrink the virtual disk file:
VBoxManage modifyhd –compact myvirtualboximage.vdi