#!/bin/sh

HD=`/opt/hmc/bin/getHDName`

# if $HD ends with a number, then add p
if [[ "${HD: -1}" =~ ^[0-9]$ ]]; then
    HDP="${HD}p"
else
    HDP="${HD}"
fi

hypervsr=$(/opt/hmc/bin/gethypervisorinfo)
if [ $? -eq 0 ]; then
    VPDINFO=""
else
    VPDINFO=`/opt/hmc/bin/GetVPD -v`
fi
MARCH=`uname -m`

setPartType() {
    local i
    # source only SUPPORTED_GPT_MTM and declare it locally
    local v=$(grep SUPPORTED_GPT_MTM /opt/hmc/data/supportedmodels.properties)
    eval "local ${v:-SUPPORTED_GPT_MTM=}"

    if [ "$hypervsr" = "POWERVM" ]; then
        # if we are on PowerVM
        PARTTYPE="msdos"
    elif [ "$MARCH" = "ppc64le" ]; then
        # if we are power but not PowerVM
        PARTTYPE="gpt"
    elif [ -z "$VPDINFO" ]; then
        # if we have no vpdinfo (i.e. gethypervisorinfo returned rc=0)
        # we must be a vm
        PARTTYPE="msdos"
    elif [ -n "$SUPPORTED_GPT_MTM" ]; then
        # If SUPPORTED_GPT_MTM is declare, set msdos for anything not
        # in that list
        PARTTYPE="msdos"
        for i in $SUPPORTED_GPT_MTM; do
            if [ "$VPDINFO" = "$i" ]; then
                PARTTYPE="gpt"
                break
            fi
        done
    else
        # We are not a VM, ppc64le, and SUPPORTED_GPT_MTM is empty
        PARTTYPE="msdos"
    fi

    # Override everything we just did via kernel commandline
    for i in $(cat /proc/cmdline); do
        case $i in
            parttype=*) PARTTYPE=${i#*=} ;;
        esac
    done
}


setPartType

# default partition devices
OLD_SWAPPARTITION=/dev/${HDP}1
ROOTPARTITION=/dev/${HDP}2
VARPARTITION=/dev/${HDP}3
SWAPPARTITION=/dev/${HDP}4
HMCBOOTPARTITION=/dev/${HDP}5
HMCDUMPPARTITION=/dev/${HDP}6
LVMPV1=/dev/${HDP}7
LVMPV2=/dev/${HDP}8

if [ "$PARTTYPE" = "msdos" ]; then
    if [ "$MARCH" = "ppc64le" ]; then
        SWAPPARTITION=/dev/HMCDataVG/SwapLV
    else
        SWAPPARTITION=/dev/${HDP}1
    fi
fi

# 5 * 255 * 63 * 512 = 41126400 bytes = ~39MB
EMPTY_CYLS=5    # 5 cylinders left at the end of last partition
# Whether or not HomeLV should be mounted
activateVolumeGroup()
{
    lvdisplay | grep "LV Status" | grep -q "NOT available"
    if [ $? -eq 0 ]; then
        echo "Activating Volume Group..."
        RunCmd vgchange -ay HMCDataVG
    fi
}
getDiskSize()
{
    local hd=$(/opt/hmc/bin/getHDName)
    local diskSize=0

    if [ "$1" == "lsblk" ]; then
        lsblk -r -n -d -b -o size "/dev/$hd"
    else
        if [ -n "$hd" ]; then
            diskSize=$(parted -s /dev/${hd} unit GB print | grep Disk | grep GB | cut -d':' -f2 | cut -d '.' -f1 | tr -d ' GB')
        fi
        # If nothing is returned for diskSize, then return 0
        if [ -z "$diskSize" ]; then
            echo 0
        else
            echo $diskSize
        fi
    fi
}

getPartitionTableType()
{
    ptype=$(parted -s /dev/${HD} print | awk -F: '/^Partition Table/ {print $2}' | tr -d ' ')
    echo $ptype
}

deactivateVolumeGroup()
{
    # loop through active volume groups and deactivate them
    local vg
    for vg in $(vgdisplay -Ac | cut -d: -f1); do
        RunCmd vgchange -an $vg
    done
}

