2

I have a simple Ubuntu 20.04 home server sharing a directory by NFS. The directory being shared is contained on a local filesystem, but not the root filesystem. Sometimes on boot the NFS server starts before the mount is ready, and so the NFS server errors out. I need to login and restart the NFS server (sudo systemctl restart nfs-server.service), or reboot and get lucky.

Is threre a way to ensure that the NFS server starts only once this filesystem is ready?


The error from the NFS server is like this:

$ systemctl status nfs-server.service
● nfs-server.service - NFS server and services
     Loaded: loaded (/lib/systemd/system/nfs-server.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Tue 2020-05-19 11:16:34 BST; 3h 10min ago
    Process: 927 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=1/FAILURE)
    Process: 931 ExecStopPost=/usr/sbin/exportfs -au (code=exited, status=0/SUCCESS)
    Process: 932 ExecStopPost=/usr/sbin/exportfs -f (code=exited, status=0/SUCCESS)

May 19 11:16:34 box systemd[1]: Starting NFS server and services...
May 19 11:16:34 box exportfs[927]: exportfs: /etc/exports [2]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/storage/Shared".
May 19 11:16:34 box exportfs[927]:   Assuming default behaviour ('no_subtree_check').
May 19 11:16:34 box exportfs[927]:   NOTE: this default has changed since nfs-utils version 1.0.x
May 19 11:16:34 box exportfs[927]: exportfs: Failed to stat /storage/Shared: No such file or directory
May 19 11:16:34 box systemd[1]: nfs-server.service: Control process exited, code=exited, status=1/FAILURE
May 19 11:16:34 box systemd[1]: nfs-server.service: Failed with result 'exit-code'.
May 19 11:16:34 box systemd[1]: Stopped NFS server and services.

The /etc/fstab line looks like so:

UUID=<blah>   /storage   btrfs   nofail,subvol=@storage,compress-force=zstd   0       0

The /etc/exports like like so:

/storage/Shared    *(ro,insecure,all_squash)
pauldoo
  • 313

1 Answers1

1

Following the tips in the comments from @steeldriver and @Rinzwind, I managed to get this working.

First, determine the name of the systemd unit that is generated for the mount point (in my case /storage):

$ systemctl list-units | grep '/storage' | grep 'mount'
  storage-.snapshots.mount      loaded active mounted   /storage/.snapshots                                                          
  storage.mount                 loaded active mounted   /storage         

Then, following these docs on how to modify an existing unit file, I added the new After= tweak for nfs-server.service:

$ cat /etc/systemd/system/nfs-server.service.d/custom.conf 
[Unit]
After=storage.mount

Next, apply the changes and restart nfs-server to confirm that the new config is picked up (note that custom.conf is listed):

$ sudo systemctl daemon-reload
$ sudo systemctl restart nfs-server.service
$ systemctl status nfs-server.service
● nfs-server.service - NFS server and services
     Loaded: loaded (/lib/systemd/system/nfs-server.service; enabled; vendor preset: enabled)
    Drop-In: /etc/systemd/system/nfs-server.service.d
             └─custom.conf
     Active: active (exited) since Sun 2020-05-24 20:52:08 BST; 6s ago
    Process: 20384 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
    Process: 20385 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS)
   Main PID: 20385 (code=exited, status=0/SUCCESS)

May 24 20:52:07 box systemd[1]: Starting NFS server and services...
May 24 20:52:07 box exportfs[20384]: exportfs: /etc/exports [2]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/storage/Shared".
May 24 20:52:07 box exportfs[20384]:   Assuming default behaviour ('no_subtree_check').
May 24 20:52:07 box exportfs[20384]:   NOTE: this default has changed since nfs-utils version 1.0.x
May 24 20:52:08 box systemd[1]: Finished NFS server and services.

Lastly, reboot, and bask in a NFS server that was started after the mount point was available.

pauldoo
  • 313