Why won't my NFS shares mount at boot?

Posted on Apr 29, 2021

This is part 2 of "Retrying with NGINX & systemd" - it turns out that I had to fix the underlying issue in order to have my NFS shares reliably mount at boot. Had the mount units accepted the same 'solution' as the nginx service I'd probably never fixed it. :)

This was a interesting trip down the systemd documentation and was actually quite simple once I understood it (after extensive research + trial and error). It all came down to not having a proper definition of when the network was up and running.


TL;DR - I use systemd-networkd and any service that relies on network connectivity should depend on that target (which they typically do through network-online.target). I have multiple vlans on my main interface but only one that provides routing (enp1s0.10), so I had to tell systemd-networkd-wait-online.service which interface that had to be up for anything to work.

# systemctl edit --full systemd-networkd-wait-online.service
Description=Wait for Network to be Configured
Before=network-online.target shutdown.target

# Here's the important part                             <-- here -->
ExecStart=/usr/lib/systemd/systemd-networkd-wait-online -i enp1s0.10


Don't forget to enable it as well:

systemctl enable --now systemd-networkd-wait-online.service


Another interesting thing I discovered was the systemd-fstab-generator, which apparently translates entries from my /etc/fstab into systemd units. Sometimes you have to ask it (by force) to update your mount units after changing your fstab.

A typical mount unit looks like this, given the fstab line in the comment:

  # fstab line:
  # /srv/nfs/things nfs4 rw,bg
  # ---
  # Resulting srv-nfs-things.mount
  # ---
  # Automatically generated by systemd-fstab-generator

  Documentation=man:fstab(5) man:systemd-fstab-generator(8)


These didn't properly update for me after changing the fstab line. So I moved the mount files to /tmp and ran /usr/lib/systemd/system-generators/systemd-fstab-generator

…and nothing. What the..?

So apparently this program implicitly writes it's output to /tmp. It doesn't take any args (from what the manual page states on my machine), but apparently you can tell it where to output it's files as one of three args. The other two args can/must be empty strings.

# /usr/lib/systemd/system-generators/systemd-fstab-generator /etc/systemd/system '' ''

I've read the man-page for systemd-fstab-generator again and there are no mentions of this, which is confusing. Most other documentation for systemd has been pretty good.

Oh well, it's working now. And my nginx doesn't need any retries at boot anymore!