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


# Force LC_ALL=C
export LC_ALL=C
 
USAGE="[-l <manager>] [-v]"

exec 3>/dev/null

help=
verbose=
version=
usage=
while case "$#" in 0) break ;; esac
do
    case "$1" in
    -l|--manager)
        case "$#" in 1) usage=t; break ;; esac
        shift
        ORACLE_ASMMANAGER="$1"
        ;;
    -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 ASMlib driver filesystem: "
    umount "$DEV" 2>&3
    if [ $? != 0 ]
    then
        echo "failed"
        echo "Unable to unmount ASMlib driver filesystem" 
        exit 1
    fi

    echo "$DEV"

    return 0
}



if [ "$ORACLEASM_DRIVER_SUPPORTED" = "true" ]
then
    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 -n "Unloading module \"${ORACLEASM_MODNAME}\": "
    echo "Not applicable with kernel $(uname -r |sed -e 's/[-_].*//')"
    echo -n "Unmounting ASMlib driver filesystem: "
    echo "Not applicable with kernel $(uname -r |sed -e 's/[-_].*//')"
    
    # If ASM driver is not supported then we may have a
    # BPF iofitler map setup so cleanup.
    if [ "$ORACLEASM_ENABLE_IOFILTER" = "true" ]
    then
    	echo -n "Removing iofilter map $ORACLEASM_IOFILTER_MAP_PATH: "
    	rm -rf "${ORACLEASM_IOFILTER_MAP_PATH}"
    	echo "done"
    fi
fi

exit 0

