Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
B
blog
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Eduardo Trujillo
blog
Commits
c98c68d6
Unverified
Commit
c98c68d6
authored
8 years ago
by
Eduardo Trujillo
Browse files
Options
Downloads
Patches
Plain Diff
feat(posts): Add post on fstrim + systemd
parent
226c9129
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
posts/2016-07-30-fstrim-systemd.md
+80
-0
80 additions, 0 deletions
posts/2016-07-30-fstrim-systemd.md
with
80 additions
and
0 deletions
posts/2016-07-30-fstrim-systemd.md
0 → 100644
+
80
−
0
View file @
c98c68d6
---
title
:
Automatically TRIM-ing your SSD using systemd
date
:
Sun Jul 30 17:34:50 UTC
2016
author
:
Eduardo Trujillo
uuid
:
ac1f6bf6-72be-4729-a545-6d6b08112149
---
If you have a Linux laptop or desktop with a solid-state drive, and happen to
have disk encryption enabled through a LUKS/LVM combo, getting
[
TRIM
][
1
]
support enabled isn't a very straightforward process. It has to be enabled on
every IO layer.
In their blog post,
[
_How to properly activate TRIM for your SSD on Linux:
fstrim, lvm and dm-crypt_
][
2
]
, Carlos Lopez gives a brief introduction on what
TRIM is, and explains why it is beneficial to enable it. The article also
describes the steps needed to enable this functionality on each IO layer
(dm-crypt, LVM, and the filesystem).
I followed most of this guide for one my own systems, and while I followed
their advice and avoided enabling the discard flag on the filesystem, I never
set up a cron job for running the trim operation periodically. So I found
myself manually executing
[
`fstrim`
][
3
]
every now and then by hand.
This quickly became slightly repetitive, so I began looking into setting up the
automation part. The guide above had an example setup using cron. However, I
never set up a cron daemon on my system. So I wondered if it was possible to
achieve the same result using systemd.
After reading some documentation on systemd unit files, I learned that is
possible to setup timers for your service units, which effectively achieves the
same result as a cron daemon.
Below I'm including a fstrim service and timer. The service mainly specifies
which command to run and the timer defines how often it should be executed.
Note that the service unit does not have a
`WantedBy`
option and its
`Type`
is
`oneshot`
. This means it won't be automatically executed, and that it is
intended to be a one off command, not a daemon. The timer does have a
`WantedBy`
option, which will result on it being started at boot.
I can check the status of the timer by using
`systemctl list-times`
and also
run the operation on demands by starting the service unit:
`systemctl start
fstrim`
. The logs are stored on the journal, which can be queried with
`journalctl -u fstrim`
.
##### /etc/systemd/system/fstrim.service
This is the service file. Here you can customize how
`fstrim`
is invoked. I use
the
`a`
and
`v`
options, which tell
`fstrim`
to automatically run on every
drive and print verbose output. Additionally, this assumes
`fstrim`
is
installed at
`/sbin/fstrim`
.
```
{#fstrimservice .ini}
[Unit]
Description=Run fstrim on all drives
[Service]
Type=oneshot
ExecStart=/sbin/fstrim -av
User=root
```
##### /etc/systemd/system/fstrim.timer
In this configuration, the
`fstrim`
command is executed by
`root`
15 minutes
after booting the machine and weekly afterwards.
```
{#fstrimtimer .ini}
[Unit] Description=Run fstrim on boot and weekly afterwards
[Timer]
OnBootSec=15min
OnUnitActiveSec=1w
[Install]
WantedBy=timers.target
```
[
1
]:
https://en.wikipedia.org/wiki/Trim_(computing)
[
2
]:
http://blog.neutrino.es/2013/howto-properly-activate-trim-for-your-ssd-on-linux-fstrim-lvm-and-dmcrypt/
[
3
]:
https://packages.gentoo.org/packages/sys-apps/util-linux
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment