#! /bin/sh
# init fragment for oracleasm
#
# chkconfig: 2345 29 20
# description: Load OracleASM driver at system boot
#
### BEGIN INIT INFO
# Provides: oracleasm
# Required-Start: $local_fs
# Should-Start: ypbind hwscan iscsi
# Required-Stop: $local_fs
# Should-Stop: ypbind hwscan iscsi
# Default-Start: 2 3 5
# Default-Stop:
# Description: Load OCFS driver at system boot
### END INIT INFO


# Force LC_ALL=C
export LC_ALL=C

# All other commands accept -v, but this script will just honor the ENV
exec 3>/dev/null
[ -n "$verbose" ] && exec 3>&2

# So we don't worry whether $sbindir is in path
ORACLEASM="/usr/sbin/oracleasm"

# For the oracleasm-Xshlib library
ORACLEASM_EXEC_PATH="/usr/lib/oracleasm"
 
if [ -f /etc/redhat-release ]
then
. /etc/init.d/functions

init_status()
{
    return 0
}

success_status()
{
    success
    echo
}

failure_status()
{
    failure $1
    echo
}

exit_status()
{
    exit $?
}
elif [ -f /etc/SuSE-release -o -f /etc/UnitedLinux-release ]
then
. /etc/rc.status

init_status()
{
    rc_reset
}

success_status()
{
    /bin/true
    rc_status -v
}

failure_status()
{
    /bin/false
    rc_status -v
}

exit_status()
{
    rc_exit
}
else
init_status()
{
    return 0
}

success_status()
{
    echo "OK"
    return 0
}

failure_status()
{
    echo "Failed"
    return 0
}

exit_status()
{
    exit $?
}
fi

init_status

# Load configuration
. "${ORACLEASM_EXEC_PATH}"/oracleasm-Xshlib


#
# if_fail()
#
# Evaluates return codes.  If 0, prints "OK", if 1, prints "Failed"
# and exits.  If 2, status is "already done" and nothing is printed.
# The rest of the functions in here all honor this convention.
#
if_fail()
{
    RC="$1"
    REASON="$2"
    if [ "$RC" = "0" ]
    then
        success_status
        return
    elif [ "$RC" = "2" ]
    then
        return
    fi
    failure_status "${REASON}"
    exit 1
}


relabel_disk()
{
    OLD="$1"
    NEW="$2"

    # We know this will error
    "${ORACLEASM}" renamedisk -l "${ORACLE_ASMMANAGER}" "${OLD}" "${NEW}" |
        grep -v -- '-f'
    echo "rerun with the force-renamedisk command." >&2
    
    echo -n "Renaming disk \"${OLD}\" to \"${NEW}\": "
    if_fail 1 "Unable to rename disk \"${OLD}\"" >&2
}

force_relabel_disk()
{
    OLD="$1"
    NEW="$2"

    echo -n "Renaming disk \"${OLD}\" to \"${NEW}\": "
    "${ORACLEASM}" renamedisk -f -v -l "${ORACLE_ASMMANAGER}" "${OLD}" \
        "$2" 1>>/var/log/oracleasm 2>&1
    if_fail "$?" "Unable to rename disk \"${OLD}\" see /var/log/oracleasm"
}



start()
{
    if [ "$ORACLEASM_ENABLED" != "true" ]
    then
        exit 0
    fi

    echo -n "Initializing the Oracle ASMLib driver: "
    "${ORACLEASM}" init -l "${ORACLE_ASMMANAGER}" -v 1>>/var/log/oracleasm 2>&1
    if_fail "$?" "Unable to initialize the ASMlib driver, see /var/log/oracleasm"

    if [ "$ORACLEASM_SCANBOOT" = "true" ]
    then
        echo -n "Scanning the system for Oracle ASMLib disks: "
        "${ORACLEASM}" scandisks -l "${ORACLE_ASMMANAGER}" -v \
            1>>/var/log/oracleasm 2>&1
        if_fail "$?" "Error scanning for disks, see /var/log/oracleasm"
    fi
}


stop()
{
    echo -n "Dropping Oracle ASMLib disks: "
    "${ORACLEASM}" dropdisks -l "${ORACLE_ASMMANAGER}" -v 1>>/var/log/oracleasm 2>&1
    if_fail "$?" "Error dropping disks, see /var/log/oracleasm"

    echo -n "Shutting down the Oracle ASMLib driver: "
    "${ORACLEASM}" exit -l "${ORACLE_ASMMANAGER}" -v 1>>/var/log/oracleasm 2>&1
    if_fail "$?" "Unable to stop the ASMlib driver, see /var/log/oracleasm"
}



