9

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?

Peter Mortensen
  • 1,029
  • 1
  • 8
  • 10
peterh
  • 9,488
  • 16
  • 59
  • 88
  • /sys/block/*/capabilities has no bit for that ([ref](https://www.kernel.org/doc/html/latest/block/capability.html)). – peterh Sep 09 '21 at 13:49
  • 2
    Heh, those docs came straight from [this answer here](https://unix.stackexchange.com/a/571106/86440) ;-). – Stephen Kitt Sep 09 '21 at 14:09

3 Answers3

16

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.

Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
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.

dcom-launch
  • 653
  • 3
  • 16
  • So why does `fstrim` only trim **mounted** "devices"? – Jeremy Boden Sep 09 '21 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. – Stephen Kitt Sep 09 '21 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. – Austin Hemmelgarn Sep 10 '21 at 06:00
3

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
muru
  • 69,900
  • 13
  • 192
  • 292
GauriK
  • 31
  • 1