The most surprising thing about systemd’s mount and automount units is that they don’t just manage when filesystems are mounted, but how they’re checked and how they recover from errors.
Let’s see systemd in action. Imagine you have a network share you want to mount automatically. Instead of digging through /etc/fstab and hoping systemd-fstab-generator does the right thing, you can create explicit systemd units.
First, the mount unit, typically named after the mount point: mnt-data.mount.
[Unit]
Description=Data Mount Point
Requires=network-online.target
After=network-online.target
[Mount]
What=//server/share
Where=/mnt/data
Type=cifs
Options=credentials=/etc/samba/credentials,uid=1000,gid=1000,vers=3.0
TimeoutSec=30
[Install]
WantedBy=multi-user.target
The [Unit] section defines dependencies. Requires=network-online.target and After=network-online.target ensure the network is up and ready before systemd even tries to mount this. This is crucial for network filesystems.
The [Mount] section is where the magic happens. What is the device or network share, Where is the mount point, and Type is the filesystem type. Options are passed directly to the mount helper (like mount.cifs in this case). TimeoutSec=30 means systemd will give up after 30 seconds if the mount doesn’t succeed.
The [Install] section tells systemd how to enable this unit. WantedBy=multi-user.target means it should be started when the system reaches the standard multi-user runlevel.
To enable this, you’d run:
sudo systemctl enable mnt-data.mount
sudo systemctl start mnt-data.mount
Now, what about automounting? This is where systemd truly shines. You create an .automount unit, which is named identically to the .mount unit but with an .automount extension: mnt-data.automount.
[Unit]
Description=Data Automount Point
[Automount]
Where=/mnt/data
TimeoutIdleSec=600
[Install]
WantedBy=multi-user.target
The [Automount] section is simple. Where specifies the mount point. TimeoutIdleSec=600 means that if the mount point /mnt/data is not accessed for 600 seconds (10 minutes), systemd will automatically unmount it. This is fantastic for temporary mounts or shares you don’t want consuming resources when idle.
To enable and start the automount unit:
sudo systemctl enable mnt-data.automount
sudo systemctl start mnt-data.automount
When you enable the .automount unit, systemd automatically creates and enables a corresponding .mount unit for you. You don’t typically create the .mount unit manually when using .automount. If you do have a .mount unit, systemd will use that. If not, it generates one based on conventions (like looking for a corresponding .device unit or, less commonly, /etc/fstab entries).
The real power of automounts is that the filesystem is only mounted when it’s first accessed. If you ls /mnt/data and the automount unit is active, systemd intercepts this access, starts the associated mnt-data.mount unit, performs the mount, and then allows the ls command to proceed.
The systemd mount units also handle filesystem checks and recovery. For block devices, you can specify fsck options in the .mount unit’s [Mount] section, or let systemd infer them. systemd’s systemd-fsck@.service can be configured to run fsck at boot. If a filesystem is marked dirty, systemd will automatically attempt to run fsck on it before mounting. It also handles mounting read-only if fsck fails to repair it, preventing boot loops.
The one thing most people don’t realize is how tightly integrated systemd mount units are with its transaction system. When systemd mounts a filesystem, it creates a unit that can be depended upon by other services. If a service Requires=mnt-data.mount, systemd ensures that the mount is active before starting that service. Conversely, if a service Wants=mnt-data.mount, systemd will attempt to mount it but won’t fail if it can’t. This declarative dependency management is far more robust than the sequential execution of fstab.
The next thing you’ll likely encounter is managing device-mapper targets or other complex storage configurations using systemd’s .device and .swap units in conjunction with .mount and .automount.