Systemd Automount

May 26, 2019 at 15:35
[/assets/archlinux]

Having recently rebuild my desktop to use Arch linux and SwayWM, I needed to automount my NAS.

There are several ways you can mount this. Assuming you have created a mount point somewhere, e.g. sudo mkdir /mnt/nas/public, then:

  • Manually using: sudo mount -t cifs -o user=dev,password=dev //192.168.0.255/public /mnt/nas/public
  • Use autofs
  • Use systemd

I’ve chosen to use systemd.

Create mount and automount unit files

The files names of each of these files must match the path you are automounting, with / replaced with -. These files must be stored in /etc/systemd/system.

We are mounting to /mnt/nas/public so the files must be called mnt-nas-public.mount with the following content:

    [Unit]
    Description=nas public mount using cifs
    Requires=network-online.target
    After=network-oneline.target

    [Mount]
    What=//192.168.0.255/public
    Where=/mnt/nas/public
    Type=cifs
    Options=username=dev,password=dev,rw

    [Install]
    WantedBy=multi-user.target

and mnt-nas-public.automount:

    [Unit]
    DefaultDependencies=no
    After=remote-fs-pre.target
    Wants=remote-fs-pre.target
    Conflicts=umount.target
    Before=umount.target

    [Automount]
    Where=/mnt/nas/public
    TimeoutIdleSec=0

    [Install]
    WantedBy=remote-fs.target

Then enable the mounts:

    systemctl enable mnt-nas-public.mount
    systemctl enable mnt-nas-public.automount

Reload systemd:

    systemctl daemon-reload

And repeat for other mount points.