[stgt] [RFC] example init.d script for tgtd

Tomasz Chmielewski mangoo at wpkg.org
Tue Oct 21 11:57:09 CEST 2008


Mike Christie schrieb:
> Tomasz Chmielewski wrote:
>> FUJITA Tomonori schrieb:
>>> On Mon, 20 Oct 2008 14:37:40 +0200
>>> Tomasz Chmielewski <mangoo at wpkg.org> wrote:
>>>
>>>> Currently, there is no standard way to start and configure tgtd via 
>>>> a init.d script.
>>>> As a result, even if any distribution packages tgt, it doesn't 
>>>> contain any startup script.
>>>>
>>>> So let's help the packagers!
>>>
>>> Really? I thought that Mike has an init script for RHEL.
>>
>> I don't know the RHEL script you mention (I found no references in the 
>> archive), is it this one (scroll down a bit on that page)?
>>
> 
> We just added it. It is not my work. It is from Ronnie. I attached the 
> file in the mail. It might be a older version than is in RHEL. I cannot 
> log into our servers because my vpn is down. I think the /var/lock file 
> and /etc/sysconfig/tgtd might be too Red Hat'ish (I do not think other 
> distros use sysconfig).

Mike,

the RHEL script will probably fail to start here (may depend on machine,
load etc.):

	daemon tgtd $TGTD_OPTIONS

	# read the config 
	tgt-admin -e -c "${TGTD_CONFIG}"


tgtd starts in the background immediately, but does not yet accept commands
from tgtadm. So adding "sleep 1" between starting tgtd and tgt-admin should help.

Same goes for "restart".



Below is a corrected version of a generic tgt init script, which may be basis
for a distribution-specific init script.

Few notes:
* in "forcedstop", targets are offlined and the target itself is put to
  the offline "State". This makes sure that:
  - initiators won't be able to reconnect if we still remove targets (i.e., systems
    with many targets and many initiators connected), or removing the targets failed
    for some reason; if the targets are able to reconnect and their target is removed,
    they will fail the connection. This may be undesired if we only wanted to restart
    the target machine or the target process.
* "forcedstop" will not remove all the targets reliably, as tgtadm sometimes
  segfaults (see my separate post)
* I added comments to explain what is done and why


#!/bin/sh

# This is an example init.d script for stopping/starting/reconfiguring tgtd.

TGTD_CONFIG=/etc/tgt/targets.conf

TASK=$1

start()
{
	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
	# Give tgtd 1 second to daemonize.
	sleep 1s
	# 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
}

stop()
{
	if [ "$RUNLEVEL" == 0 -o "$RUNLEVEL" == 6 ] ; then
	    $0 force-stop
	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
	echo -n
}

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
}

status()
{
	# Don't name this script "tgtd"...
	TGTD_PROC=$(ps -C tgtd | grep -c tgtd)
	if [ "$TGTD_PROC" -eq 2 ] ; then
	    echo "tgtd is running. Run 'tgt-admin -s' to see detailed target info."
	else
	    echo "tgtd is NOT running."
	fi
}

case $1 in
	start)
		start
  		;;
	stop)
		stop
  		;;
	forcedstop)
		forcedstop
  		;;
	restart)
		stop && sleep 1s && start
  		;;
	forcedrestart)
		forcedstop && sleep 1s && start
		;;
	reload)
		reload
		;;
	forcedreload)
		forcedreload
		;;
	status)
		status
		;;
	*)
		echo "Usage: $0 {start|stop|forcedstop|restart|forcedrestart|reload|forcedreload|status}"
		exit 2
		;;
esac






-- 
Tomasz Chmielewski
http://wpkg.org

--
To unsubscribe from this list: send the line "unsubscribe stgt" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html



More information about the stgt mailing list