What's new

Solved [SOLVED] How activate Trim for an SSD (MerlinWRT)?

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

RAH-66

Senior Member
admin@RT-AX88U:/tmp/home/root# hdparm -I /dev/sda | grep TRIM
* Data Set Management TRIM supported (limit 8 blocks)
* Deterministic read ZEROs after TRIM

May 5 10:05:17 kernel: usbcore: registered new interface driver usb-storage
May 5 10:05:17 kernel: scsi host0: uas
May 5 10:05:17 kernel: scsi 0:0:0:0: Direct-Access CT240BX5 00SSD1 4101 PQ: 0 ANSI: 6
May 5 10:05:17 kernel: sd 0:0:0:0: Attached scsi generic sg0 type 0
May 5 10:05:17 kernel: sd 0:0:0:0: [sda] 468862128 512-byte logical blocks: (240 GB/224 GiB)
May 5 10:05:17 kernel: sd 0:0:0:0: [sda] 4096-byte physical blocks
May 5 10:05:17 kernel: usbcore: registered new interface driver uas
May 5 10:05:17 kernel: sd 0:0:0:0: [sda] Write Protect is off
May 5 10:05:17 kernel: sd 0:0:0:0: [sda] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
May 5 10:05:17 kernel: sda: sda1 sda2
May 5 10:05:17 kernel: sd 0:0:0:0: [sda] Attached SCSI disk

Code:
admin@RT-AX88U:/tmp/home/root# mount | grep sda
/dev/sda1 on /tmp/mnt/opt type ext4 (rw,nodev,relatime,data=ordered)
/dev/sda2 on /tmp/mnt/TDDOWNLOAD type ext4 (rw,nodev,relatime,data=ordered)

Created /jiffs/config/fstab
Code:
/dev/sda1 /tmp/mnt/opt ext4 rw,nodev,relatime,discard,data=ordered 0 0
/dev/sda2 /tmp/mnt/TDDOWNLOAD ext4 rw,nodev,relatime,discard,data=ordered 0 0
/jiffs/scripts/init-start
Code:
#!/bin/sh
mkdir -p /tmp/mnt/opt
mkdir -p /tmp/mnt/TDDOWNLOAD

After reboot
admin@RT-AX88U:/tmp/home/root# mount | grep sda
/dev/sda1 on /tmp/mnt/opt type ext4 (rw,nodev,relatime,discard,data=ordered)
/dev/sda2 on /tmp/mnt/TDDOWNLOAD type ext4 (rw,nodev,relatime,data=ordered)

Code:
admin@RT-AX88U:/tmp/home/root# lsblk --discard | grep sd
sda               0        0B       0B         0
├─sda1            0        0B       0B         0
└─sda2            0        0B       0B         0

The first partition is mounted with the disсard option, while the second is not. Is it generally possible to run a Trim on MerlinWRT?
 
I would be shocked if this is possible on any current router.
 
An SSD aware OS.
What is missing from this OS (MerlinWRT)?
I used this SSD on this router for half a year without trim, the speeds did not drop much, but the recording gain was 4x.
According to the indicators of SMART, 2.7 TB was recorded, while the number of cell rewrites was 43, from here we have 43 * 240 GB = 10.3 TB

If the problem is in defining the bridge JMS580, I can change the vid/pid to any, for experiment (tell me which one).
 
Last edited:
I do not know what is missing specifically/technically. But having an SSD run without TRIM is different from having it run with it (the topic of this thread).

Maybe it is possible to do this? But again, I would be shocked if this were possible today.

And if it is possible, this is something that RMerlin and Asus should be compiling the firmware to support internally, in some near-future timeframe.
 
And if it is possible, this is something that RMerlin and Asus should be compiling the firmware to support internally, in some near-future timeframe.
RT-AС86U 3.0.0.4.384_82072 (last stock Asus) supports fstrim now?
Code:
admin@RT-AC86U-2498:/tmp/home/root# fstrim
BusyBox v1.24.1 (2020-08-08 01:33:36 CST) multi-call binary.

