7a444f
#!/bin/bash
7a444f
7a444f
VOLNAME=
7a444f
CONF=
7a444f
IP=
7a444f
LOG=
7a444f
DBGLVL=
7a444f
OPT=
7a444f
FILE1=
7a444f
7a444f
trap    cleanup   SIGHUP SIGINT SIGTERM
7a444f
7a444f
7a444f
        if [ $EUID -ne 0 ];  then
7a444f
                echo "You have to be root to run this script"
7a444f
                exit 1
7a444f
        fi
7a444f
7a444f
7a444f
function usage {
7a444f
        echo " Usage : nfs-ganesha.sh [-l <logfile>][-n <dbg_lvl>] -v <volname> -i <ganesha_host_ip> -f <config_file> -o <start|stop>
7a444f
               [-h]                    display this help
7a444f
               [-l <logfile>]          set the logfile for the daemon
7a444f
               [-n <dbg_lvl>]          set the verbosity level
7a444f
	       dbg_lvl options        NIV_NULL, NIV_MAJ, NIV_CRIT, NIV_EVENT, NIV_DEBUG, NIV_MID_DEBUG, NIV_FULL_DEBUG 
7a444f
               -v <volname>            name of the volume to be exported
7a444f
               -i <ganesha_host_ip>    IP of the ganesha host
7a444f
               -f <config_file>        set the config file to be used
7a444f
               -o <start|stop>         start or stop ganesha server
7a444f
                ===========Default Values=============
7a444f
                LogFile    : /tmp/nfs-ganesha.log
7a444f
                DebugLevel : NIV_EVENT"
7a444f
        }
7a444f
7a444f
while getopts "hf:i:l:n:v:o: -l help" OPTION
7a444f
           do
7a444f
             case $OPTION in
7a444f
                     f) CONF=$OPTARG
7a444f
                     ;;
7a444f
                     i) IP=$OPTARG
7a444f
                     ;;
7a444f
                     l) LOG=$OPTARG
7a444f
                     ;;
7a444f
                     n) DBGLVL=$OPTARG
7a444f
                     ;;
7a444f
                     h) usage 
7a444f
			exit 1
7a444f
                     ;;
7a444f
                     v) VOLNAME=$OPTARG
7a444f
                     ;;
7a444f
                     o) OPT=$OPTARG
7a444f
                     ;;
7a444f
             esac
7a444f
     done
