#!/bin/sh
#
# chkconfig: 345 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
# Required-Stop: $network
# Default-Start: 3 4 5
# Short-Description: iSCSI initiator
# Description: Starts and stops the iSCSI initiator
### END INIT INFO

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

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

ISCSID=`which iscsid`
ISCSIADM=`which iscsiadm`

if [ -z "$ISCSID" -o -z "$ISCSIADM" ]; then
	gprintf "open-iscsi not installed.\n"
	exit 1
fi

start_iscsid()
{
	modprobe -q iscsi_tcp
	daemon $ISCSID
	return $?
}

attach_nodes()
{
	RETVAL=0
	TARGETS=`$ISCSIADM -m node | sed 's@\[\(.*\)\] .*@\1@g'`
	for rec in $TARGETS; do
		STARTUP=`$ISCSIADM -m node -r $rec | grep "node.conn\[0\].startup" | cut -d' ' -f3`
		NAME=`$ISCSIADM -m node -r $rec | grep -E "^node\.name" | cut -d " " -f 3`
		if [ $STARTUP = "automatic" ]; then
			echo
			gprintf "Attaching $NAME: "
			$ISCSIADM -m node -r $rec -l
			# failure to attach a node is not a failure of
			# starting the service
			if [ "$?" -eq "0" ]; then
				echo_success
			else
				echo_failure
			fi
		fi
	done
	return $RETVAL
}

detach_nodes()
{
	sync
	TARGETS=`$ISCSIADM | grep "\[*\]" | sed 's@\[\(.*\)\] .*@\1@g'`
	if [ -n "$TARGETS" ]; then
		for rec in $TARGETS; do
			NAME=`$ISCSIADM -m node -r $rec | grep -E "^node\.name" | cut -d " " -f 3`
			gprintf "Detaching $NAME: "
			$ISCSIADM -m node -r $rec -u
			if [ "$?" -eq "0" ]; then
				echo_success
			else
				echo_failure
			fi
			echo
		done
	fi
}
    
stop_iscsid()
{
	# XXX - the pid file only has one pid, but we have two daemons
	killall -HUP `basename $ISCSID`
	RETVAL=$?
	modprobe -r iscsi_tcp 2>/dev/null
	return $RETVAL
}

start()
{
	RETVAL=0
	gprintf "Starting %s service: " "iSCSI initiator"
	PID=`pidofproc $ISCSID`
	if [ -z $PID ]; then
		start_iscsid
	fi
	if [ $RETVAL == "0" ]; then
		echo_success
		touch /var/lock/subsys/open-iscsi
		attach_nodes
	else
		echo_failure
	fi
	echo
	return $RETVAL
}

stop()
{
	RETVAL=0
	PID=`pidofproc $ISCSID`
	RETVAL=$?
	if [ -n "$PID" ]; then
		detach_nodes
	fi
	gprintf "Stopping %s service: " "iSCSI initiator"
	if [ -n "$PID" ]; then
		stop_iscsid
		RETVAL=$?
	fi
	if [ "$RETVAL" == "0" ]; then
		echo_success
		rm -f /var/lock/subsys/open-iscsi
		rm -f /var/run/iscsid.pid
	else
	        echo_failure
	fi
	echo
	return $RETVAL
}


restart()
{
	stop
	start
	return $?
}


case "$1" in
  start)
        start
	RETVAL=$?
        ;;
  stop)
        stop
	RETVAL=$?
        ;;
  restart)
        restart
	RETVAL=$?
        ;;
  status)
	status `basename $ISCSID`
	RETVAL=$?
	if [ "$RETVAL" -eq "0" ]; then
		gprintf "Active sessions:\n"
		$ISCSIADM -m session
	fi
        ;;
  *)
        gprintf "Usage: %s {start|stop|restart|status}\n" "open-iscsi"
        exit 1
esac

exit $RETVAL
