7

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
    Sep 9, 2021 at 13:49
  • 2
    Heh, those docs came straight from this answer here ;-). Sep 9, 2021 at 14:09

3 Answers 3

14

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

$ cat /sys/block/X/queue/discard_max_bytes

(replacing X as appropriate).

If this shows a value greater than 0, the device supports discards. Strictly speaking, discard_max_hw_bytes indicates what the hardware supports; discard_max_bytes indicates what the software supports, and the latter is usually what‘s relevant:

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

(This is in the discard_max_hw_bytes section, but it’s effectively true for both. The references will be fixed in 5.15.)

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

8

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"? 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. 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. Sep 10, 2021 at 6:00
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.