Sophie

Sophie

distrib > CentOS > 5 > x86_64 > by-pkgid > b3e7696fd616207b518b775bed3a3497 > files > 1

scsi-target-utils-1.0.14-2.el5.x86_64.rpm

#!/bin/sh
#
# chkconfig: - 39 35
#
### BEGIN INIT INFO
# Provides:          tgtd
# Required-Start:    $network
# Short-Description: Starts and stops the generic storage target daemon
# Description: tgtd provides the SCSI and software transport target state
#              machine daemon.
### END INIT INFO
#
#
# pidfile: /var/run/tgtd.pid
#
# Source function library.
. /etc/init.d/functions

# Check for and source configuration file otherwise set defaults
[ -f /etc/sysconfig/tgtd ] && . /etc/sysconfig/tgtd

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

RETVAL=0

TGTD_CONFIG=/etc/tgt/targets.conf

TASK=$1

start()
{
	echo -n $"Starting SCSI target daemon: "
	if [ -f /var/lock/subsys/tgtd ]; then
		success
		echo
		return
	fi

	echo "Starting target framework daemon"
	# Start tgtd first.
	tgtd &>/dev/null
	RETVAL=$?
	if [ "$RETVAL" -ne 0 ] ; then
	    echo "Could not start tgtd (is tgtd already running?)"
	    exit 1
	fi
	# Put tgtd into "offline" state until all the targets are configured.
	# We don't want initiators to (re)connect and fail the connection
	# if it's not ready.
	tgtadm --op update --mode sys --name State -v offline
	# Configure the targets.
	tgt-admin -e -c $TGTD_CONFIG
	# Put tgtd into "ready" state.
	tgtadm --op update --mode sys --name State -v ready

	echo
	touch /var/lock/subsys/tgtd
}

stop()
{
	echo -n $"Stopping SCSI target daemon: "

	if [ ! -f /var/lock/subsys/tgtd ]; then
		success
		echo
		return
	fi

	if [ "$RUNLEVEL" == 0 -o "$RUNLEVEL" == 6 ] ; then
	    forcedstop
	fi
	echo "Stopping target framework daemon"
	# Remove all targets. It only removes targets which are not in use.
	tgt-admin --update ALL -c /dev/null &>/dev/null
	# tgtd will exit if all targets were removed
	tgtadm --op delete --mode system &>/dev/null
	RETVAL=$?
	if [ "$RETVAL" -eq 107 ] ; then
	    echo "tgtd is not running"
	    [ "$TASK" != "restart" ] && exit 1
	elif [ "$RETVAL" -ne 0 ] ; then
	    echo "Some initiators are still connected - could not stop tgtd"
	    exit 2
	fi

	rm -f /var/lock/subsys/tgtd
	success
	echo
}

forcedstop()
{
	# NOTE: Forced shutdown of the iscsi target may cause data corruption
	# for initiators that are connected.
	echo "Force-stopping target framework daemon"
	# Offline everything first. May be needed if we're rebooting, but
	# expect the initiators to reconnect cleanly when we boot again
	# (i.e. we don't want them to reconnect to a tgtd which is still
	# working, but the target is gone).
	tgtadm --op update --mode sys --name State -v offline &>/dev/null
	RETVAL=$?
	if [ "$RETVAL" -eq 107 ] ; then
	    echo "tgtd is not running"
	    [ "$TASK" != "restart" ] && exit 1
	else
	    tgt-admin --offline ALL
	    # Remove all targets, even if they are still in use.
	    tgt-admin --update ALL -c /dev/null -f
	    # It will shut down tgtd only after all targets were removed.
	    tgtadm --op delete --mode system
	    RETVAL=$?
	    if [ "$RETVAL" -ne 0 ] ; then
		echo "Failed to shutdown tgtd"
		exit 1
	    fi
	fi
	echo -n
}

reload()
{
	echo "Updating target framework daemon configuration"
	# Update configuration for targets. Only targets which
	# are not in use will be updated.
	tgt-admin --update ALL -c $TGTD_CONFIG &>/dev/null
	RETVAL=$?
	if [ "$RETVAL" -eq 107 ] ; then
	    echo "tgtd is not running"
	    exit 1
	fi
}

forcedreload()
{
	echo "Force-updating target framework daemon configuration"
	# Update configuration for targets, even those in use.
	tgt-admin --update ALL -f -c $TGTD_CONFIG &>/dev/null
	RETVAL=$?
	if [ "$RETVAL" -eq 107 ] ; then
	    echo "tgtd is not running"
	    exit 1
	fi
}

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