From autofs to systemd.automount

The venerable autofs mechanism to automatically mount and unmount network shares still works with today's systems but lately I noticed that NFS and CIFS shares would hang when I unplug my laptop from the local network and connect at another site (e.g. work, or a random coffee shop) where the usual network shares are not reachable. More and more processes will hang (and waiting for the network resource to re-appear) and eventually the machine will be almost unusable and only a reboot may help.

Of course one could configure a VPN to make these resources available all the time, but I don't really need these network shares and I'm already running a VPN when I'm out and about, so this would be unnecessary and overly complicated. With the reign of systemd it is now possible to have systemd handle automounting via the systemd.automount unit, so let's see if it handles these situations better.

autofs

While several tutorials on how to implement this already exist, let's recap first how autofs works. The main configuration file is /etc/auto.master, containing nothing more than:
+dir:/etc/auto.master.d
+auto.master
In /etc/auto.master.d the real map files are referenced:
$ cat /etc/auto.master.d/local.autofs 
/mnt/smb /etc/auto.cifs
/mnt/nfs /etc/auto.nfs
These map files will contain the share definitions:
# auto.cifs
win0  -fstype=cifs,vers=3.0,fsc,guest,rw,nodev,nosuid,noexec,fsc ://smb/win0
win1  -fstype=cifs,vers=3.0,fsc,guest,ro,nodev,nosuid,noexec,fsc ://smb/win1

# auto.nfs
data0  -fstype=nfs,rw,nodev,nosuid,noexec,bg,intr,sec=sys,acl,fsc nfs:/mnt/data0
data1  -fstype=nfs,ro,nodev,nosuid,noexec,bg,intr,sec=sys,acl,fsc nfs:/mnt/data1
Once autofs.service is reloaded, the shares should be accessible.

systemd.automount

But let's dismantle all that and now turn to systemd.automount. For each (network) share we will need a .mount and also a .automount unit file:
$ cat /usr/local/etc/mnt-nfs-data0.mount 
[Unit]
Description=NFS data0

[Mount]
What=nfs:/mnt/data0
Where=/mnt/nfs/data0
Type=nfs4
Options=rw,nodev,nosuid,noexec,bg,intr,sec=sys,acl,fsc

[Install]
WantedBy=multi-user.target
$ cat /usr/local/etc/mnt-nfs-data0.automount 
[Unit]
Description=Automount NFS data0

[Automount]
Where=/mnt/nfs/data0

[Install]
WantedBy=multi-user.target
Link both unit files to /etc/systemd/system, repeat for each network share as needed:
sudo ln -s /usr/local/etc/mnt-nfs-data0.mount     /etc/systemd/system/
sudo ln -s /usr/local/etc/mnt-nfs-data0.automount /etc/systemd/system/
The .mount unit files only need to be linked; the .automount files need to be enabled and started:
sudo systemctl enable mnt-nfs-data0.automount
sudo systemctl start  mnt-nfs-data0.automount
With that, the share should be accessible:
$ mount | grep -m1 mnt/nfs
systemd-1 on /mnt/nfs/data0 type autofs (rw,relatime,fd=48,pgrp=1,timeout=0[...]
This configuration has now been running on my laptop for a few months and it feels like it behaves better when these network resources go away and the machine isn't locking up any more. Yay \o/