Using 'fstrim' on USB connected SSD

Preface

In Debian GNU/Linux TRIM is disabled by default on USB connected SSDs.

On internal SSDs TRIM operation is scheduled once per week, on USB connected SSDs it can be run manually.

Preparation

Login as user 'root'.

su -l

Install 'sg3-utils'.

apt update
apt upgrade -y
apt install sg3-utils

Connect the external SSD to USB port.

Checking TRIM support

Run 'lsblk --discard' to list all avaliable devices.

lsblk --discard
NAME        DISC-ALN DISC-GRAN DISC-MAX DISC-ZERO
sda                0      512B       2G         0
├─sda1             0      512B       2G         0
├─sda2             0      512B       2G         0
├─sda3             0      512B       2G         0
└─sda4             0      512B       2G         0
sdb                0        0B       0B         0
├─sdb1             0        0B       0B         0
├─sdb2             0        0B       0B         0
├─sdb3             0        0B       0B         0
├─sdb4             0        0B       0B         0
└─sdb5             0        0B       0B         0

DISC-GRAN 0 and DISC-MAX 0 indicate that TRIM is disabled.

Check if USB connected SSD supports TRIM.

sg_vpd -a /dev/sdb | grep 'Unmap'

TRIM is supported if the following line is present:

Unmap command supported (LBPU): 1

Check the path of 'provisioning_mode' file.

ls /sys/block/sdb/device/scsi_disk/*/provisioning_mode

In my case, 'ls' returns:

/sys/block/sdb/device/scsi_disk/6:0:0:0/provisioning_mode

Check SSD's provisioning mode.

cat '/sys/block/sdb/device/scsi_disk/6:0:0:0/provisioning_mode'

'cat' returns:

full

Enabling TRIM support until device is disconnected or system is rebooted

Change SSD's provisioning mode.

>| '/sys/block/sdb/device/scsi_disk/6:0:0:0/provisioning_mode' printf 'unmap\n' cat '/sys/block/sdb/device/scsi_disk/6:0:0:0/provisioning_mode'

'cat' returns:

unmap

Now 'lsblk --discard' should show non-zero values for DISC-GRAN and DISC-MAX.

lsblk --discard
NAME        DISC-ALN DISC-GRAN DISC-MAX DISC-ZERO
sda                0      512B       2G         0
├─sda1             0      512B       2G         0
├─sda2             0      512B       2G         0
├─sda3             0      512B       2G         0
└─sda4             0      512B       2G         0
sdb                0      512B       4G         0
├─sdb1             0      512B       4G         0
├─sdb2             0      512B       4G         0
├─sdb3             0      512B       4G         0
├─sdb4             0      512B       4G         0
└─sdb5             0      512B       4G         0

Get information about filesystems.

blkid | grep 'sdb' | sort

/dev/sdb1: TYPE="ext4"
/dev/sdb2: TYPE="ext4"
/dev/sdb3: TYPE="swap"
/dev/sdb5: TYPE="ntfs"

Perform the TRIM operation

Mount first partition, trim it and unmount it.

mount -o discard /dev/sdb1 /mnt/
fstrim -v /mnt
umount /mnt

Mount second partition, trim it and unmount it.

mount -o discard /dev/sdb2 /mnt/
fstrim -v /mnt
umount /mnt

Swap can be trimmed by 'swapon --discard=once' command.

Perform a single-time discard operation for the whole swap area at swapon.

swapon --discard=once /dev/sdb3
swapoff /dev/sdb3

Mount last partition, trim it and unmount it.

ntfs-3g -o discard /dev/sdb5 /mnt/
fstrim -v /mnt
umount /mnt