Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use fstrim to extend the Life of SSD driver under Linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly describes how to use fstrim under Linux to extend the life of SSD drives, the article is very detailed, has a certain reference value, interested friends must read it!

The old way to discard options

I initially enabled this feature using the discard option of the mount command. The configuration for each file system is placed in the/etc/fstab file.

# cat /etc/fstabUUID=3453g54-6628-2346-8123435f /home xfs defaults,discard 0 0

The discard option enables automatic online TRIM. There has been debate lately about whether this is the best approach due to the potential negative impact on performance. Using this option starts TRIM every time new data is written to the drive. This may introduce additional disk activity, affecting storage performance.

Cron job

I removed the discard option from the fstab file. I then created a cron job to invoke the command as scheduled.

# crontab -l@midnight /usr/bin/trim

This is the method I used recently on Ubuntu Linux systems until I learned about another method.

A new TRIM service

I recently discovered that there is a systemd service for TRIM. Fedora introduced it in version 30, and although it is not enabled by default in versions 30 and 31, it is planned to be used in version 32. If you're using Fedora Workstation 31 and you want to get started with this feature, it's pretty easy to enable it. I'll also show you how to test it below. This service is not unique to Fedora. Its existence and status will vary from release to release.

test

I like to test first to get a better idea of what's going on behind the scenes. I do this by opening the terminal and issuing commands that configure service calls.

/usr/sbin/fstrim --fstab --verbose --quiet

The-help parameter of fstrim describes this information and other parameters.

$ sudo /usr/sbin/fstrim --help Usage: fstrim [options] Discard unused blocks on a mounted filesystem. Options: -a, --all trim all supported mounted filesystems -A, --fstab trim all supported mounted filesystems from /etc/fstab -o, --offset the offset in bytes to start discarding from -l, --length the number of bytes to discard -m, --minimum the minimum extent length to discard -v, --verbose print number of discarded bytes --quiet suppress error messages -n, --dry-run does everything, but trim -h, --help display this help -V, --version display version

So now I can see that the systemd service has been configured to run the pruning operation (-fstab) on all supported mount file systems in my/etc/fstab file and print out the number of bytes dropped (-verbose), but suppress any error messages that might occur (-quiet). Understanding these options is helpful for testing. For example, I can start with the safest method, which is running empty. I'll also remove the-quiet parameter to determine if any errors occurred with the drive settings.

$ sudo /usr/sbin/fstrim --fstab --verbose --dry-run

This shows what the fstrim command does based on the file system found in the/etc/fstab file.

$ sudo /usr/sbin/fstrim --fstab --verbose

This now sends a TRIM operation to the drive and reports the number of bytes dropped in each file system. Here is an example of how I recently installed Fedora on my new NVME SSD after a fresh install.

/home: 291.5 GiB (313011310592 bytes) trimmed on /dev/mapper/wkst-home/boot/efi: 579.2 MiB (607301632 bytes) trimmed on /dev/nvme0n1p1/boot: 787.5 MiB (825778176 bytes) trimmed on /dev/nvme0n1p2/: 60.7 GiB (65154805760 bytes) trimmed on /dev/mapper/wkst-root Enable

Fedora Linux implements a systemd timer service that is scheduled to run weekly. To check its existence and current status, run systemctl status.

$ sudo systemctl status fstrim.timer

Now enable the service.

$ sudo systemctl enable fstrim.timer Verify

You can then verify that the timer is enabled by listing all timers.

$ sudo systemctl list-timers --all

The following line appears indicating that fstrim.timer exists. Note that this timer actually activates the fstrim.service service. This is where fstrim is actually called. The time-related field appears as n/a because the service is enabled and not yet running.

NEXT LEFT LAST PASSED UNIT ACTIVATESn/a n/a n/a n/a fstrim.timer fstrim.service The above is "How to use fstrim under Linux to extend the life of SSD drive" All the contents of this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report