Blob Blame Raw
#!/bin/sh
#
# iscsi: log into iSCSI targets
#
# chkconfig: 345 13 89
# description: Logs into iSCSI targets needed at system startup

# Note we should have $network in Required-Start/Stop but we don't because if
# we would require network chkconfig will put us directly after NetworkManager
# when using NM, which will make our see if the network is up test succeed
# while NM is actually still configuring the network. By not requiring network
# chkconfig will use the chkconfig header to determine our start prio, starting
# us after the old network service, but before NM (netfs does this the same).

### BEGIN INIT INFO
# Provides:          iscsi
# Required-Start:    iscsid
# Should-Start:      tgtd
# Required-Stop:     iscsid
# Should-Stop:       tgtd
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Short-Description: Starts and stops login and scanning of iSCSI devices.
# Description: iscsi provides the iSCSI state machine for software iscsi/iser
#              and partial offloaded hardware. iscsi logs into and scans
#              for iSCSI devices, and shuts them down when stopped.
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

exec="/sbin/iscsiadm"
prog="iscsi"
config="/etc/iscsi/initiatorname.iscsi"
lockfile=/var/lock/subsys/$prog
iscsid_lockfile=/var/lock/subsys/${prog}_iscsid

start() {
    [ -x $exec ] || exit 5
    [ -f $config ] || exit 6

    # if the network isn't up yet exit cleanly, NetworkManager will call us
    # again when the network is up
    [ ! -f /var/lock/subsys/network ] && ! nm-online -x >/dev/null 2>&1 && exit 0

    # if no nodes are setup to startup automatically exit cleanly
    grep -qrs "node.startup = automatic" /var/lib/iscsi/nodes
    [ $? -eq 0 ] || exit 0

    # this script is normally called from startup so log into
    # nodes marked node.startup=automatic
    echo -n $"Starting $prog: "
    $exec -m node --loginall=automatic 2>&1 > /dev/null | grep iscsiadm

    # <sigh> iscsiadm does not always give a non 0 exit status in case of
    # error so we grep for any messages to stderr and see those as errors too
    if [ ${PIPESTATUS[0]} -ne 0 -o ${PIPESTATUS[1]} -eq 0 ]; then
        failure $"Starting $prog"
        echo
        return 1
    fi

    success $"Starting $prog"
    touch $lockfile
    echo
    return 0
}

stop() {
    echo -n $"Stopping $prog: "
    $exec -m node --logoutall=automatic 2>&1 > /dev/null | grep iscsiadm

    # <sigh> iscsiadm does not always give a non 0 exit status in case of
    # error so we grep for any messages to stderr and see those as errors too
    if [ ${PIPESTATUS[0]} -ne 0 -o ${PIPESTATUS[1]} -eq 0 ]; then
        failure $"Stopping $prog"
        echo
        return 1
    fi

    success $"Stopping $prog"
    rm -f $lockfile
    echo
    return 0
}

restart() {
    stop
    start
}

reload() {
    return 3
}

force_reload() {
    restart
}

rh_status() {
    [ -f $lockfile ] || return 3

    declare -a iparams=( $(iscsiadm -m session 2>/dev/null | egrep "tcp|iser|bnx2i|be2iscsi|cxgb3i") )
    if [[ -z "${iparams[*]}" ]]; then
        # no sessions
        return 2
    fi

    return 0
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}


case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0
{start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac
exit $?