17 February 2023

Compacting your VirtualDisk in VirtualBox VM


If you selected a "Dynamically allocated storage" after some use, you may want to reclaim "unused space" and make the vdi disk smaller.

-------------------------------
1. Make sure it is "dynamic"



-------------------------------
2. Verify that the VM configuration file has   discard="true" on the disk's details.

<AttachedDevice nonrotational="true" discard="true" type="HardDisk" hotpluggable="false" port="0" device="0">

If that is not there, when you run the command below you will get

# fstrim  -v  /
fstrim: /:
the discard operation is not supported

You can add it manually with a text editor when the VM (and maybe VirtualBox too) are powered-off and closed.

There are other methods to add it via VirtualBox commands if you don't feel like editing it.


-------------------------------
3. Power on the VM and run as root (Debian linux VM here)

# fstrim  -v -a
/home: 261.7 GiB (280975212544 bytes) trimmed on /dev/sda4
/boot: 875.7 MiB (918228992 bytes) trimmed on /dev/sda2
/: 213 GiB (228729548800 bytes) trimmed on /dev/sda1

-------------------------------
4. Power off the VM (may not be necessary but I rather don't risk it).
Open a command window on the location of the VM and run

# VBoxManage modifyhd -compact Debian_11_Server_64bit.vdi
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%


Too Cool for Internet Explorer