mchristi 63a3144
#!/bin/sh
mchristi 63a3144
#
a4c8b30
# iscsi: log into iSCSI targets
a4c8b30
#
d3d8564
# chkconfig: 345 13 89
mchristi 63a3144
# description: Logs into iSCSI targets needed at system startup
a4c8b30
5a3872e
### BEGIN INIT INFO
5a3872e
# Provides:          iscsi
Mike Christie ec32d86
# Required-Start:    iscsid network
2d4a526
# Should-Start:      tgtd
a4c8b30
# Required-Stop:     iscsid
2d4a526
# Should-Stop:       tgtd
a4c8b30
# Default-Start: 3 4 5
a4c8b30
# Default-Stop: 0 1 2 6
5a3872e
# Short-Description: Starts and stops login and scanning of iSCSI devices.
5a3872e
# Description: iscsi provides the iSCSI state machine for software iscsi/iser
5a3872e
#              and partial offloaded hardware. iscsi logs into and scans
5a3872e
#              for iSCSI devices, and shuts them down when stopped.
5a3872e
### END INIT INFO
a4c8b30
mchristi 63a3144
# Source function library.
a4c8b30
. /etc/rc.d/init.d/functions
a4c8b30
a4c8b30
exec="/sbin/iscsiadm"
a4c8b30
prog="iscsi"
a4c8b30
config="/etc/iscsi/initiatorname.iscsi"
a4c8b30
lockfile=/var/lock/subsys/$prog
a4c8b30
iscsid_lockfile=/var/lock/subsys/${prog}_iscsid
a4c8b30
a4c8b30
start() {
a4c8b30
    [ -x $exec ] || exit 5
a4c8b30
    [ -f $config ] || exit 6
a4c8b30
a4c8b30
    # if the network isn't up yet exit cleanly, NetworkManager will call us
a4c8b30
    # again when the network is up
Mike Christie 13d7f9d
    [ ! -f /var/lock/subsys/network ] && ! nm-online -x >/dev/null 2>&1 && exit 3
a4c8b30
a4c8b30
    # this script is normally called from startup so log into
a4c8b30
    # nodes marked node.startup=automatic
a4c8b30
    echo -n $"Starting $prog: "
a4c8b30
    $exec -m node --loginall=automatic 2>&1 > /dev/null | grep iscsiadm
Mike Christie 3949e15
    # Ignore return code, because this command attempts to log into
Mike Christie 3949e15
    # multiple sessions and some sessions could get logged into and
Mike Christie 3949e15
    # some could be down temporarily.
a4c8b30
    success $"Starting $prog"
a4c8b30
    touch $lockfile
a4c8b30
    echo
a4c8b30
    return 0
mchristi 63a3144
}
mchristi 63a3144
Mike Christie 3949e15
iscsi_sessions_running() {
Mike Christie 3949e15
    declare -a iparams=( $(iscsiadm -m session 2>/dev/null | egrep "tcp|iser|bnx2i|be2iscsi|cxgb3i|cxgb4i") )
Mike Christie 3949e15
    if [[ -z "${iparams[*]}" ]]; then
Mike Christie 3949e15
        # no sessions
Mike Christie 3949e15
        return 2
Mike Christie 3949e15
    fi
Mike Christie 3949e15
Mike Christie 3949e15
    return 0
Mike Christie 3949e15
}
Mike Christie 3949e15
Mike Christie 3949e15
cleanup_successful_stop() {
Mike Christie 3949e15
    success $"Stopping $prog"
Mike Christie 3949e15
    rm -f $lockfile
Mike Christie 3949e15
    echo
Mike Christie 3949e15
}
Mike Christie 3949e15
a4c8b30
stop() {
Mike Christie 3949e15
    # Don't turn off iscsi if root is possibly on a iscsi disk.
Mike Christie 3949e15
    rootopts=$(awk '{ if ($1 !~ /^[ \t]*#/ && $2 == "/") { print $4; }}' /etc/mtab)
Mike Christie 3949e15
    if [[ "$rootopts" =~ "_netdev" ]] ; then
Mike Christie 3949e15
        echo $"Can not shutdown iSCSI. Root is on a iSCSI disk."
Mike Christie 3949e15
Mike Christie 3949e15
        # Just clean up lock file if this is a system shutdown/reboot.
Mike Christie 3949e15
        if [ "$RUNLEVEL" = "6" -o "$RUNLEVEL" = "0" -o "$RUNLEVEL" = "1" ]; then
Mike Christie 3949e15
            rm -f $lockfile
Mike Christie 3949e15
        fi
Mike Christie 3949e15
Mike Christie 3949e15
        exit 1
Mike Christie 3949e15
    fi
Mike Christie 3949e15
a4c8b30
    echo -n $"Stopping $prog: "
a4c8b30
Mike Christie 3949e15
    if ! iscsi_sessions_running ; then
Mike Christie 3949e15
        cleanup_successful_stop
Mike Christie 3949e15
        return 0
Mike Christie 3949e15
    fi
Mike Christie 3949e15
Mike Christie 3949e15
    if [ "$RUNLEVEL" = "6" -o "$RUNLEVEL" = "0" -o "$RUNLEVEL" = "1" ]; then
Mike Christie 3949e15
        $exec -m node --logoutall=all 2>&1 > /dev/null
Mike Christie 3949e15
    else
Mike Christie 3949e15
        $exec -m node --logoutall=automatic 2>&1 > /dev/null
Mike Christie 3949e15
    fi
Mike Christie 3949e15
Mike Christie 3949e15
    ret=$?
Mike Christie 3949e15
    # ignore ISCSI_ERR_NO_OBJS_FOUND/21
Mike Christie 3949e15
    if [[ "$ret" -ne 0 && "$ret" -ne 21 ]]; then
a4c8b30
        failure $"Stopping $prog"
a4c8b30
        echo
a4c8b30
        return 1
a4c8b30
    fi
a4c8b30
Mike Christie 3949e15
    cleanup_successful_stop
a4c8b30
    return 0
a4c8b30
}
a4c8b30
a4c8b30
restart() {
a4c8b30
    stop
a4c8b30
    start
5003024
}
5003024
a4c8b30
reload() {
a4c8b30
    return 3
mchristi 63a3144
}
mchristi 63a3144
a4c8b30
force_reload() {
a4c8b30
    restart
a4c8b30
}
a4c8b30
a4c8b30
rh_status() {
Mike Christie 3949e15
    [ -f $lockfile ] || { echo $"$prog is stopped" ; return 3 ; }
a4c8b30
Mike Christie 3949e15
    declare -a iparams=( $(iscsiadm -m session 2>/dev/null | egrep "tcp|iser|bnx2i|be2iscsi|cxgb3i|cxgb4i") )
a4c8b30
    if [[ -z "${iparams[*]}" ]]; then
a4c8b30
        # no sessions
Mike Christie 3949e15
	echo $"No active sessions"
a4c8b30
        return 2
a4c8b30
    fi
a4c8b30
Mike Christie 3949e15
    iscsiadm -m session -P 3
a4c8b30
    return 0
a4c8b30
}
a4c8b30
a4c8b30
rh_status_q() {
a4c8b30
    rh_status >/dev/null 2>&1
a4c8b30
}
a4c8b30
a4c8b30
mchristi 63a3144
case "$1" in
a4c8b30
    start)
a4c8b30
        rh_status_q && exit 0
a4c8b30
        $1
a4c8b30
        ;;
a4c8b30
    stop)
a4c8b30
        $1
a4c8b30
        ;;
a4c8b30
    restart)
a4c8b30
        $1
a4c8b30
        ;;
a4c8b30
    reload)
a4c8b30
        rh_status_q || exit 7
a4c8b30
        $1
a4c8b30
        ;;
a4c8b30
    force-reload)
a4c8b30
        force_reload
a4c8b30
        ;;
a4c8b30
    status)
a4c8b30
        rh_status
a4c8b30
        ;;
a4c8b30
    condrestart|try-restart)
a4c8b30
        rh_status_q || exit 0
a4c8b30
        restart
a4c8b30
        ;;
a4c8b30
    *)
a4c8b30
        echo $"Usage: $0
a4c8b30
{start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
a4c8b30
        exit 2
mchristi 63a3144
esac
a4c8b30
exit $?