Usage: fstrim [OPTIONS] MOUNTPOINT

-o,--offset=OFFSET Offset in bytes to discard from
-l,--length=LEN Bytes to discard
-m,--minimum=MIN Minimum extent length
-v,--verbose Print number of discarded bytes
admin@RT-AC86U-2498:/tmp/home/root#
The owners of this model check the functionality.
 
Last edited:
@L&LD I found a solution to how to turn on the trim, working fine. From here.

The solution is to switch the SCSI driver from the UAS mode to the UNMAP mode.
Code:
admin@RT-AX88U:/tmp/home/root# cat /sys/block/sda/device/scsi_disk/0\:0\:0\:0/provisioning_mode
full
admin@RT-AX88U:/tmp/home/root# echo -n unmap > /sys/block/sda/device/scsi_disk/0\:0\:0\:0/provisioning_mode
admin@RT-AX88U:/tmp/home/root# cat /sys/block/sda/device/scsi_disk/0\:0\:0\:0/provisioning_mode
unmap
admin@RT-AX88U:/tmp/home/root# lsblk --discard /dev/sda
NAME   DISC-ALN DISC-GRAN DISC-MAX DISC-ZERO
sda           0        4K       4G         0
├─sda1        0        4K       4G         0
└─sda2        0        4K       4G         0
admin@RT-AX88U:/tmp/home/root# opkg install fstrim
Installing fstrim (2.35.1-2) to root...
Downloading http://bin.entware.net/aarch64-k3.10/fstrim_2.35.1-2_aarch64-3.10.ipk                                               k
Configuring fstrim.
admin@RT-AX88U:/tmp/home/root# fstrim -v /tmp/mnt/TDDOWNLOAD
/tmp/mnt/TDDOWNLOAD: 156.2 GiB (167684620288 bytes) trimmed
admin@RT-AX88U:/tmp/home/root#
 
Last edited:
Traveling into darkness here, but on my ax88 with attached sata drive ...

I think before setting the provisioning mode you need to download sg3_utils ('opkg install sg3_utils' and you may also want 'opkg install lsblk'). Now you can run the command:

sg_vpd --page=0xb2 /dev/sda

You'll get a response like this:

Logical block provisioning VPD page (SBC):
Unmap command supported (LBUP): 1
Write same (16) with unmap bit supported (LBPWS): 0
Write same (10) with unmap bit supported (LBPWS10): 0
Logical block provisioning read zeros (LBPRZ): 0
Anchored LBAs supported (ANC_SUP): 0
Threshold exponent: 1
Descriptor present (DP): 0
Minimum percentage: 0 [not reported]
Provisioning type: 0 (not known or fully provisioned)
Threshold percentage: 0 [percentages not supported]