7a444f
7a444f
7a444f
function cleanup ()
7a444f
{
7a444f
	if [ -f /tmp/old-ganesha.conf ] 
7a444f
	then  cp /tmp/old-ganesha.conf $CONF 
7a444f
	exit 1
7a444f
	fi 
7a444f
}
7a444f
7a444f
7a444f
function check_for_stop()
7a444f
{
7a444f
	if  echo $OPT | grep -i -q  "stop" 
7a444f
	then stop_ganesha
7a444f
	fi
7a444f
}
7a444f
7a444f
function check_usage()
7a444f
{
7a444f
7a444f
     if [ "$VOLNAME" = "" ]
7a444f
     then
7a444f
             usage
7a444f
             exit 1
7a444f
     fi
7a444f
     if [ "$CONF" = "" ]
7a444f
     then
7a444f
             usage
7a444f
             exit 1
7a444f
     fi
7a444f
     if [ "$IP" = "" ]
7a444f
     then
7a444f
             usage
7a444f
     exit 1
7a444f
     fi
7a444f
     if [ "$OPT" = "" ]
7a444f
     then
7a444f
             usage
7a444f
     exit 1
7a444f
     fi
7a444f
}
7a444f
function check_ip {
7a444f
        if [[ ! $IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
7a444f
                echo "Invalid  IP , please enter the correct IP of the ganesha host."
7a444f
                usage
7a444f
                exit 1
7a444f
        fi
7a444f
}
7a444f
7a444f
7a444f
function check_volname
7a444f
{
7a444f
        if ! ls /var/lib/glusterd/vols | grep -q "$VOLNAME" ;  then
7a444f
                echo "Volume doesn't exist. Please enter a  valid volume name."
7a444f
                exit 1
7a444f
        fi
7a444f
}
7a444f
7a444f
function check_conf
7a444f
7a444f
7a444f
{
7a444f
7a444f
        if ! [ -f "$CONF" ]; then
7a444f
                echo "The config_file $CONF doesn't exist."
7a444f
                usage
7a444f
                exit 1
7a444f
        else
7a444f
                if ! [ -s "$CONF" ]; then
7a444f
                        echo "The cofig_file $CONF is empty "
7a444f
                        exit 1
7a444f
                fi
7a444f
        fi
7a444f
}
7a444f
7a444f
function  check_option
7a444f
{
7a444f
        if ! echo $OPT | grep -i  -q  -e "start"  -q -e "stop"
7a444f
        then
7a444f
                        echo  "Invalid value for option 'o': start or stop expected"
7a444f
                                exit 1
7a444f
        else if echo $OPT | grep -i -q "start"
7a444f
        then
7a444f
                OPT="start"
7a444f
        else
7a444f
                OPT="stop"
7a444f
        fi
7a444f
        fi
7a444f
}
7a444f
7a444f
7a444f
function check_debug
7a444f
{
7a444f
        if [ "$DBGLVL" = "" ] ; then
7a444f
                DBGLVL="NIV_EVENT"
7a444f
        else
7a444f
                if ! echo $DBGLVL | grep -q -e " NIV_NULL" -q -e  "NIV_MAJOR" -q -e " NIV_CRIT" -q -e "NIV_EVENT" -q -e "NIV_DEBUG" -q -e "NIV_FULL_DEBUG"
7a444f
                then
7a444f
                        echo  "Invalid value for option 'n': NIV_NULL, NIV_MAJ, NIV_CRIT, NIV_EVENT, NIV_DEBUG, NIV_MID_DEBUG or NIV_FULL_DEBUG expected."
7a444f
                        exit 1
7a444f
                fi
7a444f
        fi
7a444f
7a444f
}
7a444f
7a444f
7a444f
function check_logfile
7a444f
{
7a444f
        if [ "$LOG" = "" ]; then
7a444f
                LOG="/tmp/nfs-ganesha.log"
7a444f
        else if [ -f $LOG ]
7a444f
        then
7a444f
                cp $LOG /tmp/old-ganesha.log
7a444f
                FILE1="1"
7a444f
        fi
7a444f
        fi
7a444f
}
7a444f
7a444f
function call_checks
7a444f
{
7a444f
        check_ip
7a444f
        check_conf
7a444f
	cp $CONF /tmp/old-ganesha.conf
7a444f
7a444f
        check_debug
7a444f
        check_logfile
7a444f
        check_option
7a444f
        check_volname
7a444f
}
7a444f
function check_glusterd()
7a444f
{
7a444f
7a444f
        if ! ps aux | grep -q -e "[g]lusterd$" -q -e "[g]lusterd.pid"
7a444f
        then
7a444f
                echo "glusterd not started , please start glusterd. "
7a444f
                exit 1
7a444f
        fi
7a444f
}
7a444f
7a444f
function check_bricks()
7a444f
{
7a444f
        gluster volume status $VOLUME | grep -q  "N/A"
7a444f
        if [ "$?" -eq 0 ]
7a444f
        then
7a444f
                echo "Brick(s) not online , please check $VOLUME volume status"
7a444f
                exit 1
7a444f
        fi
7a444f
}
7a444f
7a444f
function check_glusternfs
7a444f
{
7a444f
        if  echo "$(ls /var/lib/glusterd/vols | wc -l)" | grep -q "1" ; then
7a444f
                if   ps aux | grep -q "[g]luster/nfs" ; then
7a444f
                echo "gluster-nfs server is active, needs to be disabled to proceed. "
7a444f
                while true; do
7a444f
                        read -p "Do you wish to disable gluster-nfs server ? [Y\N]" yn
7a444f
                        case $yn in
7a444f
                                [Yy]* ) gluster volume set $VOLNAME nfs.disable ON >/dev/null 2>/dev/null; break;;
7a444f
                                [Nn]* ) exit 1;;
7a444f
                                * ) echo "Please answer yes or no.";;
7a444f
                        esac
7a444f
                done
7a444f
                sleep 2
7a444f
                        if ps aux | grep -q "[g]luster/nfs" ; then
7a444f
                                 echo "Volume set unsuccessful , please try disabling gluster-nfs server "
7a444f
                                exit 1
7a444f
                         fi
7a444f
                sleep 2
7a444f
                        if rpcinfo -p | grep -q -e  "nfs_acl" ; then
7a444f
                                 echo " gluster-nfs ports still in use; please try again"
7a444f
                        exit 1
7a444f
                        fi
7a444f
7a444f
         fi
7a444f
 else
7a444f
         if    ps aux | grep -q  "[g]luster/nfs"; then
7a444f
                echo "Please disable gluster-nfs servers on all the volumes"
7a444f
                exit 1
7a444f
                 fi
7a444f
        fi
7a444f
}
7a444f
7a444f
function check_kernelnfs()
7a444f
{
7a444f
        service nfs status  | grep -q -e "dead" -q -e "stopped" -q -e "disabled" 
7a444f
        if [ "$?" -eq 1 ]; then
7a444f
                echo "kernel-NFS server is active , it needs to disabled to proceed. "
7a444f
                while true; do
7a444f
                        read -p "Do you wish to disable kernel-nfs server ? [Y\N]" yn
7a444f
                                case $yn in
7a444f
                                         [Yy]* ) service nfs stop; break;;
7a444f
                                         [Nn]* ) exit 1;;
7a444f
                                          * ) echo "Please answer yes or no.";;
7a444f
                                esac
7a444f
                done
7a444f
                sleep 2
7a444f
                service nfs status | grep -q -e "dead" -q -e "stopped" -q -e "inactive" >>/dev/null
7a444f
                if [ "$?" -eq 1 ]; then
7a444f
                        echo " kernel-NFS server couldn't be disabled , please try again"
