#!/usr/bin/bash
#
# Copyright (c) 2006, 2025, Oracle and/or its affiliates.
#
# Unload the oracleasm driver
#


# Force LC_ALL=C
export LC_ALL=C

USAGE="[-v]"

exec 3>/dev/null

help=
verbose=
version=
usage=
while case "$#" in 0) break ;; esac
do
    case "$1" in
    -v|--verbose)
        verbose=t
        exec 3>&2
        ;;
    -V|--version)
        version=t
        ;;
    -h|--help)
        help=t
        ;;
    *)
        usage=t
        ;;
    esac
    shift
done

# Load configuration
. oracleasm-Xshlib

if [ "$help" = "t" -o "$usage" = "t" ]
then
    usage
fi

if [ $(echo $UID) != 0 ]
then
    echo "This operation requires root privileges. Please run as root"
    exit 1
fi

if [ "$version" = "t" ]
then
    version
fi


#
# unload_module()
#
unload_module()
{
    if [ "$#" -lt "1" -o -z "$1" ]
    then
        die "unload_module(): Requires an argument"
    fi

    MODNAME="$1"

    MODOUT="`awk '$1 ~ /^'$MODNAME'$/{print $1,$3;exit}' < /proc/modules 2>/dev/null`"
    if [ -z "$MODOUT" ]
    then
        return
    fi
    case "$MODOUT" in
    $MODNAME\ 0)
        ;;
    $MODNAME\ *)
        return
        ;;
    *)
        echo "Error in module parsing!"
        exit 1
        ;;
    esac

    echo -n "Unloading module \"$MODNAME\": "
    modprobe -rs "$MODNAME" 2>&3
    if [ "$?" = 0 ]
    then
        echo "$MODNAME"
    else
        echo "failed"
        echo "Unable to unload module \"$MODNAME\""
        exit 1
    fi

    return
}

#
# unmount_device()
# Unmount the $ORACLE_ASMMANAGER filesystem
#
unmount_device()
{
    if [ "$#" -lt "1" -o -z "$1" ]
    then
        die "mount_device(): Requires an argument"
    fi

    DEV="$1"

    ORACLE_ASMMANAGERSEARCH="`echo "$DEV" | sed -e 's/\//\\\\\//g'`"
    MOUNTOUT="`awk '$2 ~ /^'$ORACLE_ASMMANAGERSEARCH'$/{print $2; exit}' < /proc/mounts 2>/dev/null`"

    if [ -z "$MOUNTOUT" ]
    then
        return 2
    fi

    echo -n "Unmounting oracleasm driver filesystem: "
    umount "$DEV" 2>&3
    if [ $? != 0 ]
    then
        echo "failed"
        echo "Unable to unmount oracleasm driver filesystem"
        exit 1
    fi

    echo "$DEV"

    return 0
}

echo "Stopping oracleasm service" | asm_log 2

if [ "$ORACLEASM_DRIVER_SUPPORTED" = "true" ]
then
    ${ORACLEASM_CMD} dropdisks | asm_log 1
    unmount_device "${ORACLE_ASMMANAGER}" | asm_log 1

    unload_module "${ORACLEASM_MODNAME}" | asm_log 1
else
    # For compatibility with v2, print the messages about
    # ASM filesystem
    echo "Unloading module \"${ORACLEASM_MODNAME}\": Not applicable" \
	 "with ${KERNEL_NAME}" | asm_log 1
    echo "Unmounting oracleasm driver filesystem: Not applicable" \
	 "with ${KERNEL_NAME}" | asm_log 1

    # If oracleasm driver is not supported then we may have a
    # BPF iofitler map setup so cleanup.
    if [ "$ORACLEASM_ENABLE_IOFILTER" = "true" ]
    then
	echo "Removing iofilter map" | asm_log 1
    	rm -rf "${ORACLEASM_IOFILTER_MAP_PATH}"
    fi
fi

echo "Successfully stopped oracleasm service" | asm_log 2

exit 0
