2023年2月17日 星期五

在 Linux 下對 USB 外接 SSD 執行 fstrim

參考連結,Enable Trim on an External SSD on LinuxFixing Storage Adapters for Raspberry Pi via Firmware Updates

為了保持 SSD 的效能,在使用一陣子後,必須執行 fstrim,將刪除的 block 回收。但是 USB 外接硬碟盒不一定支援 unmap 的功能,目前 (2022) 可能只有 VIA 的晶片才支援,而且要稍微費一些功夫才能執行 fstrim。

參考

在  Gentoo 下,要安裝 sys-apps/sg3_utils,並且只支援 USB3,USB2 不能用。

買的時候,參考 ArchLinux 的 wiki,選擇 Orico 的外接硬碟盒,使用的晶片是 VIA 的 VL716。

以下整段抄自 ArchLinux 的 wiki,經測試 OK。

Several USB-to-SATA bridge chips (like VL715, VL716 etc.) and also USB-to-PCIe bridge chips (like the JMicron JMS583 used in external NVMe enclosures like IB-1817M-C31) support TRIM-like commands that can be sent through the USB Attached SCSI driver (named "uas" under Linux).

But the kernel may not automatically detect this capability, and therefore might not use it. Assuming your block device in question is /dev/sdX, you can find out whether that is the case by using the command

# sg_readcap -l /dev/sdX

If in its output you find a line stating "Logical block provisioning: lbpme=0" then you know that the kernel assumes the device does not support "Logical Block Provisioning Management" because the (LBPME) bit is not set.

If this is the case, then you should next find out whether the "Vital Product Data" (VPD) page on "Logical Block Provisioning" of your device tells of supported mechanisms for unmapping data. You can do this using the command:

# sg_vpd -a /dev/sdX

Look for lines in the output that look like this:

Unmap command supported (LBPU): 1
Write same (16) with unmap bit supported (LBPWS): 0
Write same (10) with unmap bit supported (LBPWS10): 0

This example would tell you the device supports the "UNMAP" command.

Have a look at the output of

$ cat /sys/block/sdX/device/scsi_disk/*/provisioning_mode

If the kernel did not detect the capability of your device to unmap data, then this will likely return "full". Apart from "full", the kernel SCSI storage driver currently knows the following values for provisioning_mode:

unmap
writesame_16
writesame_10
writesame_zero
disabled

For the example above, you could now write "unmap" to "provisioning_mode" to ask the kernel to use that:

# echo "unmap" >/sys/block/sdX/device/scsi_disk/*/provisioning_mode

This should immediately enable you to use tools like "blkdiscard" on /dev/sdX or "fstrim" on filesystems mounted on /dev/sdX.

If you want to enable a "provisioning_mode" automatically when an external device of a certain vendor/product is attached, this can be automated via the "udev" mechanism. First find the USB Vendor and Product IDs:

$ cat /sys/block/sdX/../../../../../../idVendor
$ cat /sys/block/sdX/../../../../../../idProduct

Then create or append to a udev rule file (example here using idVendor 152d and idProduct 0583):

# echo 'ACTION=="add|change", ATTRS{idVendor}=="152d", ATTRS{idProduct}=="0583", SUBSYSTEM=="scsi_disk", ATTR{provisioning_mode}="unmap"' >>/etc/udev/rules.d/10-uas-discard.rules

(You can also use the lsusb command to look for the relevant idVendor/idProduct.)


沒有留言:

張貼留言

網誌存檔