Saturday, June 8, 2024

Fstrim on a Raspberry Pi with a Samsung T5/T7

I have a couple of Pi's with USB connected SSD's. One is a Pi4 with a Samsung T5 and the other is a Pi5 with a Samsung T7. If you try to run fstrim on these if doesn't do anything, even though both devices support trim. After a bit of google-foo I managed to find a solution.

Identify vendor/product ids
First you'll need to identify the vendor id and product id. You can do this with the lsusb command. It will give you two hex values for each device. The first is the vendor and the second is the product id.

$ lsusb
Bus 004 Device 002: ID 04e8:4001 Samsung Electronics Co., Ltd PSSD T7
Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

In this case you can see the vendor is 04e8 and the product id is 4001 for my T7.

The same command on my Pi4 with a T5...

$ lsusb
Bus 002 Device 002: ID 04e8:61f5 Samsung Electronics Co., Ltd Portable SSD T5
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 002: ID 2109:3431 VIA Labs, Inc. Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

In this case you can see the vendor is 04e8 and the product id is 61f5 for my T5.

Create udev rule
Now we have the two ids we can create a udev rule to tell it that trim is supported by the device. We need to add the rule by editing a file.

$ sudo nano /etc/udev/rules.d/55-usb-ssd-trim.rules

And then paste in the following (for a T7). Change the idProduct if you have a T5.

ACTION=="add|change",ATTRS{idVendor}=="04e8", ATTRS{idProduct}=="4001", SUBSYSTEM=="scsi_disk",ATTR{provisioning_mode}="unmap"

Save this (Ctrl-O) and exit (Ctrl-X).

I believe you have to reboot for it to pickup the new rule but I could be wrong here. I rebooted anyway. After this you should be able to run fstrim.

$ sudo fstrim -av
/boot/firmware: 446.9 MiB (468604928 bytes) trimmed on /dev/sda1
/: 0 B (0 bytes) trimmed on /dev/sda2

Check fstrim.timer
You can also check the fstrim.timer service is running. It will usually run fstrim once a week.

$ sudo systemctl status fstrim.timer
fstrim.timer - Discard unused blocks once a week
Loaded: loaded (/lib/systemd/system/fstrim.timer; enabled; preset: enabled)
Active: active (waiting) since Sat 2024-06-08 18:29:50 AEST; 3h 23min ago
Trigger: Mon 2024-06-10 00:23:17 AEST; 1 day 2h left
Triggers: * fstrim.service
Docs: man:fstrim

Press Q to exit from systemctl.
You can see its waiting until Monday at 0:23AM before it will run fstrim.

No comments: