1. Overview

Solid-state drives (SSDs) are becoming more popular and affordable as storage devices for personal computers and laptops. They offer faster performance, lower power consumption, and less noise than traditional hard disk drives (HDDs). However, unlike HDDs, SSDs have limited write cycles and reduced performance over time unless we take special measures. To overcome these issues, SSDs have a special feature called TRIM.

In this tutorial, we’ll understand what TRIM is, why it’s important for SSDs, and how to enable and configure it in Linux.

2. Understanding TRIM

Before we can discuss how to enable TRIM, it’s essential we understand why TRIM is necessary for SSDs.

2.1. What Is TRIM?

TRIM is a feature of the Advanced Technology Attachment (ATA) command set that enables the operating system to send a signal to the SSD when a file is deleted or moved, indicating which data blocks are no longer needed. The SSD can then mark these blocks as free and erase them in the background without affecting the user’s experience.

2.2. Why Use TRIM

TRIM improves the SSD’s performance by reducing the amount of data that needs to be written and overwritten. When the SSD receives a write request, it can quickly find an empty block and write the data there without having to erase an old block first. This reduces the write amplification, which is the ratio of the actual amount of data written to the SSD and the amount of data requested by the host. Notably, a lower write amplification means a faster and more efficient SSD.

In addition, TRIM extends the SSD’s lifespan by minimizing the wear and tear of the flash cells. SSDs use a technology called NAND flash memory which stores data in cells that can be electrically programmed and erased. However, each cell has a limited number of write cycles, after which it becomes unreliable and unusable. By using TRIM, the SSD can reduce the number of write cycles and prolong the life of the flash cells.

2.3. Why Is TRIM Necessary for SSDs?

TRIM is necessary for SSDs because of the way they handle write operations. Unlike HDDs, which can overwrite data directly on the magnetic platters, SSDs can’t overwrite data on the flash cells. Instead, they have to perform a read-modify-write operation, which involves three steps:

  1. read block of data
  2. modify block with new data
  3. write block back to the SSD

This process is slow and inefficient. It also increases the write amplification and the wear and tear of the flash cells.

To avoid this problem, SSDs use a technique called garbage collection. Basically, this technique reorganizes the data on the SSD and erases the blocks that are no longer in use. However, garbage collection isn’t aware of the file system’s view of the data and it can’t tell which blocks are actually free and which are still occupied.

This is where TRIM comes in handy, as it provides the SSD with the information it needs to perform garbage collection more effectively.

Without TRIM, the SSD would eventually run out of free blocks and become slower and less reliable. This degradation is especially true for older SSDs that have a smaller capacity and a lower endurance. Therefore, TRIM is essential for maintaining the optimal performance and lifespan of SSDs.

3. Checking TRIM Support

Before enabling TRIM in Linux, we need to check if the SSD supports TRIM. We can check the specifications of our SSD by using the lsblk command:

$ lsblk -o NAME,MODEL,SERIAL,SIZE /dev/sda
NAME   MODEL                     SERIAL            SIZE
sda    Samsung SSD 850 EVO 250GB S21NNXCGA18542L 232.9G
├─sda1                                             512M
├─sda2                                           117.1G
├─sda3                                           114.7G
└─sda4                                             540M

We use the -o option to specify the columns we want to display in the output. Thus, this command displays the name, model, serial number, and size of our SSD. We can then search for the SSD model online and check its specifications.

Alternatively, we can use the hdparm command:

$ sudo hdparm -I /dev/sda

ATA device, with non-removable media
	Model Number:       Samsung SSD 850 EVO 250GB               
	Serial Number:      S21NNXCGA18542L     
	Firmware Revision:  EMT02B6Q
	Transport:          Serial, ATA8-AST, SATA 1.0a, SATA II Extensions, SATA Rev 2.5, SATA Rev 2.6, SATA Rev 3.0
	Enabled	Supported:
	   *	SMART feature set
	   *	Data Set Management TRIM supported (limit 8 blocks)
Device Sleep:
	DEVSLP Exit Timeout (DETO): 50 ms (drive)
	Minimum DEVSLP Assertion Time (MDAT): 30 ms (drive)
Checksum: correct

The -I option instructs hdparm to retrieve and display detailed information about the specified ATA device. The output of this code snippet contains data about our SSD:

  • firmware version
  • features
  • capabilities

We can then look for the word TRIM or Data Set Management TRIM supported in the output. If we see it, it usually means that the SSD supports TRIM. If we don’t see it, then the SSD isn’t expected to support TRIM or we’ve disabled it in the firmware.

4. Enabling TRIM for ext4

After checking the TRIM support, we might need to enable TRIM for our file system. Notably, some file systems don’t support TRIM at all, but, many common ones do:

ext4 is one of the most widely used file systems in Linux. So, we’ll see how to check the existing mount options for the ext4 file system.

4.1. Checking Existing Mount Options

The mount options decide whether TRIM is on or off for each file system. So, to check the existing mount options for a file system, we can use the mount command to list all the mounted file systems with their options and filter the output with the grep command:

$ mount | grep ext4
/dev/sda2 on / type ext4 (rw,relatime,errors=remount-ro)

From the output above, we can interpret the options enabled for the ext4 file system on /dev/sda2:

  • rw: file system is mounted with read and write permissions
  • relatime: access time is updated relative to modification or change time

If the discard option is in the output, it usually means that TRIM is on for the ext4 file system.

Alternatively, we can use the findmnt command to list the mount points and their options:

$ findmnt -t ext4 -o TARGET,OPTIONS
TARGET                                   OPTIONS
/                                        rw,relatime,errors=remount-ro

In the code snippet above, the -t option filters the output based on the file system type. The output is the target mount point and the options of the ext4 file systems.

4.2. Modifying the /etc/fstab File

To permanently enable TRIM for ext4 file systems, we can modify the /etc/fstab file, responsible for performing automatic mounts.

In particular, we need to add the discard option to the mount options of our file system.

Now, let’s use the cat command to take a look at the content of /etc/fstab after we’ve added the discard option:

$ cat /etc/fstab
# /etc/fstab: static file system information.
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda2 during installation
UUID=81232c4d-81f3-4f4d-aa2a-a918eaf7ca59 /               ext4    errors=remount-ro,discard 0       1
# /boot/efi was on /dev/sda1 during installation
UUID=6E07-3FC9  /boot/efi       vfat    umask=0077      0       1
/swapfile                                 none            swap    sw              0       0

Let’s enumerate the whitespace-separated fields indicated in the output from left to right:

  1. <file system>/dev/sda2 is the device we want to enable TRIM for
  2. <mount point>: root file system is mounted at /
  3. <type>: file system type is ext4
  4. <options>: mount options include errors=remount-ro,discard
  5. <dump>: set to 0 indicating that the dump utility shouldn’t back up the file system
  6. <pass>: set to 1 indicating that the file system shouldn’t be checked during the automatic file system check (fsck) at boot time

At this point, the file system will be mounted with the specified options after a reboot.

Still, we can remount the file system to apply the change immediately:

$ sudo mount -o remount /

Now, let’s confirm that we’ve enabled the discard option:

$ findmnt -t ext4 -o TARGET,OPTIONS
TARGET                                   OPTIONS
/                                        rw,relatime,discard,errors=remount-ro

Thus, we verify from the output that we’ve added the discard option.

However, this approach to enabling TRIM can make deletion much slower and increase CPU usage.

5. Using fstrim

Alternatively, we can run a TRIM manually by using the fstrim command:

$ sudo fstrim -v /
/: 22.2 GiB (23802130432 bytes) trimmed

The output is the amount of freed space. Also, the -v option provides a more detailed and informative output.

However, if we don’t perform this manual TRIM operation regularly, we may not be able to reclaim unused data blocks on the SSD. Therefore, automated solutions, such as scheduled TRIM operations, help ensure regular maintenance without relying on manual intervention.

6. Automating TRIM With Cron Jobs

As we’ve seen, using the discard mount option to enable TRIM for our file systems can lead to high CPU usage. Also, manually performing TRIM operations might not help. Therefore, it can be preferable to use a cron job to automatically invoke TRIM. This way, we have control over TRIM execution, without having to manually do it.

A cron job is a task scheduled to run at a specific time or interval. In this case, we can use it to automate TRIM operations regularly.

Let’s create a script named trim in the /etc/cron.daily directory to run the fstrim command once a day and trim our file system:

$ cat /etc/cron.daily/trim 
# This script runs the fstrim command once a day and trims all the mounted file systems that support TRIM.
# It also appends the output of the command to the /var/log/trim.log file, which you can use to monitor the trim operations.

sudo fstrim -v --all >> /var/log/trim.log

Notably, the /etc/cron.daily directory contains scripts that run once a day. Therefore, the script runs the fstrim command once a day and trims all the mounted file systems. In addition, it also appends the output of the command to the /var/log/trim.log file which we can use to monitor the trim operations.

We need to make the script executable using chmod:

$ sudo chmod +x /etc/cron.daily/trim

Subsequently, the cron daemon runs the script once a day.

7. Conclusion

In this article, we’ve understood what TRIM is and how to enable TRIM manually. Also, we looked at how to automate TRIM with cron jobs.

In conclusion, implementing the TRIM operation can be crucial for optimizing the performance of SSDs. TRIM plays an important role in maintaining the efficiency of SSDs by managing and reclaiming unused storage space.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.