File tgt.init of Package tgt

#!/bin/sh
#
# /etc/init.d/tgtd
#
### BEGIN INIT INFO
# Provides:          tgtd
# Required-Start:    $remote_fs $network
# Should-Start:
# Required-Stop:     $remote_fs $network     
# Should-Stop:
# Default-Start:     3 5
# Default-Stop:      
# Short-Description: generic storage target daemon
# Description:       Starts and stops the generic storage target subsystem
### END INIT INFO

#
#

DAEMON=/usr/sbin/tgtd
TGTD_CONFIG=/etc/tgt/targets.conf

# Source LSB init functions
. /etc/rc.status

rc_reset

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

start()
{
	echo "Starting target framework daemon"
	# Start tgtd first.
	tgtd &>/dev/null
	RETVAL=$?
	if [ "$RETVAL" -ne 0 ] ; then
	    rc_failed -v
	else
	# 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
	    rc_failed 0
	fi
	rc_status -v
}

stop()
{
	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
	    rc_failed 7
	    [ "$TASK" != "restart" ] && exit 1
	elif [ "$RETVAL" -ne 0 ] ; then
	    echo -n "(Some initiators are still connected)"
	    rc_failed 1
	fi
	rc_status -v
}

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
	    rc_failed 7
	    [ "$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
		rc_failed 1
	    fi
	fi
}

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
	    rc_failed 7
	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
	    rc_failed 7
	fi
}

case "$1" in
  start)
	echo -n "Starting SCSI target service: "
	start
	rc_status -v
        ;;
  stop)
	echo -n "Stopping SCSI target service: "
	stop
	if [ $RETVAL != "0" ]; then
	    rc_failed
	else
	    modprobe -r scsi_tgt 2>/dev/null
	    modprobe -r crc32c 2>/dev/null
	    rc_failed 0
	fi
	rc_status -v
        ;;
  forcedstop)
	forcedstop
	rc_status -v
	;;
  forcereload)
	forcedreload
	rc_status -v
	;;
  restart|reload)
	TASK=$1
	stop
	RETVAL=$?
	if [ $RETVAL -eq 0 ] ; then
	    start
	fi
	rc_status -v
        ;;
  status)
	echo -n "Checking for SCSI target service"
	checkproc $DAEMON
	rc_status -v
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart|reload|forcedreload|forcedstop|status}"
        exit 1
esac
rc_exit
openSUSE Build Service is sponsored by