Linux Commands

How to Use the fstrim Linux Command

If your Linux system runs on a Solid-State Drive (SSD), then knowing how best to maintain and manage it is worthwhile. SSD allows block discard operations, which trim blocks that are not in use, giving more life to the disk. There are different ways of reclaiming the stale pages of an SSD, and one reliable method is using the fstrim Linux command.

This guide covers how SSD works in storing data and explains how to TRIM and discard unused blocks in SSD using fstrim. Let’s get into it.

What is SSD, and How Does it Store Data?

An SSD is a type of hard disk that supports new technology and performs better. However, its continuous use, if not maintained, leads to degraded performance.

The SSD uses pages, which are the fixed size for units where data is written and read from, and the pages are grouped into larger units, making a block. With SSD, data gets read and written to pages individually, but data erasing occurs at the block level. Besides, only pages that have been zeroed can be written to, which is a limitation as overwriting data is impossible.

Any time the SSD needs to undertake data modification, it must read the old location of the data, modify it in memory, and finally make the modification to the new location, which must be a zeroed page. The old location of the data gets marked as stale and requires reclaiming through the SSD’s garbage collecting processes.

The bad side of using the garbage collecting process is that the actual erasure of the data is not done, and the SSD can’t use the page until it gets informed that the page can be overwritten.

The solution is to use TRIM, which updates the SSD of stale pages and trims them, marking them zeroed and available for use. However, the manual and continuous trim can be expensive and degrade system performance. Instead, regular discarding of unused blocks is the best option. Here is where fstrim comes into play.

How to Use fstrim

Before anything, check if your Linux system supports TRIM using the command below.

$ sudo hdparm -I /dev/sda | grep “TRIM”

You should get a response similar to the one below. Replace /dev/sda with your SSD or keep it if it is the one.

To view the currently available mount options, run the command below.

$ findmnt -O discard

In our case, we have no mount options, implying that the continuous TRIM is disabled.

Also, you can view the /etc/fstab using an editor to see the defined mount options for your system.

To disable the continuous TRIM, remove the word discard from all the lines. Save the file, then exit.

Working With Periodic TRIM in Linux

We will be working with Ubuntu for our example, and the example we use should work across almost all versions of the OS. Ubuntu is a systemd distribution, implying that fstrim has a script that is periodically set to TRIM the SSD. The fstrim tool gets managed by the fstrim.service systemd service unit and the time is fstrim.timer.

To view the status of the currently defined fstrim, use the command below.

$ systemctl status fstrim.timer

It should return to an active status like in the image below.

If you wish to edit the periodic TRIM, you should edit two files. For instance, the default TRIM period is weekly. To change that to hourly, start by opening the /usr/lib/systemd/system/fstrim.timer file and changing the “weekly” to “hourly.”

$ sudo nano /usr/lib/systemd/system/fstrim.timer

Next, open the fstrim.service file.

$ sudo nano /usr/lib/systemd/system/fstrim.service

Replace line 8 to read:

ExecStart=/sbin/fstrim -av

Lastly, save and close the files, reload the daemon, and restart the fstrim.timer using the two commands below.

$ sudo systemctl daemon-reload
$ sudo systemctl restart fstrim.timer

That should do the magic, and your fstrim is now set to your preferred period.

Wrap Up

SSDs provides an easy way to manage and maintain them using different Linux utilities. We’ve covered one such tool, fstrim, which helps set periodical discards for unused blocks on your SSD. You now have a way of enhancing your SSD’s performance and lifespan on your Linux system.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.