systemd: wait for NFS server

My new home server mounts some NFS shares from the NAS. When electricity is restored after an outage, both NAS and home server boot up at the same time. The new home server is much faster than my rusty NAS, and so services depending on NFS mount data would start too early, possibly removing data from their database (e.g. Gerbera or paperless-ngx).

To not run into that problem I added a systemd service that waits for the NAS to be available before trying to mount the NFS shares.

At first a service that waits for the NAS ("disa", short for DiskStation) to be reachable ping:

/etc/systemd/system/wait-for-disa.service
[Unit]
Description=Blocks until it successfully pings disa 192.168.3.96
After=network-online.target
 
[Service]
ExecStartPre=/usr/bin/bash -c "while ! ping -c1 192.168.3.96; do sleep 1; done"
ExecStart=/usr/bin/sh -c "echo good to go"
RemainAfterExit=yes
 
[Install]
WantedBy=multi-user.target

This has to be enabled with

$ systemctl daemon-reload
$ systemctl enable wait-for-disa

Now the NFS mounts are configured to wait for that service to become available:

/etc/fstab
disa:/volume2/media  /mnt/media-disa  nfs  x-systemd.after=wait-for-disa.service,timeo=50

A systemctl daemon-reload and all is set.

Written by Christian Weiske.

Comments? Please send an e-mail.