How to Shrink your Dynamic Disk on VirtualBox Ubuntu guest

How to Shrink your Dynamic Disk on VirtualBox Ubuntu guest:

Original instructions I found here:

https://www.howtogeek.com/312883/how-to-shrink-a-virtualbox-virtual-machine-and-free-up-disk-space/

I updated these commands to work for my system.

In Ubuntu Guest, grab zerofree:

sudo apt install zerofree

Now you will have to boot up Ubuntu in recovery mode:
Shut down Ubuntu.
Start it back up and hit “esc” while it is booting.
Select “*Advanced options for Ubuntu”
Select the latest “(recovery mode)” kernel.
Select “root”

Run df to find your root partition

df

It will look something like “/dev/sda1”

Now run zerofree:

zerofree -v /dev/sda1

When zerofree is done, halt the system:

halt

Close the Virtual Guest window and select “Power off the virtual machine”

Now you will need to run the “VBoxManage.exe” to shrink the physical snapshots and vdi file. Do the Snapshots first. Commands should look like this:

cd "C:\Users\Reynold\VirtualBox VMs\YourVirtualDiskName\Snapshots"

"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" modifymedium disk "{0e420dca-8044-4d3a-a256-cf90ca19de42}" --compact

cd "C:\Users\Reynold\VirtualBox VMs\YourVirtualDiskName"

"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" modifymedium disk "YourVirtualDiskName.vdi" --compact

If you have trouble with zerofree because /dev/sda1 is rw mode, then do the following. Found fix for this here:

https://ubuntuforums.org/showthread.php?t=2399268

In single user root mode again:

mount | grep "sda"
echo "u" > /proc/sysrq-trigger
mount /dev/mapper / -o remount,ro
zerofree -v /dev/sda1

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.