#!/usr/bin/bash
#
# Copyright (c) 2006, 2025, Oracle and/or its affiliates.
#
# Return a disk to the operating system
#


# Force LC_ALL=C
export LC_ALL=C

USAGE="[-v] <label> <device>"

exec 3>/dev/null

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

ORACLEASM="/usr/sbin/oracleasm"

# 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



verify_usable()
{
    if [ "$#" != "2" -o -z "$1" -o -z "$2" ]
    then
        die "verify_usable(): Requires an argument"
    fi

    DISKNAME="$1"
    DEV="$2"

    OUTPUT="$(oracleasm-candidate-p -v "${DEV}" 2>&1)"
    if [ $? != 0 ]
    then
        echo "$OUTPUT" | sed -e 's/^[^:]*: *//' >&2
        [ "$force" != "t" ] && die

        oracleasm-candidate-p -v -f "${DEV}" 2>&3
        [ $? != 0 ] && die
        echo "Forced - continuing anyway" >&2
    fi

    if [ "$force" != "t" ]
    then
	check_disk_group_member "${DEV}" "createdisk"
    fi

    INFO="$(oracleasm-read-label "${DEV}" 2>&3)"
    if [ $? != 0 ]
    then
        echo "Unable to access device \"${DEV}\"" | asm_log 1
        exit 1
    fi

    if [ -n "$INFO" ]
    then
        LABEL="$(echo "$INFO" | cut -f1 -d:)"
        echo "Device \"$DEV\" is already labeled for ASM disk \"${LABEL}\"" | asm_log 1
        exit 1
    fi

    if oracleasm-querydisk "${DISKNAME}" 1>&3 2>&3
    then
        echo "Disk \"${DISKNAME}\" already exists" | asm_log 1
        exit 1
    fi

    return 0
}

create_disk()
{
    if [ "$#" != "2" -o -z "$1" -o -z "$2" ]
    then
        die "create_disk(): Requires an argument"
    fi

    DISKNAME="$1"
    DEV="$2"

    echo -n "Writing disk header: "
    oracleasm-write-label -c "${DEV}" "${LABEL}" 2>&1 | asm_log 2
    if [ ${PIPESTATUS[0]} != 0 ]
    then
        echo "failed"
        echo "Unable to label device \"${DEV}\"" | asm_log 1
        exit 1
    fi
    echo "done"

    echo -n "Instantiating disk: "

    if [ "$ORACLEASM_DRIVER_SUPPORTED" = "true" ]
    then
        oracleasm-instantiate-disk "${DEV}" "${LABEL}" 2>&3
        if [ $? != 0 ]
        then
            echo "failed"
            echo -n "Clearing disk header: "
            oracleasm-write-label -d "${DEV}" 2>&1 | asm_log 2
            if [ "${PIPESTATUS[0]}" != 0 ]
            then
                echo "failed"
                echo "Unable to clear disk \"${DEV}\"" | asm_log 1
                exit 1
            fi
            echo "done"
            die
        fi
    fi

    perm_disk "${DISKNAME}"
    if [ $? = 1 ]
    then
        echo "failed"
        echo "Unable to change ownership of disk \"${DISKNAME}\"" | asm_log 1
        exit 1
    fi

    iofilter_map_add "${LABEL}"
    if [ $? != 0 ]
    then
        echo "Failed to add disk \"${LABEL}\" to iofilter map" | \
	    asm_log 1
    fi

    # refresh blkid cache so it has updated info
    blkid >/dev/null

    echo "done"

    return 0
}


if [ $# != 2 -o -z "$1" -o -z "$2" ]
then
    usage
fi

LABEL="$(upper_disk "$1")"
shift
if [ -z "$LABEL" ]
then
    exit 1
fi

DEV="$1"
shift

if [ "$ORACLEASM_DRIVER_SUPPORTED" = "true" ]
then
    "${ORACLEASM}" status 1>&3 2>&3
    if [ $? = 1 ]
    then
        die "oracleasm module not loaded or /dev/oracleasm not mounted."
    fi
fi

verify_usable "$LABEL" "$DEV"
create_disk "$LABEL" "$DEV"

exit 0
