#!/usr/bin/sh
#
# Copyright (c) 2002, 2024, Oracle and/or its affiliates.
#
# 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"
 
init_status()
{
    return 0
}

success_status()
{
    echo "OK"
    return 0
}

failure_status()
{
    echo "Failed"
    return 0
}

exit_status()
{
    exit $?
}
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
}

SYSTEMCTL_ENABLED=`systemctl is-enabled oracleasm.service`

start()
{
    if [ "$ORACLEASM_ENABLED" != "true" -o "$SYSTEMCTL_ENABLED" != "enabled" ]
    then
        echo "Make sure to enable oracleasm via systemctl & 'oracleasm configure -e'"
        exit 1 
    fi

    echo -n "Initializing the Oracle ASMLib driver: "
    "${ORACLEASM}" init -l "${ORACLE_ASMMANAGER}" -v 2>&1 | asm_log 2 
    if_fail "${PIPESTATUS[0]}" "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 2>&1 | asm_log 2
        if_fail "${PIPESTATUS[0]}" "Error scanning for disks, see /var/log/oracleasm"
    fi
}


stop()
{
    if [ "$ORACLEASM_ENABLED" != "true" -o "$SYSTEMCTL_ENABLED" != "enabled" ]
    then
        echo "Make sure to enable oracleasm via systemctl & 'oracleasm configure -e'"
        exit 1 
    fi
    echo -n "Dropping Oracle ASMLib disks: "
    "${ORACLEASM}" dropdisks -l "${ORACLE_ASMMANAGER}" -v 2>&1 | asm_log 2
    if_fail "${PIPESTATUS[0]}" "Error dropping disks, see /var/log/oracleasm"

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

case "$1" in
    start)
        echo "/etc/init.d/oracleasm is deprecated in favor of userspace tool: /usr/sbin/oracleasm"
        ;;

    start_sysctl)
        start
        ;;

    status)
        echo "/etc/init.d/oracleasm is deprecated. Use 'oracleasm status'"
        ;;

    configure)
        echo "/etc/init.d/oracleasm is deprecated. Use 'oracleasm configure -i'"
        ;;

    enable)
        echo "/etc/init.d/oracleasm is deprecated. Use 'oracleasm configure -e'"
        ;;

    disable)
        echo "/etc/init.d/oracleasm is deprecated. Use 'oracleasm configure -d'"
        ;;

    createdisk)
        echo "/etc/init.d/oracleasm is deprecated. Use 'oracleasm createdisk'"
        ;;

    deletedisk)
        echo "/etc/init.d/oracleasm is deprecated. Use 'oracleasm deletedisk'"
        ;;

    renamedisk)
        echo "/etc/init.d/oracleasm is deprecated. Use 'oracleasm renamedisk'"
        ;;

    force-renamedisk)
        echo "/etc/init.d/oracleasm is deprecated. Use 'oracleasm renamedisk -f'"
        ;;

    listdisks)
        echo "/etc/init.d/oracleasm is deprecated. use 'oracleasm listdisks'"
        ;;

    listiids)
        echo "/etc/init.d/oracleasm is deprecated. use 'oracleasm listiids'"
        ;;

    deleteiids)
        echo "/etc/init.d/oracleasm is deprecated. use 'oracleasm deleteiids'"
        ;;

    querydisk)
        echo "/etc/init.d/oracleasm is deprecated. Use 'oracleasm querydisk'"
        ;;

    scandisks)
        echo "/etc/init.d/oracleasm is deprecated. Use 'oracleasm scandisks'"
        ;;

    update-driver)
        echo "/etc/init.d/oracleasm is deprecated in favor of userspace tool: /usr/sbin/oracleasm"
        ;;

    stop)
        echo "/etc/init.d/oracleasm is deprecated in favor of userspace tool: /usr/sbin/oracleasm"
        ;;

    stop_sysctl)
        stop
        ;;

    restart)
        echo "/etc/init.d/oracleasm is deprecated in favor of userspace tool: /usr/sbin/oracleasm"
        ;;

    restart_sysctl)
        stop
        start
        ;;
    *)
        echo "/etc/init.d/oracleasm is deprecated in favor of userspace tool: /usr/sbin/oracleasm"
        exit 1
esac

exit 0

