Stop broken NFS mounts from locking a directory

nfs

I have a somewhat interesting setup: A server with multiple remote NFS servers mounted in a folder, with that folder then being re-exported over Samba. Think of it as a share proxy, keeping all the shared folders in one place.

My issue though is that whenever one of the mounts goes down (server restarted, service restarted, external hard drive that the server was exporting removed, etc) any attempt to read the mount blocks forever. This also means that running ls in that directory freezes and users connecting over Samba also freeze. This has also caused a few times one of my cron jobs to block which almost crashed the server because it had hundreds of blocked processes. This is getting very annoying as I usually have to bring up a terminal that isn't waiting for ls to finish (can't cancel it), run for i in *; do sudo umount -l -f $i; done;, hope that it works, fix the issue, then remount everything.

Is there a way to mount an NFS share with the stipulation that if the connection fails for whatever reason (preferably with a retry period) then the mount un-mount's itself or at least doesn't block?

Best Answer

Normally when mounting NFS it's a good idea to have flags set similar to this:

bg,intr,soft
   bg      If  the  first  NFS  mount  attempt times out, retry the mount in the 
           background.  After a mount operation is backgrounded, all subsequent mounts
           on the same NFS  server  will  be  backgrounded immediately, without first
           attempting the mount.  A missing mount point is treated as a timeout, to
           allow for nested NFS mounts.
   soft    If  an  NFS  file operation has a major timeout then report an I/O error
           to the calling program.  The default is to continue retrying NFS file
           operations indefinitely.
   intr    If  an  NFS  file  operation  has  a major timeout and it is hard mounted,
           then allow signals to interupt the file operation and cause it to return
           EINTR to the calling program.  The default is to not allow file operations
           to be interrupted.

You can in addition set:

timeo=5,retrans=5,actimeo=10,retry=5

which should allow the NFS mount to timeout and make the directory inaccessible if the NFS server drops the connection rather then waiting in retries.

Take a look at this link for more information about NFS mount options

Related Question