Here are the options (more info at https://gist.github.com/cathay4t/e80e02a737242a5f3824606543631bfe):

  • If LBP VPD is not supported:
    • Use UNMAP command if BL VPD says MAXIMUM UNMAP BLOCK DESCRIPTOR COUNT bigger than 1.
    • Use Write same 16 command otherwise.
  • If LBP VPD is supported:
    • Use UNMAP 0x42 command if LBP VPD says LBUP is 1.
    • Use WRITE SAME (16) 0x93 command if LBP VPD says LBPWS is 1.
    • Use WRITE SAME (10) 0x41 command if LBP VPD says LBPWS10 is 1.
    • Disable unmap support otherwise.

Even after provisioning to unmap, I got the following output from fstrim:

FITRIM ioctl failed: Input/output error
 
it's "paper" rubbish, its controller does not support unmap, only writesame_16
$ sudo sg_vpd --page=0xb2 /dev/sda
Logical block provisioning VPD page (SBC):
Unmap command supported (LBPU): 0
Write same (16) with unmap bit supported (LBWS): 1
Write same (10) with unmap bit supported (LBWS10): 0
Logical block provisioning read zeros (LBPRZ): 0
everything is here https://wiki.archlinux.org/index.php/Solid_state_drive#External_SSD_with_TRIM_support
the only difference command:
echo "unmap" >/sys/block/sdX/device/scsi_disk/*/provisioning_mode
echo -n unmap > /sys/block/sdX/device/scsi_disk/*/provisioning_mode
verification:
cat /sys/block/sdX/device/scsi_disk/*/provisioning_mode
lsblk --discard /dev/sdX

What is the model of your USB-SATA controller and what is the command output hdparm -I /dev/sdX | grep TRIM
 
Last edited:
it's "paper" rubbish, its controller does not support unmap, only writesame_16

everything is here https://wiki.archlinux.org/index.php/Solid_state_drive#External_SSD_with_TRIM_support
the only difference command:

verification:


What is the model of your USB-SATA controller and what is the command output hdparm -I /dev/sdX | grep TRIM
The controller the author was looking at did not support unmap. The method to query the drive (sg_vpd --page=0xb2 /dev/sda) is valid
My drive is a Sabrent 120GB usb-sata (120GB)
output from: hdparm -I /dev/sda | grep TRIM
* Data Set Management TRIM supported (limit 8 blocks)
* Deterministic read ZEROs after TRIM
 
lsusb on the router doesn't seem to accept any flags. the only output i can get is:

Bus 002 Device 002: ID 152d:1561
Bus 001 Device 001: ID 1d6b:0002
Bus 002 Device 001: ID 1d6b:0003
Bus 003 Device 001: ID 1d6b:0002
Bus 004 Device 001: ID 1d6b:0001
 
Bus 002 Device 002: ID 152d:1561

It`s JMicron, most likely model JMS578, but you must make sure of this by disassembling the case and reading the markings on the chip.
There were a lot of firmware patches for this model, visit the container manufacturer's website and read this.

/jffs/scripts/trim.ssd
Code:
#!/bin/sh
busybox --list | grep -qw fstrim || opkg list-installed | grep -qw fstrim || opkg install fstrim > /dev/null 2>&1
DEV=$(mount | grep -E $1.*ext4 | sed -rn 's/^\/dev\/(.{4}).+/\1/p')
if [ -n "${DEV}" ]; then
   echo -n unmap > $(find /sys/block/sd${DEV:2:1}/device/ -name provisioning_mode)
   if [ -n "$(fstrim -v $1 | grep -w trimmed)" ]; then
      if [ -f /jffs/scripts/scribe ]; then LOG=/opt/var/log/messages ; else LOG=/tmp/syslog.log ; fi
      cru a TRIM_SSD_${1:9} "3${DEV:3:1} */12 * * * fstrim -v $1 >> $LOG 2>&1"
      if [ -z "$(grep -w '#!/bin/sh' /jffs/scripts/unmount)" ]; then
         echo '#!/bin/sh' > /jffs/scripts/unmount ; echo 'cru d TRIM_SSD_${1:9}' >> /jffs/scripts/unmount ; chmod -R 755 /jffs/scripts/unmount
      elif [ -z "$(grep -w 'cru d TRIM_SSD_${1:9}' /jffs/scripts/unmount)" ]; then
           echo 'cru d TRIM_SSD_${1:9}' >> /jffs/scripts/unmount
      fi
   else
      echo -n disabled > $(find /sys/block/sd${DEV:2:1}/device/ -name provisioning_mode)
   fi
fi

Run once
Code:
chmod -R 755 /jffs/scripts/trim.ssd ; if [ -z "$(grep -w '#!/bin/sh' /jffs/scripts/post-mount)" ]; then echo '#!/bin/sh' > /jffs/scripts/post-mount ; echo '. /jffs/scripts/trim.ssd' >> /jffs/scripts/post-mount ; chmod -R 755 /jffs/scripts/post-mount ; elif [ -z "$(grep -w '. /jffs/scripts/trim.ssd' /jffs/scripts/post-mount)" ]; then echo '. /jffs/scripts/trim.ssd' >> /jffs/scripts/post-mount ; chmod -R 755 /jffs/scripts/post-mount ; fi
 
Last edited:

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top