#!/bin/sh
# Copyright (c) 2005 Novell Inc.
# All rights reserved.
#
# Author: Charles Coffing
# Please send feedback to http://www.suse.de/feedback/
#
# /etc/init.d/unh_iscsi_initiator
#
### BEGIN INIT INFO
# Provides:           unh-iscsi-initiator
# Required-Start:     $network
# Should-Start:
# Required-Stop:      $network
# Should-Stop:
# Default-Start:      3 5
# Default-Stop:       0 1 2 4 6
# Short-Description:  UNH iSCSI initiator
# Description:        The UNH iSCSI initiator connects to iSCSI targets
### END INIT INFO

. /etc/rc.status
rc_reset

UNH_ISCSI=unh_iscsi_initiator
UNH_ISCSI_BIN=/sbin
UNH_ISCSI_LOG=/var/log/unh_iscsi_initiator.log
UNH_ISCSI_DEV=${UNH_ISCSI}
KERNEL_VER="`/bin/uname -r`"

unh_log() {
    echo "$*" >> ${UNH_ISCSI_LOG}
}

create_device() {
    MAJOR="`cat /proc/devices | grep -w $UNH_ISCSI | awk '{print $1;}'`"
    [ -z "$MAJOR" ] && return

    rm -f /dev/${UNH_ISCSI_DEV}
    mknod /dev/${UNH_ISCSI_DEV} c ${MAJOR} 0
}

remove_device() {
    rm -f /dev/${UNH_ISCSI_DEV} > /dev/null 2>&1
}

check_loaded() {
    LOADED="`lsmod | grep -w ${1}`"
    [ ! -z "$LOADED" ] && return 0
    return 3  # LSB-defined value
}

mod_in_use() {
    usage=`lsmod | eval grep \"^${1}\" | awk '{print $3;}'`
    [ "0${usage}" -gt 0 ] && return 0
    return 1
}

load_module() {
    modprobe scsi_mod >> ${UNH_ISCSI_LOG} 2>&1
    modprobe sd_mod >> ${UNH_ISCSI_LOG} 2>&1
    
    MOD_DIR=/lib/modules/${KERNEL_VER}/extra
    MOD_OBJ=${MOD_DIR}/${1}.ko
    unh_log "insmod ${MOD_OBJ}"
    insmod ${MOD_OBJ} >> ${UNH_ISCSI_LOG} 2>&1
    [ $? -eq 0 ] && return 0
    return 1
}

unload_module() {
    MODULE=$1
    check_loaded $MODULE
    if [ $? -eq 0 ]; then
        rmmod ${MODULE} >> ${UNH_ISCSI_LOG} 2>&1
        return $?
    fi

    return 0
}

unh_iscsi_start() {
    DATE="`date`"
    mv -f ${UNH_ISCSI_LOG} ${UNH_ISCSI_LOG}.old > /dev/null 2>&1
    unh_log "UNH iSCSI initiator loading at ${DATE} on Linux kernel ${KERNEL_VER}"

    check_loaded ${UNH_ISCSI}
    [ $? -eq 0 ] && return 0

    load_module ${UNH_ISCSI}
    [ $? -eq 0 ] || return $?

    create_device ${UNH_ISCSI}
    unh_log "UNH iSCSI initiator started successfully."
    return 0
}

unh_iscsi_mount() {
    unh_log "Connecting to UNH iSCSI targets"
    ${UNH_ISCSI_BIN}/iscsi_connect >> ${UNH_ISCSI_LOG} 2>&1
    ret=$?
    if [ $ret -eq 0 ]; then
        unh_log "UNH iSCSI targets connected."
    elif [ $ret -eq 2 ]; then
        unh_log "ERROR: Some iSCSI targets failed to connect."
        # treat as success
    else
        unh_log "ERROR: All iSCSI targets failed to connect."
        return 1
    fi

    unh_log "Mounting file systems on UNH iSCSI targets"
    ${UNH_ISCSI_BIN}/iscsi_mount >> ${UNH_ISCSI_LOG} 2>&1
    ret=$?
    if [ $ret -eq 0 ]; then
        unh_log "File systems on UNH iSCSI disks mounted."
    elif [ $ret -eq 2 ]; then
        unh_log "ERROR: Some iSCSI disks failed to mount."
        # treat as success
        ret=0
    else
        unh_log "ERROR: All iSCSI disks failed to mount."
        ret=1
    fi

    return $ret
}

unh_iscsi_umount() {
    unh_log "Unmounting file systems on UNH iSCSI targets"
    ${UNH_ISCSI_BIN}/iscsi_umount >> ${UNH_ISCSI_LOG} 2>&1
    ret=$?
    if [ $ret -eq 0 ]; then
        unh_log "File systems on UNH iSCSI disks unmounted."
    else
        unh_log "ERROR: Some or all iSCSI disks failed to unmount."
        return 1
    fi

    unh_log "Disconnecting from UNH iSCSI targets"
    ${UNH_ISCSI_BIN}/iscsi_disconnect >> ${UNH_ISCSI_LOG} 2>&1
    ret=$?
    if [ $ret -eq 0 ]; then
        unh_log "UNH iSCSI targets disconnected."
    elif [ $ret -eq 2 ]; then
        unh_log "ERROR: Some iSCSI targets failed to disconnect."
        ret=1
    else
        unh_log "ERROR: All iSCSI targets failed to disconnect."
        ret=1
    fi

    return $ret
}

unh_iscsi_stop() {
    mod_in_use ${UNH_ISCSI}
    if [ $? -eq 0 ]; then
        unh_log "ERROR: Module can not be unloaded while in use."
        return 1
    fi

    unload_module $UNH_ISCSI
    if [ $ret -eq 0 ]; then
        remove_device
        unh_log "Module unloaded successfully."
        return 0
    fi

    unh_log "ERROR: Module could not be unloaded."
    return 1
}

case "$1" in
    start)
        echo -n "Starting UNH iSCSI initiator "
        unh_iscsi_start && unh_iscsi_mount
        rc_status -v
        ;;
    stop)
        echo -n "Shutting down UNH iSCSI initiator "
        unh_iscsi_umount && unh_iscsi_stop
        rc_status -v
        ;;
    restart|force-reload)
        $0 stop
        $0 start
        rc_status
        ;;
    try-restart)
        check_loaded ${UNH_ISCSI}
        if [ $? -eq 0 ]; then
            $0 restart
        else
            rc_reset
        fi
        rc_status
        ;;
    reload)
        # Unsupported
        rc_failed 3
        rc_status -v
        ;;
    status)
        echo -n "Checking for UNH iSCSI initiator "
        check_loaded ${UNH_ISCSI}
        rc_status -v
        ;;
    *)
        echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload}"
        exit 1
        ;;
esac
rc_exit

