11 March 2025

Edit fstrim.timer configuration [Debian Linux 11]


# Edit fstrim.timer configuration [Debian Linux 11]


There are some guides out there, located in forums. I didn't like very much what I saw.

Here I describe what I did and the results.


### Checking

```bash
$ systemctl cat fstrim.timer
# /lib/systemd/system/fstrim.timer
[Unit]
Description=Discard unused blocks once a week
Documentation=man:fstrim
ConditionVirtualization=!container

[Timer]
OnCalendar=weekly
AccuracySec=1h
Persistent=true
RandomizedDelaySec=6000

[Install]
WantedBy=timers.target

```


### Checking

```bash
$ systemctl status fstrim.timer
● fstrim.timer - Discard unused blocks once a week
     Loaded: loaded (/lib/systemd/system/fstrim.timer; enabled; vendor preset: enabled)
     Active: active (waiting) since Sun 2025-03-09 18:22:05 CET; 1 day 14h ago
    Trigger: Mon 2025-03-17 00:38:54 CET; 5 days left
   Triggers: ● fstrim.service
       Docs: man:fstrim

Warning: some journal files were not opened due to insufficient permissions.

```

### Checking

```bash
$ sudo fstrim -av
/home: 120 MiB (125820928 bytes) trimmed on /dev/sda4
/boot: 0 B (0 bytes) trimmed on /dev/sda2
/: 554.1 MiB (581054464 bytes) trimmed on /dev/sda1

```


### Checking

```bash
$ systemctl list-timers fstrim.timer --all
NEXT                        LEFT        LAST                        PASSED  UNIT         ACTIVATES
Mon 2025-03-17 00:09:02 CET 5 days left Mon 2025-03-10 08:31:27 CET 24h ago fstrim.timer fstrim.service

1 timers listed.

```



### Edit the file to change weekly -> daily


```bash
$ sudo vi /etc/systemd/system/fstrim.timer

```

Verify


```bash
$ cat /etc/systemd/system/fstrim.timer
[Unit]
Description=Discard unused blocks once a week
Documentation=man:fstrim
ConditionVirtualization=!container

[Timer]
OnCalendar=daily
AccuracySec=1h
Persistent=true
RandomizedDelaySec=6000

[Install]
WantedBy=timers.target

```


### Reload


#### Correct order

```bash
$ sudo systemctl daemon-reload

```

```bash
$ sudo systemctl restart fstrim.timer

```

#### Note

If you try the `restart` without doing the `daemon-reload` first then you get


```bash
$ sudo systemctl restart fstrim.timer
Warning: The unit file, source configuration file or drop-ins of fstrim.timer changed on disk. Run 'systemctl daemon-reload' to reload units.

```


### Verify



```bash
$ sudo systemctl status fstrim.timer
● fstrim.timer - Discard unused blocks once a week
     Loaded: loaded (/etc/systemd/system/fstrim.timer; enabled; vendor preset: enabled)
     Active: active (waiting) since Tue 2025-03-11 09:22:21 CET; 14s ago
    Trigger: Wed 2025-03-12 00:37:18 CET; 15h left
   Triggers: ● fstrim.service
       Docs: man:fstrim

Mar 11 09:22:21 osboxes systemd[1]: Stopped Discard unused blocks once a week.
Mar 11 09:22:21 osboxes systemd[1]: Stopping Discard unused blocks once a week.
Mar 11 09:22:21 osboxes systemd[1]: Started Discard unused blocks once a week.

```

```bash
$ systemctl list-timers fstrim.timer --all
NEXT                        LEFT     LAST                        PASSED  UNIT         ACTIVATES
Wed 2025-03-12 00:37:18 CET 15h left Tue 2025-03-11 09:22:16 CET 36s ago fstrim.timer fstrim.service

1 timers listed.

```

The NEXT changed from next week to next day.


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.




Too Cool for Internet Explorer