Skip to content

SSD TRIM on Linux: fstrim.timer versus the discard mount option

Learn how to enable SSD TRIM on Linux with fstrim.timer, and how to check with lsblk --discard that your discards still reach the drive through LUKS, LVM, mdraid and virtio. You will also see why the continuous discard mount option is rarely the right default.

This guide shows you how to set up TRIM on your Linux servers, the message that tells a solid-state drive (SSD) which blocks it no longer has to track. Without it, the drive keeps shuffling around data you deleted long ago, which burns flash endurance and makes write speed turn spiky under load. For almost every server the answer is fstrim.timer, a systemd timer that runs one batched cleanup pass a week. The continuous discard mount option sends one of those cleanup messages on every file you delete, and that is rarely what you want. Any layer between the filesystem and the drive can drop a discard without reporting an error, so you will also learn to read lsblk --discard and find the one that did.

What you’ll accomplish

  • Confirm the physical device advertises discard, and read lsblk --discard output without guessing.
  • Trace a discard down through LUKS, LVM, mdraid and a virtio disk, and identify the exact layer swallowing it.
  • Enable and schedule fstrim.timer, after a dry run that shows which mount points it will touch.
  • Choose per filesystem: periodic for ext4 and XFS, discard=async for Btrfs, inline discard only where it is justified.
  • Make TRIM genuinely return capacity to a thin-provisioned host image or a SAN LUN.

Prerequisites

  • A Linux kernel from the 4.x series or newer. Commands below were run on 6.8 and behave the same on 5.15.
  • util-linux 2.30 or newer for fstrim and lsblk --discard. The --listed-in form in the modern fstrim.service unit arrived in 2.37; older distributions ship a unit calling fstrim -Av instead. Both are fine.
  • systemd, for the timer. Without systemd, the same fstrim command belongs in a weekly cron job.
  • root or sudo, plus cryptsetup and lvm2 if those layers are in play.
  • Distro differences are real. Fedora, RHEL 8+, Debian 10+ and Ubuntu 18.04+ enable fstrim.timer out of the box; minimal Arch, Alpine and most container-host images do not. Check rather than assume.
  • ZFS is out of scope: it does not use fstrim at all, it has zpool trim and the autotrim pool property.

How it works

A filesystem and an SSD disagree about what “free” means. When you delete a file, the filesystem marks blocks available in its own metadata and moves on; the drive never hears about it. To the flash translation layer, every block ever written still holds live data, so garbage collection keeps relocating that dead data during erase-block reclamation. The result is write amplification: one host write costs several flash writes. The drive burns endurance on garbage, and once the spare area is squeezed, write latency turns spiky under sustained load.

A discard is the message that settles the disagreement — ATA calls it TRIM, SCSI calls it UNMAP, NVMe calls it Dataset Management with the Deallocate attribute. It tells the device which logical ranges no longer matter, so garbage collection can skip them. For why this matters more on some devices than others, our comparison of NVMe and SATA SSDs for cloud VMs covers the queueing and controller side.

The catch is that a discard is advisory and travels through a stack of block layers, each of which can refuse to forward it without raising an error.

Layered diagram showing a discard passing from the filesystem down through dm-crypt, LVM, mdraid, a virtual disk and finally to the SSD, with the option each layer requires listed alongside

There are two ways to emit discards. Periodic trim runs fstrim, which asks the filesystem via the FITRIM ioctl to walk its free-space map and issue discards for large contiguous ranges. Continuous discard makes the filesystem issue a discard inline as blocks are freed, on every delete and every truncate. The second sounds better and is usually worse.

Side-by-side comparison of periodic fstrim, which batches one trim pass per week, against continuous discard, which emits a separate discard for every file deletion

Inline discard fragments the work into thousands of tiny UNMAP commands, and on drives with slow discard handling — plenty of consumer SATA parts, some older enterprise SAS ones — those commands sit in the queue ahead of your reads. fstrim coalesces free space into a few large ranges and issues them once, off the hot path.

Step-by-step

1. Confirm the physical device supports discard. The kernel exposes this per block device in sysfs. Zero means no discard support, or a device that lies about it.

cat /sys/block/nvme0n1/queue/discard_max_bytes
2199023255552

For SATA SSDs behind a controller, hdparm reports the ATA-level capability:

sudo hdparm -I /dev/sda | grep -i trim
	   *	Data Set Management TRIM supported (limit 8 blocks)
	   *	Deterministic read ZEROs after TRIM

2. Read the whole stack at once with lsblk --discard. The single most useful command in the guide. It prints discard properties for every device in the tree, so you can see exactly where they collapse to zero.

lsblk --discard
NAME             DISC-ALN DISC-GRAN DISC-MAX DISC-ZERO
nvme0n1                 0      512B       2T         0
├─nvme0n1p1             0      512B       2T         0
└─nvme0n1p2             0      512B       2T         0
  └─cryptroot           0        0B       0B         0
    ├─vg0-root          0        0B       0B         0
    └─vg0-data          0        0B       0B         0

