{"id":4809,"date":"2024-04-27T02:15:00","date_gmt":"2024-04-27T06:15:00","guid":{"rendered":"https:\/\/www.both.org\/?p=4809"},"modified":"2024-04-22T20:46:30","modified_gmt":"2024-04-23T00:46:30","slug":"extend-the-life-of-your-linux-ssd-drive-with-fstrim","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=4809","title":{"rendered":"Extend the life of your Linux SSD drive with fstrim"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"4809\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">1    <\/span>\r\n<\/div><\/div>\n<p>Over the past several years, solid-state drives (SSD) have become the standard form of data storage. Mechanical drives can still be purchased if very large capacities are your need. SSDs have benefits over their mechanical spinning ancestors like silent and cooler operation and a faster interface spec. 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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The TRIM service<\/h2>\n\n\n\n<p>Fedora introduced the trim service into their Linux distribution in release 32, and it is enabled in all current releases. This service is not unique to Fedora. The existence and status will depend on an individual distribution basis.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Test<\/h3>\n\n\n\n<p>The service can be tested to better understand what is happening behind the scenes. I do this by opening a terminal and issuing the command to show the help on my Fedora 39 Linux system. The <strong>&#8211;help<\/strong> argument to <strong>fstrim<\/strong> will describe these and other arguments.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ <strong>sudo \/usr\/sbin\/fstrim --help<\/strong>\n\nUsage:\n fstrim &#91;options] &lt;mount point>\n\nDiscard unused blocks on a mounted filesystem.\n\nOptions:\n -a, --all                trim mounted filesystems\n -A, --fstab              trim filesystems from \/etc\/fstab\n -I, --listed-in &lt;list>   trim filesystems listed in specified files\n -o, --offset &lt;num>       the offset in bytes to start discarding from\n -l, --length &lt;num>       the number of bytes to discard\n -m, --minimum &lt;num>      the minimum extent length to discard\n -t, --types &lt;list>       limit the set of filesystem types\n -v, --verbose            print number of discarded bytes\n     --quiet-unsupported  suppress error messages if trim unsupported\n -n, --dry-run            does everything, but trim\n\n -h, --help          display this help\n -V, --version       display version\nArguments:\n  arguments may be followed by the suffixes for\n   GiB, TiB, PiB, EiB, ZiB, and YiB (the \"iB\" is optional)\n\nFor more details see fstrim(8).<\/code><\/pre>\n\n\n\n<p>Let&#8217;s look at the service itself.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ <strong>cat \/usr\/lib\/systemd\/system\/fstrim.service<\/strong>\n&#91;Unit]\nDescription=Discard unused blocks on filesystems from \/etc\/fstab\nDocumentation=man:fstrim(8)\nConditionVirtualization=!container\n\n&#91;Service]\nType=oneshot\nExecStart=\/usr\/sbin\/fstrim --listed-in \/etc\/fstab:\/proc\/self\/mountinfo --verbose --quiet-unsupported\nPrivateDevices=no\nPrivateNetwork=yes\nPrivateUsers=no\nProtectKernelTunables=yes\nProtectKernelModules=yes\nProtectControlGroups=yes\nMemoryDenyWriteExecute=yes\nSystemCallFilter=@default @file-system @basic-io @system-service<\/code><\/pre>\n\n\n\n<p>Now I can see that the systemd service is configured to run the trim on all supported mounted filesystems in my <strong>\/etc\/fstab<\/strong> and <strong>\/proc\/self\/mountinfo<\/strong> files <strong>&#8211;listed-in<\/strong> and print the number of discarded bytes <strong>&#8211;verbose<\/strong> but suppress any error messages if trim is unsupported <strong>&#8211;quiet-unsupported<\/strong>. Knowing these options is helpful for testing. For instance, I can start with the safest one, which is the dry run. I\u2019ll leave off the <strong>quiet-unsupported<\/strong> argument so I can determine if any errors will occur with my drive setup.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ <strong>sudo \/usr\/sbin\/fstrim --listed-in \/etc\/fstab:\/proc\/self\/mountinfo --verbose --dry-run<\/strong>\n\n<\/code><\/pre>\n\n\n\n<p>This will show what the <strong>fstrim<\/strong> command will do based on the file systems that it finds.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ <strong>sudo \/usr\/sbin\/fstrim --listed-in \/etc\/fstab:\/proc\/self\/mountinfo --verbose<\/strong>\n\n<\/code><\/pre>\n\n\n\n<p>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 on a NVME SSD.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/home: 54.3 GiB (58316726272 bytes) trimmed on \/dev\/mapper\/home\n\/boot\/efi: 579.8 MiB (607936512 bytes) trimmed on \/dev\/nvme0n1p1\n\/boot: 0 B (0 bytes) trimmed on \/dev\/nvme0n1p2\n\/: 4.1 GiB (4352933888 bytes) trimmed on \/dev\/mapper\/root<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">The fstrim timer service<\/h3>\n\n\n\n<p>Fedora Linux implements a systemd timer service, scheduled to run on a weekly basis. To check the existence and current status, run <strong>systemctl status<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ <strong>sudo systemctl status fstrim.timer<\/strong>\n&#91;sudo] password for alan: \n\u25cf fstrim.timer - Discard unused filesystem blocks once a week\n     Loaded: loaded (\/usr\/lib\/systemd\/system\/fstrim.timer; enabled; preset: enabled)\n     Active: active (waiting) since Sun 2024-04-14 21:06:49 EDT; 1 week 0 days ago\n    Trigger: Mon 2024-04-22 01:02:17 EDT; 3h 37min left\n   Triggers: \u25cf fstrim.service\n       Docs: man:fstrim\n\nApr 14 21:06:49 myhost systemd&#91;1]: Started fstrim.timer - Discard unused filesystem blocks once a week.<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Verify<\/h3>\n\n\n\n<p>You can verify that the timer is enabled by listing all of the timers.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ <strong>sudo systemctl list-timers --all<\/strong><\/code><\/pre>\n\n\n\n<p>The following line referring to the <strong>fstrim.timer<\/strong> will appear. Notice that the timer actually activates <strong>fstrim.service<\/strong>. This is from where the actual <strong>fstrim<\/strong> is called.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nNEXT                            LEFT LAST                             PASSED UNIT           ACTIVATES\nMon 2024-04-22 01:02:17 EDT 3h 37min Mon 2024-04-15 00:36:40 EDT  6 days ago fstrim.timer   fstrim.service<\/code><\/pre>\n\n\n\n<p>This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 to reclaim unused blocks on the device.<\/p>\n","protected":false},"author":429,"featured_media":2899,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"no","_lmt_disable":"no","footnotes":""},"categories":[84,5],"tags":[132,91,121],"class_list":["post-4809","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hardware","category-linux","tag-hardware","tag-linux","tag-ssd"],"modified_by":"David Both","_links":{"self":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/4809","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/users\/429"}],"replies":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4809"}],"version-history":[{"count":21,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/4809\/revisions"}],"predecessor-version":[{"id":5053,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/4809\/revisions\/5053"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/2899"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4809"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4809"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4809"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}