7a444f
                        exit 1
7a444f
                else
7a444f
                        echo "kernel-NFS server successfully disabled"
7a444f
                fi
7a444f
        fi
7a444f
}
7a444f
7a444f
7a444f
function check_volstart()
7a444f
{
7a444f
7a444f
        if ! cat /var/lib/glusterd/vols/$1/info | grep -q "status=1" 
7a444f
        then
7a444f
                echo "Volume $1 is not started, please start the volume."
7a444f
                exit 1
7a444f
        fi
7a444f
}
7a444f
7a444f
function check_ganesha
7a444f
{
7a444f
        if  ps aux | grep -q "[g]anesha.nfsd" ;  then
7a444f
                echo "NFS-ganesha server is already active , nothing to do."
7a444f
                exit 1
7a444f
        fi
7a444f
}
7a444f
7a444f
7a444f
function check_ports
7a444f
{
7a444f
        if netstat -an | grep -q "2049$" ; then
7a444f
                echo "Port 2049 is already in use , exiting"
7a444f
                exit 1
7a444f
        fi
7a444f
}
7a444f
7a444f
function check_fsal
7a444f
{
7a444f
7a444f
       if !  ls /usr/lib64/ganesha | grep -q "libfsalgluster.so"  ; then
7a444f
               echo "Cannot find shared object libfsalgluster.so , exiting"
7a444f
               exit 1
7a444f
       fi
7a444f
}
7a444f
7a444f
7a444f
7a444f
7a444f
function start_ganesha
7a444f
{
7a444f
	check_ganesha
7a444f
        #check_rpms
7a444f
        check_fsal
7a444f
        check_glusterd
7a444f
        check_volstart $1
7a444f
        check_kernelnfs
7a444f
        check_glusternfs  $1
7a444f
        check_ports
7a444f
        check_conf $3
7a444f
        check_bricks $1
7a444f
        sed -i /^[[:space:]]*\#/!s/volume.*/"volume=$1,hostname=$2\";"/ $3
7a444f
        sed -i /^[[:space:]]*\#/!s/Pseudo.*/Pseudo="\"\/$1\";"/ $3
7a444f
        sed -i s/Path.*/Path="\"\/$1\";"/ $3
7a444f
        sed -i 's/\r//g' $3
7a444f
        /usr/bin/ganesha.nfsd -f $CONF  -L  $LOG -N $DBGLVL -d  >/dev/null 2>/dev/null
7a444f
7a444f
        sleep 5
7a444f
7a444f
}
7a444f
7a444f
7a444f
function stop_ganesha
7a444f
{
7a444f
        if ! ps aux | grep -q "[g]anesha.nfsd" ;  then
7a444f
                echo "NFS-ganesha server already inactive,nothing to do."
7a444f
                exit 1
7a444f
        fi
7a444f
        kill -9 `cat /var/run/ganesha.pid`
7a444f
        if ps aux | ganesha.nfsd ; then
7a444f
                if  rpcinfo -p | grep -q -e "nfs" ; then
7a444f
                        echo "NFS-ganesha server could not be stopped, please try again"
7a444f
                fi
7a444f
        else
7a444f
                echo " NFS-ganesha server is now inactive."
7a444f
		exit 0
7a444f
        fi
7a444f
}
7a444f
7a444f
7a444f
7a444f
function check_for_stop()
7a444f
{
7a444f
        if  echo $OPT | grep -i -q  "stop"
7a444f
        then stop_ganesha
7a444f
        fi
7a444f
}
7a444f
7a444f
7a444f
7a444f
check_for_stop
7a444f
check_usage
7a444f
call_checks
7a444f
7a444f
if [ "$OPT" = "start"  ]
7a444f
then
7a444f
        start_ganesha $VOLNAME $IP $CONF
7a444f
        if  ! ps aux | grep -q "[g]anesha.nfsd" ; then
7a444f
                start_ganesha $VOLNAME $IP $CONF
7a444f
        fi
7a444f
        sleep 5
7a444f
        if ! ps aux | grep  -q "[g]anesha.nfsd"
7a444f
        then
7a444f
                if [ "$FILE1" = "1" ]
7a444f
                then
7a444f
                cp /tmp/old-ganesha.log $LOG
7a444f
                rm -rf /tmp/ganesha.log
7a444f
                fi
7a444f
                cp /tmp/old-ganesha.conf $CONF
7a444f
		rm -rf /tmp/ganesha.conf
7a444f
                 echo "Failed to start NFS-ganesha server , please see $LOG for details"
7a444f
         else
7a444f
                echo ""
7a444f
                echo "NFS-ganesha server started."
7a444f
                 echo "============Volume exports============"
7a444f
                 showmount -e $IP
7a444f
		echo "======================================"
7a444f
		rm -rf /tmp/old-ganesha.conf
7a444f
         fi
7a444f
fi
7a444f
7a444f
  if  [ "$OPT" = "stop" ]
7a444f
then
7a444f
                stop_ganesha
7a444f
		exit 0
7a444f
7a444f
fi
7a444f