This commit is contained in:
Xingyu Wang 2020-03-02 20:36:59 +08:00
parent 69f04a92a8
commit 665f2f083c

View File

@ -7,95 +7,90 @@
[#]: via: (https://opensource.com/article/20/2/trim-solid-state-storage-linux)
[#]: author: (Alan Formy-Duval https://opensource.com/users/alanfdoss)
Extend the life of your SSD drive with fstrim
在 Linux 下使用 fstrim 延长 SSD 驱动器的寿命
======
A new systemd service to make your life easier.
> 这个新的系统服务可以使你的生活更轻松。
![Linux keys on the keyboard for a desktop computer][1]
Over the past decade, solid-state drives (SSD) have brought about a new way of managing storage. SSDs have benefits like silent and cooler operation and a faster interface spec, compared to their elder spinning ancestors. Of course, new technology brings with it new methods of maintenance and management. SSDs have a feature called TRIM. This is essentially a method for reclaiming unused blocks on the device, which may have been previously written, but no longer contain valid data and therefore, can be returned to the general storage pool for reuse. Opensource.coms Don Watkins first wrote about TRIM in his 2017 article ["Solid-state drives in Linux: Enabling TRIM for SSDs."][2]
在过去的十年中固态驱动器SSD带来了一种管理存储的新方法。与上一代的转盘产品相比SSD 具有无声、更冷却的操作和更快的接口规格等优点。当然新技术带来了新的维护和管理方法。SSD 具有称为 TRIM 的功能。从本质上讲这是一种用于回收设备上未使用的块的方法该块可能先前已被写入但不再包含有效数据因此可以返回到通用存储池以供重用。Opensource.com 的 Don Watkins 在首先其 2017 年的文章《[Linux 固态驱动器:为 SSD 启用 TRIM][2]》中介绍了 TRIM 的内容。
If you have been using this feature on your Linux system, then you are probably familiar with the two methods described below.
如果你一直在 Linux 系统上使用此功能,则你可能熟悉下面描述的两种方法。
### The old ways
### 老的方式
#### Discard
I initially enabled this with the discard option to the mount command. The configuration is placed into the **/etc/fstab** file for each file system.
#### 丢弃选项
我最初使用 `mount` 命令的 `discard` 选项启用了此功能。每个文件系统的配置都放在 `/etc/fstab` 文件中。
```
# cat /etc/fstab
UUID=3453g54-6628-2346-8123435f  /home  xfs  defaults,discard   0 0
```
The discard option enables automatic online TRIM. There has recently been debate on whether this is the best method due to possible negative performance impacts. Using this option causes a TRIM to be initiated every time new data is written to the drive. This may introduce additional activity that interferes with storage performance.
丢弃选项可启用自动的在线 TRIM。由于可能会对性能造成负面影响最近关于这是否是最佳方法一直存在争议。使用此选项会在每次将新数据写入驱动器时启动 TRIM。这可能会引入其他活动从而影响存储性能。
#### Cron
I removed the discard option from the **fstab** file. Then I created a cron job to call the command on a scheduled basis.
#### Cron 作业
我从 `fstab` 文件中删除了丢弃选项。然后,我创建了一个 cron 作业来按计划调用该命令。
```
# crontab -l
@midnight /usr/bin/trim
```
This is the method I used most recently on my Ubuntu Linux systems until I learned about another way.
这是我最近在 Ubuntu Linux 系统上使用的方法,直到我了解到另一种方法。
### A new TRIM service
### 一个新的 TRIM 服务
I recently discovered that a systemd service for TRIM exists. Fedora [introduced][3] this into their distribution in version 30, and, although it is not enabled by default in versions 30 and 31, it is planned to be in version 32. If youre working on Fedora Workstation 31 and you want to begin using this feature, you can enable it very easily. Ill also show you how to test it below. This service is not unique to Fedora. The existence and status will depend on an individual distribution basis.
我最近发现有一个用于 TRIM 的 systemd 服务。Fedora 在版本 30 中将其[引入][3],尽管默认情况下在版本 30 和 31 中未启用它,但计划在版本 32 中使用它。如果你使用的是 Fedora 工作站 31并且你想要开始使用此功能可以非常轻松地启用它。我还将在下面向你展示如何对其进行测试。该服务并非 Fedora 独有的服务。它是否存在和地位将因发行版而异。
#### Test
I like to test first, to better understand what is happening behind the scenes. I do this by opening a terminal and issuing the command that the service is configured to call.
#### 测试
我喜欢先进行测试,以更好地了解幕后情况。我通过打开终端并发出配置服务调用的命令来执行此操作。
```
`/usr/sbin/fstrim --fstab --verbose --quiet`
/usr/sbin/fstrim --fstab --verbose --quiet
```
The **help** argument to **fstrim** will describe these and other arguments.
`fstrim``-help` 参数将描述这些信息和其他参数。
```
$ sudo /usr/sbin/fstrim --help
Usage:
 fstrim [options] <mount point>
fstrim [options] <mount point>
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 &lt;num&gt;  the offset in bytes to start discarding from
 -l, --length &lt;num&gt;  the number of bytes to discard
 -m, --minimum &lt;num&gt; the minimum extent length to discard
 -v, --verbose       print number of discarded bytes
     --quiet         suppress error messages
 -n, --dry-run       does everything, but trim
-a, --all trim all supported mounted filesystems
-A, --fstab trim all supported mounted filesystems from /etc/fstab
-o, --offset <num> the offset in bytes to start discarding from
-l, --length <num> the number of bytes to discard
-m, --minimum <num> 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
-h, --help display this help
-V, --version display version
```
So, now I can see that the systemd service is configured to run the trim on all supported mounted filesystems in my **/etc/fstab** file **fstab** and print the number of discarded bytes **verbose** but suppress any error messages that might occur **quiet**. Knowing these options is helpful for testing. For instance, I can start with the safest one, which is the dry run. Ill also leave off the quiet argument so I can determine if any errors will occur with my drive setup.
因此,现在我可以看到这个 systemd 服务已配置为在我的 `/etc/fstab` 文件中的所有受支持的挂载文件系统上运行该修剪操作( `-fstab`),并打印出丢弃的字节数(`-verbose`),但是抑制了任何可能会发生的错误消息(`quiet`)。了解这些选项对测试很有帮助。例如,我可以从最安全的方法开始,即空运行。 我还将去掉 `-quiet` 参数,以便确定驱动器设置是否发生任何错误。
```
`$ sudo /usr/sbin/fstrim --fstab --verbose --dry-run`
$ sudo /usr/sbin/fstrim --fstab --verbose --dry-run
```
This will simply show what the **fstrim** command will do based on the file systems that it finds configured in your **/etc/fstab** file.
这就会显示 `fstrim` 命令根据在 `/etc/fstab` 文件中找到的文件系统要执行的操作。
```
`$ sudo /usr/sbin/fstrim --fstab --verbose`
$ sudo /usr/sbin/fstrim --fstab --verbose
```
This will now send the TRIM operation to the drive and report on the number of discarded bytes from each file system. Below is an example after my recent fresh install of Fedora on a new NVME SSD.
现在,这会将 TRIM 操作发送到驱动器,并报告每个文件系统中丢弃的字节数。以下是我最近在新的 NVME SSD 上全新安装 Fedora 之后的示例。
```
/home: 291.5 GiB (313011310592 bytes) trimmed on /dev/mapper/wkst-home
@ -104,44 +99,40 @@ This will now send the TRIM operation to the drive and report on the number of d
/: 60.7 GiB (65154805760 bytes) trimmed on /dev/mapper/wkst-root
```
#### Enable
Fedora Linux implements systemd timer service, scheduled to run on a weekly basis. To check the existence and current status, run **systemctl status**.
#### 启用
Fedora Linux 实现了一个计划每周运行它的 systemd 计时器服务。要检查其是否存在和当前状态,请运行 `systemctl status`
```
`$ sudo systemctl status fstrim.timer`
$ sudo systemctl status fstrim.timer
```
Now, enable the service.
现在,启用该服务。
```
`$ sudo systemctl enable fstrim.timer`
$ sudo systemctl enable fstrim.timer
```
#### Verify
Then you can verify that the timer is enabled by listing all of the timers.
#### 验证
然后,你可以通过列出所有计时器来验证该计时器是否已启用。
```
`$ sudo systemctl list-timers --all`
$ sudo systemctl list-timers --all
```
The following line referring to the **fstrim.timer** will appear. Notice that the timer actually activates **fstrim.service**. This is from where the actual **fstrim** is called. The time-related fields show **n/a** because the service has just been enabled and has not run yet.
会显示出下列表明 `fstrim.timer` 存在的行。注意,该计时器实际上激活了 `fstrim.service`。这是 `fstrim` 实际调用的地方。与时间相关的字段显示为 `n/a`,因为该服务已启用且尚未运行。
```
NEXT   LEFT    LAST   PASSED   UNIT           ACTIVATES
n/a    n/a     n/a    n/a      fstrim.timer   fstrim.service
```
### Conclusion
### 结论
This service seems like the best way to run TRIM on your drives. It is much simpler than having to create your own crontab entry to call the **fstrim** command. It is also safer not having to edit the **fstab** file. It has been interesting to watch the evolution of solid-state storage technology and nice to know that it appears Linux is moving toward a standard and safe way to implement it.
该服务似乎是在驱动器上运行 TRIM 的最佳方法。这比必须创建自己的 crontab 条目来调用 `fstrim` 命令要简单得多。不必编辑 `fstab` 文件也更安全。观察固态存储技术的发展很有趣,并且我很高兴看到 Linux 似乎正在朝着标准且安全的方向实现它。
In this article, learn how solid state drives differ from traditional hard drives and what it means...
在本文中,学习了固态驱动器与传统硬盘驱动器有何不同以及它的含义...
--------------------------------------------------------------------------------
@ -149,7 +140,7 @@ via: https://opensource.com/article/20/2/trim-solid-state-storage-linux
作者:[Alan Formy-Duval][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
译者:[wxy](https://github.com/wxy)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
@ -157,5 +148,5 @@ via: https://opensource.com/article/20/2/trim-solid-state-storage-linux
[a]: https://opensource.com/users/alanfdoss
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/linux_keyboard_desktop.png?itok=I2nGw78_ (Linux keys on the keyboard for a desktop computer)
[2]: https://opensource.com/article/17/1/solid-state-drives-linux-enabling-trim-ssds
[2]: https://linux.cn/article-8177-1.html
[3]: https://fedoraproject.org/wiki/Changes/EnableFSTrimTimer (Fedora Project WIKI: Changes/EnableFSTrimTimer)