#!/bin/sh
SHELL=/bin/sh
PATH=/etc:/bin:/sbin:/bin:/usr/bin:/usr/sbin:/usr/local/bin
# set mount/remount request flags
mount=false
remount=false
# remote system name
remotesystem="$1"
# rw/ro
if [ "$2" = "rw" ]; then
mountmode="-w"
else
mountmode="-r"
fi
# remote share name
remoteshare="$3"
# local mount point
mountpoint="$4"
# file to indicate local mount status
testfile=${mountpoint}/"$5"
# rw test file
rw_testfile=${mountpoint}/nfs_enforcer_rw_testfile
# command locations
pingcmd=/sbin/ping
showmountcmd=/usr/bin/showmount
grepcmd=/usr/bin/grep
mountcmd=/sbin/mount
umountcmd=/sbin/umount
statcmd=stat
touchcmd=/usr/bin/touch
rmcmd=/bin/rm
# --- end variables ---
# make sure the mountpoint is not stale
statresult=`${statcmd} ${mountpoint} 2>&1 | ${grepcmd} "Stale"`
if [ "${statresult}" != "" ]; then
#result not empty: mountpoint is stale; remove it
${umountcmd} -f ${mountpoint}
fi
# ping the remote system (2 sec timeout)
remoteping=`${pingcmd} -c1 -o -q -t2 ${remotesystem} | grep " 0.0%"`
# make sure the remote system is reachable
if [ "${remoteping}" != "" ] ; then
# query the availability of the remote share; not empty result indicates OK
offsiteshare=`${showmountcmd} -e ${remotesystem} | ${grepcmd} "${remoteshare}"`
if [ "${offsiteshare}" != "" ] ; then
# make sure the local mount point (directory) exists (so that [re-]mount, if necessary, is valid)
if [ -d ${mountpoint} ] ; then
localmount=`${mountcmd} | ${grepcmd} "${mountpoint}"`
# make sure the share test file is _not_ present (to make sure the mountpoint is inactive)
if [ ! -f ${testfile} ] ; then
# make sure the local mountpoint is inactive (double checking)
if [ "${localmount}" = "" ] ; then
# all set to go; request mount
mount=true
fi
else
# make sure the local mountpoint is active (double checking)
if [ "${localmount}" != "" ] ; then
# attempt to create a test file..
${touchcmd} ${rw_testfile} > /dev/null 2>&1
# ..and test its existence; first handle RW mounted shares:
if [ -f ${rw_testfile} ] ; then
# share was RO requested
if [ "$2" = "ro" ]; then
remount=true
fi
# Delete the testfile
${rmcmd} ${rw_testfile}
# hanle RO mounted shares:
else
# share was RW requested
if [ "$2" = "rw" ]; then
remount=true
fi
fi
fi
fi
fi
fi
fi
# perform remount (unmount, request mount)
if $remount ; then
${umountcmd} -f ${mountpoint}
mount=true
fi
# perform mount when so requested
if $mount ; then
${mountcmd} ${mountmode} -t nfs ${remotesystem}:${remoteshare} ${mountpoint}
fi
exit 0