# enables multipath in install environment
enableMultipath()
{
    deactivateVolumeGroup

    # only enable if multipath command is available
    if hash multipath 2>/dev/null; then
        [ ! -d /hmc/etc/multipath ] && mkdir /hmc/etc/multipath

        # Make sure user_friendly_names is off
        cat > /hmc/etc/multipath.conf << EOF
defaults {
    user_friendly_names no
    find_multipaths yes
    failback immediate
}
EOF
        # restart just incase multipathd isn't running or is running and needs
        # to reload the config
        systemctl restart multipathd
    fi
}

CreateLogicalVolumes()
{
    DumpLVSize=8G
    DataLVSize=8G
    HomeLVSize=10G
    LogLVSize=4G
    diskSize=`getDiskSize`
    if [[ $diskSize -ge 80 && $diskSize -lt 120 ]]; then
        # Can't have a separate HomeLV, set the size to 4MB
        # This will not allow a file system to be created
        # To use this LV we need to first extend its size then
        # run mkfs.extX on it first.
        HomeLVSize=4M
    elif [[ $diskSize -ge 120 && $diskSize -lt 160 ]]; then
        DumpLVSize=16G
        DataLVSize=20G
    elif [[ $diskSize -ge 160 && $diskSize -lt 180 ]]; then
        DumpLVSize=30G
        DataLVSize=40G
        LogLVSize=6G
    elif [[ $diskSize -ge 180 && $diskSize -lt 250 ]]; then
        DumpLVSize=40G
        DataLVSize=50G
        LogLVSize=6G
    elif [[ $diskSize -ge 250 && $diskSize -lt 300 ]]; then
        DumpLVSize=50G
        DataLVSize=110G
        LogLVSize=8G
    elif [[ $diskSize -ge 300 && $diskSize -lt 500 ]]; then
        DumpLVSize=60G
        DataLVSize=140G
        LogLVSize=8G
    elif [[ $diskSize -ge 500 && $diskSize -lt 750 ]]; then
        DumpLVSize=120G
        DataLVSize=220G
        LogLVSize=8G
    elif [[ $diskSize -ge 750 && $diskSize -lt 1000 ]]; then
        DumpLVSize=250G
        DataLVSize=300G
        LogLVSize=8G
    elif [[ $diskSize -ge 1000 ]]; then
        DumpLVSize=350G
        DataLVSize=400G
        LogLVSize=20G
    fi
    echo "Creating Logical Volumes..."
    waitForDevices
    if [ "$SWAPPARTITION" = "/dev/HMCDataVG/SwapLV" ]; then
        RunCmd lvcreate -vvvv -y -L2G -n SwapLV HMCDataVG
    fi
    RunCmd lvcreate -vvvv -y -L${DumpLVSize} -n DumpLV HMCDataVG
    RunCmd lvcreate -vvvv -y -L${HomeLVSize} -n HomeLV HMCDataVG
    RunCmd lvcreate -vvvv -y -L20G -n ExtraLV HMCDataVG
    RunCmd lvcreate -vvvv -y -L${LogLVSize} -n LogLV HMCDataVG
    RunCmd lvcreate -vvvv -y -L4G -n UpgradeLV HMCDataVG
    RunCmd lvcreate -vvvv -y -L${DataLVSize} -n DataLV HMCDataVG
}

waitForDevices() {
    local i
    local count=0
    local flag=1
    while [ "$flag" = "1" -a "$count" -lt 20 ]; do
        for i in 1 2 3 4 5 6 7 8; do
            if [ ! -e /dev/${HDP}${i} ]; then
                sleep 3
                break
            fi
            flag=0
        done
        count=$((count + 1))
    done
}

removeVolumeGroup() {
    echo "Removing HMCDataVG..."
    RunCmd -noerr vgreduce -a -f HMCDataVG --removemissing
    RunCmd -noerr vgremove -f HMCDataVG
}

