tags:

views:

1622

answers:

2

I want to write a sh/bash script that can determine whether a particular directory is a mount point for an NFS filesystem.

eg something like

$ mkdir localdir
$ mkdir remotedir
$ mount host:/share ./remotedir
$ classify_dirs.sh
 -->  localdir is local
 -->  remotedir is an NFS mount point
+3  A: 

This question is effectively a dup of how-can-i-tell-if-a-file-is-on-a-remote-filesystem-with-perl

The short answer is to use the stat command

eg

$ stat -f -L -c %T localdir
ext2/ext3
$ stat -f -L -c %T remotedir
nfs

Then a directory is an NFS mount point if its type is 'nfs' and its parent directory isn't.

AndrewR
A: 

Since you asked for a script, this one will do any directory you desire, giving you the file system type of that directory and its immediate subdirectories. You can simply update the translation code as new types are discovered.

#!/bin/bash

function xlat {
    retVal=" [Not recognized]"
    if [[ "$1" == "ext2/ext3" ]] ; then
        retVal=" [Normal]"
    fi
    if [[ "$1" == "tmpfs" ]] ; then
        retVal=" [Temporary]"
    fi
    if [[ "$1" == "sysfs" ]] ; then
        retVal=" [System]"
    fi
    if [[ "$1" == "proc" ]] ; then
        retVal=" [Process]"
    fi
    if [[ "$1" == "nfs" ]] ; then
        retVal=" [NFS]"
    fi
    echo ${retVal}
}

baseDir="$1"
if [[ -z "${baseDir}" ]] ; then
    baseDir=.
fi
if [[ ! -d "${baseDir}" ]] ; then
    echo "ERROR: '${baseDir}' is not a directory."
    exit
fi
echo "Processing '${baseDir}':"
for dirSpec in $(find ${baseDir} -maxdepth 1 -type d) ; do
    dirType=$(stat -f -L -c %T ${dirSpec})
    echo "   ${dirSpec} is '${dirType}'" $(xlat "${dirType}")
done

Here's the output from one of my zLinux boxes (from "classify_dirs.sh /"):

Processing '/':
   / is 'ext2/ext3' [Normal]
   /boot is 'ext2/ext3' [Normal]
   /selinux is 'UNKNOWN (0xfffffffff97cff8c)' [Not recognized]
   /bin is 'ext2/ext3' [Normal]
   /srv is 'ext2/ext3' [Normal]
   /dev is 'tmpfs' [Temporary]
   /lib64 is 'ext2/ext3' [Normal]
   /home is 'ext2/ext3' [Normal]
   /lib is 'ext2/ext3' [Normal]
   /misc is 'UNKNOWN (0x187)' [Not recognized]
   /sys is 'sysfs' [System]
   /opt is 'ext2/ext3' [Normal]
   /tmp is 'ext2/ext3' [Normal]
   /var is 'ext2/ext3' [Normal]
   /proc is 'proc' [Process]
   /usr is 'ext2/ext3' [Normal]
   /net is 'UNKNOWN (0x187)' [Not recognized]
   /lost+found is 'ext2/ext3' [Normal]
   /root is 'ext2/ext3' [Normal]
   /media is 'ext2/ext3' [Normal]
   /mnt is 'ext2/ext3' [Normal]
   /etc is 'ext2/ext3' [Normal]
   /sbin is 'ext2/ext3' [Normal]
paxdiablo