I have an NFS server on a Pi 4 which works rather well except I'm often getting "Stale file handle" on the client after rebooting the Pi. That is while the client has the share mounted, the Pi reboots, upon it being booted the client spits out the error when trying a file operation like ls. What's causing this and how can I fix it?
The problem is that the filesystem that's exported by NFS isn't always ready when NFS starts. In this particular case, there's an external disk that is asynchronously mounted on boot and sometimes NFS starts before that happens. The result is that NFS can't export the filesystem.
One good solution is to use systemd to create a dependency between NFS and the filesystem. There are multiple ways to do this but perhaps the simplest is:
- Add the option
x-systemd.before=nfs-server.serviceto the mount options for the filesystem
Once complete, nfs-server.service will only be started after the filesystem is mounted and will therefore successfully export it.