If you want to auto-mount an NFS share during boot time, you may run into the issue that by the time the auto-mount procedure is being executed, the network is not yet up and hence the mount of the NFS share will fail. Luckily, there is an easy way around it by using systemd
and let it worry about when to mount the share.
The secret lies within some parameters provided alongside the mount directive in /etc/fstab
. As you may know, /etc/fstab
is used by the Linux kernel to determine what to mount during boot time, and as such, NFS shares can equally be mounted via it. However, if you just provide a regular entry like for a partition or disk, you will run into the issue that the NFS share is being mounted way before the network interfaces have been established.
To overcome that, you want to provide the following parameters inside the /etc/fstab
entry:
x-systemd.automount,x-systemd.requires=network-online.target,x-systemd.device-timeout=10s
You can find good documentation of these and more parameters in the Ubuntu manuals. However, in short:
x-systemd.automount
: asystemd
automount unit will be created for the file system.x-systemd.requires=network-online.target
: configures aRequires=
and anAfter=
dependency between the created mount unit and the specified other unit. In short, this tellssystemd
to run the automount after the network is online, meaning that this is the secret sauce to make sure the NFS mount has a network available.x-systemd.device-timeout
: configures how longsystemd
should wait for the mount to show up before giving up on the entry from/etc/fstab
. You can use units such asms
,s
,min
, andh
, the default iss
for seconds.
So, to put it all together, this is the /etc/fstab
entry that you are looking for:
<server IP address>:<NFS share> <local mount location> nfs nofail,x-systemd.automount,x-systemd.requires=network-online.target,x-systemd.device-timeout=10s
The nofail
is not required anymore for the scenario of the network not being available yet, systemd
has taken care of that. However, there can be other reasons for the share not being available, for example, the NFS server itself being unreachable, so nofail
is a safe bet to make sure the machine comes up regardless of the NFS mount.
Once you have added the entry in /etc/fstab
and rebooted the system, you will find an additional entry reported by the mount
command:
systemd-<N> on <local mount location> type autofs (rw,relatime,fd=46,pgrp=1,timeout=0,minproto=5,maxproto=5,direct)