SysGhost
Posts: 2
Joined: Thu Jun 10, 2021 10:30 am

fstrim SystemD service and timer

Thu Jun 10, 2021 10:46 am

I've noticed that the fstrim command (binary) is included in the OS image, but the fstrim.service and fstrim.timer files are missing in SystemD.
These files are usually found in the util-linux package. But not in the case here.

I took the liberty and created these files myself, and it works just fine with the already included fstrim command.
Here's a lil "how to":

Create a file named /etc/systemd/system/fstrim.service and put this in it:

Code: Select all

[Unit]
Description=Discard unused blocks on filesystems from /etc/fstab
Documentation=man:fstrim(8)
ConditionVirtualization=!container

[Service]
Type=oneshot
ExecStart=/usr/sbin/fstrim -A
PrivateDevices=no
PrivateNetwork=yes
PrivateUsers=no
ProtectKernelTunables=yes
ProtectKernelModules=yes
ProtectControlGroups=yes
MemoryDenyWriteExecute=yes
SystemCallFilter=@default @file-system @basic-io @system-service
Create another file named /etc/systemd/system/fstrim.timer and put this in it:

Code: Select all

[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
Then enable the timer unit:

Code: Select all

sudo systemctl enable fstrim.timer
Now you're good to go. All your trim-capable volumes will be trimmed once a week.

Suggestions, alternatives and such are welcome.

RonR
Posts: 2193
Joined: Tue Apr 12, 2016 10:29 pm
Location: US

Re: fstrim SystemD service and timer

Thu Jun 10, 2021 6:13 pm

SysGhost wrote:
Thu Jun 10, 2021 10:46 am
I've noticed that the fstrim command (binary) is included in the OS image, but the fstrim.service and fstrim.timer files are missing in SystemD.
These files are usually found in the util-linux package. But not in the case here.

I took the liberty and created these files myself, and it works just fine with the already included fstrim command.
Here's a lil "how to":

...

Suggestions, alternatives and such are welcome.

They're already included in Raspberry Pi OS images:

/usr/lib/systemd/system/fstrim.service
/usr/lib/systemd/system/fstrim.timer

All that's needed is to create an /etc/udev/rules.d/ssd-trim.rules file:

Code: Select all

lsusb
=====
Bus 002 Device 004: ID 174c:55aa ASMedia Technology Inc. Name: ASM1051E SATA 6Gb/s bridge, ASM1053E SATA 6Gb/s bridge, ASM1153 SATA 3Gb/s bridge, ASM1153E SATA 6Gb/s bridge

/etc/udev/rules.d/ssd-trim.rules
================================
ACTION=="add|change", ATTRS{idVendor}=="174c", ATTRS{idProduct}=="55aa", SUBSYSTEM=="scsi_disk", ATTR{provisioning_mode}="unmap"

And then run:

Code: Select all

systemctl enable fstrim.timer && reboot

SysGhost
Posts: 2
Joined: Thu Jun 10, 2021 10:30 am

Re: fstrim SystemD service and timer

Fri Jun 11, 2021 7:56 am

Those were missing completely for me. Which package provides those files?
EDIT:
I reinstalled util-linux, and the two missing files are now present.

RonR
Posts: 2193
Joined: Tue Apr 12, 2016 10:29 pm
Location: US

Re: fstrim SystemD service and timer

Fri Jun 11, 2021 8:08 am

SysGhost wrote:
Fri Jun 11, 2021 7:56 am
Those were missing completely for me. Which package provides those files?

They come installed in the Raspberry Pi OS image files. You'd have to ask the Raspberry Pi OS developers where they got them.

Return to “Raspberry Pi OS”