#!/bin/sh
#
# ###################################################################
# Copyright (c) 2005-2016, EMC Corporation. All Rights Reserved.
#
# /etc/init.d/PowerPathPost
# This file is a part of EMC PowerPath.
#
# chkconfig: 35 24 77
# description: Configures FCoE, Iscsi devices during boot and runs \
#              PPME recovery if required on bootup
#
# ###################################################################
#
### BEGIN INIT INFO
# Provides:          PowerPathPost
# Required-Start:
# Required-Stop:
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Description:       PowerPathPost -- Configures FCoE, Iscsi \
#                    devices during boot and runs PPME recovery \
#                    if required on bootup
### END INIT INFO

PATH=/usr/bin/:/bin:/sbin:/usr/sbin

RHEL7_HOST=no
RHEL6_HOST=no 
RHEL5_HOST=no
SLES10_HOST=no
OL55_HOST=no
OL6_HOST=no
OL7_HOST=no
CXS6_HOST=no
SLES11SP3_HOST=no
SLES11SP4_HOST=no
SLES12_HOST=no
RHEL=no
OL=no

# SW_PROTOCOL can be iSCSI,FCoE or both
SW_PROTOCOL=no
SW_ISCSI_PROTOCOL_ACTIVE=no
SW_FCOE_PROTOCOL_ACTIVE=no
fcoe_ports_online=0

# By default, the dm dependencies should be reset to pseudo devices
# except for older platforms such as SLES10, RHEL5 and OL55
RESET_DM_DEPS=yes

# Used to pass mounted pseudo partition device name from
# function check_holders_holders.
#
g_partition_dev_name=""

#
# Store powermt display and  powermt display dev=all output.
#
powermt_display=`powermt display`
powermt_display_all=`powermt display dev=all`

pp_start_lvm()
{
    if test -x /sbin/vgchange -a -x /sbin/vgscan ; then
        /sbin/vgscan > /dev/null 2>&1

        if test -r  /etc/sysconfig/lvm ; then
            . /etc/sysconfig/lvm
        fi

        /sbin/vgchange -a y $LVM_VGS_ACTIVATED_ON_BOOT > /dev/null 2>&1

        if [ $? -eq 5 ]; then  ## "no volume groups" ==> success
            /bin/true
        fi
    fi
}

#
# Check if a dm device of a LVM volume is contained in a holders directory.
#
# Parameter 1 - holders directory
#           2 - the dm device of the mounted LVM volume
# Return values 0 - The the dm device is not contained in the holders
#                   directory.
#               1 - The dm device is contained in the holders directory.
#
check_holders()
{
    local holders_path=$1
    local dm=$2

    for h in $holders_path/* ; do
        [ "${h##*/}" != "*" ] || break
        [ "${h##*/}" = "${dm##*/}" ] && return 1
    done
    return 0
}