Read that top to bottom. The NVMe device and its partitions advertise discard. The moment the tree passes through cryptroot, DISC-GRAN and DISC-MAX go to zero and everything below inherits that zero — dm-crypt refusing to forward discards, which is its default. DISC-ZERO reads 0 on essentially all modern kernels; the discard_zeroes_data flag it reflects was removed in 4.12 and is not a problem indicator.

3. Dry-run fstrim before you touch anything. --dry-run skips the ioctl but still enumerates the mount points that would be processed, which is a fast sanity check on what -a actually covers.

sudo fstrim -av --dry-run
/boot: 0 B (0 bytes) trimmed on /dev/nvme0n1p1
/home: 0 B (0 bytes) trimmed on /dev/mapper/vg0-data
/: 0 B (0 bytes) trimmed on /dev/mapper/vg0-root

The byte counts are zero because nothing ran. The list is what matters: a filesystem missing here will never be trimmed by fstrim -a, usually because it is a bind mount, a read-only mount, or sits on a device the kernel reports as non-discardable.

4. Let LUKS forward discards. With cryptsetup 2.x you can set the flag on a running mapping and persist it into the LUKS2 header in one command, no reboot involved.

sudo cryptsetup --allow-discards --persistent refresh cryptroot
sudo cryptsetup luksDump /dev/nvme0n1p2 | grep -i flags
Flags:       allow-discards

Confirm the live device-mapper table picked it up:

sudo dmsetup table cryptroot | grep -o allow_discards
allow_discards

For LUKS1, or for volumes opened from /etc/crypttab, add the option to the fourth field instead and rebuild the initramfs if the volume is decrypted at boot:

cryptroot UUID=8f3d1a52-7c04-4b19-9f2e-1d3a5c6e8b70 none luks,discard
sudo update-initramfs -u -k all
update-initramfs: Generating /boot/initrd.img-6.8.0-45-generic

Read the trade-off in the troubleshooting section before doing this on a laptop or on anything holding regulated data.

5. Set issue_discards in LVM. This knob is widely misunderstood. It does not control whether fstrim works — fstrim reaches the physical volume through device-mapper regardless. It controls whether LVM discards the underlying extents on lvremove, lvreduce and pvmove, so freed extents come back to the drive or the thin pool.

sudo sed -i 's/^\(\s*\)issue_discards = 0/\1issue_discards = 1/' /etc/lvm/lvm.conf
sudo lvmconfig devices/issue_discards
issue_discards=1

If you run LVM thin pools, they have their own discard policy; our guide on LVM thin snapshots and backups covers how pool space is accounted.

6. Enable the timer. fstrim.timer ships in util-linux, not systemd. It defaults to OnCalendar=weekly with Persistent=true, so a machine powered off at the scheduled moment catches up on next boot.

sudo systemctl enable --now fstrim.timer
systemctl list-timers fstrim.timer --no-pager
NEXT                        LEFT     LAST                        PASSED UNIT         ACTIVATES
Mon 2026-08-03 00:34:12 UTC 22h left Sun 2026-07-27 00:21:05 UTC 6d ago fstrim.timer fstrim.service

Weekly is right for almost everyone. Move to daily only if the workload churns a large fraction of the device every day; hourly just recreates the inline-discard problem with extra steps. Override with a drop-in rather than editing the shipped unit:

sudo systemctl edit fstrim.timer
[Timer]
OnCalendar=
OnCalendar=daily
RandomizedDelaySec=1h

The empty OnCalendar= line is required: it clears the vendor value instead of adding a second schedule.

7. Pass discards into virtual machines. A guest can do everything right and still discard into a void if the hypervisor disk is not configured for it. In libvirt the attribute lives on the driver element:

<disk type='file' device='disk'>
  <driver name='qemu' type='qcow2' cache='none' discard='unmap'/>
  <source file='/var/lib/libvirt/images/app01.qcow2'/>
  <target dev='vda' bus='virtio'/>
</disk>

The guest must then see non-zero discard properties. Check inside the VM:

lsblk --discard /dev/vda
NAME DISC-ALN DISC-GRAN DISC-MAX DISC-ZERO
vda         0      512B       1G         0

virtio-scsi has carried discard far longer than virtio-blk, which needs QEMU 4.0 or newer on the host and a 5.0 or newer kernel in the guest. If DISC-MAX reads 0B on a vda, switching the bus to scsi behind a virtio-scsi controller is often quicker than arguing with an old hypervisor.

8. Pick the right filesystem behaviour. ext4 and XFS both default to no inline discard and both work correctly with periodic fstrim — leave them alone. Btrfs is the exception: since kernel 6.2 it enables discard=async by default, queueing discards and draining them off the transaction commit path, which gives continuous reclaim without the latency penalty. Verify what is actually mounted:

findmnt -no OPTIONS / | tr ',' '\n' | grep discard
discard=async

No output on an ext4 or XFS root is the correct result. If the filesystem itself is still undecided, our ext4, XFS and ZFS comparison covers the wider trade-offs.

Validate it works

Re-run the stack check. Every row should now carry the device’s granularity down to the leaves:

