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