A few years ago, I set up autofs to handle my network shares. While this works, occasionally I ran into some issues with autofs, and of course it’s one more thing you need to setup & install. Recently I learned that
systemd can also handle automatic mounting. Turns out, that’s as easy as setting up autofs and it fixed some of the issues I’ve had with autofs, so without further ado – let’s convert an existing autofs mount to systemd!
In this blog post, I’ll set up a SMB share, pointing to a network share
//my-server/myshare. The share will be mounted to
/mnt/net/smb/myshare. If you want to follow along, make sure you remember which directory you’re using as the target – this will become important in a second.
Before I continue, a brief interlude – I’ve not pieced this together by myself, instead I heavily relied on several blog posts and of course, ServerFault. For the rest of the post I’ll be linking to the official documentation where you’re supposed to find all this stuff :)
We’ll be saving our configuration in
/etc/systemd/system, which is the recommended location for units created by the administrator. The first file we’ll create describes the mount point. This is done using a mount unit. One thing to note here is that the filename needs to follow the naming scheme described in the unit documentation, which basically boils down to replacing
/mnt/net/smb/myshare turns into
mnt-net-smb-myshare.mount. The contents are relatively straightforward, with
What providing the source path,
Where the target, and
Options storing the cifs options that will be used to mount the share:
[Unit] Description=myshare mount [Mount] What=//my-server/myshare Where=/mnt/net/smb/myshare Type=cifs Options=rw,file_mode=0700,dir_mode=0700,uid=1000 DirectoryMode=0700 [Install] WantedBy=multi-user.target
There’s nothing else to do here, you can use
systemctl daemon-reload to reload the config, and inspect the mount, but right now it’s a regular mount and no auto-mounting is happening yet. For that, we need an automount unit. It follows the exact same naming convention, except the file extension must be
.automount. This file contains the
[Automount] section, and it has one mandatory entry,
[Unit] Description=myshare automount [Automount] Where=/mnt/net/smb/myshare [Install] WantedBy=multi-user.target
This is the unit we want to enable and start automatically, so we need to perform the following steps:
systemctl daemon-reloadto reload the configuration
systemctl start mnt-net-smb-myshare.automountto start the unit – so we can use it right away
systemctl enable mnt-net-smb-myshare.automountto enable the auto-start of the unit
And that’s it, at this point, if we
/mnt/net/smb/myshare, we should see the unit get triggered. We can check this using
systemctl status mnt-net-smb-myshare.automount. The output will tell you which process triggered the mounting.
And that’s it! Other types of mounts work the same, I’m using the same setup for
nfs mounts of instance. Thanks for reading!