Email WhatsApp
WeChat
WeChat QR

Table of Contents

SSD TRIM with fstrim

On Linux, fstrim tells a mounted filesystem to report its unused blocks to the drive. On an SSD that accepts discard, those ranges give the controller room to plan subsequent writes. The command takes a mount point, so a root filesystem and a second disk mounted at /srv/data are two separate checks.

Find the filesystem first, especially if the board boots from eMMC and the application writes to an SSD. You can have a perfectly readable disk in a USB enclosure while the kernel reports zero discard support for that connection. The quickest way to learn which case you have is to look at the block device and then run fstrim on the intended mount.

Illustrated SATA SSD on a Linux fstrim guide cover
TRIM support depends on the filesystem and the connection to the drive.

Find the filesystem and the drive

Check the mount point

Run findmnt against the path your application actually uses. The command below uses /srv/data as an example. If that disk failed to mount and the directory still exists, the command can return the root filesystem instead. Read the TARGET column before doing anything with that result.

findmnt -T /srv/data -o TARGET,SOURCE,FSTYPE
lsblk -o NAME,TYPE,FSTYPE,MOUNTPOINTS,TRAN
lsblk --discard
Illustration of a single-board computer with an M.2 NVMe SSD installed
An M.2 NVMe SSD mounted on a single-board computer.

This is especially useful for a KiwiPi single-board server that keeps the OS on one drive and SQLite, container volumes or recordings on another. The numbers from a Linux disk space check reflect what the filesystem can still allocate. TRIM happens after the filesystem has freed space. If a partition is full, find the files taking it up first.

Follow discard through the connection

Look at DISC-MAX for the block device that backs the mount. A nonzero value is the maximum discard request size Linux exposes for that device. If the source is a device-mapper volume, read the row for that volume too. The physical SSD might advertise discard while an intermediate layer doesn’t pass it through.

Illustration of a 2.5-inch SATA SSD connected to a USB-to-SATA adapter
A SATA SSD connected through USB. Discard has to pass through the adapter.

A native PCIe NVMe drive and a SATA SSD behind USB can both be visible in Linux. On the USB path, the enclosure’s bridge and firmware have to carry the operation to the drive. The USB link speed and the label on the box leave that part open. If the disk itself is missing, check NVMe detection first, then come back to discard once the mounted filesystem is visible.

Diagram showing a discard request from the filesystem through the block device to the SSD
Check the mount and block device before testing whether discard reaches the storage path.

Run fstrim on the mounted filesystem

Test one mount

For an SSD-backed root filesystem, sudo fstrim -v / sends a request for the mounted root. If the SSD holds a separate data filesystem, pass its mount point instead. Run only the line for the mount you identified above. The kernel and filesystem handle the actual request, so it’s worth doing the first run when a storage-heavy job can wait.

sudo fstrim -v /
# Example for a separate mounted SSD filesystem
sudo fstrim -v /srv/data

The number after bytes trimmed can look impressive. It represents the amount the filesystem submitted for potential discard. The drive makes its own decisions about when to reclaim the space. The same number can show up on a second run. An unsupported-operation error is a better reason to revisit the connection than a large byte count is to run the command twice.

Result What to check
findmnt returns a different target Check that the intended SSD is mounted
DISC-MAX is zero on the source device Inspect the storage path and any mapping layer
DISC-MAX is nonzero but fstrim fails Check the filesystem, device stack and error text
fstrim -v returns successfully Check the timer before adding any scheduled job

Schedule periodic TRIM

Inspect the systemd units

On a systemd image that ships the util-linux timer, check when it runs and which service it calls. The upstream timer runs weekly and is persistent, so a missed run can be caught after the machine returns. The units on your particular image can have different settings or overrides. systemctl cat shows what systemd has loaded for both units.

systemctl list-timers --all fstrim.timer
systemctl cat fstrim.timer fstrim.service
journalctl -u fstrim.service --no-pager -n 20

If the timer exists, the run succeeded on the intended filesystem and you want periodic TRIM, enable it with sudo systemctl enable --now fstrim.timer. Check the next run and the service log after it fires. Read the service command if your data drive is mounted on demand. A timer can run while that filesystem is absent, and some packaged invocations skip unsupported mounts without printing an error.

FAQ

Will fstrim free more space according to df?

Deleting data and closing the last open reference free filesystem blocks. fstrim sends an onward request for blocks that are already free, so df can show the same space before and after.

Do I need the discard mount option?

A periodic fstrim uses a separate filesystem operation. You can run it without adding the continuous discard mount option, as long as the mounted filesystem and storage path support discard.

Is weekly TRIM enough for a home server?

The util-linux manual suggests weekly trimming for most desktop and server systems. Check the actual timer and the service log before changing its schedule.

Can fstrim work on eMMC?

Linux can send discard to eMMC when the device and kernel expose it. On a board that boots from eMMC, check the source for / with findmnt -T / -o TARGET,SOURCE. Look up that device in lsblk --discard before you try sudo fstrim -v /.

Why does TRIM fail on my LUKS volume?

Check the mapped device in lsblk --discard. The SSD can report a nonzero DISC-MAX while /dev/mapper/cryptdata shows zero, because dm-crypt ignores discard by default. Letting discard through can reveal which parts of the encrypted disk are unused.

Can I trim just the directory where Docker keeps its data?

Run findmnt -T /srv/data/docker -o TARGET,SOURCE on the host, using your actual data path. If TARGET is /srv/data, pass that mount point to fstrim. It works on free blocks across the filesystem, outside the Docker directory too.

Does fstrim -av check every mounted disk?

fstrim -av tries mounted filesystems on discard-capable devices. It can skip unsupported and read-only filesystems without reporting an error. To check a newly installed SSD, pass its confirmed mount point to fstrim -v and read the result for that filesystem.

Sources

Related Products

Kiwipi-5Pro

KiwiPi 5 Pro

KiwiPi 5 Pro is powered by Rockchip RK3588 8-core 64-bit processor with 4 Cortex-A76 (2.2GHz), 4 Cortex-A55(1.8GHz), and an independent NEON coprocessor.

Get in Touch about Your Needs

Contact Us