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
a4c8b3
# Note we should have $network in Required-Start/Stop but we don't because if
a4c8b3
# we would require network chkconfig will put us directly after NetworkManager
a4c8b3
# when using NM, which will make our see if the network is up test succeed
a4c8b3
# while NM is actually still configuring the network. By not requiring network
a4c8b3
# chkconfig will use the chkconfig header to determine our start prio, starting
a4c8b3
# us after the old network service, but before NM (netfs does this the same).
a4c8b3
5a3872
### BEGIN INIT INFO
5a3872
# Provides:          iscsi
a4c8b3
# Required-Start:    iscsid
a4c8b3
# Required-Stop:     iscsid
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
a4c8b3
    [ ! -f /var/lock/subsys/network -a ! -f /var/lock/subsys/NetworkManager ] && exit 0
a4c8b3
a4c8b3
    # if no nodes are setup to startup automatically exit cleanly
a4c8b3
    grep -qrs "node.startup = automatic" /var/lib/iscsi/nodes
a4c8b3
    [ $? -eq 0 ] || exit 0
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
a4c8b3
a4c8b3
    # <sigh> iscsiadm does not always give a non 0 exit status in case of
a4c8b3
    # error so we grep for any messages to stderr and see those as errors too
a4c8b3
    if [ ${PIPESTATUS[0]} -ne 0 -o ${PIPESTATUS[1]} -eq 0 ]; then
a4c8b3
        failure $"Starting $prog"
a4c8b3
        echo
a4c8b3
        return 1
a4c8b3
    fi
a4c8b3
a4c8b3
    success $"Starting $prog"
a4c8b3
    touch $lockfile
a4c8b3
    echo
a4c8b3
    return 0
mchristi 63a314
}
mchristi 63a314
a4c8b3
stop() {
a4c8b3
    echo -n $"Stopping $prog: "
a4c8b3
    $exec -m node --logoutall=automatic 2>&1 > /dev/null | grep iscsiadm
a4c8b3
a4c8b3
    # <sigh> iscsiadm does not always give a non 0 exit status in case of
a4c8b3
    # error so we grep for any messages to stderr and see those as errors too
a4c8b3
    if [ ${PIPESTATUS[0]} -ne 0 -o ${PIPESTATUS[1]} -eq 0 ]; then
a4c8b3
        failure $"Stopping $prog"
a4c8b3
        echo
a4c8b3
        return 1
a4c8b3
    fi
a4c8b3
a4c8b3
    success $"Stopping $prog"
a4c8b3
    rm -f $lockfile
a4c8b3
    echo
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() {
a4c8b3
    [ -f $lockfile ] || return 3
a4c8b3
a4c8b3
    declare -a iparams=( $(iscsiadm -m session 2>/dev/null | egrep "tcp|iser") )
a4c8b3
    if [[ -z "${iparams[*]}" ]]; then
a4c8b3
        # no sessions
a4c8b3
        return 2
a4c8b3
    fi
a4c8b3
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
        rh_status_q || exit 0
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 $?