RunCmd() {
    NOERR=0
    NOLOG=0
    if [ "$1" = "-noerr" ] ; then
        shift
        NOERR=1
    fi
    if [ "$1" = "-nolog" ] ; then
        shift
        NOLOG=1
    fi
    LOG=/tmp/HmcInstall.log
    if [ "$NOLOG" = "0" ]; then
        echo "Running command $*" >> $LOG
    fi
    echo "*************************************************************" >> $LOG
    $* >> $LOG 2>&1
    rc=$?
    echo >> $LOG 2>&1
    if [ "$NOERR" = "0" ]; then
        if [ $rc -ne 0 ]; then
            if [ "$1" = "/opt/hmc/bin/setupRoot" ]; then
                exit 3
            elif [ "$1" = "mount" ]; then
                exit 2
            elif [ "$1" = "sfdisk" ]; then
                exit 4
            elif [ "$1" = "/opt/hmc/bin/MakeHmcFS" ]; then
                exit 5
            elif [ "$1" = "mkfs.ext3" ]; then
                exit 5
            elif [ "$3" = "/usr/sbin/grub2-mkconfig" ]; then
                exit 6
            else
                exit $rc
            fi
        fi
    else
        rc=0
    fi
    return $rc
}

ShowCancelMSG() {
    CANCELMSG01="**************************************************************"
    CANCELMSG02="*                                                            *"
    CANCELMSG03="*           O P E R A T I O N   C A N C E L L E D            *"
    CANCELMSG04="*        The operation has been cancelled by the user        *"
    CANCELMSG05="*           Please shutdown or reboot the machine            *"
    CANCELMSG06="*            Press the Power button to shutdown              *"
    CANCELMSG07="*             Press CTRL - ALT - DEL to reboot               *"

    busybox clear
    echo; echo; echo; echo; echo; echo; echo; echo; echo
    echo; echo; echo; echo; echo; echo; echo; echo; echo
    echo "                                  $CANCELMSG01"
    echo "                                  $CANCELMSG02"
    echo "                                  $CANCELMSG03"
    echo "                                  $CANCELMSG02"
    echo "                                  $CANCELMSG04"
    echo "                                  $CANCELMSG02"
    echo "                                  $CANCELMSG05"
    echo "                                  $CANCELMSG02"
    echo "                                  $CANCELMSG06"
    echo "                                  $CANCELMSG02"
    echo "                                  $CANCELMSG07"
    echo "                                  $CANCELMSG02"
    echo "                                  $CANCELMSG01"
    while true
    do
        busybox sleep 10000
    done
}

ShowFatalErrorMsg() {
    FATAL_ERR_MSG01="**************************************************************"
    FATAL_ERR_MSG02="*                                                            *"
    FATAL_ERR_MSG03="*             U N R E C O V E R A B L E  E R R O R           *"
    FATAL_ERR_MSG04="*               An unrecoverable error has occured.          *"
    FATAL_ERR_MSG05="*                    Please contact support.                 *"
    busybox clear
    echo; echo; echo; echo; echo; echo; echo; echo; echo
    echo; echo; echo; echo; echo; echo; echo; echo; echo
    echo "                               $FATAL_ERR_MSG01"
    echo "                               $FATAL_ERR_MSG02"
    echo "                               $FATAL_ERR_MSG03"
    echo "                               $FATAL_ERR_MSG04"
    echo "                               $FATAL_ERR_MSG02"
    echo "                               $FATAL_ERR_MSG01"
}

