在 Linux 中使用 SSD（固态驱动器）：启用 TRIM
磁盘 SSD
![](/data/attachment/album/201702/05/184842xf7yyd74l774yb77.jpg)


当我在运行 Linux 的计算机上安装我的第一块固态驱动器（SSD）后，我开始探索如何用好它们。SSD 在操作方式上与传统磁性驱动器不同，并且它们需要在软件上另行处理以达到功能优化。


在传统磁盘驱动器上，删除时所删除的文件不会从磁盘中完全删除。这就是为什么你可以恢复已删除的文件的原因。基本上，文件系统仅引用磁盘上文件的位置，并且当文件被删除时，该引用被擦除，以允许你在这些空间中写入新数据覆盖原来的数据。然而，对于 SSD，新数据只能写在驱动器上完全新的或已擦除的单元上。因为必须在写入之前清除空间，如果在写入文件时尚未有足够的可用空间，则必须首先擦除该空间。这可能会对性能产生负面影响。


如果操作系统在写入新数据之前就擦除了未使用的空间，而不是在写入时同时进行擦除，则可以提高文件保存性能。这种做法就是 [TRIM](https://en.wikipedia.org/wiki/Trim_(computing))。 TRIM 命令本质上允许你的操作系统告诉驱动器哪些区域的数据不再使用，以便擦除它们，加快驱动器将来的写入，可以 SSD 的用户提供更佳的体验。


在 Linux 中，`fstrim` 提供此功能，它可以为写入新数据而准备驱动器，并延长驱动器的使用寿命。由于在我使用的 Linux 发行版上 SSD 的 trim 不是自动的，所以必须去调度该操作，否则 SSD 的性能会随着时间的推移而降低。


为了在驱动器上运行 `fstrim`，驱动器本身以及其上的文件系统必须支持 TRIM。在文件系统挂载过程中可以启用 TRIM。例如，为了将设备 `/dev/sda2` 启用 TRIM 挂载到 `/mnt`，你要运行：



```
mount -t ext4 -o discard /dev/sda2 /mnt

```

一旦启用，TRIM 过程本身就相当简单。TRIM SSD 也可以在命令行或 cron 任务中手动完成。作为超级用户（使用 `su` 或 `sudo`），运行 `fstrim / -v` 以完成手动 trim，或者设置 cron 任务以在计算机未使用时定期为你运行此命令。对于 `fstrim` 的完整选项列表请参考它的 [man 手册](http://man7.org/linux/man-pages/man8/fstrim.8.html)。


硬件支持根据使用的驱动器接口类型如 PCI、[ATA](https://en.wikipedia.org/wiki/Trim_(computing)#ATA)、[SCSI](https://en.wikipedia.org/wiki/Trim_(computing)#SCSI) 还是 [SD/MMC](https://en.wikipedia.org/wiki/Trim_(computing)#SD.2FMMC) 而有所不同。你需要咨询你的 Linux 供应商以了解你的特定发行版是如何支持 TRIM 的。


例如，红帽提供以下 [SSD 磁盘指南](https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Storage_Administration_Guide/ch-ssd.html)。“性能随着所使用的块数接近磁盘容量而降低，性能影响程度因供应商而异，但是所有设备都会遇到一些性能降低。为了解决性能降低问题，主机系统（例如 Linux 内核）使用丢弃请求以通知存储器给定范围的块不再使用。”


[Debian wiki](https://wiki.debian.org/SSDOptimization) 提供了 SSD 使用的一些基本注意事项：使用 Linux 3.2 或更高版本内核，使用 SSD 的最新固件，使用 EXT4 文件系统，并且“在正常工作负载下有足够的 DRAM 用来操作而不用使用交换空间“。




---


作者简介：


Don Watkins - 教育家、教育技术专家、企业家、开源倡导者。教育心理学硕士、教育领导硕士、Linux 系统管理员、CCNA、使用 Virtual Box 虚拟化。关注我 @Don\_Watkins。




---


via: <https://opensource.com/article/17/1/solid-state-drives-linux-enabling-trim-ssds>


作者：[Don Watkins](https://opensource.com/users/don-watkins) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/%E6%A0%A1%E5%AF%B9%E8%80%85ID)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
After installing my first solid state drive (SSD) in a computer that was running Linux, I have begun to explore how to take care of them. Solid state drives are different than traditional magnetic drives in the way that they operate, and they require different care from the software side in order to function optimally.

On traditional magnetic drives, deleted files are not completely removed from the disk at the time of deletion. This is why you can recover deleted files. Essentially, the filesystem just references the location of a file on the disk, and when a file is deleted, that reference is erased, allowing you to write new data over old data in these blank spaces. However, with SSDs, new data can only be written on completely new or erased cells of the drive. Because the space must be cleared prior to a write, if enough free space is not already available at the time a file is being written, it must be erased first. This can negatively affect performance.


If the operating system were to erase unused space before writing new data, at a time when the device is not simultaneously trying to write, file saving performance could be improved. Enter [TRIM](https://en.wikipedia.org/wiki/Trim_(computing)). A TRIM command essentially allows your operating system to tell the drive which areas of data are no longer being used so that it come wipe them, speeding up the drive for future writes, and providing users of SSDs with a more optimum experience.

In Linux, **fstrim** provides this functionality, readying the drive for new data to be written and extends the life of the drive over the long term. Since trimming SSDs is not automatic on the Linux distributions that I have used, it is imperative that it be scheduled or the performance of the SSD will degrade over time.

In order to run fstrim on a drive, the drive itself, as well as the file system sitting on top of it, must support TRIM. Enabling TRIM can be done during the filesystem mounting process. For example, in order to mount the device **/dev/sda2** to **/mnt** with TRIM enabled, you would run:

```
````mount -t ext4 -o discard /dev/sda2 /mnt`


TOnce enabled, the TRIM process itself is rather simple. Trimming your SSD can also be accomplished manually on the command line or in a cron job. As a super user (using su or sudo), run **fstrim / -v** to accomplish manual trimming, or set up a cron job to run this command for you on a regular basis when your computer is not in use. And for a complete list of fstrim, options refer to its [man page](http://man7.org/linux/man-pages/man8/fstrim.8.html).

Hardware support varies depending on the type of drive interface used whether PCI, [ATA](https://en.wikipedia.org/wiki/Trim_(computing)#ATA), [SCSI](https://en.wikipedia.org/wiki/Trim_(computing)#SCSI) or [SD/MMC](https://en.wikipedia.org/wiki/Trim_(computing)#SD.2FMMC). It's also worth consulting with your Linux vendor to learn more about how your particular distribution may support TRIM.

For example, Red Hat offers the following the [SSD disk guidelines](https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Storage_Administration_Guide/ch-ssd.html). "Performance degrades as the number of used blocks approaches the disk capacity. The degree of performance impact varies greatly by vendor. However, all devices experience some degradation. To address the degradation issue, the host system (for example, the Linux kernel) may use discard requests to inform the storage that a given range of blocks is no longer in use."

The [Debian wiki ](https://wiki.debian.org/SSDOptimization)offers some basic cautions for SSD use: Use a Linux kernel 3.2 or newer, use the latest firmware for the SSD, use the EXT4 file system, and "have enough DRAM required to operate without swap space under normal workloads."

## 19 Comments