#!/usr/bin/bash
#
# Copyright (c) 2006, 2025, Oracle and/or its affiliates.
#
# Load and initialize 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
check_oracleasm_config

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


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

    DEV="$1"
    if [ -e "$DEV" ]
    then
        if [ -d "$DEV" ]
        then
            return
        fi
        echo "dev_create(): File $DEV is not a directory"
        exit 1
    fi

    echo -n "Creating $DEV mount point: "
    mkdir "$DEV" 2>&3
    if [ $? = 0 ]
    then
        echo "$DEV"
    else
        echo "failed"
        echo "Unable to create mount point $DEV"
        exit 1
    fi
}


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

    MODNAME="$1"

    MODOUT="`awk '$1 ~ /^'$MODNAME'$/{print $1;exit}' < /proc/modules 2>/dev/null`"
    if [ -n "$MODOUT" ]
    then
        return
    fi

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

    ORACLEASM_BLK_PARM="/sys/module/oracleasm/parameters/use_logical_block_size"

    if [ -f $ORACLEASM_BLK_PARM ]
    then
	if [ "$ORACLEASM_USE_LOGICAL_BLOCK_SIZE" = "true" ]
	then
            echo "Configuring \"$MODNAME\" to use device logical block size "
	    echo -n "Y" > $ORACLEASM_BLK_PARM
	else
            echo "Configuring \"$MODNAME\" to use device physical block size "
	    echo -n "N" > $ORACLEASM_BLK_PARM
	fi
    fi

    return
}


#
# mount_device()
# Mount the $ORACLE_ASMMANAGER filesystem
#
mount_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 [ -n "$MOUNTOUT" ]
    then
        return
    fi

    echo -n "Mounting oracleasm driver filesystem: "
    mount $OPTS -t oracleasmfs oracleasmfs $DEV 2>&3
    if [ $? != 0 ]
    then
        echo "failed"
        echo "Unable to mount oracleasm driver filesystem"
        exit 1
    fi

    for i in ${DEV}/.[a-z]*
    do
        if [ "$i" = ${DEV}'/.[a-z]*' ]
        then
            break
        fi
        perm_disk "$i"
        if [ $? = 1 ]
        then
            echo "failed"
            echo "Unable to fix oracleasm driver permissions"
            exit 1
        fi
    done

    perm_disk ${DEV}/iid
    if [ $? = 1 ]
    then
        echo "failed"
        echo "Unable to fix oracleasm driver permissions"
        exit 1
    fi

    chmod 0770 ${DEV}/iid
    if [ $? = 1 ]
    then
        echo "failed"
        echo "Unable to fix oracleasm driver permissions"
        exit 1
    fi

    echo "$DEV"

    return
}

# Used when oracleasm driver is not supported in the kernel
# set permissions on ASM disk files (block devices) in devfs
set_disk_permissions ()
{
   if [ "$ORACLEASM_DRIVER_SUPPORTED" = "true" ]
   then
	return
   fi

   # For each disk that has ASM label, set the permissions to
   # match with the configured userid.
   disklist="$(asm_disk_path)"
   for DISK in ${disklist}
   do
      perm_disk "${DISK}"
   done
}

echo "Starting oracleasm service" | asm_log 2

if [ "$ORACLEASM_DRIVER_SUPPORTED" = "true" ]
then
    dev_create "${ORACLE_ASMMANAGER}" | asm_log 1
    load_module "${ORACLEASM_MODNAME}" | asm_log 1
    mount_device "${ORACLE_ASMMANAGER}" | asm_log 1
else

    # To be compatible with v2 print the same messages about
    # oracleasm driver filesystem.
    echo "Mounting oracleasm driver filesystem: Not applicable" \
	 "with ${KERNEL_NAME}" | asm_log 1

    # Using ASMLIB v3
    # Setup permissions on the ASM disks
    set_disk_permissions | asm_log 1
fi

if [ "$ORACLEASM_DRIVER_SUPPORTED" = "false" -o "$ORACLEASM_SCANBOOT" = "true" ]
then
    "${ORACLEASM_CMD}" scandisks -v
    if [ $? != 0 ]
    then
        echo "Disk scan failed" | asm_log 1
        exit 1
    else
	echo "Disk scan successful" | asm_log 1
    fi
fi

echo "Successfully started oracleasm service" | asm_log 2

exit 0
