TRIM on USB SSD with Linux

Vijay
Written by Vijay on
TRIM on USB SSD with Linux

I have an external USB SSD. I bought a StarTech.com USB 3.1 to 2.5" SATA Hard Drive Adapter with model number USB312SAT3CB to connect it to my computer. Problem was my computer with Linux OS wasn’t recognizing the external SSD as something which supports TRIM. Should I give up all the goodness of TRIMming it? I didn’t want to.

It is Linux, you can fix anything (or at-least try to)

First, I had to update the adapter firmware as given in other reviews/posts using a windows computer. Verified that Windows OS could successfully TRIM the drive. Then I moved over to my Ubuntu Linux machine for the next steps.

Configure udev to enable TRIM

I created an Ext4 partition but I couldn’t see any support for TRIM on that partition. In order for the kernel to recognize this adapter as something which supports discard option I had to add a new udev rule.

$ cat /etc/udev/rules.d/50-usb-ssd-trim.rules
# Enable TRIM on startech connected 860EVO SSD
ACTION=="add|change", ATTRS{idVendor}=="174c", ATTRS{idProduct}=="55aa", SUBSYSTEM=="scsi_disk", ATTR{provisioning_mode}="unmap"

With udev rules in place, next time when I plugin the SSD it was shown to support TRIM.

$ lsblk --discard /dev/sda
NAME DISC-ALN DISC-GRAN DISC-MAX DISC-ZERO
sda         0      512B       4G         0
└─sda1      0      512B       4G         0

Let the filesystem do the TRIM

That’s interesting. Now that the block device supports discard option, the filesystem should be able to use it. I mounted the partition using discard option. I put something like below in fstab and was able to successfully run fstrim on the partition.

/dev/disk/by-label/860EVO /mnt/860EVO auto nosuid,nodev,nofail,x-gvfs-show,discard 0 0

Don’t forget to secure external disk

That was it, my SSD supported fstrim. But, I still couldn’t use it because any external storage should be encrypted to secure the data. Read on to find how I was able to encrypt the partition keeping the support for TRIM.

I created a LUKS encrypted volume to enable encryption. For TRIM support I added discard option in /etc/crypttab. It didn’t work. I tried manually decrypting the drive using this command.

sudo cryptsetup open --allow-discards /dev/sda1 luks-ead60769-c0c7-43ed-ad5f-0541a01d5d60

This seemed to work. What was the difference between using discard in /etc/crypttab and --allow-discards? ¯\_(ツ)_/¯

With the data secured, the SSD still supported TRIM

$ lsblk --discard /dev/sda
NAME                                          DISC-ALN DISC-GRAN DISC-MAX DISC-ZERO
sda                                                  0      512B       4G         0
└─sda1                                               0      512B       4G         0
  └─luks-ead60769-c0c7-43ed-ad5f-0541a01d5d60        0      512B       4G         0

Block device supported TRIM. I ran the actual trim operation after mounting the filesystem to see everything in action.

$ sudo fstrim -v /mnt/860EVO
/mnt/860EVO: 916.7 GiB (984267231232 bytes) trimmed

That was cool, but took me a while and many trial and errors to get everything lined up to get it working.