09 March 2025

Enabling storage space reclaim in virtualbox VMs.


Enabling storage space reclaim in virtualbox VMs.

My case:
 - Host: Debian Linux
 - VM: Debian Linux

Recommended reading: https://superuser.com/questions/646559/virtualbox-and-ssds-trim-command-support


# Verify that the Host can trim

```
$ lsblk --discard
NAME   DISC-ALN DISC-GRAN DISC-MAX DISC-ZERO
sda           0      512B       2G         0
├─sda1        0      512B       2G         0
├─sda2        0      512B       2G         0
├─sda3        0      512B       2G         0
└─sda4        0      512B       2G         0
sr0           0        0B       0B         0
```

If you only see zeros, not good. You need to resolve that first and then come back.


# You can trim on the host

```
$ sudo fstrim -va
/boot/efi: 142 MiB (148893696 bytes) trimmed on /dev/nvme0n1p1
/extra: 0 B (0 bytes) trimmed on /dev/nvme0n1p7
/: 0 B (0 bytes) trimmed on /dev/nvme0n1p6
```


# Do the same on the vm

If you can do the same, then all good.

If the output of `lsblk --discard` shows zeros, then probably the VM does not have the correct configuration.


# Check the vm settings

Run FROM the location of VM files, with the VM powered-off.


```
$ cat *.vbox | grep -C 2 StorageController

 <StorageController name="SATA" type="AHCI" PortCount="1" useHostIOCache="true" Bootable="true" IDE0MasterEmulationPort="0" IDE0SlaveEmulationPort="1" IDE1MasterEmulationPort="2" IDE1SlaveEmulationPort="3">
          <AttachedDevice nonrotational="true" discard="true" type="HardDisk" hotpluggable="false" port="0" device="0">
             <Image uuid="{4e19...80}"/>

```


if your output does not have `discard="true"` then you need to add it.


Note: Adding the text manually on the file, even if the VM is off, WILL NOT HELP. I tested it. You need to unregister & register the vdisk with the proper settings.


Save the uuid as

`$ hd="4e19...80"`


# Get the full path of the vdi file

```
$ cat *.vbox | grep  $hd
        <HardDisk uuid="{4e19...80}" location="file.vdi" format="VDI" type="Normal"/>
          <Image uuid="{4e19...80}"/>
        <HardDisk uuid="{4e19...80}" location="/full/path/to/file.vdi" format="VDI" type="Normal"/>
            <Image uuid="{4e19...80}"/>
```

Save it
`vdipath="/full/path/to/file.vdi"`



# Detach the vdisk from the vm

Use the virtualbox UI for that. Probably you can do it with a command, but I don't provide untested commands.


# Unregister the vdisk from Virtualbox

`$ VBoxManage closemedium disk $hd`



# Re-Register the vdisk with the vm with the needed settings

## List the vms and save the name in the variable `vm`

```
$ VBoxManage list vms
"ThisVM" {8a52....ce4}
```

`$ vm = "8a52....ce4"`


This is the important part

`$ VBoxManage storageattach $vm --storagectl "SATA" --port 0 --device 0 --nonrotational on --discard on --medium $vdipath --type hdd`


You can grep again the .vbox file to verify.


Now power on, and run the `lsblk --discard`, it should be fine.




0 Comments:

Post a Comment

<< Home

Too Cool for Internet Explorer