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

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

PIDFILE=/var/run/iscsid.pid
BASEDIR=/

# where to make a tree of device name dirs and symlinks.  unset to disable
LINKDIR=/dev/iscsi
DIR_MODE=755

start() {
        # Do sanity checks before we start..
        if [ ! -f /etc/iscsi.conf ]; then
                echo $"Could not find /etc/iscsi.conf!"
                exit 1
        elif [ -s $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
	    echo "## DO NOT EDIT OR REMOVE THIS FILE!" > /etc/initiatorname.iscsi
	    echo "## If you remove this file, the iSCSI daemon will not start." >> /etc/initiatorname.iscsi
	    echo "## If you change the InitiatorName, existing access control lists" >> /etc/initiatorname.iscsi
	    echo "## may reject this initiator.  The InitiatorName must be unique">> /etc/initiatorname.iscsi
	    echo "## for each iSCSI initiator.  Do NOT duplicate iSCSI InitiatorNames." >> /etc/initiatorname.iscsi
            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_module ; then
               echo $"Could not load module iscsi.o"
               return 1
           fi
	fi

	# By default, we try to load the scsi disk driver module.
	# If SCSI support is in modules, sd_mod won't get loaded
	# until after a /dev/sd* device is opened.  This means no 
	# messages about disks will be logged until a disk device 
	# is opened.  Worse, mounting by label won't work, since
	# it relies on /proc/partitions, which won't get updated
	# until the SCSI disk driver is loaded, creating a circular
	# dependency.  To work around these problems, we try to load 
	# the disk driver here.  If you're not using SCSI disks, 
	# you can comment this out.
	modprobe sd_mod > /dev/null 2>&1

        echo -n " iscsid"
        if [ "$DEBUG_ISCSI" ] ; then
	    if [ -e /proc/scsi/scsi ] ; then
		# log SCSI error handling
#		echo "scsi log scan 5" > /proc/scsi/scsi
		echo "scsi log timeout 3" > /proc/scsi/scsi
		echo "scsi log error 5" > /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 queue on" > $hba
#		    echo "log flow on" > $hba
		    echo "log eh on" > $hba
#		    echo "log alloc on" > $hba
		done
            fi
	    if [ $LINKDIR ] ; then
		# clear out any subdirs that may have been left by a system crash
		for dir in $LINKDIR/* ; do
		    if echo "$dir" | egrep -q "^$LINKDIR/bus[0-9]+$" && [ -d $dir ] ; then
			rm -rf -- $dir
		    fi
		done
		iscsid -d -l $LINKDIR -m $DIR_MODE
            else
		iscsid -d
            fi
        else
	    if [ $LINKDIR ] ; then
		# clear out any subdirs that may have been left by a system crash
		for dir in $LINKDIR/* ; do
		    if echo "$dir" | egrep -q "^$LINKDIR/bus[0-9]+$" && [ -d $dir ] ; then
			rm -rf -- $dir
		    fi
		done
		iscsid -l $LINKDIR -m $DIR_MODE
            else
		iscsid
            fi
        fi

        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

        if [ -n "$(pidofproc iscsid)" ] ; then
	    echo -n " iscsid"
	    killproc iscsid
	    sleep 2
	    killproc iscsid -9
            rm -f $PIDFILE
        fi

	# shutdown the kernel module
	if [ -d /proc/scsi/iscsi ] ; then
	    echo -n " iscsi"
	    for hba in /proc/scsi/iscsi/* ; do
		echo "shutdown" > $hba
	    done
	fi

	rm -f /var/lock/subsys/iscsi

        echo

}

reload() {
        if [ -f $PIDFILE ] ; then
            kill -HUP `head -1 $PIDFILE`
        fi
}

restart() {
        stop
        start
}

case "$1" in
 start) start ;;
 stop) stop ;;
 reload) reload ;;
 debug) debug ;;
 status) status iscsid ;;
 restart) restart ;;
 condrestart)
	[ -f /var/lock/subsys/iscsi ] && restart || :
	;;
 redebug)
        stop
        debug
        ;;
 *)
	echo $"Usage: $0 {start|stop|restart|reload|status|condrestart}"
	exit 1
        ;;
esac

exit 0