Saturday, September 2, 2017

Shrinking the Linux guest storage file of VirtualBox

Shrinking the dynamic storage file of VirtualBox used to be tedious.  First need to zero out the free space in the guest and then compacting the file from host.

With Linux supporting TRIM and VirtualBox supporting DISCARD, it can be done much easier within the guest.

First, on the host, prepare the storage file with DISCARD support:

VBoxManage storageattach "Ubuntu server" --storagectl "SATA" --port 0 --discard on --nonrotational on

- "Ubuntu server" is the VM name
- use "--storagectrl" and "--port" to specify the storage controller

Then whenever the storage file needs to be compacted, execute fstrim in the guest. e.g.

sudo fstrim -v /

where "/" is the mount point.

No comments: