Blob Blame History Raw
#!/bin/sh
#
# chkconfig: 345 13 89
# description: Logs into iSCSI targets needed at system startup
#
### BEGIN INIT INFO
# Provides:          iscsi
# Required-Start:    $network $iscsid
# Required-Stop:     $network $iscsid
# Short-Description: Starts and stops login and scanning of iSCSI devices.
# Description: iscsi provides the iSCSI state machine for software iscsi/iser
#              and partial offloaded hardware. iscsi logs into and scans
#              for iSCSI devices, and shuts them down when stopped.
### END INIT INFO
#
# Source function library.
. /etc/init.d/functions

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

RETVAL=0

start()
{
	if [ -f /var/lock/subsys/iscsi ] ; then
		echo
		success
		return
	fi

	# Not sure if this is ok.
	# It was done for compatiblity where users were used to
	# just starting the iscsi service, but for root boot we
	# need need iscsid start earlier than the devices we
	# log into below.
	status iscsid
	RETVAL=$?

	if [ $RETVAL -ne 0 ]; then
		/etc/init.d/iscsid start
	fi

	# this script is normally called from startup so log into
	# nodes marked node.startup=automatic
	iscsiadm -m node --loginall=automatic
	touch /var/lock/subsys/iscsi
	success
	echo
}

cleanup_iscsi()
{
	/etc/init.d/iscsid stop
	rm -f /var/lock/subsys/iscsi
	success
}

stop()
{
	if [ ! -f /var/lock/subsys/iscsi ]; then
		echo
		success
		return
	fi

	declare -a iparams=( $(iscsiadm -m session 2>/dev/null | egrep "tcp|iser") )
	if [[ -z "${iparams[*]}" ]]; then
		# no sessions so we can just quit
		cleanup_iscsi
		return
	fi

	# don't turn off iscsi if root is possibly on a iscsi disk
	rootopts=$(awk '{ if ($1 !~ /^[ \t]*#/ && $2 == "/") { print $4; }}' /etc/mtab)
	if [[ "$rootopts" =~ "_netdev" ]] ; then
		echo $"Can not shutdown iSCSI. Root is on a iSCSI disk."
		exit 1
	fi

	iscsiadm -m node --logoutall=all
	cleanup_iscsi
}

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