#!/bin/bash

#
# From PowerPath 7.0 during BFS host kernel upgrader exclude PowerPath pseudo(emcpower) device 
# filter from lvm.conf at the time of creating ramdisk image by automatically(yum update) or 
# manually to avoid system boot failure.
#


PPBFS_HOST=no
ROOT_VG=`mount 2>/dev/null | grep "on / " | grep mapper | sed -e 's/^\/dev\/mapper\/\(.*\) on.*/\1/'`
if [ "$ROOT_VG" == "" ]
then
    PPBFS_HOST=no
fi

# If a snapshot of the root logical volume exists, then dmsetup table will
# give details of the dm device.  Hence add the prefix '-real' to the name
# so that dmsetup table will give the details of the original pseudo device.
if dmsetup table $ROOT_VG | grep -q "snapshot-origin"; then
    ROOT_VG="${ROOT_VG}-real"
fi

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_NAME_PSEUDO=`powermt display dev=$DEV_NAME 2>/dev/null| grep -e "Pseudo name" | sed -e 's/^Pseudo name=\(.*\)/\1/'`
if [ "$DEV_NAME_PSEUDO" != "" ]
then
    PPBFS_HOST=yes
fi


#-----------------------------------------------------
##   LVM Filter modification
#-----------------------------------------------------
LVM_PP_FILTER_UPDATE()
{
    if [ -f $LVM ]; then
        if [[ $LVM == "/etc/lvm/lvm.conf" ]]; then
            cp $LVM ${LVM}_bfs_changes
        fi
        cp $LVM $TMP_LVM
        cat $TMP_LVM | grep "###${SCRIPT_NAME}###"  > /dev/null 2>&1

        if [ $? -ne 0 ]; then
            # Comment out existing lvm filter.
            if [[ ${VERSION} == "RHEL6" ]]; then
                sed -i "s%^[ \t]*filter.*%    ###${SCRIPT_NAME}###$(date)###\n    #& \n    #########\n& %" $TMP_LVM
            #elif [[ ${VERSION} == "RHEL7" || ${VERSION} == "SLES12" ]]; then
            #    sed -i "s%^[ \t]*global_filter[ \t]*=.*%    ###${SCRIPT_NAME}###$(date)###\n    ##& \n    #########\n& %" $TMP_LVM
            fi
        fi

        if [[ $ACTION == "remove" ]]; then
            if [[ ${VERSION} == "RHEL6" ]]; then
                # Update lvm filter for rhel6 and sles11.
                cat $TMP_LVM |  sed -e '/^[ \t]*filter/  {
                    s%\"a/emcpower.*/\"[ \t]*,[ \t]*\"r/sd.\*/\"[ \t]*,[ \t]*\"r/disk.\*/\"%\"r/emcpower.\*/\", \"a/sd.\*/\"%g
                    s%\"r/sd.\*/\"%%g
                    s%\"r|/dev/sd.\*|\"%%g
                    s%,[ \t]*,[ \t]*,%,%g
                    s%,[ \t]*,%,%g
                    s%\[ [ \t]*,%\[ %g
                    s%,[ \t]*\]% \]%g
                    }'  > $TMP1_LVM
                cat $TMP1_LVM | grep "^[ \t]*filter" |\
                    grep "\"r/emcpower.\*/\", \"a/sd.\*/\"" > /dev/null 2>&1

                if [ $? -ne 0 ]; then
                    sed -i "s%^[ \t]*filter.*%    ###${SCRIPT_NAME} remove###$(date)###\n    #& \n    ########\n& %" $TMP_LVM
                    # Overwrite lvm filter, if the lvm filter is failed to update above.
                    cat $TMP_LVM |  sed -e '/^[ \t]*filter/  s%\[.*\]%\[ \"r/emcpower.\*/\", \"a/sd.\*/\" \]%g'  > $TMP1_LVM
                fi
            elif [[ ${VERSION} == "RHEL7" || ${VERSION} == "SLES12" || ${VERSION} == "SLES15" || ${VERSION} == "RHEL8" ]]; then
                # Update lvm filter for rhel7, sles12 and sles15
                cat $TMP_LVM |  sed -e '/^[ \t]*global_filter[ \t]*=/  {
                    s%\"a/emcpower.*/\"[ \t]*,[ \t]*\"r/sd.\*/\"[ \t]*,[ \t]*\"r/disk.\*/\"%\"r/emcpower.\*/\", \"a/sd.\*/\"%g
                    s%\"r/sd.\*/\"%%g
                    s%\"r|/dev/sd.\*|\"%%g
                    s%,[ \t]*,[ \t]*,%,%g
                    s%,[ \t]*,%,%g
                    s%\[ [ \t]*,%\[ %g
                    s%,[ \t]*\]% \]%g
                    }'  > $TMP1_LVM
                cat $TMP1_LVM | grep "^[ \t]*global_filter" |\
                    grep "\"r/emcpower.\*/\", \"a/sd.\*/\"" > /dev/null 2>&1

                if [ $? -ne 0 ]; then
                    #sed -i "s%^[ \t]*global_filter[ \t]*=.*%    ###${SCRIPT_NAME} remove###$(date)###\n    ##& \n    ########\n& %" $TMP_LVM
                    # Overwrite lvm filter, if the lvm filter is failed to update above.
                    cat $TMP_LVM |  sed -e '/^[ \t]*global_filter[ \t]*=/  s%\[.*\]%\[ \"r/emcpower.\*/\", \"a/sd.\*/\" \]%g'  > $TMP1_LVM
                fi

            fi

        fi

        if [ -f $TMP1_LVM ]; then
            cp $TMP1_LVM $LVM
        elif [ -f $TMP_LVM ]; then
            cp $TMP_LVM $LVM
        fi
    fi
}

