Shrinking Ubuntu 20.04 VM .vdi on macOS VirtualBox

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 #