#!/bin/sh
#
# chkconfig: - 13 87
# description: Starts and stops the iSCSI initiator
#
# pidfile: /var/run/iscsid.pid
# config:  /etc/iscsid.conf
# processname: iscsid

### BEGIN INIT INFO
# Provides: open-iscsi
# Required-Start: $network $local_fs
# Required-Stop: $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: iSCSI initiator
# Description: Starts and stops the iSCSI initiator
### END INIT INFO

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/rc.d/init.d/functions

ISCSID=iscsid
ISCSIADM=iscsiadm

PIDFILE=/var/run/iscsid.pid
LOCKFILE=/var/lock/subsys/open-iscsi
RETVAL=0

attach_nodes()
{
	echo -n "Attaching nodes: "
	$ISCSIADM -m node --loginall=automatic 2> /dev/null
	RETVAL=$?
	if [ $RETVAL == "0" ]; then
		echo_success
	elif [ $RETVAL -eq "19" ]; then
		echo -n "No records found!"
		echo_passed
                RETVAL=0
	else
		echo_failure
	fi
	echo
	sleep 5
	action 'Mounting netdev filesystems:' mount -a -O _netdev
	return 0
}

detach_nodes()
{
	echo -n "Detaching nodes: "
	sync
	$ISCSIADM -m node --logoutall=all
	RETVAL=$?
	if [ $RETVAL == "0" ]; then
		echo_success
	else
		echo_failure
	fi
	echo
	return 0
}

umount_all_luns()
{
    local d m dev p s

    cat /proc/mounts | sed -ne '/^\/dev\/.*/p' | while read d m t o x; do 
	if [ "$m" = "/" ] ; then 
	    continue;
	fi
	if [ -L "$d" ] ; then
	    d=$(readlink -f $d)
	fi
	dev=${d##/dev}

	if [ "${dev##/sd}" = "$dev" ] ; then
	    continue;
	fi
	p="/sys/block${dev%%[0-9]*}"

	if [ ! -d ${p} ] && [ ! -d ${p}/device ] ; then
	    continue;
	fi

	s=$(cd -P ${p}/device && echo $PWD)

	case "$s" in
	    */session[0-9]*/*)
		# This is an iSCSI device
		echo -n "Unmount $m: "
		umount "$m"
		RETVAL=$?
		if [ $RETVAL == "0" ]; then
			echo_success
		else
			echo_failure
		fi
		echo
		#return 0
	    ;;
	esac
    done
}

start()
{
	modprobe -q iscsi_tcp
	modprobe -q ib_iser
	start_daemon --displayname "iSCSI initiator" --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user root -- $ISCSID
	RETVAL=$?
	if [ $RETVAL == "0" ]; then
		attach_nodes
	fi
	return $RETVAL
}

stop()
{
	t=$($ISCSIADM -m session 2> /dev/null)
	if [ "$t" ]; then
		umount_all_luns
		detach_nodes
	fi
	stop_daemon --displayname "iSCSI initiator" --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user root -- $ISCSID
	RETVAL=$?
	modprobe -r iscsi_tcp 2>/dev/null
	modprobe -r ib_iser 2>/dev/null
	return $RETVAL
}

restart()
{
	stop
	start
}

case "$1" in
	start)
	    start
	;;
	stop)
	    stop
	;;
	reload)
	    restart
	;;
	restart)
	    restart
	;;
	condstop)
	    if [ -e "$LOCKFILE" ]; then
		stop
	    fi
	;;
	condrestart)
	    if [ -e "$LOCKFILE" ]; then
		restart
	    fi
	;;
	condreload)
	    if [ -e "$LOCKFILE" ]; then
		restart
	    fi
	;;
	status)
	    status `basename $ISCSID`
	    RETVAL=$?
	    if [ "$RETVAL" -eq "0" ]; then
		echo "Active sessions:"
		$ISCSIADM -m session
	    fi
	;;
	*)
	    msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
	RETVAL=1
esac

exit $RETVAL
