#!/bin/bash
# 
# Copyright (c) 2006, 2024, Oracle and/or its affiliates.
#
# Display current status of 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

STATUS1=0
STATUS2=0

echo -n $"Checking if the oracleasm kernel module is loaded: "

# if no ASM driver is supported then nothing to check
if [ "$ORACLEASM_DRIVER_SUPPORTED" = "false" ]
then
    echo -e $"no (Not required with kernel $(uname -r |sed -e 's/[-_].*//'))"
else
    grep '^oracleasm ' /proc/modules >/dev/null 2>&1
    STATUS1="$?"
    if [ "$STATUS1" -eq  0 ]
    then
	echo "yes"
    else
	echo "no"
    fi
fi

echo -n $"Checking if $ORACLE_ASMMANAGER is mounted: "

if [ "$ORACLEASM_DRIVER_SUPPORTED" = "false" ]
then
    echo -e $"no (Not required with kernel $(uname -r |sed -e 's/[-_].*//'))"
else
    ORACLE_ASMMANAGERSEARCH="$(echo $ORACLE_ASMMANAGER | sed -e 's/\//\\\//g')"
    grep "^oracleasmfs $ORACLE_ASMMANAGERSEARCH oracleasmfs" /proc/mounts >/dev/null 2>&1
    STATUS2="$?"

    if [ "$STATUS2" -eq  0 ]
    then
	echo "yes"
    else
        echo "no"
    fi
fi

# Report which I/O interface is in use: KABI_V2 or KABI_V3
echo -n "Checking which I/O Interface is in use: "
if [ "$ORACLEASM_DRIVER_SUPPORTED" = "true" ]
then
    echo "oracleasm driver (KABI_V2)"
else
    echo "io_uring (KABI_V3)"
fi


# io_uring is used for UEK7+ kernels so check if the feature is
# enabled/disabled
if [ "$ORACLEASM_DRIVER_SUPPORTED" = "false" ]
then
    echo -n "Checking if io_uring is enabled: "
    check_io_uring_disabled
    if [ $? -eq 0 ]
    then
	echo "yes"
    else
	echo "no"
    fi
fi

# For V3, check if ASM disks have the correct ownership and
# permissions.
if [ "$ORACLEASM_DRIVER_SUPPORTED" = "false" ]
then
    echo -n $"Checking if ASM disks have the correct ownership" \
	"and permissions: "
    oracleasm-listdisks -l "${ORACLE_ASMMANAGER}" 2>&3 | while read LINE
    do
        check_perm "$(asm_disk_path "${ORACLE_ASMMANAGER}" "${LINE}")" 2>&3
        if [ $? = 1 ]
        then
            #XXX echo -e "NO. \nFix permissions on ASM disk \"$LINE\"" | asm_log 1
	    exit 1
        fi
    done
    if [ $? -eq 0 ]
    then
	echo "yes"
    else
	STATUS2=1
	echo "no"
    fi
    if [ "$ORACLEASM_ENABLE_IOFILTER" = "true" ]
    then
	# Check if iofilter path is setup
	echo -n $"Checking if ASM I/O filter is set up: "
	if [ -e "${ORACLEASM_IOFILTER_MAP_PATH}" ]
	then
		# map already exists
		echo "yes"
	else
		STATUS2=1
		echo "no"
	fi
    fi
fi

if [ "$STATUS1" -ne 0 ] || [ "$STATUS2" -ne 0 ]
then
    exit 1
else
    exit 0
fi
