NFS mounts and systemd
NFS mount failing to mount on reboot
An issue that was occuring for me was that upon reboot, the NFS mount
configured in /etc/fstab
would attempt to mount before the network was up. So
the NFS mount would fail as neither the network or DNS were working at the
point the mount was attempted.
I tried various suggestions to get it working such as using _netdev
in the
“mount options” field in /etc/fstab
but those did not work.
My solution
After some various searching I settled on this solution.
DNS online systemd service file
I created a service file called: /etc/systemd/system/dns-online@.service
[Unit]
Description=Wait for DNS to be online: %I
After=network-online.target
[Service]
ExecStartPre=/usr/bin/bash -c "while ! host %i; do sleep 1; done"
ExecStart=/usr/bin/bash -c "echo dns-online: DNS is online. Found %i"
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
systemd mount file
I then created a systemd mount file to do the mount. This could also be done in /etc/fstab
The mount file was created in /etc/systemd/system/
For example if you wanted to mount an NFS mount in /opt/nfs
you would create
the file: /etc/systemd/system/opt-nfs.mount
. Note that the file name of the
mount file must match the mount point in the file.
For this example we want to be able to check that EXAMPLE.COM
is available.
You would need to change it to something that you know will be available in
your environment. Maybe you would want to use google.com
. EXAMPLE.COM
is
all uppercase so that you know you need to change it.
And the /etc/systemd/system/opt-nfs.mount
file would look like this:
[Unit]
Description=NFS mount of /opt/nfs
Requires=dns-online@EXAMPLE.COM.service
After=dns-online@EXAMPLE.COM.service
# If you need your NFS mount to occur "before" some other service then add a
# `Before=` line
# Before=DESIRED_SERVICE_NAME_HERE
[Mount]
What=NFS.EXAMPLE.COM:/
Where=/opt/nfs
Type=nfs
Options=_netdev,defaults
[Install]
WantedBy=multi-user.target