#!/bin/sh 

#
# Script to configure PowerPath into initramfs
#

TMP_MODP=/tmp/powerpath.conf
PP_MODP=/etc/modprobe.d/powerpath.conf

# On RHEL6/OL6 (that use dracut), specify the powerpath.conf file with only
# the array of the root device being managed.  Otherwise all the array classes
# will be managed and during /etc/init.d/PowerPath start, any of the classes
# that may be 'unmanaged' by the user cannot be unmanaged.  Hence specify only
# the root dev array class as managed so that PP starts with only that array
# as managed while initramfs loads.  Then during /etc/init.d/PowerPath start,
# the manage/unmanage settings of other classes will be set through
# powermt init boot.
ROOT_VG=`mount 2>/dev/null | grep "on / " | grep mapper | sed -e 's/^\/dev\/mapper\/\(.*\) on.*/\1/'`
DM_TABLE=`dmsetup table $ROOT_VG 2>/dev/null`

DEV_MAJOR=`echo $DM_TABLE | sed -e 's/.* \([0-9]*\):\([0-9]*\).*/\1/'`
DEV_MINOR=`echo $DM_TABLE | sed -e 's/.* \([0-9]*\):\([0-9]*\).*/\2/'`

DEV_NAME_FULL=`grep -e "^ \+$DEV_MAJOR \+ $DEV_MINOR \+" /proc/partitions | sed -e 's/^ *[0-9]* *[0-9]* *[0-9]* *//'`
DEV_NAME=`echo $DEV_NAME_FULL | sed -e 's/[0-9]*//g'`

DEV_CLASS=`powermt display dev=$DEV_NAME 2>/dev/null| grep -e "ID=" | grep -v "Logical" | sed -e 's/^\(.*\) ID=.*/\1/'`

case "$DEV_CLASS" in
    Symmetrix)
        LAM_NAME=symm
        ;;
    CLARiiON)
        LAM_NAME=clariion
        ;;
    "HP xp")
        LAM_NAME=hpxp
        ;;
    Hitachi)
        LAM_NAME=Hitachi
        ;;
    Ess)
        LAM_NAME=ess
        ;;
    Invista)
        LAM_NAME=invista
        ;;
    VPLEX)
        LAM_NAME=vplex
        ;;
    VNX)
        LAM_NAME=vnx
        ;;
    NetApp)
        LAM_NAME=netapp
        ;;
    XtremIO)
        LAM_NAME=xtremio
        ;;
    "HP HSx")
        LAM_NAME=hphsx
        ;;
    Generic)
        LAM_NAME=Generic
        ;;
esac

if [ "$LAM_NAME" == "Generic" ]
then
    cat $PP_MODP | sed -e "s/^.*genericclass=\(.*\)/options emcp genericclass=\1/" > $TMP_MODP
else
    cat $PP_MODP | sed -e "s/^options emcp managedclass=.*/options emcp managedclass=$LAM_NAME/" > $TMP_MODP
fi

# Install the BFS powerpath.conf file in a non-standard location since
# dracut itself copies /etc/modprobe.d/powerpath.conf file to the initrd
# staging area, but would not allow it to be overwritten again.
inst_simple $TMP_MODP /etc/opt/emcpower/powerpath.conf
unlink $TMP_MODP


inst_hook pre-trigger 02 "${moddir}/emcp_pre-trigger.sh"
inst_simple "${moddir}/emcp_settled.sh"     /etc/opt/emcpower/emcp_settled.sh 

inst /bin/fgrep
inst /bin/awk
inst /bin/echo 
inst /sbin/lsmod
inst /sbin/modprobe

inst /sbin/pp_inq
inst /etc/opt/emcpower/emcpmgr /etc/opt/emcpower/emcpmgr

inst_rules  /etc/udev/rules.d/05-udev-pp.rules
inst_simple /lib/udev/pp_udev.sh

#
# When creating initramfs using dracut for the first time, 
# save the device config files into $dbbakdir.
# Every subsequent creation of initramfs will use the saved config files
#

dbbakdir=/etc/emc/bfs
if [ -d ${dbbakdir} ]; then
    # Not first time, so use the saved configuration files
    inst_simple ${dbbakdir}/emcp_devicesDB.idx /etc/emcp_devicesDB.idx
    inst_simple ${dbbakdir}/emcp_devicesDB.dat /etc/emcp_devicesDB.dat
else
    ## first-time creation of initramfs.
    inst_simple /etc/emcp_devicesDB.idx /etc/emcp_devicesDB.idx
    inst_simple /etc/emcp_devicesDB.dat /etc/emcp_devicesDB.dat

    # take backup of database when creating initramfs with PP
    # for the first time

    mkdir -p ${dbbakdir}
    if [ $? -ne 0 ]; then 
        echo "Unable to create backup directory $dbbakdir for database"
        exit 1
    fi

    cp -pf /etc/emcp_devicesDB.idx ${dbbakdir}/emcp_devicesDB.idx 
    if [ $? -ne 0 ]; then
        echo  "Unable to save backup of /etc/emcp_devicesDB.idx"
        exit 1
    fi
    cp -pf /etc/emcp_devicesDB.dat ${dbbakdir}/emcp_devicesDB.dat 
    if [ $? -ne 0 ]; then
        echo "Unable to save backup of /etc/emcp_devicesDB.dat"
        exit 1 
    fi
fi

inst_simple /etc/opt/emcpower/.pp_version 

pp_version=`cat /etc/opt/emcpower/.pp_version`
os_version=$(uname -r)
pp_install_dir=/etc/opt/emcpower/EMCpower.LINUX-${pp_version}

for i in $(ls ${pp_install_dir}/bin/.drivers_*  2>/dev/null); 
do
    inst_simple ${i}
done

for mods in $(ls /lib/modules/${os_version}/powerpath/*.ko 2>/dev/null); 
do
    inst ${mods}
done

# OPT 441242: During early boot when Powerpath modules are loaded,
# the hostname is not set in the kernel and set to (none) by default.
# Since MpxReserveKey is dependent on hostname, in a cluster setup
# on BFS all nodes would end up with the same MpxReserveKey.
# To address this, save the current hostname into initramfs
# which can be accessed during early boot stage.
if [ -f /etc/opt/emcpower/hostname.pp ]; then
    rm /etc/opt/emcpower/hostname.pp
fi

cat /proc/sys/kernel/hostname > /etc/opt/emcpower/hostname.pp
inst_simple /etc/opt/emcpower/hostname.pp