ShowInvalidUpgradeMsg() {
    INVALID_UPGR_MSG01="**************************************************************"
    INVALID_UPGR_MSG02="*                                                            *"
    INVALID_UPGR_MSG03="*           I N V A L I D    U P G R A D E                   *"
    INVALID_UPGR_MSG04="*        The current HMC version on the hard disk cannot     *"
    INVALID_UPGR_MSG05="*        be upgraded to this new version                     *"
    INVALID_UPGR_MSG06="*           Please shutdown or reboot the machine            *"
    INVALID_UPGR_MSG07="*            Press the Power button to shutdown              *"
    INVALID_UPGR_MSG08="*             Press CTRL - ALT - DEL to reboot               *"

    busybox clear
    echo; echo; echo; echo; echo; echo; echo; echo; echo
    echo; echo; echo; echo; echo; echo; echo; echo; echo
    echo "                                  $INVALID_UPGR_MSG01"
    echo "                                  $INVALID_UPGR_MSG02"
    echo "                                  $INVALID_UPGR_MSG03"
    echo "                                  $INVALID_UPGR_MSG04"
    echo "                                  $INVALID_UPGR_MSG05"
    echo "                                  $INVALID_UPGR_MSG06"
    echo "                                  $INVALID_UPGR_MSG02"
    echo "                                  $INVALID_UPGR_MSG07"
    echo "                                  $INVALID_UPGR_MSG08"
    echo "                                  $INVALID_UPGR_MSG02"
    echo "                                  $INVALID_UPGR_MSG01"
    while true
    do
        busybox sleep 10000
    done
}
ShowDiskTooSmallMSG() {
    ERRORDISK01="**************************************************************"
    ERRORDISK02="*                                                            *"
    ERRORDISK03="*    I N S U  F F I C I E N T   D I S K  C A P A C I T Y     *"
    ERRORDISK04="*       The hard drive detected does not have enough space   *"
    ERRORDISK05="*           Please shutdown or reboot the machine            *"
    ERRORDISK06="*            Press the Power button to shutdown              *"
    ERRORDISK07="*             Press CTRL - ALT - DEL to reboot               *"

    busybox clear
    echo; echo; echo; echo; echo; echo; echo; echo; echo
    echo; echo; echo; echo; echo; echo; echo; echo; echo
    echo "                                  $ERRORDISK01"
    echo "                                  $ERRORDISK02"
    echo "                                  $ERRORDISK03"
    echo "                                  $ERRORDISK02"
    echo "                                  $ERRORDISK04"
    echo "                                  $ERRORDISK05"
    echo "                                  $ERRORDISK06"
    echo "                                  $ERRORDISK02"
    echo "                                  $ERRORDISK07"
    echo "                                  $ERRORDISK02"
    echo "                                  $ERRORDISK01"
    while true
    do
        busybox sleep 10000
    done
}

ShowInvalidVpdMSG() {
    ERRORVPD01="**************************************************************"
    ERRORVPD02="*                                                            *"
    ERRORVPD03="*           U N S U P P O R T E D   H A R D W A R E          *"
    ERRORVPD04="*       Model $1 is not a supported model for HMC       *"
    ERRORVPD05="*           Please shutdown or reboot the machine            *"
    ERRORVPD06="*            Press the Power button to shutdown              *"
    ERRORVPD07="*             Press CTRL - ALT - DEL to reboot               *"

    busybox clear
    echo; echo; echo; echo; echo; echo; echo; echo; echo
    echo; echo; echo; echo; echo; echo; echo; echo; echo
    echo "                                  $ERRORVPD01"
    echo "                                  $ERRORVPD02"
    echo "                                  $ERRORVPD03"
    echo "                                  $ERRORVPD02"
    echo "                                  $ERRORVPD04"
    echo "                                  $ERRORVPD02"
    echo "                                  $ERRORVPD05"
    echo "                                  $ERRORVPD02"
    echo "                                  $ERRORVPD06"
    echo "                                  $ERRORVPD02"
    echo "                                  $ERRORVPD07"
    echo "                                  $ERRORVPD02"
    echo "                                  $ERRORVPD01"
    while true
    do
        busybox sleep 10000
    done
}

ShowServerMSG() {
    NOSRVRMSG01="**************************************************************"
    NOSRVRMSG02="*                                                            *"
    NOSRVRMSG03="*              S E R V E R   N O T   F O U N D               *"
    NOSRVRMSG04="*            The server $1 was not found           *"
    NOSRVRMSG05="*           Please shutdown or reboot the machine            *"
    NOSRVRMSG06="*            Press the Power button to shutdown              *"
    NOSRVRMSG07="*             Press CTRL - ALT - DEL to reboot               *"

    busybox clear
    echo; echo; echo; echo; echo; echo; echo; echo; echo
    echo; echo; echo; echo; echo; echo; echo; echo; echo
    echo "                                  $NOSRVRMSG01"
    echo "                                  $NOSRVRMSG02"
    echo "                                  $NOSRVRMSG03"
    echo "                                  $NOSRVRMSG02"
    echo "                                  $NOSRVRMSG04"
    echo "                                  $NOSRVRMSG02"
    echo "                                  $NOSRVRMSG05"
    echo "                                  $NOSRVRMSG02"
    echo "                                  $NOSRVRMSG06"
    echo "                                  $NOSRVRMSG02"
    echo "                                  $NOSRVRMSG07"
    echo "                                  $NOSRVRMSG02"
    echo "                                  $NOSRVRMSG01"
    while true
    do
        busybox sleep 10000
    done
}

