Blob Blame 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()
{
	echo -n $"Setting up iSCSI targets: "
	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
}

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

	# If this is a final shutdown/halt, do nothing since
	# lvm/dm, md, power path, etc do not always handle this nicely.
	# The kernel will do the right thing and shutdown devices (send
	# cache syncs, start_stops, etc) that need it.
	if [ "$RUNLEVEL" = "6" -o "$RUNLEVEL" = "0" -o "$RUNLEVEL" = "1" ]; then
		rm -f /var/lock/subsys/iscsi
		success
		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
	/etc/init.d/iscsid stop
	rm -f /var/lock/subsys/iscsi
	success
}

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