sudoedit.com!

Systemd Automount

Update 2022-06-29

While the procedure outlined below works, setting up systemd automounts is actually far easier than I realized and can be done with just the /etc/fstab file.

You can just put the following into /etc/fstab:

<ip or hostname>:/path/to/nfs/export /mnt/test nfs x-systemd.automount, \
x-systemd.DirectoryMode=true,x-systemd.TimeoutIdleSec=10min 0 0

Then: systemctl daemon-restart systemctl start mnt-test.automount

And that's it. You don't need to create the systemd unit file because systemd does it for you in the background based on what's in the fstab file.


Original Post

Systemd has the ability to manage file systems in the same way it can manage services on a Linux system.

A Systems Administrator can replace the autofs service with the systemd automounter.

Why should I replace autofs?

Having a consistent interface to manage services and mount points on a system is an advantage all on its own.

Beyond the consistency with the interface, using the systemd automounter means you get to eliminate a running service (autofs).

The real question is: Why are you still installing autofs?


How to set up systemd automount with NFS

For this example you'll need an NFS file system available to you, though you could also use an lvm based file system for testing. See my linux lvm guide for more information on setting up lvm if you aren't sure how.

Step 1 - Create a unit file

Using your favorite text editor create a file at /etc/systemd/system/mnt-test.automount. If vim isn't your favorite text editor, then you should feel bad about yourself... run vimtutor in your terminal someday when you have 45 minutes to burn and learn all about what you've been missing.

Your mnt-test.automount file should contain the following.

[Unit]
Description=Mount nfs export

[Install]
WantedBy=multi-user.target

[Automount]
What=<ip or hostname>:/path/to/nfs/export
Where=/mnt/test
Type=nfs
DirectoryMode=true
TimeoutIdleSec=60
Options=defaults

About this file.

This is a pretty bare bones unit file. If you've used systemd to create service files this should look pretty familiar.

Description: The description can be anything you want it to be. I would include the name of the volume you are mounting and maybe the source server or application it's for.

[Automount]: Systemd will use this section to determine what to mount, where to mount it, what type of file system it is, and other options.

What: What are we mounting. For nfs this would be something like What=192.168.1.38:/path/to/nfs.

Where: Where systemd should mount the volume. Seems pretty self explanatory.

Type: What type of file system is this? xfs, nfs, ext4... whatever it is put it here.

DirectoryMode: This tells systemd to create any paths that don't already exist. If this works correctly you shouldn't have to create /mnt/test, systemd will do that for you.

TimeoutIdleSec: How many seconds after no activity has been detected on a mounted file system should systemd wait to unmount it.

Options: You can put any mount options you want here. I just used defaults to illustrate it as an example.

Step 2 - Update the fstab file

Once again using your favorite text editor, open /etc/fstab and add your nfs export to the file specifying x-systemd.automount where the mount options would normally go, as shown below.

<ip or hostname>:/path/to/nfs/export /mnt/test nfs x-systemd.automount 0 0

Step 3 - Refresh services

Reload the system daemon, start, and enable the mnt-test.mount unit.

sudo systemctl daemon-reload
sudo systemctl start mnt-test.automount
sudo systemctl enable mnt-test.automount

Otherwise you can reboot the system if you want.

Things to Note:

From the systemd.automount man pages:

Automount units must be named after the automount directories they control. Example: the automount point /home/lennart must be configured in a unit file home-lennart.automount.

Make sure you name the unit file to match this syntax. If you wanted to mount a volume to /backups/laptop then you would name your unit file backups-laptop.automount. Otherwise you'll get errors and the mount unit will refuse to start.


If you found this useful please support the blog.

Fastmail

I use Fastmail to host my email for the blog. If you follow the link from this page you'll get a 10% discount and I'll get a little bit of break on my costs as well. It's a win win.


Backblaze

Backblaze is a cloud backup solution for Mac and Windows desktops. I use it on my home computers, and if you sign up using the link on this page you get a free month of service through backblaze, and so do I. If you're looking for a good backup solution give them a try!

Thanks!

Luke