ShowMountMSG() {
    NOMNTMSG01="**************************************************************"
    NOMNTMSG02="*                                                            *"
    NOMNTMSG03="*          M O U N T   P O I N T   N O T   F O U N D         *"
    NOMNTMSG04="*          The  mount $1 was not found         *"
    NOMNTMSG05="*           Please shutdown or reboot the machine            *"
    NOMNTMSG06="*            Press the Power button to shutdown              *"
    NOMNTMSG07="*             Press CTRL - ALT - DEL to reboot               *"

    busybox clear
    echo; echo; echo; echo; echo; echo; echo; echo; echo
    echo; echo; echo; echo; echo; echo; echo; echo; echo
    echo "                                  $NOMNTMSG01"
    echo "                                  $NOMNTMSG02"
    echo "                                  $NOMNTMSG03"
    echo "                                  $NOMNTMSG02"
    echo "                                  $NOMNTMSG04"
    echo "                                  $NOMNTMSG02"
    echo "                                  $NOMNTMSG05"
    echo "                                  $NOMNTMSG02"
    echo "                                  $NOMNTMSG06"
    echo "                                  $NOMNTMSG02"
    echo "                                  $NOMNTMSG07"
    echo "                                  $NOMNTMSG02"
    echo "                                  $NOMNTMSG01"
    while true
    do
        busybox sleep 10000
    done
}

ShowMissingAutoCfg() {
    NOCFGMSG01="**************************************************************"
    NOCFGMSG02="*                                                            *"
    NOCFGMSG03="*          M I S S I N G    C O N F I G    V A L U E         *"
    NOCFGMSG04="*     The configuration of the TFTP server should have a     *"
    NOCFGMSG05="*    value for the autocfg parameter to use the auto mode    *"
    NOCFGMSG06="*           Please shutdown or reboot the machine            *"
    NOCFGMSG07="*            Press the Power button to shutdown              *"
    NOCFGMSG08="*             Press CTRL - ALT - DEL to reboot               *"

    busybox clear
    echo; echo; echo; echo; echo; echo; echo; echo; echo
    echo; echo; echo; echo; echo; echo; echo; echo; echo
    echo "                                  $NOCFGMSG01"
    echo "                                  $NOCFGMSG02"
    echo "                                  $NOCFGMSG03"
    echo "                                  $NOCFGMSG02"
    echo "                                  $NOCFGMSG04"
    echo "                                  $NOCFGMSG02"
    echo "                                  $NOCFGMSG05"
    echo "                                  $NOCFGMSG02"
    echo "                                  $NOCFGMSG06"
    echo "                                  $NOCFGMSG02"
    echo "                                  $NOCFGMSG07"
    echo "                                  $NOCFGMSG02"
    echo "                                  $NOCFGMSG08"
    echo "                                  $NOCFGMSG02"
    echo "                                  $NOCFGMSG01"
    while true
    do
        busybox sleep 10000
    done
}

ShowMissingAutoFile() {
    NOFILEMSG01="**************************************************************"
    NOFILEMSG02="*                                                            *"
    NOFILEMSG03="*          M I S S I N G    C O N F I G    F I L E           *"
    NOFILEMSG04="*     The config file $1 was not found     *"
    NOFILEMSG05="*           Please shutdown or reboot the machine            *"
    NOFILEMSG06="*            Press the Power button to shutdown              *"
    NOFILEMSG07="*             Press CTRL - ALT - DEL to reboot               *"

    busybox clear
    echo; echo; echo; echo; echo; echo; echo; echo; echo
    echo; echo; echo; echo; echo; echo; echo; echo; echo
    echo "                                  $NOFILEMSG01"
    echo "                                  $NOFILEMSG02"
    echo "                                  $NOFILEMSG03"
    echo "                                  $NOFILEMSG02"
    echo "                                  $NOFILEMSG04"
    echo "                                  $NOFILEMSG02"
    echo "                                  $NOFILEMSG05"
    echo "                                  $NOFILEMSG02"
    echo "                                  $NOFILEMSG06"
    echo "                                  $NOFILEMSG02"
    echo "                                  $NOFILEMSG07"
    echo "                                  $NOFILEMSG02"
    echo "                                  $NOFILEMSG01"
    while true
    do
        busybox sleep 10000
    done
}

CheckConfig() {
    ERROR=""
    case $OP in
        Backup|Restore|Upgrade|Install ) ;;
        * ) ERROR="$ERROR BADOP";;
    esac
    case $MEDIA in
        disk|media|network );;
        * ) ERROR="$ERROR BADMD";;
    esac
    case $TRANS in
        nfs|ssh );;
        * ) ERROR="$ERROR BADTR";;
    esac
    IF=$INTERFACE
    /sbin/ip link show $IF > /dev/null 2>&1
    if [ $? -ne 0 ]; then
        ERROR="$ERROR BADIF"
    fi
    if [ "$ERROR" != "" ]; then
        CONFIGERR01="**********************************************************"
        CONFIGERR02="*                                                        *"
        CONFIGERR03="*    C O N F I G    F I L E    H A S     E R R O R S     *"
        CONFIGERR04="               Bad operation : $OP"
        CONFIGERR05="               Bad media : $MEDIA"
        CONFIGERR06="               Bad transtype : $TRANS"
        CONFIGERR07="               Bad interface : $IF"

        busybox clear
        echo; echo; echo; echo; echo; echo; echo; echo; echo
        echo; echo; echo; echo; echo; echo; echo; echo; echo
        echo "                                  $CONFIGERR01"
        echo "                                  $CONFIGERR02"
        echo "                                  $CONFIGERR03"
        echo "                                  $CONFIGERR02"
        echo "                                  $CONFIGERR01"
        echo ""
        for i in $ERROR
        do
            case $i in
                BADOP ) echo "                                  $CONFIGERR04";;
                BADMD ) echo "                                  $CONFIGERR05";;
                BADTR ) echo "                                  $CONFIGERR06";;
                BADIF ) echo "                                  $CONFIGERR07";;
            esac
        done
        while true
        do
            busybox sleep 10000
        done
    fi
}

CheckServer() {
    ping -c1 ${1} >/dev/null 2>&1
    if [ $? -ne 0 ]; then
        ps -ef | grep X > /dev/null 2>&1
        if [ $? -ne 0 ]; then
            ShowServerMSG ${1}
        fi
        if [ "$2" = "auto" ]; then
            ShowServerMSG ${1}
        fi
        exit 34
    fi
}

SettleDisk() {
    RunCmd udevadm settle
}

CreateDisk() {
    deactivateVolumeGroup
    if [ "$MARCH" = "ppc64le" ]; then
        createDisk_ppc64le
    else
        createDisk_x86_64
    fi
}

createDisk_ppc64le() {
    if [ "$PARTTYPE" = "gpt" ]; then
        createDisk_gpt "prep"
    else
        createDisk_msdos "8MiB" "16MiB"
        RunCmd parted -a optimal --script /dev/${HD} -- \
            set 2 boot off \
            set 1 boot on \
            set 1 prep on

        # In this config we zero out our prep partition,
        # just in case anything was there
        RunCmd -noerr dd if=/dev/zero of=/dev/${HDP}1
    fi
}

createDisk_x86_64() {
    if [ "$PARTTYPE" = "gpt" ]; then
        createDisk_gpt "bios_grub"
    else
        createDisk_msdos "2048s" 2
    fi
}

createDisk_msdos() {
    local part1=$1
    local part2=$2
    # old msdos style partition
    # we set / to boot
    # We move back the logical partitions 4MB because
    # parted complains we can't start where we requested
    RunCmd parted -a optimal --script /dev/${HD} -- \
        mklabel msdos \
        unit GiB \
        mkpart primary    $part1 $part2 \
        mkpart primary    $part2 18 \
        set 2 boot on \
        mkpart primary    18 24 \
        mkpart extended   24 -${EMPTY_CYLS}cyl \
        mkpart logical    $((24 * 1024 + 4))MiB 26 \
        mkpart logical    $((26 * 1024 + 4))MiB 30 \
        mkpart logical    $((30 * 1024 + 4))MiB 38 \
        mkpart logical    $((38 * 1024 + 4))MiB -${EMPTY_CYLS}cyl \
        set 7 lvm on \
        set 8 lvm on
}

createDisk_gpt() {
    local bootname=$1
    RunCmd parted -a optimal --script /dev/${HD} -- \
        mklabel gpt \
        unit GiB \
        mkpart $bootname    2048s  8MiB \
        set 1 $bootname on \
        mkpart rootfs         8MiB 16 \
        mkpart var              16 22 \
        mkpart swap             22 24 \
        mkpart boot             24 26 \
        mkpart dump             26 30 \
        mkpart datavg7          30 38 \
        mkpart datavg8          38 -${EMPTY_CYLS}cyl

    # For gpt partitions, wipe out the prep or bios_grub partitions
    # just in case we had any information there before
    RunCmd -noerr dd if=/dev/zero of=/dev/${HDP}1
}

mountDir() {
    CheckServer ${1} ${4}
    mkdir -p ${3}
    RunCmd -noerr mount -o nolock,ro ${1}:${2} ${3}
    if [ $? -ne 0 ]; then
        ps -ef | grep X > /dev/null 2>&1
        if [ $? -ne 0 ]; then
            ShowMountMSG ${2}
        fi
        if [ "$4" = "auto" ]; then
            ShowMountMSG ${2}
        fi
        exit 33
    fi
}

ExitMessage() {
    OPER=${1:-Install}
    RSLT=${2:-Succesful}
    TIME=${3:-10}

    while [ $TIME != 0 ]; do
        echo "$OPER Process $RSLT. Exiting in $TIME seconds ........"; sleep 1
        TIME=`expr $TIME - 1`
    done
}


udev_stop() {
#   pidNum=`ps -ef |grep udevd |awk '{ print $2 }'`
#   kill -9 $pidNum
    systemctl stop systemd-udevd
}

########################################################
# GRUB2 enablement                                     #
########################################################
createGrub2Space() {
    if [ "$(getPartitionType)" = "msdos" ]; then
        local start end
        # get the first and last sector of partition 1
        read start end <<< $(parted --script /dev/${HD} unit s print | awk '/^ 1/ {print $2 " " $3}')

        # if the starting sector isn't 2048, then go through and delete and recreate it
        if [ "$start" != "2048s" ]; then
            # delete partition 1, and then recreate it
            # the mkpart command only works for msdos partitions
            RunCmd parted --script /dev/${HD} \
                rm 1 \
                mkpart primary linux-swap 2048s $end
        fi
    fi
}

getPartitionType() {
    parted --script /dev/${HD} print | \
        awk -F: '/^Partition Table/ { gsub(/ /, "", $2); print $2 }'
}

runCmdOnTerm() {
    _ctty="$(RD_DEBUG= getarg rd.ctty=)" && _ctty="/dev/${_ctty##*/}"
    if [ -z "$_ctty" ]; then
        _ctty=console
        while [ -f /sys/class/tty/$_ctty/active ]; do
            _ctty=$(cat /sys/class/tty/$_ctty/active)
            _ctty=${_ctty##* } # last one in the list
        done
        _ctty=/dev/$_ctty
    fi
    [ -c "$_ctty" ] || _ctty=/dev/tty1
    case "$(/usr/bin/setsid --help 2>&1)" in *--ctty*) CTTY="--ctty";; esac

    setsid $CTTY /bin/sh -l -c "export TERM=bterm;$1 0<>$_ctty 1<>$_ctty 2<>/dev/null"
}
