linuxbashunixshellnfs

Is there a good way to detect a stale NFS mount


I have a procedure I want to initiate only if several tests complete successfully.

One test I need is that all of my NFS mounts are alive and well.

Can I do better than the brute force approach:


mount | sed -n "s/^.* on \(.*\) type nfs .*$/\1/p" | 
while read mount_point ; do 
  timeout 10 ls $mount_point >& /dev/null || echo "stale $mount_point" ; 
done

Here timeout is a utility that will run the command in the background, and will kill it after a given time, if no SIGCHLD was caught prior to the time limit, returning success/fail in the obvious way.


In English: Parse the output of mount, check (bounded by a timeout) every NFS mount point. Optionally (not in the code above) breaking on the first stale mount.


Solution

  • You could write a C program and check for ESTALE.

    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    #include <iso646.h>
    #include <errno.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(){
        struct stat st;
        int ret;
        ret = stat("/mnt/some_stale", &st);
        if(ret == -1 and errno == ESTALE){
            printf("/mnt/some_stale is stale\n");
            return EXIT_SUCCESS;
        } else {
            return EXIT_FAILURE;
        }
    }