10

fstrim requires the Linux block device to be mounted, and it is not very verbose. blkdiscard could tell, but also that would require a write operation.

Can I somehow tell if a block device supports trimming/discarding, without actually trying to trim/discard something on it?

2
  • /sys/block/*/capabilities has no bit for that (ref).
    – peterh
    Commented Sep 9, 2021 at 13:49
  • 2
    Heh, those docs came straight from this answer here ;-). Commented Sep 9, 2021 at 14:09

3 Answers 3

18

You can check the device’s maximum discard sizes, e.g.

$ cat /sys/block/X/queue/discard_max_hw_bytes

(replacing X as appropriate).

If this shows a value greater than 0, the device supports discards:

A discard_max_hw_bytes value of 0 means that the device does not support discard functionality.

The maximum supported discard size is indicated by discard_max_bytes in the same directory; this can be smaller than the hardware-supported value to limit discard latencies (and can be written to to change the limit):

While discard_max_hw_bytes is the hardware limit for the device, this setting is the software limit. Some devices exhibit large latencies when large discards are issued, setting this value lower will make Linux issue smaller discards and potentially help reduce latencies induced by large discard operations.

This works on many different block devices, not just disks: loop devices, device mapper devices, etc.

9

Utilize the hdparm command, which lets you perform low level commands on a disk device.

The option hdparm -I /dev/sda (or insert block device instead of sda), will query the drive for information. There is a section that lists the supported operations of the drive.

Running hdparm -I /dev/sda | grep TRIM on a TRIM supporting drive will output something like:

    *    Data Set Management TRIM supported (limit 1 block)

There is also a lot of other useful information in the output, the hdparm command is very useful.

Note this will only work on SATA and some SCSI devices, since hdparm is designed to run SATA commands on devices.

3
  • So why does fstrim only trim mounted "devices"? Commented Sep 9, 2021 at 18:11
  • 4
    @Jeremy because fstrim trims file systems, not block devices — it asks the file system to trim itself, so the file system has to be mounted. If you want to discard blocks from a block device, without mounting it, use blkdiscard; you’ll have to figure out which blocks to discard. Commented Sep 9, 2021 at 19:05
  • 3
    Note that hdparm only really works for ATA devices (and possibly some SCSI devices). NVMe, MMC/SD, some USB devices, device-mapper targets, ramdisks, and plenty of other device types do not return any useful information when you run hdparm -I against them. Commented Sep 10, 2021 at 6:00
4

To check if the device supports trim/discard one can also use linux lsblk utility. If the device's discard limits like granularity and max_sectors is non-zero, then the discard/trim support is enabled, e.g.,

$ lsblk -D
NAME      DISC-ALN DISC-GRAN DISC-MAX DISC-ZERO
sda              0      512B       2G         0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .