#! /bin/sh
# chkconfig: - 83 20
# description: iSCSI daemon

# Source function library.
[ -f /etc/init.d/functions ] || exit 0
. /etc/init.d/functions

# Source function library.
[ -f /etc/init.d/functions ] || exit 0
. /etc/init.d/functions

PIDFILE=/var/run/iscsid.pid
KERNNELVER="$(uname -r)"

PATH="/sbin:/bin:/usr/sbin:/usr/bin:$PATH"
export PATH
BASEDIR=/

test -d $BASEDIR || exit 0

start() {
        # Do sanity checks before we start..
        if [ ! -f /etc/iscsi.conf ]; then
                echo $"Could not find /etc/iscsi.conf!"
                exit 1
        elif [ -f $PIDFILE ] && kill -0 `head -1 $PIDFILE` >/dev/null ; then
                echo $"iSCSI daemon already running"
                exit 1
        fi

        if [ ! -f /etc/initiatorname.iscsi ] ; then
            echo "InitiatorName file /etc/initiatorname.iscsi is missing!"
            exit 1
        fi

        # see if we need to generate a unique iSCSI InitiatorName
        if grep -q "^GenerateName=yes" /etc/initiatorname.iscsi ; then
            printf "InitiatorName=%s\n" `$BASEDIR/sbin/iscsi-iname` > /etc/initiatorname.iscsi
        fi

        # start
        echo -n $"Starting iSCSI: iscsi"

        if lsmod | grep -q "^iscsi" ; then 
	    :
	else
	    if ! modprobe iscsi ; then
		echo $"Could not load module iscsi.o"
		return 1
	    fi
        fi

        # directing daemon output to a file can prevent the daemon from
        # getting a replacement socket into the kernel in a timely fashion,
        # so debugging output is now by request only, and is intended to go
        # to a whatever tty the rc.iscsi script was invoked in.
        echo -n " iscsid"
        if [ "$DEBUG_ISCSI" ] ; then
	    if [ -e /proc/scsi/scsi ] ; then
		# log SCSI error handling
		echo "scsi log error 5" > /proc/scsi/scsi
		echo "scsi log timeout 4" > /proc/scsi/scsi
	    fi
	    if [ -e /proc/sys/kernel/sysrq ] ; then
		# enable magic SysRq
		echo "1" > /proc/sys/kernel/sysrq
	    fi	
	    if [ -d /proc/scsi/iscsi ] ; then
		# turn on some useful kernel module debug messages by default
		for hba in /proc/scsi/iscsi/* ; do
		    echo "log sense always" > $hba
		    echo "log login on" > $hba
		    echo "log init on" > $hba
		    echo "log eh on" > $hba
		    echo "log alloc on" > $hba
		done
            fi
            iscsid -d
        else
	    iscsid
        fi

        echo -n $" iscsilun"
        iscsilun boot

        echo ""

	touch /var/lock/subsys/iscsi 
}

debug () {
        DEBUG_ISCSI=1
        export DEBUG_ISCSI
        start
}

stop () {
        echo -n $"Stopping iSCSI:"

        sync

        # unmount all filesystems on iSCSI devices
        echo -n $" umount"
        iscsi-umountall -t -k

        sync
        sleep 3

        killproc iscsilun > /dev/null 2>&1

        if [ -n "$(pidofproc iscsid)" ] ; then
	    echo -n " iscsid"
	    killproc iscsid
            sleep 2
	    killproc iscsi-rx -9 > /dev/null 2>&1
	    sleep 1
	    killproc iscsi-timer -9 > /dev/null 2>&1
            rm -f $PIDFILE
        fi

        echo -n " iscsi"
        if lsmod | grep -q "^iscsi" ; then 
            if ! rmmod iscsi ; then 
                echo $"The iSCSI module could not be unloaded."
                exit 1;
            fi
        fi

	if [ -e /var/lock/subsys/iscsi ] ; then
 	    rm /var/lock/subsys/iscsi
        fi

        echo
}

case "$1" in
 start)
	start
        ;;
 stop)
	stop
        ;;
 restart)
        stop
        start
        ;;
 debug)
	debug
        ;;
 redebug)
        stop
        debug
        ;;
 *)
        echo "Unknown request $1"
        ;;
esac

exit 0
