Thursday, March 5, 2015

Virtualbox: Shrinking VM Disk Images

Source: http://snippets.khromov.se/shrinking-a-virtualbox-linux-image-with-zerofree/
  • Download the SystemRescueCd Live Image
  • Mount it as a CD on your virtual machine and boot into the live distro
  • Find all available partition and LVM (volume groups) with:
  • fdisk -l
  • After we find our partition run zerofree to zero out the free space. In our example we will run it on the LVM root partition
  • zerofree -v /dev/mapper/vg_rheldev-lv_root
  • Finally, boot down your virtual machine, and run VBoxManage on the host to resize the .vdi file
  • C:\>"c:\Program Files\Oracle\VirtualBox\VBoxManage.exe" modifyhd "c:
    \Users\jerome\VirtualBox VMs\Monster\Mobiquithings.vdi" --compact
With this method, my VM shrunk from 200 GiB to 26 GiB

NB: The other method mentioned on the net replacing zerofree with dd like in
dd -if /dev/zero -of /bigemptyfile -bs 4096; rm /bigemptyfile
was not satisfactory for me.