lsblk --discard
NAME             DISC-ALN DISC-GRAN DISC-MAX DISC-ZERO
nvme0n1                 0      512B       2T         0
├─nvme0n1p1             0      512B       2T         0
└─nvme0n1p2             0      512B       2T         0
  └─cryptroot           0      512B       2T         0
    ├─vg0-root          0      512B       2T         0
    └─vg0-data          0      512B       2T         0

Then run a real trim and read the byte counts — the end-to-end proof that discards are accepted, not merely permitted:

sudo fstrim -av
/boot: 371.4 MiB (389415936 bytes) trimmed on /dev/nvme0n1p1
/home: 214.7 GiB (230528991232 bytes) trimmed on /dev/mapper/vg0-data
/: 48.2 GiB (51753517056 bytes) trimmed on /dev/mapper/vg0-root

Confirm the scheduled run rather than the manual one:

sudo systemctl start fstrim.service && journalctl -u fstrim.service -n 3 --no-pager
Starting Discard unused filesystem blocks...
fstrim[4417]: /: 48.2 GiB (51753517056 bytes) trimmed on /dev/mapper/vg0-root
Finished Discard unused filesystem blocks.

On a thin-provisioned VM the meaningful test runs on the host, not in the guest. Note the image size, delete a large file in the guest, run fstrim, look again:

qemu-img info /var/lib/libvirt/images/app01.qcow2 | grep 'disk size'
disk size: 41.3 GiB

If that number falls after the guest trims, the discard traversed the entire stack and the host reclaimed the space. If it does not move, something between the guest filesystem and the qcow2 file is dropping the discard.

Troubleshooting

fstrim exits successfully but nothing is trimmed. Run lsblk --discard and find the first row where DISC-MAX becomes 0B. That is the culprit; everything below it is a symptom. The usual offender is dm-crypt without allow-discards, followed by a virtual disk without discard=unmap. A louder relative of this failure — fstrim: /mnt/data: FITRIM ioctl failed: Operation not supported — means the filesystem itself refused, typically a network mount, a loop device, or a filesystem without FITRIM support.

The second fstrim reports 0 B and you assume it broke. It did not. ext4 remembers which block groups it already trimmed since the last mount and skips them, so an immediate second run legitimately returns almost nothing. XFS does not track this and re-trims all free space every run — which is why the same command reports gigabytes on XFS and zero on ext4 minutes apart. Judge success by the first run after a mount, or after real deletions.

LUKS is configured but discards still stop at the mapper. Two things get missed. cryptsetup refresh changes the running mapping, but a root volume opened from the initramfs is reopened at every boot from the initramfs copy of the configuration — so /etc/crypttab needs the discard option and the initramfs needs regenerating, or on dracut systems rd.luks.options=discard on the kernel command line. Second, --persistent only works on LUKS2 headers; a LUKS1 volume has nowhere to store the flag and must receive it at every open.

The RAID controller is not passing UNMAP. mdraid forwards discard on RAID 0, 1 and 10. For RAID 4/5/6 it is disabled by default behind a module parameter, because a drive that does not deterministically zero trimmed blocks can corrupt parity reconstruction:

cat /sys/module/raid456/parameters/devices_handle_discard_safely
N

Leave that at N unless the drive vendor documents deterministic zeroing after TRIM. Hardware controllers are worse: most LSI, Broadcom, MegaRAID and PERC cards in RAID mode terminate UNMAP at the controller, and there is no host-side fix. If DISC-MAX reads 0B on the exported volume, the only real remedy is switching that card to HBA or JBOD mode and rebuilding.

Discard on an encrypted volume leaks information. A genuine trade-off, not a formality. allow-discards reveals to anyone holding the ciphertext which sectors are unused — how full the filesystem is, roughly where its metadata sits, often enough structure to fingerprint the filesystem type. On a server in a rack you control that is usually an acceptable price for the endurance benefit. On a laptop that travels, on removable media, or under a compliance regime that assumes the encrypted blob is opaque, it is not. Decide deliberately and write the decision down; this is the one setting here with a security cost.

When this is the wrong approach

If you run ZFS, none of this applies. fstrim has no effect on a zpool; use zpool trim for a manual pass or autotrim=on for continuous behaviour — and note that ZFS leans toward the periodic form for the same latency reasons.

On a network-attached cloud volume the accounting rarely works in your favour. Discards may be accepted and may even help the provider’s back end, but you are billed for provisioned capacity, so trimming does not reduce the bill — and the endurance argument disappears, because you are not the one replacing the flash. Keep the timer enabled, it costs nothing, but do not build a project around it.

If the device is rotational there is nothing to reclaim: DISC-GRAN reads 0B and fstrim refuses. Spinning encrypted arrays sometimes get allow-discards anyway during a copy-paste rollout, buying the information leak with no benefit at all.

And if a filesystem is filling up right now, TRIM is not the tool. Discard reclaims capacity for the device’s internal bookkeeping; it does not give the filesystem back a single byte. When df says a volume is full, work it from the filesystem side — our guide on no space left on device covers deleted-but-open files, inode exhaustion and the other usual causes.

The authoritative references are the fstrim(8) manual page and the Arch Wiki page on solid state drives, which tracks distribution defaults more carefully than most vendor documentation.

If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.