mchristi 6425a35
#!/bin/sh
mchristi 6425a35
#
dgregor 2628f30
# iscsi: log into iSCSI targets
dgregor 2628f30
#
mchristi 6425a35
# chkconfig: 345 13 89
mchristi 6425a35
# description: Logs into iSCSI targets needed at system startup
dgregor 2628f30
dgregor 2628f30
# Note we should have $network in Required-Start/Stop but we don't because if
dgregor 2628f30
# we would require network chkconfig will put us directly after NetworkManager
dgregor 2628f30
# when using NM, which will make our see if the network is up test succeed
dgregor 2628f30
# while NM is actually still configuring the network. By not requiring network
dgregor 2628f30
# chkconfig will use the chkconfig header to determine our start prio, starting
dgregor 2628f30
# us after the old network service, but before NM (netfs does this the same).
dgregor 2628f30
dgregor 2628f30
### BEGIN INIT INFO
dgregor 2628f30
# Provides:          iscsi
dgregor 2628f30
# Required-Start:    iscsid
dgregor 2628f30
# Required-Stop:     iscsid
dgregor 2628f30
# Default-Start: 3 4 5
dgregor 2628f30
# Default-Stop: 0 1 2 6
dgregor 2628f30
# Short-Description: Starts and stops login and scanning of iSCSI devices.
dgregor 2628f30
# Description: iscsi provides the iSCSI state machine for software iscsi/iser
dgregor 2628f30
#              and partial offloaded hardware. iscsi logs into and scans
dgregor 2628f30
#              for iSCSI devices, and shuts them down when stopped.
dgregor 2628f30
### END INIT INFO
dgregor 2628f30
mchristi 6425a35
# Source function library.
dgregor 2628f30
. /etc/rc.d/init.d/functions
dgregor 2628f30
dgregor 2628f30
exec="/sbin/iscsiadm"
dgregor 2628f30
prog="iscsi"
dgregor 2628f30
config="/etc/iscsi/initiatorname.iscsi"
dgregor 2628f30
lockfile=/var/lock/subsys/$prog
dgregor 2628f30
iscsid_lockfile=/var/lock/subsys/${prog}_iscsid
mchristi 6425a35
dgregor 2628f30
start() {
dgregor 2628f30
    [ -x $exec ] || exit 5
dgregor 2628f30
    [ -f $config ] || exit 6
mchristi 6425a35
dgregor 2628f30
    # if the network isn't up yet exit cleanly, NetworkManager will call us
dgregor 2628f30
    # again when the network is up
dgregor 2628f30
    [ ! -f /var/lock/subsys/network -a ! -f /var/lock/subsys/NetworkManager ] && exit 0
mchristi 6425a35
dgregor 2628f30
    # if no nodes are setup to startup automatically exit cleanly
dgregor 2628f30
    grep -qrs "node.startup = automatic" /var/lib/iscsi/nodes
dgregor 2628f30
    [ $? -eq 0 ] || exit 0
mchristi 6425a35
dgregor 2628f30
    # this script is normally called from startup so log into
dgregor 2628f30
    # nodes marked node.startup=automatic
dgregor 2628f30
    echo -n $"Starting $prog: "
dgregor 2628f30
    $exec -m node --loginall=automatic 2>&1 > /dev/null | grep iscsiadm
mchristi a3c1930
    # Ignore return code, because this command attempts to log into
mchristi a3c1930
    # multiple sessions and some sessions could get logged into and
mchristi a3c1930
    # some could be down temporarily.
dgregor 2628f30
    success $"Starting $prog"
dgregor 2628f30
    touch $lockfile
dgregor 2628f30
    echo
dgregor 2628f30
    return 0
mchristi 6425a35
}
mchristi 6425a35
mchristi 600a051
iscsi_sessions_running() {
mchristi 600a051
    declare -a iparams=( $(iscsiadm -m session 2>/dev/null | egrep "tcp|iser|bnx2i|be2iscsi|cxgb3i|cxgb4i") )
mchristi 600a051
    if [[ -z "${iparams[*]}" ]]; then
mchristi 600a051
        # no sessions
mchristi 600a051
        return 2
mchristi 600a051
    fi
mchristi 600a051
mchristi 600a051
    return 0
mchristi 600a051
}
mchristi 600a051
mchristi 600a051
cleanup_successful_stop() {
mchristi 600a051
    success $"Stopping $prog"
mchristi 600a051
    rm -f $lockfile
mchristi 600a051
    echo
mchristi 600a051
}
mchristi 600a051
dgregor 2628f30
stop() {
mchristi a3c1930
    # Don't turn off iscsi if root is possibly on a iscsi disk.
mchristi a3c1930
    rootopts=$(awk '{ if ($1 !~ /^[ \t]*#/ && $2 == "/") { print $4; }}' /etc/mtab)
mchristi a3c1930
    if [[ "$rootopts" =~ "_netdev" ]] ; then
mchristi a3c1930
        echo $"Can not shutdown iSCSI. Root is on a iSCSI disk."
mchristi a3c1930
mchristi a3c1930
        # Just clean up lock file if this is a system shutdown/reboot.
mchristi a3c1930
        if [ "$RUNLEVEL" = "6" -o "$RUNLEVEL" = "0" -o "$RUNLEVEL" = "1" ]; then
mchristi a3c1930
            rm -f $lockfile
mchristi a3c1930
        fi
mchristi a3c1930
mchristi a3c1930
        exit 1
mchristi a3c1930
    fi
mchristi a3c1930
dgregor 2628f30
    echo -n $"Stopping $prog: "
dgregor 2628f30
mchristi 600a051
    if ! iscsi_sessions_running ; then
mchristi 600a051
        cleanup_successful_stop
mchristi 600a051
        return 0
mchristi 600a051
    fi
mchristi 600a051
mchristi a3c1930
    if [ "$RUNLEVEL" = "6" -o "$RUNLEVEL" = "0" -o "$RUNLEVEL" = "1" ]; then
mchristi a3c1930
        $exec -m node --logoutall=all 2>&1 > /dev/null
mchristi a3c1930
    else
mchristi a3c1930
        $exec -m node --logoutall=automatic 2>&1 > /dev/null
mchristi a3c1930
    fi
mchristi a3c1930
mchristi a3c1930
    ret=$?
mchristi a3c1930
    # ignore ISCSI_ERR_NO_OBJS_FOUND/21
mchristi a3c1930
    if [[ "$ret" -ne 0 && "$ret" -ne 21 ]]; then
dgregor 2628f30
        failure $"Stopping $prog"
dgregor 2628f30
        echo
dgregor 2628f30
        return 1
dgregor 2628f30
    fi
dgregor 2628f30
mchristi 600a051
    cleanup_successful_stop
dgregor 2628f30
    return 0
mchristi 6425a35
}
mchristi 6425a35
dgregor 2628f30
restart() {
dgregor 2628f30
    stop
dgregor 2628f30
    start
dgregor 2628f30
}
dgregor 2628f30
dgregor 2628f30
reload() {
dgregor 2628f30
    return 3
dgregor 2628f30
}
dgregor 2628f30
dgregor 2628f30
force_reload() {
dgregor 2628f30
    restart
dgregor 2628f30
}
dgregor 2628f30
dgregor 2628f30
rh_status() {
mchristi 1a4adfc
    [ -f $lockfile ] || { echo $"$prog is stopped" ; return 3 ; }
dgregor 2628f30
mchristi a3c1930
    declare -a iparams=( $(iscsiadm -m session 2>/dev/null | egrep "tcp|iser|bnx2i|be2iscsi|cxgb3i|cxgb4i") )
dgregor 2628f30
    if [[ -z "${iparams[*]}" ]]; then
dgregor 2628f30
        # no sessions
mchristi 1a4adfc
	echo $"No active sessions"
dgregor 2628f30
        return 2
dgregor 2628f30
    fi
dgregor 2628f30
mchristi 1a4adfc
    iscsiadm -m session -P 3
dgregor 2628f30
    return 0
dgregor 2628f30
}
dgregor 2628f30
dgregor 2628f30
rh_status_q() {
dgregor 2628f30
    rh_status >/dev/null 2>&1
dgregor 2628f30
}
dgregor 2628f30
dgregor 2628f30
mchristi 6425a35
case "$1" in
dgregor 2628f30
    start)
dgregor 2628f30
        rh_status_q && exit 0
dgregor 2628f30
        $1
dgregor 2628f30
        ;;
dgregor 2628f30
    stop)
dgregor 2628f30
        $1
dgregor 2628f30
        ;;
dgregor 2628f30
    restart)
dgregor 2628f30
        $1
dgregor 2628f30
        ;;
dgregor 2628f30
    reload)
dgregor 2628f30
        rh_status_q || exit 7
dgregor 2628f30
        $1
dgregor 2628f30
        ;;
dgregor 2628f30
    force-reload)
dgregor 2628f30
        force_reload
dgregor 2628f30
        ;;
dgregor 2628f30
    status)
dgregor 2628f30
        rh_status
dgregor 2628f30
        ;;
dgregor 2628f30
    condrestart|try-restart)
dgregor 2628f30
        rh_status_q || exit 0
dgregor 2628f30
        restart
dgregor 2628f30
        ;;
dgregor 2628f30
    *)
dgregor 2628f30
        echo $"Usage: $0
dgregor 2628f30
{start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
dgregor 2628f30
        exit 2
mchristi 6425a35
esac
dgregor 2628f30
exit $?