3

In a bourne shell script (#!/bin/sh) how can check to see if a remote NFS share is mounted and, if it is not, mount it? I've got an ugly set of cat, greps and ifs using the output of 'mount' at the moment but it doesn't seem to be doing a reliable job.

pgs
  • 3,471
  • 18
  • 19
DrStalker
  • 6,676
  • 24
  • 76
  • 106
  • 2
    It would be helpful to see your own effort in solving this. If you have a code that's unreliable, show it here and help us make it reliable. – Karolis T. Jul 15 '09 at 06:27

9 Answers9

5

If possible, setting up automount ( autofs ) would be the standard way to do this. It might already be in your distribution (comes with CentOS / Redhat default install ). Here is a tutorial.

Why use Automount?

Automounting is the process where mounting and unmounting of certain filesystems is done automatically by a daemon. If the filesystem is unmounted, and a user attempts to access it, it will be automatically (re)mounted. This is especially useful in large networked environments and for crossmounting filesystems between a few machines (especially ones which are not always online).

Kyle Brandt
  • 82,107
  • 71
  • 302
  • 444
3

Can you grep /etc/mtab for the device? grep -c '/mnt/foo' /etc/mtab if grep outputs '1' then /mnt/foo is mounted.

beggs
  • 386
  • 2
  • 9
3

Use mountpoint.

mountpoint -q /path/to/nfs/share || mount -t nfs server:/nfs/share /path/to/nfs/share

(I don't know how widespread or portable mountpoint is; it's provided by the initscripts package on my Debian server.)

Josh Kelley
  • 963
  • 1
  • 7
  • 17
2

In solaris

If your checking that the system where the script is running has a remote filesystem mounted then

ISMOUNTED=`/usr/sbin/mount | grep "^/path/to/mount "`
if [ "$ISMOUNTED" = "" ]
then
    mountcommand*
fi

*mountcommand could be /usr/sbin/mount /path/to/mount if there is a corresponding entry in the /etc/vfstab or /usr/sbin/mount remotehost:/remote/path /path/to/mount

user9517
  • 114,104
  • 20
  • 206
  • 289
1

You may be able to do something with stat. The "device" field will be different across different filesystems. So, assuming you want to see if /mnt/foo is mounted, you'd compare the output of stat -c%d /mnt/ to stat -c%d /mnt/foo/. If the device is different, something is mounted there.

if [ `stat -c%d /mnt/` -eq `stat -c%d /mnt/foo/` ]; then
    mount /mnt/foo
fi
derobert
  • 1,288
  • 12
  • 22
1

When it comes down to it, shell programming is about plugging together small discrete tools using pipes to produce some kind of compound utility. A utility that did what you're asking for in a "smart" way wouldn't really match the Unix philosophy.

If you want to do it more intelligently, you might want to look at doing this in Perl or Python or C, where you can use the library functions to talk to the portmapper to get information about mounted filesystems as a data structure. You can then intelligently perform the tasks to change the current state to the state you want.

James F
  • 6,549
  • 1
  • 25
  • 23
1

Just to throw another idea out there, the df command can tell you the mounted filesystem of a directory. If you throw in the -l option, you get a pretty easy test to see if a directory is on a local filesystem or not.

$ cd /net/remoteshare
$ df -l .
df: no file systems processed
$ echo $?
1

Chad Huneycutt
  • 2,096
  • 1
  • 16
  • 14
0
#!/bin/bash
mountpoint='your mountpoint'
mobileno=""
smsurl=""
mntcmd='/bin/mount -a'
mntchkcmd='/bin/mount'   
###Check mount point available or not IF not alert to service own
${mntchkcmd} | /bin/grep ${mountpoint} > /dev/null 2>&1
if [ $? != 0 ]
        then
    ${mntcmd}
    sleep 5
    ${mntchkcmd} | /bin/grep ${mountpoint} > /dev/null 2>&1
    if [ $? != 0 ]
        then
        echo "issue with mount point"
        exit
    fi
fi
###If mount point is available then check it for utilisation.
warn=90
critical=95
disksize=$(/bin/df -k ${mountpoint} | /bin/grep ${mountpoint} | /bin/awk -F% '{print $1}' | /bin/awk '{print $4}')
if [ ${disksize} -gt ${warn} ]
    then
        echo 'warn'
    elif [ ${disksize} -gt ${critical} ] 
    then
        echo 'critical' 
fi
Bart De Vos
  • 17,761
  • 6
  • 62
  • 81
0

A few suggestions:

  • If your distribution has an RC script to handle NFS mounts then it would be prudent to use or it check it's status. You can run into trouble if you incorrectly assume that services such as portmap and statd are already started.

  • It's often more reliable to use /proc/mounts in favour of output from mount or the possibly out-of-date content of /etc/mtab.

  • Use grep -qs and check the return code to determine whether a mount is present or not.

  • Assuming that all of the NFS mounts listed in /etc/fstab should be mounted then you can mount them universally with mount -a -t nfs,nfs4. Anything already mounted will be ignored.

Dan Carley
  • 25,189
  • 5
  • 52
  • 70