#-----------------------------------------------------
##   Miscellaneous Function
#-----------------------------------------------------
LVM_FILTER_DEFAULT()
{
    if [[ $ACTION == "remove" ]]; then
        # Update lvm filter for removel of r/emcpower entry.
        if [[ ${VERSION} == "RHEL6" ]]; then
            cat $LVM | sed -e '/^[ \t]*filter/  {
                       s%\"r/emcpower.\*/\", \"a/sd.\*/\"%\"a/.\*/\"%g
                       }'  > $TMP_LVM
        elif [[ ${VERSION} == "RHEL7" || ${VERSION} == "SLES12" || ${VERSION} == "SLES15" || ${VERSION} == "RHEL8" ]]; then
             cat $LVM | sed -e '/^[ \t]*global_filter[ \t]*=/ {
                        s%global_filter%# global_filter%g
                        s%\"r/emcpower.\*/\", \"a/sd.\*/\"%%g
                        }'  > $TMP_LVM
        fi
        cp $TMP_LVM $LVM

    fi
}


REDHAT=/etc/redhat-release
SLES_12=/etc/os-release
SLES=/etc/SuSE-release
SCRIPT_NAME=$(basename $0)
if [ -f ${initdir}/etc/lvm/lvm.conf ]; then
    LVM=${initdir}/etc/lvm/lvm.conf
else
    LVM=/etc/lvm/lvm.conf
fi
TMP_LVM=/tmp/tmp_${SCRIPT_NAME}_lvm.conf
TMP1_LVM=/tmp/tmp1_${SCRIPT_NAME}_lvm.conf

#-----------------------------------------------------
##   Linux Kernels Distribution Check
#-----------------------------------------------------

if [ -f ${REDHAT} ]; then
    VERSION=RHEL$(cat ${REDHAT} \
        | sed 's/^.*release \([0-9]\)[0-9.]*.*/\1/') > /dev/null 2>&1
elif [ -f ${SLES_12} ]; then
    cat ${SLES_12} | grep "SUSE Linux Enterprise Server 15" > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        VERSION=SLES15
    fi
    cat ${SLES_12} | grep "SUSE Linux Enterprise Server 12" > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        VERSION=SLES12
    fi
elif [ -f ${SLES} ]; then
    VERSION=SLES$(cat ${SLES} | awk '/^VERSION/ {version=$NF}
                 END {print version}') > /dev/null 2>&1
fi

#-----------------------------------------------------
##   MAIN Function
#-----------------------------------------------------
if [[ $PPBFS_HOST == "yes" ]]; then

    case $VERSION in
    RHEL8|RHEL7|RHEL6|SLES12|SLES15)

        ACTION="remove"

        LVM_PP_FILTER_UPDATE
        LVM_FILTER_DEFAULT

    ;;
    esac

    if [[ $LVM == "/etc/lvm/lvm.conf" ]]; then

        inst_simple $LVM

        cp -f ${LVM}_bfs_changes $LVM
        rm -f ${LVM}_bfs_changes

    fi
fi

