Systemd: Automount nfs export

Hi,

systemd can mount cifs filesystems at boot or on demand like autofs.

First cifs mount at boot time. Determine the share and the mount point.

For example:

Export: 192.168.56.1:/mp
Local mountpoint: /mnt/mp

Create the mount point

root@debdev:~# mkdir /mnt/mp

Create the systemd definition file. The Folder for custom systemd unit files is /etc/systemd/system.

Create a new file mnt-mp.mount in the diretory /etc/systemd/system. The filename must contain the mountpointname where the slashes are replaced with “minus”. Filename for /mnt/mp => mnt-mp.mount

If this does not match you will get an error like:

root@debdev:~# journalctl |grep storage
mnt-mp.mount's Where= setting doesn't match unit name. Refusing.

This is a working unit


[Unit]
  Description=nfs mount script
  Requires=network-online.target
  After=network-online.service

[Mount]
  What=192.168.56.1:/mp
  Where=/mnt/mp
  Options=
  Type=nfs

[Install]
  WantedBy=multi-user.target

Enable the previous defined config and check if error occurs.

systemctl enable mnt-mp.mount
systemctl status mnt-mp.mount

To mount and unmount your share start or stop the unit.

root@debdev:~# systemctl start mnt-mp.mount
root@debdev:~# mount|grep 192

192.168.56.1:/mp on /mnt/mp type nfs (rw,relatime,...
root@debdev:~# systemctl stop mnt-mp.mount

If you make changes to your unit file while its still active, call

systemctl daemon-reload

to reload it.

It’s also possible to mount your share just on demand. Like an automounter.

Define your mount unit mnt-mp.mount like above but do not enable it via systemctl.

Create an automount unit /etc/systemd/system/mnt-mp.automount. The automount unit starts the mount unit (mnt-mp.mount) on demand.


[Unit]
  Description=nfs mount script
  Requires=network-online.target
  After=network-online.service

[Automount]
  Where=/mnt/mp
  TimeoutIdleSec=10

[Install]
  WantedBy=multi-user.target

Enable the automount unit

systemctl enable mnt-mp.automount

This will create an autofs entry in the mount tab

root@debdev:~# mount|grep systemd
systemd-1 on /mnt/mp type autofs (rw,relatime,fd=27,pgrp=1,timeout=300,minproto=5,maxproto=5,direct)


root@debdev:~# systemctl status mnt-mp.automount
● mnt-mp.automount - nfs mount script
Loaded: loaded (/etc/systemd/system/mnt-mp.automount; enabled)
Active: active (running) since Fr 2016-03-18 12:21:37 CET; 9min ago
Where: /mnt/mp

 

and browse to the mountpoint and see what happens:

root@debdev:~ # ls -l /mnt/mp/Andreas/Adoring_Human_Flesh/
-rwxr-xr-x 1 root root 9361449 Mai 2 2015 ZOOM0041.MP3
-rwxr-xr-x 1 root root 6881906 Mai 2 2015 ZOOM0045.MP3
-rwxr-xr-x 1 root root 5959680 Mai 2 2015 ZOOM0046.MP3
-rwxr-xr-x 1 root root 5880058 Mai 2 2015 ZOOM0051.MP3

Michael

Advertisment to support michlstechblog.info

4 thoughts on “Systemd: Automount nfs export”

  1. Tank you.

    Best explanation i found so far. I am looking for a way to unmount on demand to. Use-case: I have a NFS server (Openwrt) with an attached USB drive that gets exported. I want to be able to unplug it, attach an other. So far i get that working with TimeoutIdleSec=10. But no way to do that from ad example Nautilus. Maybe you can help.

    1. Hi defrancoj,

      sudo apt-get install etherwake
      sudo etherwake 00:11:22:33:44:55
      

      I think its very diffcult to get this working in an automount environment. Because the automounter have to wait before the machine is completly up and runs in several timeouts…

      Michael

  2. Just a quick correction regarding the automounter. You need to start it with:

    systemctl start mnt-mp.automount

    before it will show up with the mount command.

Leave a Reply to Michael Cancel reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.