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