First of all download SystemRescueCd, which we will use later to manipulate our virtual disk.
Mount it to VM with VirtualBox GUI. Upon launching the VM it should automatically boot to SystemRescueCd. Choose the default option to go to the recovery system.
Next run fdisk -l
to see the disk partitions. To me it's /dev/sda5
. Then run
zerofree -v /dev/sda5
to start filling the empty space with zerofree. It should not take too much time, 1 or 2 minutes should do it. Then finally shutdown the system by shutdown now
.
On your host, go to the .vdi
location and run
VBoxManage modifyhd vm-disk001.vdi compact
Give it some time and the image should be shrinked.
References #
- https://snippets.khromov.se/shrinking-a-virtualbox-linux-image-with-zerofree/
- https://unix.stackexchange.com/questions/42015/mount-is-busy-when-trying-to-mount-as-read-only-so-that-i-can-run-zerofree
- http://www.beginninglinux.com/home/virtualbox/shrinking-vdi-with-zerofree-on-ubuntu-as-guest-os