I use NFS to share data across the computers inside my LAN’s firewall. If you do as well, read on! If not … well, sorry, this one isn’t for you.
One of the problems I’ve had with network shares (including NFS) is that if the remote host goes offline, the other computers using the shared drive freak out, and hang while waiting for the remote host to come back online.
In a rare instance of "don’t re-invent the wheel", I actually checked to see if someone else had solved this problem before spending a lot of time writing code. And lo and behold, I found nfs-automount by Ville Walveranta on GitHub, written back in 2013.
The problem I almost immediately ran into was where he had (cleverly) used the
showmounts command to check for the remote NFS share’s existence before attempting to connect. The problem is that
showmounts uses RPC, which while used for NFSv2 and NFSv3, was discontinued for NFSv4 (reference).
I kind of kludged a fix to make the script compatible for NFSv4:
- If an RPC error is returned, it now assumes that it is an NFSv4 mount.
- If there is an error with mounting (either because the mount does not exist or a server misconfiguration, resulting in a “backgrounding” message) it reports the mount as unmounted and also calls the unmount routine for the local mountpoint. It also returns the full error message in the logs rather just whether or not it succeeded.
While I’ve submitted a pull request to Ville Walveranta’s repo, it doesn’t seem that he’s made any contributions to GitHub in quite some time, so I’ve also forked the repository and created my own version 1.1.1 release with these changes incorporated. With those bits in place, the script is working automatically as a cronjob on my Debian Bullseye system. Check it out at https://github.com/uriel1998/nfs_automount