case "$1" in
    start)
        start
	;;
	
    status)
        "${ORACLEASM}" status -l "${ORACLE_ASMMANAGER}"
        exit 0
        ;;

    configure)
        "${ORACLEASM}" configure -l "${ORACLE_ASMMANAGER}" -i
        [ $? != 0 ] && exit 1  # oracleasm configure will print an error
        . "${ORACLEASM_EXEC_PATH}"/oracleasm-Xshlib # Reload
        if [ "$ORACLEASM_ENABLED" = "true" ]
        then
            start
        else
            stop
        fi
        ;;

    enable)
        "${ORACLEASM}" configure -l "${ORACLE_ASMMANAGER}" -e
        [ $? != 0 ] && exit 1  # oracleasm configure will print an error
        . "${ORACLEASM_EXEC_PATH}"/oracleasm-Xshlib # Reload
        start
        ;;

    disable)
        "${ORACLEASM}" configure -l "${ORACLE_ASMMANAGER}" -d
        [ $? != 0 ] && exit 1  # oracleasm configure will print an error
        . "${ORACLEASM_EXEC_PATH}"/oracleasm-Xshlib # Reload
        stop
        ;;

    createdisk)
        if [ -z "$2" -o -z "$3" ]
        then
            echo "Action \"createdisk\" requires two arguments, the device and the label." >&2
            echo "See oracleasm.init(8) for more information." >&2
            exit 1
        fi
	echo -n "Marking disk \"$2\" as an ASM disk: "

        "${ORACLEASM}" createdisk -v -l "${ORACLE_ASMMANAGER}" "$2" "$3" \
            1>>/var/log/oracleasm 2>&1
        if_fail "$?" "Unable to create disk \"$2\", see /var/log/oracleasm"
        ;;

    deletedisk)
        if [ -z "$2" ]
        then
            echo "Action \"deletedisk\" requires an argument specifying the disk to delete." >&2
            echo "See oracleasm.init(8) for more information." >&2
            exit 1
        fi
        echo -n "Removing ASM disk \"$2\": "
	"${ORACLEASM}" deletedisk -v -l "${ORACLE_ASMMANAGER}" "$2" \
            1>>/var/log/oracleasm 2>&1
        if_fail $? "Unable to delete disk \"$2\", see /var/log/oracleasm"
        ;;

    renamedisk)
        if [ -z "$2" -o -z "$3" ]
        then
            echo "Action \"renamedisk\" requires two arguments, the disk to rename (by label or device) and the new label." >&2
            echo "See oracleasm.init(8) for more information." >&2
            exit 1
        fi
        relabel_disk "$2" "$3"
        ;;
    
    force-renamedisk)
        if [ -z "$2" -o -z "$3" ]
        then
            echo "Action \"renamedisk\" requires two arguments, the disk to rename (by label or device) and the new label." >&2
            echo "See oracleasm.init(8) for more information." >&2
            exit 1
        fi
        force_relabel_disk "$2" "$3"
        ;;

    listdisks)
        "${ORACLEASM}" listdisks -l "${ORACLE_ASMMANAGER}"
        ;;

    querydisk)
        shift
        "${ORACLEASM}" querydisk -l "${ORACLE_ASMMANAGER}" "$@"
        ;;

    scandisks)
        echo -n "Scanning the system for Oracle ASMLib disks: "
        "${ORACLEASM}" scandisks -l "${ORACLE_ASMMANAGER}" -v \
            1>>/var/log/oracleasm 2>&1
        if_fail "$?" "Error scanning for disks, see /var/log/oracleasm"
        ;;

    update-driver)
        shift  # Remove the command so we can pass arguments along.
        echo -n "Updating to the latest Oracle ASMLib driver: "
        "${ORACLEASM}" update-driver -v "$@" 1>>/var/log/oracleasm 2>&1
        if_fail "$?" "Update failed, see /var/log/oracleasm"
        ;;

    stop)
        stop
        ;;

    restart)
        stop
        start
	;;

    *)
	echo "Usage: $0 {start|stop|restart|enable|disable|configure|createdisk|deletedisk|querydisk|listdisks|scandisks|status}"
        exit 1
esac

exit 0

