site  contact  subhomenews

Hopeful fix fstrim on USB SSD

January 28, 2025 — BarryK

I posted about compiling the kernel 6.12.11 with change to hopefully support TRIM over USB:

...there is also a link to forum discussion.

After more reading, I have devised code in the 'init' script in the initrd, that intends to detect if a USB SSD supports TRIM, then enables usage of fstrim. This is the modified code:

###execute TRIM on ssd###
TRIMflg=''
if [ "$WKG_FS" == "ext4" ];then #200302 200621 now have full hdparm.
#20250127 discard_granularity test not work 6.12.x kernel...
#TESTdiscard="$(cat /sys/block/${WKG_DRV}/queue/discard_granularity 2>/dev/null)"
TESTdiscard="$(cat /sys/block/${WKG_DRV}/queue/discard_max_bytes 2>/dev/null)"
if [ -n "$TESTdiscard" ];then
if [ "$TESTdiscard" != "0" ];then
TRIMflg='1'
fi
fi
#20250128 hdparm detect and CONFIG_USB_UAS kernel now maybe can run fstrim...
if [ "$TESTdiscard" == "0" ];then
hdparm -I /dev/${WKG_DRV} | grep 'Data Set Management TRIM supported'
if [ $? -eq 0 ];then
#fstrim may still not work, depends if usb interface supports usb-attached-scsi
#ref: https://askubuntu.com/questions/860618/fstrim-on-external-ssd-drives-dont-work
#ref: https://www.jeffgeerling.com/blog/2020/enabling-trim-on-external-ssd-on-raspberry-pi
#figuring out my own method, this matches scsi number to drive:
T1="$(ls -d -1 /sys/bus/scsi/drivers/sd/*/block/${WKG_DRV} | head -n 1)"
#ex: /sys/bus/scsi/drivers/sd/6:0:0:0/block/sdb
if [ -n "$T1" ];then
T2="$(echo -n "$T1" | rev | cut -f 3 -d '/' | rev)" #ex: 6:0:0:0
T3="$(find /sys/devices/ -path "*/scsi_disk/${T2}/*" -name provisioning_mode)"
#ex: /sys/devices/pci0000:00/0000:00:14.0/usb2/2-4/2-4:1.0/host6/target6:0:0/6:0:0:0/scsi_disk/6:0:0:0/provisioning_mode
if [ -n "$T3" ];then
T4="$(cat "$T3")"
if [ "$T4" == "full" ];then
echo unmap > $T3
#ex: this changes /sys/block/sdb/queue/discard_max_bytes from 0 to 4294966784
fi
#test again...
TESTdiscard="$(cat /sys/block/${WKG_DRV}/queue/discard_max_bytes 2>/dev/null)"
if [ "$TESTdiscard" != "0" ];then
TRIMflg='1'
fi
fi
fi
fi
fi
fi
export TRIMflg #read in /sbin/fscheck

Now, if the USB interface supports USB-attached-SCSI-protocol (UASP), then hopefully fstrim will work after doing the above.  

Tags: easy