#
# Check for a pseudo device's holders' holders or partitions' holders
# to see if the pseudo device contains the dm device of a mounted
# LVM volume.
#
# Parameter 1 - pseudo device name
#           2 - whether the psdudo device is a partition(1: yes 0: no)
#           3 - the dm device of the mounted LVM volume
# Return values 0 - The pseudo device doesn't contain the dm device.
#               1 - The pseudo device contains the dm device.
#
check_holders_holders()
{
    local dev=$1
    local is_partition=$2
    local dm=$3

    if [ $is_partition -eq 1 ] ; then
        empty_string="emcpower*"
        parent_path="/sys/devices/virtual/block/$dev/$empty_string"
    else
        empty_string="*"
        parent_path="/sys/devices/virtual/block/$dev/holders/$empty_string"
    fi

    for p in $parent_path ; do
        [ "${p##*/}" != "$empty_string" ] || break

        if [ $is_partition -eq 1 ] ; then
            holders_path="$p/holders/"
            g_partition_dev_name="${p##*/}"
        else
            holders_path="/sys/devices/virtual/block/"${p##*/}"/holders/"
        fi
        check_holders $holders_path $dm
        if [ $? -eq 1 ] ; then
            [ $is_partition -eq 1 ] && g_partition_dev_name="${p##*/}"
            return 1
        fi
    done
    return 0
}

#
# If umount fails and the mounted device is a LVM volume, then
# reset the dm table for dm devices associated with an iSCSI
# pseudo device to use pseudo device's first active native
# path to prevent open-iscsi:stop from logging out the iSCSI
# session the native path belongs to.
#
# parameter 1 - the pseudo device or pseudo device partition
#
reset_dm_table_to_use_native()
{
    local dev=$1
    local partition_no="${dev##*[a-z]}"
    local dev_without_partition="${dev%$partition_no}"
    local pseudo_major=`grep -e "$dev$" /proc/partitions | \
        sed -e "s/^ *\([0-9]*\) *[0-9]* *[0-9]* *$dev$/\1/"`""
    local pseudo_minor=`grep -e "$dev$" /proc/partitions | \
        sed -e "s/^ *[0-9]* *\([0-9]*\) *[0-9]* *$dev$/\1/"`
    local pseudo_major_minor="$pseudo_major:$pseudo_minor"
    local native_dev_names=`echo "$powermt_display_all" | \
                awk  -vpseudo="${dev_without_partition}" '$0 ~ pseudo \
                {flag=1;next} /$^/ {flag=0} flag { print }' \
                egrep 'active|standby|unlic|asb:iopf' | grep -v unknown | \
                sed -ne 's:\(.*\) \(sd[a-z]*\) \(.*\):\2:p')`
    local table_file="/tmp/poweriscsi_table_file"

    for n in $(echo $native_dev_names)
    do
        first_native_path="$n$partition_no"
        break
    done

    [ "$first_native_path" != "" ] || return


    native_major=`grep -e "$first_native_path$" /proc/partitions | \
        sed -e "s/^ *\([0-9]*\) *[0-9]* *[0-9]* *$first_native_path$/\1/"`""
    native_minor=`grep -e "$first_native_path$" /proc/partitions | \
        sed -e "s/^ *[0-9]* *\([0-9]*\) *[0-9]* *$first_native_path$/\1/"`
    native_major_minor="$native_major:$native_minor"

    DM_LIST=`dmsetup ls 2>/dev/null | grep -v "No devices found" | \
        sed -e 's/^\(\S*\) *.*/\1/'`
    DM_MAJOR=`cat /proc/devices|grep device-mapper|awk '{print $1}'`

    for DM_DEV in $DM_LIST
    do
        OLD_TABLE=`dmsetup table $DM_DEV 2>/dev/null`
        NEW_TABLE=`echo "$OLD_TABLE" | \
            sed -e "s/$pseudo_major_minor/$native_major_minor/g"`

        if [ "$OLD_TABLE" != "$NEW_TABLE" ] ; then
            echo "$NEW_TABLE" > $table_file
            dmsetup suspend $DM_DEV 2>/dev/null
            dmsetup reload $DM_DEV $table_file 2>/dev/null
            dmsetup resume $DM_DEV 2>/dev/null
            rm $table_file 2>/dev/null
        fi
    done
}

#
# Unmount any mount points associated with a pseudo device that
# are using the specific transport protocol
#
# Parameter 1 - an pseudo device such as emcpowera
# Parameter 2 - a transport protocol (i.e. iSCSI, FCoE)
#
umount_if_mounted()
{
    local dev=$1
    local protocol=$2
    local d m x islink holder_path mount_link

    cat /proc/mounts | sed -ne '/^\/dev\/.*/p' | while read d m x; do
        islink=0
        if [ -L "$d" ] ; then
            # Mounted device is LVM volume such as /dev/mapper/vg01-lvol0.
            # Use readlink to get its dm device, such /dev/dm-1.
            d=$(readlink -f $d)
            islink=1
        fi
        [ -b "$d" ] || continue
        [ "$m" != "/" ] || continue  # Skip root mount point

        is_mounted=0
        g_partition_dev_name=""

        if [ $islink -eq 0 ] ; then
            #
            # The mounted device can be /dev/emcpowera, /dev/emcpowera2,
            # /dev/sda and /dev/sda2, etc.
            #
            # Strip the path and partition from the mount device.
            #
            d="${d##*/}"
            d="${d%"${d##*[a-z]}"}"

            if [ "$dev" = "$d" ]; then
               is_mounted=1
            else # Check native path as well
            echo "$powermt_display_all" \
                       | awk  -vpseudo="${dev##*/}" '$0 ~ pseudo \
                       {flag=1;next} /$^/ {flag=0} flag { print }' \
                       | grep $protocol > /dev/null 2>&1
                if [ $? -eq 0 ] ; then
                    is_mounted=1
                fi
            fi
        else
            #
            # Mounted device is a LVM volume. Check for $dev's holders and
            # holders' holders(i.e., in the case of mirrored LVM volumes)
            # to see if $dev contains the dm device of the LVM volume.
            #
            # Check for partitions' holders as well because volume can be
            # created with partitions.
            #
            check_holders "/sys/devices/virtual/block/$dev/holders/" $d && \
                check_holders_holders $dev 0 $d && \
                check_holders_holders $dev 1 $d
            is_mounted=$?
        fi

        if [ $is_mounted -eq 1 ] ; then
            if  ! umount $m 2> /dev/null  && [ $islink -eq 1 ] ; then
                #
                # If umount fails and the mounted device is a LVM volume,
                # then reset the dm table for dm devices associated with this
                # pseudo device to use pseudo device's first active native
                # path to prevent open-iscsi:stop from logging out the FCoE
                # session this the native path belongs to.
                #
                if [ "$g_partition_dev_name" != "" ] ; then
                    reset_dm_table_to_use_native $g_partition_dev_name
                else
                    reset_dm_table_to_use_native $dev
                fi
            fi
        fi
    done
}


#
# Check if a device-mapper device is mounted by UUID and maps to a
# LVM logical volume which uses EMC PowerPath pseudo device names.
# If a match is found unmount the file system which corresponds to
# the device-mapper device.
#
# Parameter 1 - protocol string to search for (i.e. iSCSI, FCoE)
#
umount_if_mounted_by_uuid()
{
    local protocol=$1
    #
    # Loop through the file systems that are currently mounted on
    # device-mapper devices.
    #
    cat /proc/mounts | grep -e "^\/dev\/dm-" | awk '{print $1}' \
        | while read filesystem;
    do
        #
        # Use the block ID command to get the UUID for the
        # device-mapper device.
        #
        dm_dev_uuid=`/sbin/blkid $filesystem | awk 'BEGIN {FS = "\""} \
            ; {print $2}'`
        #
        # For each device-mapper volume still mounted loop through the
        # LVM volume groups and check if it has an EMC PowerPath pseudo
        # device.
        #
        vgdisplay -c | awk 'BEGIN {FS = ":"} ; {print $1}' | while read vg;
        do
            pp_dev=`vgdisplay -v $vg 2>/dev/null | grep -e "PV Name" \
                | grep -e "emcpower" | awk '{print $3}'`
            if [ "$pp_dev" != "" ] ; then
                #
                # Get the EMC PowerPath pseudo device name from the
                # LVM physical volume.
                #
                pp_pseudo=`echo $pp_dev | awk 'BEGIN {FS = "/"} \
                    ; {print $3}'`
                #
                # strip the partition
                #
                pp_pseudo="${pp_pseudo##*/}"
                pp_pseudo="${pp_pseudo%"${pp_pseudo##*[a-z]}"}"
                #
                # Check if the EMC PowerPath pseudo device matches
                # the specified transport protocol
                #
                is_match=`echo "$powermt_display_all" | \
                       awk  -vpseudo="${pp_pseudo}" '$0 ~ pseudo \
                       {flag=1;next} /$^/ {flag=0} flag { print }' \
                       | grep -e $protocol`
                if [ "$is_match" != "" ] ; then
                    #
                    # We have a LVM volume group that has an EMC PowerPath
                    # pseudo device. Check to see if our current
                    # device-mapper device UUID matches a LVM logical
                    # volume UUID from this volume group.
                    #
                    vgdisplay -v $vg 2>/dev/null | grep -e "LV Name" \
                        | awk '{print $3}' | while read lv;
                    do
                        #
                        # Use the block ID command to get the UUID for the
                        # LVM logical volume.  Then compare the current
                        # device-mapper UUID with the LVM logical volume
                        # UUID.
                        #
                        lvm_uuid=`/sbin/blkid $lv | awk 'BEGIN {FS = "\""} \
                            ; {print $2}'`
                        if [ "$dm_dev_uuid" = "$lvm_uuid" ] ; then
                            #
                            # We have matching UUIDs. Search the fstab file
                            # for the mount point for unmounting.
                            #
                            fs_to_unmount=`grep -e $dm_dev_uuid /etc/fstab \
                                | awk '{print $2}'`
                            /bin/umount $fs_to_unmount
                            #
                            # Deactivate the volume group
                            #
                            /sbin/vgchange -a n $vg > /dev/null 2>&1
                        fi
                    done
                fi
            fi
        done
    done
}


umount_pp_all()
{
    local protocol=$1

    for dev in /dev/emcpower?*
    do
        [ "${dev##*/}" != "emcpower?*" ] || break

        #
        # Skip partitions here.  They will be handled in
        # umount_if_mounted()
        #
        if [ "`expr \"${dev##*/}\" : \
            \"emcpower[a-z][a-z]*[0-9][0-9]*\"`" != "0" ] ; then
            continue
        fi

        echo "$powermt_display_all" | \
                       awk  -vpseudo="${dev##*/}" '$0 ~ pseudo \
                       {flag=1;next} /$^/ {flag=0} flag { print }' \
                       | grep $protocol > /dev/null 2>&1
        if [ $? -eq 0 ] ; then
            umount_if_mounted "${dev##*/}" $protocol
        fi
    done
}


#
# Check if the kdump initrd image needs to be updated.  If it had
# been updated during a kernel or a OS upgrade when PowerPath is
# installed, it will have PowerPath recommended parameters in fstab
# and lvm.conf files and therefore won't load properly as PowerPath
# is not configured inside it.  Hence update it with default fstab
# and lvm.conf parameters.
#
check_and_fix_kdump_initrd_img()
{
    PP_KDUMP_INITRD_MARKER=/etc/opt/emcpower/.pp_kdump_marker
    if [ "$RHEL7_HOST" == "yes" ]; then
        KDUMP_INITRD_IMG=/boot/initramfs-$(uname -r)kdump.img
    else
        KDUMP_INITRD_IMG=/boot/initrd-$(uname -r)kdump.img
    fi
    FSTAB=/etc/fstab
    FSTAB_TMP=/tmp/tmp_pp_fstab
    FSTAB_BAK=/tmp/tmp_pp_fstab.bak
    LVM=/etc/lvm/lvm.conf
    LVM_TMP=/tmp/tmp_pp_lvm.conf
    LVM_BAK=/tmp/tmp_pp_lvm.conf.bak
    KDUMP_CONF=/etc/kdump.conf

    # If kdump initrd image does not exist, then nothing further to do
    if [ ! -f $KDUMP_INITRD_IMG ]; then
        return;
    fi

    ROOT_VG=`mount 2>/dev/null | grep "on / " | grep mapper | sed -e 's/^\/dev\/mapper\/\(.*\) on.*/\1/'`

    # If root volume group is not a device mapper device, it means
    # this is not a BFS config and nothing more needs to be done.
    if [ -z $ROOT_VG ]; then
        return;
    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]* *//'`

    # If this is not a BFS host, then nothing further to do
    echo $DEV_NAME_FULL | grep emcpower > /dev/null 2>&1
    if [ $? -ne 0 ]; then
        return;
    fi

    # Check if PowerPath had already recreated the kdump initrd img
    # and it has not been recreated hence
    if [ -f $PP_KDUMP_INITRD_MARKER ]; then
        TIMESTAMP1=`stat -c "%Y" $PP_KDUMP_INITRD_MARKER`
        TIMESTAMP2=`stat -c "%Y" $KDUMP_INITRD_IMG`
        if [ "$TIMESTAMP1" -ge "$TIMESTAMP2" ]; then
            return;
        fi
    fi

    # Create a fstab file with default values for /boot
    fstab_boot=$(cat $FSTAB | grep -v "^[ \t]*#" |\
            grep "\/boot " | awk '{ print $1 }')

    if [ `echo $fstab_boot | grep -c "UUID" ` -le 0 ]; then
        uuid=$(blkid $fstab_boot |\
                sed -ne 's:\(.*\) \(UUID=.*\) \(.*\):\2:p' |\
                awk '{ print $1 }' | sed 's/"//g')
        cat $FSTAB |\
                sed -e "/\/boot /  s%^${fstab_boot}%${uuid}%g" > $FSTAB_TMP
    else
        cp -f $FSTAB $FSTAB_TMP
    fi

    # Create a lvm.conf file with default filter
    if [ "$RHEL7_HOST" == "yes" ]; then
        cat $LVM |  sed -e '/^[ \t]*global_filter/  s%\[.*\]%\[ \"r/emcpower.\*/\", \"a/sd.\*/\" \]%g' \
                        -e '/^[ \t]*filter/  s%\[.*\]%\[ \"r/emcpower.\*/\", \"a/sd.\*/\" \]%g'  > $LVM_TMP
    else
        cat $LVM |  sed -e '/^[ \t]*filter/  s%\[.*\]%\[ \"r/emcpower.\*/\", \"a/sd.\*/\" \]%g'  > $LVM_TMP
    fi

    # Take backup of fstab and lvm.conf files
    cp -f $FSTAB $FSTAB_BAK
    cp -f $LVM $LVM_BAK
    sleep 1;sync

    # Overwrite fstab and lvm.conf with default values
    cp -f $FSTAB_TMP $FSTAB
    cp -f $LVM_TMP $LVM
    sleep 3;sync

    # Add "blacklist powerpath" entry in /etc/kdump.conf
    if [ "$RHEL6_HOST" == "yes" -o "$OL6_HOST" == "yes" ]; then
        if [ -f $KDUMP_CONF ]; then
            count_bp=`grep -v "[^ ]*[^\t]*#" $KDUMP_CONF | grep -c "blacklist powerpath"`
            if [ $count_bp -eq 0 ]; then
                echo "blacklist powerpath" >> $KDUMP_CONF
            fi
        fi
    fi

    # Update timestamp of /etc/kdump.conf.  This will trigger kdump
    # to recreate the kdump initrd image the next time it is started
    touch /etc/kdump.conf
    sleep 1;sync

    # Restart kdump.  This will recreate the kdump initrd image
    if [ "$RHEL7_HOST" == "yes" ]; then
        /usr/bin/kdumpctl restart > /dev/null 2>&1
    else
        /etc/init.d/kdump restart > /dev/null 2>&1
    fi
    sleep 3;sync

    # Restore fstab and lvm.conf files
    cp -f $FSTAB_BAK $FSTAB
    cp -f $LVM_BAK $LVM
    sleep 3;sync

    # Cleanup
    rm -f $FSTAB_BAK
    rm -f $FSTAB_TMP
    rm -f $LVM_BAK
    rm -f $LVM_TMP

    sleep 1
    touch $PP_KDUMP_INITRD_MARKER
}


#-----------------
# main starts here
#-----------------

#
# This script needs to be run only if PowerPath is active.
#
lsmod | grep -w '^emcp' > /dev/null
if [ $? != 0 ]; then
    if [ -e /var/lock/subsys/PowerPathPost ]; then
        rm /var/lock/subsys/PowerPathPost
    fi
    exit
fi

pp_config_complete="/etc/emc/.pp_config_complete"
if [ -f /etc/SuSE-release ]; then
   host_os=`awk '/^VERSION/ {version=$3}
                  END {print version}' /etc/SuSE-release`
   patch_level=`awk '/^PATCHLEVEL/ {patch=$3}
                    END {print patch}' /etc/SuSE-release`
   if [ "${host_os}" == "10" ]; then
      SLES10_HOST=yes
   elif [ "${host_os}" == "11" ]&& [ "${patch_level}" == "3" ]; then 
      SLES11SP3_HOST=yes
   elif [ "${host_os}" == "11" ]&& [ "${patch_level}" == "4" ]; then 
      SLES11SP4_HOST=yes
   elif [ "${host_os}" == "12" ]; then
      SLES12_HOST=yes
   fi
elif [ -f /etc/oracle-release ]; then
    if [ $(uname -r | grep -c uek) -ne 0 ]
    then
        oracle_version=`sed '/^[ \t]*#/d;s/^.*release \([0-9]\)[0-9.]*/\1/' \
                                              /etc/oracle-release`
        oracle_update=`sed '/^[ \t]*#/d;s/^.*release [0-9]\([.0-9]\)*.*/\1/' \
                                              /etc/oracle-release`
        OL=yes
        if [ "${oracle_version}" == "5" ]; then
           if [ -n "${oracle_update}" ] && [ ${oracle_update} -le 5 ]; then
              OL55_HOST=yes
           fi
        elif [ "${oracle_version}" == "6" ]; then
           OL6_HOST=yes
        elif [ "${oracle_version}" == "7" ]; then
           OL7_HOST=yes
        fi
    else
        rhel_version=`sed '/^[ \t]*#/d;s/^.*release \([0-9]\)[0-9.]* .*/\1/' \
                                               /etc/redhat-release`
        RHEL=yes
        if [ "${rhel_version}" == "6" ]; then
           RHEL6_HOST=yes
        elif [ "${rhel_version}" == "5" ]; then
           RHEL5_HOST=yes
        fi
    fi
elif [ -f /etc/redhat-release ]; then
    citrix_release=`sed -n '/^[ \t]*#/d;s/XenServer release/&/p' \
                                              /etc/redhat-release`
    if [ -n "$citrix_release" ]
    then
        CXS6_HOST=yes
    else
        rhel_version=`sed '/^[ \t]*#/d;s/^.*release \([0-9]\)[0-9.]* .*/\1/' \
                                            /etc/redhat-release`
        RHEL=yes

        if [ "${rhel_version}" == "5" ]; then
            RHEL5_HOST=yes
        elif [ "${rhel_version}" == "6" ]; then
            RHEL6_HOST=yes
        elif [ "${rhel_version}" == "7" ]; then
            RHEL7_HOST=yes
        fi
    fi
fi

if  [ "$RHEL5_HOST" == "yes"  -o  "$SLES10_HOST" == "yes" \
      -o "$OL55_HOST" == "yes" ]; then
        RESET_DM_DEPS=no
fi

if [ "`iscsiadm --mode node 2> /dev/null`" == "" ]; then
    SW_ISCSI_PROTOCOL_ACTIVE=no
else
    SW_ISCSI_PROTOCOL_ACTIVE=yes
fi

if [ "$RHEL7_HOST" == "yes" -o "$OL7_HOST" == "yes" -o "$SLES12_HOST" == "yes" ]; then
    if [ -f /usr/lib/systemd/system/iscsid.service -a \
        -f /usr/lib/systemd/system/fcoe.service ]; then
        SW_PROTOCOL=both
    elif [ -f /usr/lib/systemd/system/iscsid.service ]; then
        SW_PROTOCOL=iscsi
    elif [ -f /usr/lib/systemd/system/fcoe.service ]; then
        SW_PROTOCOL=fcoe
    fi
else
    if [ -f /etc/init.d/fcoe -o -f /etc/init.d/boot.fcoe -o \
        -f /etc/init.d/iscsi -o -f /etc/init.d/open-iscsi ]; then
        SW_PROTOCOL=fcoe
        if [ -f /etc/init.d/iscsi -o -f /etc/init.d/open-iscsi ]; then
            SW_PROTOCOL=iscsi
            if [ -f /etc/init.d/fcoe -o -f /etc/init.d/boot.fcoe ]; then
                SW_PROTOCOL=both
            fi
        fi
    fi
fi

case "$1" in
start)

    if [ -d /var/lock/subsys ] ; then
        touch /var/lock/subsys/PowerPathPost
    fi

    fcoe_ports_found="no"
    if [ "$SW_PROTOCOL" == "iscsi" -o "$SW_PROTOCOL" == "fcoe" -o \
        "$SW_PROTOCOL" == "both" ]; then
               
        while true
        do
            #FCoE
            if [ "$SW_PROTOCOL" == "fcoe" -o "$SW_PROTOCOL" == "both" ];then
                #
                # Wait for upto 30 seconds for atleast some of
                # the FCoE ports to be initialized
                #
                for (( i=0; i<6; i++ ))
                    do
                    fcoe_ports=`/usr/sbin/fcoeadm -i | \
                           grep "Symbolic Name" | wc -l`
                    if [ ${fcoe_ports} -gt 0 ]; then
                        fcoe_ports_found="yes"
                        break;
                    fi
                    sleep 5
                done

                if [ ${fcoe_ports_found} == "yes" ]; then
                    sleep 10
                    #
                    # Wait for upto 150 seconds for all the FCoE ports to
                    # become online
                    #
                    for (( i=0; i<30; i++ ))
                        do
                        fcoe_ports=`/usr/sbin/fcoeadm -i | \
                                          grep "Symbolic Name" | wc -l`
                        fcoe_ports_online=`/usr/sbin/fcoeadm -i | \
                                         grep State | grep Online | wc -l`
                        if [ ${fcoe_ports_online} -gt 0 ]; then
                            SW_FCOE_PROTOCOL_ACTIVE=yes
                        fi
                        if [ ${fcoe_ports} -eq ${fcoe_ports_online} ]; then
                             break;
                        fi
                        sleep 5
                    done
                else
                    if [ "$SW_PROTOCOL" != "both" ]; then
                        break;
                    fi
                fi
            fi

            if [ "$SW_FCOE_PROTOCOL_ACTIVE" == "yes" -o "$SW_ISCSI_PROTOCOL_ACTIVE" == "yes" ]; then
                if [ -x /sbin/udevadm ]; then
                    /sbin/udevadm settle --timeout=180
                else
                    if [ -x /sbin/udevsettle ]; then
                        /sbin/udevsettle --timeout=180
                    else
                        if [ -e /etc/SuSE-release ]; then
                            if [ -s /var/run/fcoemon.pid -o \
                                    -s /var/run/iscsid.pid ]; then
                                sleep 10
                                for (( cnt=0; $cnt < 360; cnt++ )); do
                                     hw=`ps ax | grep -w hwscan | \
                                                grep -v grep | wc -l`
                                     if [ $hw -eq 0 ]; then
                                         sleep 6
                                         hw=`ps ax | grep -w hwscan | \
                                                 grep -v grep | wc -l`
                                         if [ $hw -eq 0 ]; then
                                             break;
                                         fi
                                     fi
                                     sleep 10
                                done
                            fi
                        fi
                    fi
                fi

                for ((i=0; (i<30);i++)); do
                    sdfound=1
                    for sd in `/bin/ls -d /sys/block/sd* 2> /dev/null`; do
                        bsd=`basename $sd`
                        if [ ! -e /dev/$bsd ]; then
                            sdfound=0
                            sleep 2
                            break;
                        fi
                    done
                    if [ "$sdfound" -eq 1 ]; then
                        break
                    fi
                done

                /sbin/powermt config
                #  Wait for udev to finish creating emcpower devices
                pdfound=1
                for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do
                pdfound=1
                    for pd in `/bin/ls -d /sys/block/emcpower* \
                               2> /dev/null`; do
                        bpd=`basename $pd`
                        if [ ! -e /dev/$bpd ]; then
                            pdfound=0
                            sleep 2
                            break;
                        fi
                    done
                    if [ "$pdfound" -eq 1 ]; then
                        break;
                    fi
                done
                /sbin/powermt load > /dev/null 2>&1
                break;
            else
                break;
            fi  # "$SW_FCOE_PROTOCOL_ACTIVE" == "yes" -o "$SW_ISCSI_PROTOCOL_ACTIVE" == "yes"
        done  #while true

        pp_start_lvm;

        # Device mapper devices may have been started with
        # dependencies on native devices.  Reset the
        # dependencies to emcpower devices
        if [ "$RESET_DM_DEPS" == "yes" ]; then
            /etc/opt/emcpower/pp_reset_dm_deps.sh
        fi

        if [ "$SW_FCOE_PROTOCOL_ACTIVE" == "yes" -o "$SW_ISCSI_PROTOCOL_ACTIVE" == "yes" ]; then
            # Run "mount -a" to mount all mount points in /etc/fstab
            mount -a 2>/dev/null
        fi
    fi    
    if [ "$RHEL6_HOST" == "yes" -o "$OL6_HOST" == "yes"  -o \
         "$OL7_HOST" == "yes" -o "$RHEL7_HOST" == "yes" ]; then
         check_and_fix_kdump_initrd_img;
    fi                  
    /sbin/powermt save >  /dev/null 2>&1
   
    # PPME
    if [ -f /sbin/powermig ] ; then
        /sbin/powermig recover -all -no > /dev/null 2>&1
        /sbin/powermig info -all -query > /dev/null 2>&1
        /sbin/powermig options -startupInProgress 0 > /dev/null 2>&1
    fi
    #
    # Set the IO scheduler for XtremIO devices by running the script
    # pp_udev_xio.sh. If the host is booting, scheduler is reset in
    # PowerPathPost script. After the pp_udev_xio.sh executes, a marker
    # file .pp_config_complete is created. The code block is added to
    # ensure that there are no udev storms in case of max config setup.
    # Once host boots up, the udev rule 80-udev-pp-xio.rules will be 
    # triggered for new XIO devices being added and for devices which
    # transition from dead to alive.
    #
    /lib/udev/pp_udev_xio.sh all  > /dev/null 2>&1
    touch $pp_config_complete
    cp /etc/opt/emcpower/80-udev-pp-xio.rules \
       /etc/udev/rules.d/80-udev-pp-xio.rules > /dev/null 2>&1
    if [ -x /sbin/udevadm ]; then
        /sbin/udevadm control --reload-rules
    else
        # rhel5 supports udevcontrol command
        if [ -x /sbin/udevcontrol ]; then
            /sbin/udevcontrol reload_rules
        fi
    fi
    ;;
stop)

    #  
    # The marker file should be removed during host reboot. Hence removing
    # marker file here.
    #
    if [ -f $pp_config_complete ]; then
        rm -f $pp_config_complete
    fi
    #
    # Delete the udev rule in the directory /etc/udev/rules.d/
    # to avoid triggering udev rules when PowerPath is not
    # active.
    #
    rm -f /etc/udev/rules.d/80-udev-pp-xio.rules

    if [ "$RHEL" == "yes" -o "$OL" == "yes" ] ; then
        #
        # Patch system files to start PowerPath automatically on boot
        # in case they were overwritten during a kernel upgrade.
        #
        ETCFILE=/etc/rc.d/rc.sysinit
        grep BEGINPP $ETCFILE  >> /dev/null 2>>/dev/null
        if [ $? -eq 1 ]; then
            sed --in-place -e '/remount,rw \//a \
            ###BEGINPP\
            # Configure and initialize PowerPath.\
            if [ -f /etc/init.d/PowerPath ]; then\
                /etc/init.d/PowerPath start\
            fi\
            ###ENDPP' $ETCFILE
            echo "rc.sysinit does not have a patch to start PowerPath.\
                    Inserting the patch to start PowerPath."
        fi
    fi

    #PPME 
    # The existence of /etc/emc/ppme ensures that PP has been
    # started, otherwise the umd open will fail (opt 400490).
    if [ -d /etc/emc/ppme ]; then
    
        #PPME recovery all
        if [ "$RHEL" == "yes" -o "$OL" == "yes" ] ; then
            /sbin/powermig recover -all -no > /dev/null 2>&1
        fi

        /sbin/powermig transition -no > /dev/null 2>&1
    fi

    #Fcoe
    if [ "$SW_PROTOCOL" == "fcoe" -o "$SW_PROTOCOL" == "both" ]; then
 
        #
        # Umount all mount points associated with each PP FCoE device.
        #
        umount_pp_all FCoE
  
        #
        # Check existing mounted file systems to see if there
        # are device-mapper devices mounted by UUID which use
        # EMC PowerPath pseudo devices.
        #
        umount_if_mounted_by_uuid FCoE

        hbano=`echo "$powermt_display" | \
                       grep FCoE | awk '{ print $1 }'`
        for i in $hbano; do
            powermt remove force hba=$i
        done
    fi

    #iSCSI
    if [ "$SW_PROTOCOL" == "iscsi" -o "$SW_PROTOCOL" == "both" ]; then

        #
        # Umount all mount points associated with each PP iSCSI device. 
        #
        umount_pp_all iSCSI
                            
        #
        # Check existing mounted file systems to see if there
        # are device-mapper devices mounted by UUID which use 
        # EMC PowerPath pseudo devices.
        #
        umount_if_mounted_by_uuid iSCSI
        
        hbano=`echo "$powermt_display" | \
                       grep iSCSI | awk '{ print $1 }'`
        
        for i in $hbano; do
            powermt remove force hba=$i
        done
    fi

    if [ -e /var/lock/subsys/PowerPathPost ]; then
        rm /var/lock/subsys/PowerPathPost
    fi
    ;;
    *)
    ;;
esac





