#!/bin/sh
#
# ###################################################################
# Copyright (c) 2002-2016, EMC Corporation. All Rights Reserved.
#
# /etc/init.d/PowerPath
# This file is a part of EMC PowerPath.
#
# description: Script to load and configure EMC PowerPath drivers.
#
# ###################################################################
#


#
# Functions to preserve exceptional command status return values
#

rc_reset()
{
    rc_cmd_stat=0
    rc_script_stat=0
}

rc_check()
{
    rc_cmd_stat=$?
    test $rc_cmd_stat -ne 0 && rc_script_stat=$rc_cmd_stat
    return $rc_cmd_stat
}

rc_status()
{
    rc_check

    if [ "$1" = "-v" ]
        then
        case "$rc_script_stat" in
            0)
              echo "`eval_gettext \" done\"`"
              ;;
            *)
              echo "`eval_gettext \" failed\"`"
              ;;
        esac
    fi
    return $rc_script_stat
}

rc_exit()
{
    exit $rc_script_stat
}

#
# PowerPath.common
#

# 
# Log start details if tracing is enabled.
#
trace_base=/etc/opt/emcpower
if [ -f $trace_base/PP_TRACE_START -a -z "$PP_RESTART" ]
then
    PP_RESTART=T;export PP_RESTART
    echo '===========' >> $trace_base/PPStartTrace
    /bin/date          >> $trace_base/PPStartTrace
    echo $0 $@         >> $trace_base/PPStartTrace
    echo '===========' >> $trace_base/PPStartTrace
    
    sh -x $0 $@  2>> $trace_base/PPStartTrace
    exit
fi

#---------------------------------------
# Begin declaration of global variables.
#---------------------------------------
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/etc/opt/emcpower/bin
export PATH

PPBASE="/etc/opt/emcpower/EMCpower.LINUX-6.3.0.01.00"

SLES12_HOST=no
SLES11_HOST=no
SLES10_HOST=no
RHEL5_HOST=no
RHEL55_HOST=no
OL55_HOST=no
PPME_SUPPORT=yes
SYSTEMD_SUPPORT=no

# 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


TMP_MODP=/tmp/modprobe.conf.pp
PP_MODP=/etc/modprobe.conf.pp


# Citrix XenServer doesn't have seperate /etc/<ditro>-release file.
# Use redhat release file to identify Citrix distro.
if [ -f /etc/redhat-release ]; then
   citrix_release=`sed -n '/^[ \t]*#/d;s/XenServer release/&/p' \
            /etc/redhat-release`
fi

if [ -f /etc/SuSE-release ]; then
    host_os=`awk '/^VERSION/ {version=$3}
                  END {print version}' /etc/SuSE-release`

    if [ "${host_os}" == "12" ]; then
        SLES12_HOST=yes
        TMP_MODP=/tmp/powerpath.conf
        PP_MODP=/etc/modprobe.d/powerpath.conf
    elif [ "${host_os}" == "11" ]; then
        SLES11_HOST=yes
    elif [ "${host_os}" == "10" ]; then
        SLES10_HOST=yes
    fi
elif [ -f /etc/ovs-release ]; then
    ovs_version=`sed '/^[ \t]*#/d;s/^.*release \([0-9]\)[0-9.]*/\1/' \
                                        /etc/ovs-release`
    if [ "${ovs_version}" == "3" ]; then
        PPME_SUPPORT=no
    fi
    if [ -d /etc/modprobe.d ]; then
        TMP_MODP=/tmp/powerpath.conf
        PP_MODP=/etc/modprobe.d/powerpath.conf
    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`
        

        if [ "${oracle_version}" == "5" ]; then
          if [ -n "${oracle_update}" ] && [ ${oracle_update} -le 5 ]; then
             OL55_HOST=yes
          fi
        elif [ "${oracle_version}" == "6" -o "${oracle_version}" == "7" ]; then
           TMP_MODP=/tmp/powerpath.conf
           PP_MODP=/etc/modprobe.d/powerpath.conf
        fi
    else
        rhel_version=`sed '/^[ \t]*#/d;s/^.*release \([0-9]\)[0-9.]* .*/\1/' \
                                            /etc/redhat-release`

        rhel_update=`sed '/^[ \t]*#/d;s/^.*release [0-9]\([.0-9]\)* .*/\1/' \
                                           /etc/redhat-release`
        if [ "${rhel_version}" == "6" -o "${rhel_version}" == "7" ]; then
           TMP_MODP=/tmp/powerpath.conf
           PP_MODP=/etc/modprobe.d/powerpath.conf
        elif [ "${rhel_version}" == "5" ]; then
           RHEL5_HOST=yes
           if [ -n "${rhel_update}" ] && [ ${rhel_update} -ge 5 ]; then
              RHEL55_HOST=yes
           fi
         fi
    fi
elif [ -n "$citrix_release" ]; then
    PPME_SUPPORT=no
    TMP_MODP=/tmp/powerpath.conf
    PP_MODP=/etc/modprobe.d/powerpath.conf
elif [ -f /etc/redhat-release ]; then
    rhel_version=`sed '/^[ \t]*#/d;s/^.*release \([0-9]\)[0-9.]* .*/\1/' \
                                        /etc/redhat-release`

    rhel_update=`sed '/^[ \t]*#/d;s/^.*release [0-9]\([.0-9]\)* .*/\1/' \
                                        /etc/redhat-release`

    if [ "${rhel_version}" == "6" -o "${rhel_version}" == "7" ]; then
        TMP_MODP=/tmp/powerpath.conf
        PP_MODP=/etc/modprobe.d/powerpath.conf
    elif [ "${rhel_version}" == "5" ]; then
        RHEL5_HOST=yes 
        if [ -n "${rhel_update}" ] && [ ${rhel_update} -ge 5 ]; then
            RHEL55_HOST=yes
        fi
    fi
fi
if  [ "$RHEL5_HOST" == "yes"  -o  "$SLES10_HOST" == "yes" \
      -o "$OL55_HOST" == "yes" ]; then
        RESET_DM_DEPS=no
fi

if [ `uname -m` == "s390x" ]; then
    PPME_SUPPORT=no
fi

if [ "${rhel_version}" == "7" -o "${oracle_version}" == "7" -o "${SLES12_HOST}" == "yes" ]; then
    SYSTEMD_SUPPORT=yes
fi
#-------------------------------------
# End declaration of global variables.
#-------------------------------------

#-------------------------------------------
# Begin internationalization specific lines.
#-------------------------------------------
type -P gettext.sh > /dev/null 2>&1 && \
    type -p gettext >/dev/null  2>&1

if [ $? -ne 0 ]; then
    gettext()
    {
        echo "$*"
    }

    eval_gettext()
    {
        eval "echo \"$*\""
    }
else
    . gettext.sh
    TEXTDOMAIN=PowerPath
    export TEXTDOMAIN

    TEXTDOMAINDIR=$PPBASE/i18n/catalog
    export TEXTDOMAINDIR
fi
#-----------------------------------------
# End internationalization specific lines.
#-----------------------------------------


#--------------------------------------------------------------
# For logging messages to system log using logger if available.
#--------------------------------------------------------------
pp_logger ()
{
    msg=$*
    type logger > /dev/null 2>&1

    if [ $? -ne 0 ]
    then
        echo ${msg}
    else
        logger ${msg}
    fi
}

#-------------------------------------------------------
# Check if PowerPath start is into system startup logic.
#-------------------------------------------------------
validate_boot_logic()
{
    if [ -f /etc/oracle-release ]
    then
        if [ $(uname -r | grep -c uek) -ne 0 ]
        then
            VENDOR=oracle
        else
            VENDOR=redhat
        fi
    elif [ -f /etc/SuSE-release ]
    then
        VENDOR=suse
    elif [ -f /etc/asianux-release ]
    then
        VENDOR=asianux
    elif [ -f /etc/redhat-release ]
    then
        if [ -n "$citrix_release" ]
        then
            VENDOR=citrix
        else
            VENDOR=redhat
        fi
    fi

    case "$VENDOR" in
        suse)
            MDFILE=/etc/init.d/boot.md
            grep 'boot\.powerpath' $MDFILE >> /dev/null 2>>/dev/null 
            if [ $? -eq 1 ]; then
                string=`grep "# Required-Start:" $MDFILE`
                sed --in-place -e "s/$string/$string boot.powerpath/" $MDFILE
                if [ -x /sbin/insserv ]; then
                    /sbin/insserv
                fi

                pp_logger boot.md does not have a code to start PowerPath. Inserting the code to start PowerPath.
                echo "`eval_gettext \"boot.md does not have a code to start PowerPath. Inserting the code to start PowerPath.\"`"
            fi
        ;;

        oracle|citrix|redhat|asianux)
            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

                pp_logger rc.sysinit does not have a code to start PowerPath. Inserting the code to start PowerPath.
                echo "`eval_gettext \"rc.sysinit does not have a code to start PowerPath. Inserting the code to start PowerPath.\"`"
            fi
        ;;
    esac
}

#-------------------------------------------------------
# PowerPath and DM-Multipath cannot co-exist on the host.
# Do not allow PowerPath to start if DM-Multipath is
# configured and managing devices.
#-------------------------------------------------------

check_dm_multipath_config ()
{
    continue_pp_start=true

    # On SUSE, DM-Multipath will start after PowerPath starts.
    # Hence, if DM-Multipath is configured to start on boot,
    # do not continue PowerPath start.

    if [ -f /etc/SuSE-release ] && [ "$HOST_BOOTED_UP" == "no" ]
    then
        if chkconfig multipathd --list  2>/dev/null \
            | grep  '[35]:on' >/dev/null 2>&1
        then
            continue_pp_start=false
        fi
    elif lsmod | grep -w '^dm_multipath' >/dev/null
    then
        if [ -f /sbin/multipath -a -f /sbin/multipathd ]
        then
            num=`/sbin/multipath -ll -v1 | grep -c '.*'`
            if [ $num -gt 0 ]; then
                continue_pp_start=false
            fi
        fi
    fi

    if [ $continue_pp_start = "false" ]
    then
        echo "`eval_gettext \"Aborting PowerPath start since DM-Multipath is active. Refer to PowerPath for Linux Installation and Administration Guide for more information \"`"
        pp_logger Aborting PowerPath start since DM-Multipath is active. Refer to PowerPath for Linux Installation and Administration Guide for more information 
    fi

    /bin/$continue_pp_start
}

#-----------------------------------------------------------
# Deactivate LVM volume groups present on PowerPath devices.
#-----------------------------------------------------------
pp_stop_lvm()
{
    if test -x /sbin/vgchange -a -x /sbin/vgscan ; then
        open_vol=""
        vgname=`vgdisplay 2>/dev/null | grep "VG Name" \
                | sed -e 's/VG Name/ /g'`

        for i in $vgname
        do
            pv_names=`vgdisplay -v $i 2>/dev/null \
                      | grep "PV Name" | awk '{ print $3 }'`

            #
            # Extract the device name of the first pv only
            # omitting any partition number.
            #
            first_pv=`echo $pv_names \
                      | sed -e 's/^\/dev\/\([a-zA-Z]*\)[0-9]*.*$/\1/'`

            #
            # Find the pseudo device.
            #
            pseudo=`powermt display dev=$first_pv 2>/dev/null \
                    | grep "Pseudo name" | sed -e 's/Pseudo name=//'`

            if [ "$pseudo" != "" ] ; then
                err_stop=`vgchange -a n $i 2>&1 | grep "open logical volume"`
                if [ "$err_stop" != "" ]; then
                    if [ "$open_vol" = "" ]; then
                        mesg="Following LVM volume groups are in use:"
                        echo "`eval_gettext \"$mesg\"`"
                        open_vol=$err_stop
                    fi
                    var1=$i
                    var2=`echo $pv_names`
                    mesg="      Volume Group:"
                    echo "`eval_gettext \"$mesg \\$var1 (\\$var2)\"`"
                fi
            fi
        done
    fi

    if [ "$open_vol" != "" ]; then
        echo
        mesg1="These open logical volume devices use LUNs"
        mesg2="from PowerPath managed devices,"
        echo "`eval_gettext \"$mesg1 $mesg2\"`"
        mesg1="Please reissue the command after"
        mesg2="closing these volumes."
        echo "`eval_gettext \"$mesg1 $mesg2\"`"
        return 1
    fi
    return 0
}

#----------------------------------------------------------------------
# Check if auto_activation_volume_list is enabled in /etc/lvm/lvm.conf.
#----------------------------------------------------------------------
check_if_auto_activation_volume_list_is_enabled()
{
    auto_activation_volume_list=`/bin/sed -e 's/^[ \t]*//' /etc/lvm/lvm.conf \
        | grep "^auto_activation_volume_list" | grep \"`
    if [ "$auto_activation_volume_list" != "" ]; then
        return 0
    else
        return 1
    fi
}

#--------------------------
# Active LVM volume groups.
#--------------------------
pp_start_lvm()
{
    # In SLES12 SP2, as the lvmetad.service starts before PowerPath and
    # PowerPathPost, the logical volumes on pseudo devices will not be
    # added to the lvm cache.  Hence, call pvscan --cache to update.
    /sbin/pvscan --cache > /dev/null 2>&1

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

        check_if_auto_activation_volume_list_is_enabled
        if [ $? -eq 0 ]; then
            /sbin/vgchange -a ay > /dev/null 2>&1
        else
            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
    fi
}

#-------------------------------------
# Wait for /dev/emcpower to appear.
# $1 is the number of seconds to wait.
#-------------------------------------
dev_emcpower_is_back()
{
    /bin/bash -c "for ((cnt=$1/3; \$cnt > 0; cnt--)); do \
                      test -c /dev/emcpower && break; \
                      sleep 3; \
                  done; \
                  test -c /dev/emcpower"
}

#-------------------------------------------------------------------------
# Load saved driver state.  If powermt load fails because of dead paths or
# changed native devices, then dont fail PP start.
#-------------------------------------------------------------------------
load_saved_settings()
{
    pmtload=`/sbin/powermt load 2>&1`
    pmtload_ret=$?

    if [ $pmtload_ret -ne 0 ]; then
        echo $pmtload |  grep "Error occurred loading saved driver state" |
                         grep "Loading continues" > /dev/null 2>&1
        if [ $? -eq 0 ]; then
            /bin/true
        else
            return $pmtload_ret
        fi
    fi
}

#--------------------------
# Configure PowerPath paths
#--------------------------
configure_pp()
{
    # In BFS setups on newer kernels, when PowerPath starts from initrd image
    # only the array class of the root device will be managed and all other
    # classes will be unmanaged.  Call powermt init boot to set the states
    # of the other LAMs now.  In local boot hosts, when base driver is
    # loaded, the manage/unmanage settings of all the LAMs will be sent as
    # kenrel parameters and hence powermt init boot is not really needed.
    # But there is no harm in still calling powermt init boot.
    /sbin/powermt init boot > /dev/null 2>&1

    if [ -f /etc/emcp_devicesDB.dat ]; then 
        if [ -f /etc/emcp_devicesDB.idx ]; then
            /etc/opt/emcpower/emcpmgr map -p > /dev/null 2>&1
            rc_check
        fi
    fi

    #
    # If the system has no license, config error is ok.
    #
    if /sbin/emcpreg -l 2>&1 | grep '^Key' > /dev/null 2>&1; then
        /sbin/powermt config > /dev/null 2>&1
    else
        /sbin/powermt config > /dev/null 2>&1
        /bin/true
    fi
    rc_check

    #
    # Wait for udev events to settle.If the hald
    # daemon is running,then udev events may take
    # more time to settle especially if there are
    # a large number of devices present.
    #
    if [ -x /sbin/udevadm ]; then
        /sbin/udevadm settle --timeout=2700
    else
        if [ -x /sbin/udevsettle ]; then
            /sbin/udevsettle --timeout=2700
        fi
    fi

    #
    # 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

    if [ "$pdfound" -eq 0 ]; then
        [ 1 = 0 ]
        rc_check
    fi

    #
    # load the saved settings('powermt load') when
    # the PowerPath persistent device DB file exists.
    #  
    if [ -f /etc/powermt_custom.xml -o -f /etc/powermt.custom ]; then
        load_saved_settings
        rc_check
        if [ -f /etc/powermt.custom ] ; then
          rm -f /etc/powermt.custom
        fi
        if [ -f /etc/powermt.custom.lock ] ; then
          rm -f /etc/powermt.custom.lock
        fi
    fi

    /etc/opt/emcpower/emcpmgr map > /dev/null 2>&1
    rc_check

    /etc/opt/emcpower/powercf -C > /dev/null 2>&1
    rc_check

    if [ ! -e /etc/powermt_custom.xml ]; then 
        /sbin/powermt save > /dev/null 2>&1
        rc_check
    fi 

    if [ "$PPME_SUPPORT" == "yes" ]; then
        # Run powermig in case 1st open of umd fails from system crash.
        # This will cause next powermig to open umd, recover, and
        # continue successfully.
        /sbin/powermig info -all -count > /dev/null 2>&1

        if ! /sbin/powermig info -all -count | grep -q "No migrations found"
        then
            /sbin/powermig options -startupInProgress 1  > /dev/null 2>&1
        fi 
        /sbin/powermig transition -startup -noprompt   > /dev/null 2>&1

        # Don't fail PowerPath start even if the above powermig command
        # returns an error.
        /bin/true
    fi

    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
}

####
#   import common script routines here.
#   DO NOT REMOVE THE FOLLOWING LINE
###############################################################################
# Start of common.sh - Common script shared by PowerPath.common and
#                      EMCpower.LINUX.spec. 
##############################################################################

# Stop stordaemons. They will get restarted automatically.
pp_stop_stordaemons()
{
    if [ -x /usr/storapi/storbin/stordaemon ]; then
        /usr/storapi/storbin/stordaemon shutdown all > /dev/null 2>&1
    elif [ -x /usr/storapi/bin/stordaemon ]; then
            /usr/storapi/bin/stordaemon shutdown all > /dev/null 2>&1
    fi
}

# Check if Naviagent/Hostagent is running.
# Returns 0 if Naviagent/Hostagent is not running
#         1 if Naviagent is running
#         2 if Hostagent is running
is_navi_running()
{
    ps -C naviagent >> /dev/null 2>&1
    if [ $? -eq 0 ]; then
        return 1
    else
        # Check for Hostagent which is Unisphere equivalent of Naviagent.
        ps -C hostagent >> /dev/null 2>&1
        if [ $? -eq 0 ]; then
           return 2
        fi
    fi
    return 0  # Naviagent/Hostagent is not running.
}

# Return script to start/stop Naviagent/Hostagent. Be sure to match $1 with
# the return values from is_navi_running from above.
get_navi_script_loc()
{
  if [ $1 -eq 1 ]; then
      echo "/etc/init.d/naviagent"
  elif [ $1 -eq 2 ]; then
      echo "/etc/init.d/hostagent"
  else
      echo "" # Return an empty string if Naviagent/Hostagent is not running.
  fi
}

#
# Stop Naviagent/Hostagent and wait for upto 60 seconds for it to stop completely
# Returns 0 if Naviagent/Hostagent has been stop successfully
#         1 if failed to stop Naviagent/Hostagent
#
stop_navi()
{
    $1 stop > /dev/null 2>&1
    for cnt in 1 2 3 4 5 6 7 8 9 10 11 12
    do
    is_navi_running
    if [ $? -ne 0 ]; then
        sleep 5
    else
        return 0
    fi
    done

    return 1
}

restart_navi()
{
    #
    ## Restart Naviagent/Hostagent if we stopped it earlier.
    ## $1 not equal to 0 means Naviagent/Hostagent was running earlier and we stopped it.
    #
    if [ $1 -ne 0 ]; then
        $2 start > /dev/null 2>&1
        is_navi_running
        if [ $? -eq 0 ]; then
            echo "`eval_gettext \"Unable to restart Naviagent/Hostagent.  Please restart it manually.\"`"
        fi
    fi
}

# Check if Naviagent/Hostagent is running. Variable g_naviloc
# returns the Naviagent or Hostagent script for stopping and
# restarting the agent. Agent will be stopped and restarted
# in upgrade, uninstall, and "PowerPath stop".
#
is_navi_running
g_navi_running=$?
g_naviloc=`get_navi_script_loc $g_navi_running`

####

#-----------------------------------------------------------
# Check for Naviagent/Hostagent and powermt before stopping PowerPath.
#-----------------------------------------------------------
ok_to_stop()
{

    ps -C powermt >> /dev/null 2>&1
    if [ $? -eq 0 ]; then
        echo "`eval_gettext \"The powermt command is running.\"`"
        echo "`eval_gettext \"Please stop powermt and then reissue \\$script_name stop.\"`"
        return 1
    fi

    if [ "$PPME_SUPPORT" == "yes" ]; then
        mig_info=`/sbin/powermig info -all`

        if [ $? -eq 0 ]; then
            echo "$mig_info" | grep 'No migrations found' > /dev/null
            if test $? -ne 0 ; then 
                echo "`eval_gettext \"PowerPath migrations are present.\"`"
                echo "`eval_gettext \"Please cleanup the migrations and then reissue \\$script_name stop.\"`"
                return 1
            fi
        fi
    fi

    #
    ## Stop Navigent/Hostagent if it is running before stopping PP 
    #
    if [ $g_navi_running -ne 0 ]; then
        stop_navi $g_naviloc
        if [ $? -ne 0 ]; then
            echo "`eval_gettext \"Failed to stop Naviagent/Hostagent.\"`"
            echo "`eval_gettext \"Please stop Naviagent/Hostagent and then reissue \\$script_name stop.\"`" 
            return 1
        fi
    fi

    pp_stop_stordaemons

    if pp_stop_lvm ; then
        return 0
    fi


    #
    ## Restart Naviagent/Hostagent if we stopped it earlier
    #
    if [ $g_navi_running -ne 0 ]; then
        restart_navi $g_navi_running $g_naviloc
    fi

    return 1
}

#----------------------------------------------------------
# Load the extensions.  Dependencies will load base driver.
# Supply necessary module parameters and kernel metadata.
#----------------------------------------------------------
load_drivers()
{
    #
    # Wait for all the udev events to settle
    #
    if [ "$SLES11_HOST" == "yes" -o "$SLES12_HOST" == "yes" ]; then
        if [ -x /sbin/udevadm ]; then
            /sbin/udevadm settle --timeout=900
        fi
    fi

    klh=`fgrep -w kallsyms_lookup_name /proc/kallsyms | awk '{ print $1 }'`

    if [ "$RHEL55_HOST" == "yes" ]; then
        failfastflag="osfailfast=0x1"
    else
        failfastflag="osfailfast=0x0"
    fi

    emcpo=`cat $PP_MODP  | grep -w "options emcp"`

    touch $TMP_MODP > /dev/null 2>&1
    if ! [ -w $TMP_MODP ] ; then
        modp_path=$(echo $TMP_MODP | sed 's#/[^/]*$##')
        echo "`eval_gettext \"\\$modp_path is not accessible for creating \
temporary files.\"`"
        echo "`eval_gettext \"Please make sure \\$modp_path is available \
for read write operations and try again.\"`"
        /bin/false
        return
    fi

    cat $PP_MODP | grep -v -w "options emcp" > $TMP_MODP

    if [ "$emcpo" == "" ]; then
        #
        # No entry yet in /etc/modprobe.d/powerpath.conf
        #
        echo "options emcp emcplu=0x$klh $failfastflag" >> $TMP_MODP
    else
        #
        # Some emcp options entry found
        #
        oline=`cat $PP_MODP | grep -w "options emcp"\
          | sed -s 's/emcplu=0x[a-f0-9]*//' \
          | sed -s 's/osfailfast=0x[0-9]*//'` 
        echo "$oline emcplu=0x${klh} $failfastflag" >> $TMP_MODP
    fi
    mv -f $TMP_MODP $PP_MODP

    #
    # Call depmod to generate modules.dep before drivers are loaded.
    # This used to be done by "powercf -m", which is called so often, it
    # resulted in many unnecessary depmod calls. 
    #
    /sbin/depmod -a > /dev/null 2>&1

    for d in `cat $ext; cat $mgr`
    do
        /sbin/modprobe -q -s --first-time $d
        rc_check
        if [ $? -ne 0 ] ; then
            echo "`eval_gettext \"PowerPath could not load module \\$d\"`"
            break
        fi
    done

    if dev_emcpower_is_back 60; then
        recreate_emcpower_device
        /etc/opt/emcpower/powercf -K        > /dev/null 2>&1
        rc_check
    else
        rc_check
        unload_drivers
    fi

    if rc_status ; then
        /sbin/modprobe -q -s --first-time emcpioc
        rc_check
        if [ $? -eq 0 ] ; then
            /sbin/modprobe -r -q -s --first-time emcpioc
        fi
    fi
}

#-------------------------------
# Unload drivers and extensions.
#-------------------------------
unload_drivers()
{
    #
    # Call depmod to generate the module dependencies before the unload.
    #
    /sbin/depmod -a > /dev/null 2>&1

    do_unload=0
    retry=4
    powermt prep_drv_unload > /dev/null 2>&1

    #
    # At this point, we really want the driver unload to succeed.
    # Try hard to make it happen.
    #
    while [ $retry -gt 0 ]
    do
        powermt remove dev=all 2> /var/tmp/.pp_exit
        devr=$?
        cat /var/tmp/.pp_exit | grep "not found" > /dev/null 2>&1
        cdevs=$?
        zerolen=`grep -c '.*' /var/tmp/.pp_exit`
        rm /var/tmp/.pp_exit

        #
        # Reset rc status since we are not willing to
        # report an error just yet.
        #
        rc_reset
        if [ $devr -eq 0 ] ; then
            do_unload=1
            retry=0 #success
        else
            if [ "$cdevs" -eq 0 -o "$zerolen" -eq 0 ] ; then
                do_unload=1
                retry=0 #success
            else
                sleep 5
                retry=$((retry - 1))
            fi
        fi
    done
    
    if [ $do_unload -eq 1 ] ; then
        /sbin/modprobe -r -q -s  `cat $mgr; cat $ext; echo emcp`
        uerr=$?
        return $uerr
    else
        echo "`eval_gettext \"PowerPath devices are open.\"`"
        echo "`eval_gettext \"Please close these devices and then try again.\"`"
        /bin/false
        uerr=$?
        return $uerr
    fi
}

#-----------------------------------------------------
# Create /dev/emcpower based on CURRENT misc driver.
# These values can be different when PP loads from RD.
#-----------------------------------------------------
recreate_emcpower_device()
{
    rm -f /dev/emcpower
    mmaj=`cat /sys/class/misc/emcpower/dev | gawk -F : '{print $1}'`
    mmin=`cat /sys/class/misc/emcpower/dev | gawk -F : '{print $2}'`
    mknod /dev/emcpower c $mmaj $mmin > /dev/null

    if [ $? -ne 0 ]; then
        echo "`eval_gettext \"failed to create emcpower device\"`"
    fi

}

###
### DO NOT REMOVE THE FOLLOWING LINE
# Redhat x86_64 specific:
#
# Install only if this is a Redhat, x86_64 system
#

EXPECTED_VENDOR=redhat
VENDOR_NAME=RedHat
VENDOR_OS_NAME=RHEL7
EXPECTED_OS_REV=7
VENDOR_NAME="Redhat"
EXPECTED_ISA=x86_64
MIN_UPGRADE_REV=5.0.0

check_vendor_rev()
{
    local rhel_version

    rhel_version=`sed '/^[ \t]*#/d;s/^.*release \([0-9]\)[0-9.]* .*/\1/' \
              /etc/redhat-release`

    test "$1" = "$rhel_version"
}

#-----------------------------------------------------------------------
# identify_platform -
# Determine the set of drivers files compatible with the /lib/modules
# subdirectory passed as an argument and echo the tag for that set of
# drivers to stdout.
#-----------------------------------------------------------------------

identify_platform()
{
    local platform=

    platform=`echo $1 | \
    gawk -v V=rhel7 -v M=_x8664 \
        '/3.10.[0-9.-]+(el7)/      {print V M; exit 0}
          {exit 1}'`

    if [ $? -ne 0 ]
    then
        if [ "$1" = `uname -r` ]; then
            pre_error "This PowerPath package does not support this kernel."
            cleanup_error_exit
        fi
    fi

    test "$platform" && echo $platform
}
###

#-------------------------------------------------------------------
# For BFS setup install PowerPath modules of required kernel version
#-------------------------------------------------------------------
bfs_install_modules()
{
    for os in /lib/modules/*
    do
      case $os in
          /lib/modules/`uname -r`)
              for modpath in $os/powerpath/*
              do
                rm -f $modpath
              done
              ;;
          *)
              rm -rf $os/powerpath
              ;;
      esac
      for modpath in $os/extra/emcp*
      do
        rm -f $modpath
      done
    done

    ## Install new modules.
    TAG=`identify_platform \`uname -r\``
    if [ -z "$TAG" ]; then
        echo "The installed PowerPath package does not support the upgraded kernel."
        exit 1
    fi

    MODULE_BASE=/lib/modules/`uname -r`

    mkdir -p $MODULE_BASE/powerpath 2>/dev/null

    for mod in `ls $PPBASE/bin/driver/$TAG`
    do
      cp -pf $PPBASE/bin/driver/$TAG/$mod $MODULE_BASE/powerpath/$mod
    done

    depmod

    echo `uname -r` > $PPBASE/../.os_version
}

#----------------------------------
# Create marker file and udev rules
#----------------------------------
create_marker_file_and_udev_rule()
{
    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
        #
        # rhel 5 supports udevcontrol command
        #
        if [ -x /sbin/udevcontrol ]; then
            /sbin/udevcontrol reload_rules
        fi
    fi
}

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

rc_reset

#
# Check if the script has been called by ExecStartPost or ExecStopPost
# after a previous error exit.  No need to do anything in that case.
#
if [ "$SYSTEMD_SUPPORT" == "yes" -a -f /tmp/.pp_systemctl_check ]
then
    #
    # Check if the marker file created on a previous error exit is less
    # than 5 seconds old.   In corner cases where after the marker file is
    # created on an error exit, the host crashed or power recycled
    # immediately, the script should not exit here.
    #
    age=$(expr $(date +%s) -  $(date +%s -r "/tmp/.pp_systemctl_check"))
    if [ $age -ge 0 ] && [ $age -lt 5 ] 
    then
        rm -f /tmp/.pp_systemctl_check
        /bin/true
        rc_exit
    else
        rm -f /tmp/.pp_systemctl_check
    fi
fi

#
# Check if host has booted up fully.
#
HOST_BOOTED_UP=no
if [ "$SYSTEMD_SUPPORT" == "yes" ]; then
    target=`systemctl get-default`
    sysinit_status=`systemctl status sysinit.target | grep Active | sed -e 's/[ ]*Active: \([a-z]*\).*/\1/'`
    if [ "$target" != "rescue.target" -o "$target" != "runlevel1.target" ]; then
        if [ "$sysinit_status" == "active" ]; then
            HOST_BOOTED_UP=yes
        fi
    fi
else
    if [ "$RUNLEVEL" != "S" -a "$UPSTART_EVENTS" != "startup" ]; then
        HOST_BOOTED_UP=yes
    fi
fi


#
# Check root authorization only if the host has already booted up.
#
if [ "$HOST_BOOTED_UP" == "yes" ]; then
    ruid=`id -ru`
    euid=`id -u`
    if [ $ruid -ne 0 -o $euid -ne 0 ]; then
        echo
        echo "`eval_gettext \"Insufficient user privilege.\"`"
        echo
        rc_exit
    fi
fi
rc_reset

script_name=PowerPath
ext=$PPBASE/bin/.drivers_ext
mgr=$PPBASE/bin/.drivers_mgr
pp_config_complete="/etc/emc/.pp_config_complete"

case "$1" in
    start)

        echo -n "`eval_gettext \"Starting \\$script_name: \"`"

        #
        # If modules are loaded and if host has booted up fully, exit
        # after printing message when PowerPath start is called.   In case
        # of BFS, where modules are loaded in initrd/initramfs, continue
        # with PowerPath start.
        #
        lsmod | grep -w '^emcp' > /dev/null
        if test $? -eq 0 ; then
            if [ "$HOST_BOOTED_UP" == "yes" ]; then
                echo -e "\n`eval_gettext \"PowerPath is already running.\"`"
                rc_status -v
                rc_exit
            fi
        fi

        #
        # Remove the marker_file 'pp_config_complete' file if it exists. 
        # The marker file should be removed here inorder to handle host 
        # crash/ungraceful shutdown scenarios where 'PowerPathPost stop' 
        # does not get invoked.
        #
        if [ -f $pp_config_complete ]; then
             rm $pp_config_complete
        fi

        check_dm_multipath_config

        if rc_status; then
            #
            # On SuSE, hostname is not available at this point during startup.
            # Also MpxReserveKey is dependent on Hostname.  During boot if
            # hostname is null then all the nodes in the cluster get the same
            # MpxReserveKey.  Hence set the hostname manually before starting 
            # the daemon.
            #
            if [ -f /etc/SuSE-release ] && [ "$HOST_BOOTED_UP" == "no" ]
            then
                cat /etc/HOSTNAME | sed -e 's/\..*//g' > /proc/sys/kernel/hostname
            fi

            #
            # We check if PowerPath start is still in boot logic. 
            #
            validate_boot_logic

            # Call depmod for BFS setup
            lsmod | grep -w '^emcp' > /dev/null

            if [ $? == 0 ] && [ "$HOST_BOOTED_UP" == "no" ]
            then
                depmod
            fi

            #
            # If a new kernel or new PowerPath is installed, stop
            # PowerPath, if running, and fix things up for the
            # new kernel or new PowerPath
            #
            if test -f $PPBASE/../.new_pp_version || \
                     test "`cat $PPBASE/../.os_version`" != `uname -r`
            then
                #
                # Check if it is a BFS host and install modules.  On a BFS
                # host, after a PowerPath upgrade or a kernel upgrade,
                # PowerPath modules would already be loaded from ramdisk
                # image and hence this condition will be met only in those
                # conditions.
                #
                lsmod | grep -w '^emcp' > /dev/null

                if [ $? == 0 ] && [ "$HOST_BOOTED_UP" == "no" ]
                then
                    bfs_install_modules
                else
                    if lsmod | grep -w '^emcp' > /dev/null
                    then
                        if [ -f $PPBASE/../.new_pp_version ]; then
                            echo "`eval_gettext \"PowerPath: Can't start, old modules loaded.\"`"
                        else
                            $0 stop
                        fi
                    fi
    
                    if [ "$PP_RESTART" ]
                    then
                        sh -x $PPBASE/enable
                    else
                        $PPBASE/enable
                    fi
                    rc_check
                fi
            fi
        fi
        
        #
        # Load PP iff it is not already loaded.
        #
        if rc_status ; then
            lsmod | grep -w '^emcp' > /dev/null
            if [ $? -ne 0 ]; then
                load_drivers
                rc_check
            else
                if dev_emcpower_is_back 60; then
                    recreate_emcpower_device
                    /etc/opt/emcpower/powercf -K        > /dev/null 2>&1
                    rc_check
                else
                    rc_check
                    unload_drivers
                fi

                #
                # Remove PIOC if it was loaded during RD boot.
                #
                lsmod | grep emcpioc > /dev/null
                if [ $? -eq 0 ]; then
                    /sbin/rmmod emcpioc > /dev/null
                fi
            fi

            if rc_status ; then
                if dev_emcpower_is_back 60; then

                    #
                    # Configure PowerPath devices.
                    #
                    configure_pp
                    
                    #
                    # Don't start Management Daemon if the host is still
                    # booting up since the network and snmpd service will
                    # not have started. Instead the daemon will be started
                    # when the EmcpMond.service or emcp_mond.sh service
                    # starts and by then, network and the snmpd service, if
                    # configured, should also have started.
                    #
                    if [ "$HOST_BOOTED_UP" == "yes" ]; then
                        if [ "$SYSTEMD_SUPPORT" == "yes" ]; then
                            systemctl start EmcpMond.service
                        else
                            /etc/init.d/emcp_mond.sh start
                        fi
                    fi

                    #
                    # Stop and start naviagent/hostagent so that it can
                    # send registration commands through Powerpath.  If
                    # this is not done, then naviagent may still send
                    # registration commands through sg devices which cannot
                    # be intercepted in PowerPath and which will result in
                    # wiping out PowerPath registration information on the
                    # array.
                    #
                    if [ $g_navi_running -ne 0 ]; then
                        stop_navi $g_naviloc
                        if [ $? -eq 0 ]; then
                            restart_navi $g_navi_running $g_naviloc
                        fi
                    fi
                else
                    rc_check
                    unload_drivers
                fi
            else
               lsmod | grep -w '^emcp' > /dev/null
                   if [ $? -eq 0 ]; then
                       echo "`eval_gettext \"PowerPath: unable to load PowerPath modules.\"`"
                       unload_drivers
                   fi
            fi
        fi
        rc_check
        #     
        # Set the IO scheduler to NOOP for XtremIO devices by running the
        # script pp_udev_xio.sh. If the host is up and running, then the 
        # scheduler is set to NOOP from the PowerPath startup script. If
        # the host is booting, scheduler is set from PowerPathPost script. 
        # After the pp_udev_xio.sh executes, a marker file is created. 
        # The udev rules will be triggered for newly added XIO devices and
        # for devices transitioning from dead to alive only if the marker
        # file exists. 
        #
        # This code block has been added to ensure that there are no udev
        # storms in case of max config setup. The udev rule is copied to
        # /etc/udev/rules.d so that it is triggered for new XIO devices
        # and for devices which transtion from dead to alive.
        # 
        if [ "$HOST_BOOTED_UP" == "yes" ]; then
            /lib/udev/pp_udev_xio.sh all > /dev/null 2>&1
            create_marker_file_and_udev_rule
        fi
        rc_status -v
        ;;

    stop)
        #
        # In case of RHEL7 SLES12 and OL7, systemd will try to stop PowerPath
        # during shutdown which is not desired. So the stop sequence of
        # PowerPath will be terminated in case the host is one of the above
        # mentioned and is shutting down using the basic.target unit which gets
        # deactivated during shutdown.
        #

        if [ "$SYSTEMD_SUPPORT" == "yes" ]; then
            sysinit_status=`systemctl status basic.target | grep Active | sed -e 's/[ ]*Active: \([a-z]*\).*/\1/'`
            if [ "$sysinit_status" != "active" ]; then
                    rc_status -v
                    rc_exit
            fi
        fi

        echo -n "`eval_gettext \"Stopping \\$script_name: \"`"

        lsmod | grep -w '^emcp' > /dev/null
        if test $? -ne 0 ;
        then
            echo "`eval_gettext \"PowerPath is not running\"`"
        else
            #
            # The marker file .pp_config_complete should be deleted when 
            # PowerPath stop is executed.
            #
            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

            #
            # Trespass can happen if new devices are added or some devices 
            # are removed by "powermt remove" before "PowerPath stop". The 
            # reason is that vgscan used by us issues "read" to all block 
            # devices.  We configure all devices to avoid unnecessary
            # trespass.
            #
            /sbin/powermt config > /dev/null 2>&1

            if ok_to_stop; then
                if [ "$SYSTEMD_SUPPORT" == "yes" ]; then
                    systemctl stop EmcpMond.service
                else
                    /etc/init.d/emcp_mond.sh stop
                fi

                mond_pid=`/bin/ps -eu root | awk '{for(;NF!=0;){if(getline<=0) break;
                         i=NF;a=split($i,b,"/");if(b[a]=="emcp_mond"){print $1;}}}'`
                if [ "${mond_pid}" != "" ]; then
                    echo "`eval_gettext \"PowerPath Management daemon could not be stopped.  Please try again.\"`"
                    if [ "$SYSTEMD_SUPPORT" == "yes" ]; then
                        systemctl start EmcpMond.service
                    fi
                    exit 1
                fi

                /sbin/powermt save > /dev/null 2>&1
                rc_check

                #
                # Disable autoconfig so that devices don't get autoconfigured
                # in the brief time window after the devices are removed but
                # before 'powermt prep_drv_unload' is called.  If devices are
                # in use and cannot be removed, then autoconfig will get
                # enabled when 'powermt config' is called.
                #
                /sbin/powermt disable_autoconfig > /dev/null 2>&1

                /etc/opt/emcpower/emcpmgr unmap > /dev/null 2>&1
                rc_check

                #
                # On hosts with large number of devices, sometimes the first
                # powermt remove dev=all command may not remove all the
                # devices properly, even when none of them are in use.
                # This is because of the powermt config done at the start
                # of PowerPath stop.  This config may have triggered 
                # haldaemon which maybe sending some IOs to the native
                # devices and hence all the devices may not be removed.
                # Hence try again for upto 30 seconds.
                #
                if [ -x /sbin/udevadm ]; then
                    /sbin/udevadm settle --timeout=180
                else
                    if [ -x /sbin/udevsettle ]; then
                        /sbin/udevsettle --timeout=180
                    fi
                fi

                #
                # Remove all devices from PowerPath.
                #
                for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do
                    /sbin/powermt remove dev=all 2> /var/tmp/.pp_exit
                    devr=$?
                    cat /var/tmp/.pp_exit | grep "not found" > /dev/null 2>&1
                    cdevs=$?
                    zerolen=`grep -c '.*' /var/tmp/.pp_exit`
                    rm /var/tmp/.pp_exit
                    if [ $devr -eq 0 -o $cdevs -eq 0 -o $zerolen -eq 0 ]
                    then
                        break
                    fi
                    sleep 2
                done

                #
                # Reset rc status since we are not willing to quit.
                #
                rc_reset

                #
                # Proceed if all devices have been removed from PowerPath.
                #
                if [ $devr -eq 0 -o $cdevs -eq 0 -o $zerolen -eq 0 ] ; then

                    unload_drivers
                    rc_check
                    if [ $? -eq 0 ] ; then
                        #
                        # Wait for udev events to settle. If the hald
                        # daemon is running,then udev events may take
                        # more time to settle especially if there are
                        # a large number of devices present.
                        #
                        if [ -x /sbin/udevadm ]; then
                            /sbin/udevadm settle --timeout=2700
                        else
                            if [ -x /sbin/udevsettle ]; then
                                /sbin/udevsettle --timeout=2700
                            fi
                        fi
                    else
                        echo "`eval_gettext \"PowerPath could not unload PowerPath modules.\"`"
                        echo "`eval_gettext \"Please reboot the host and try again.\"`"
                    fi
                else
                    /etc/opt/emcpower/emcpmgr map -p > /dev/null 2>&1

                    /sbin/powermt config > /dev/null 2>&1
                    /sbin/powermt load > /dev/null 2>&1

                    #
                    # Start the PowerPath Management daemon.
                    #
                    if [ "$SYSTEMD_SUPPORT" == "yes" ]; then
                        systemctl start EmcpMond.service
                    else
                        /etc/init.d/emcp_mond.sh start
                    fi

                    create_marker_file_and_udev_rule

                    echo "`eval_gettext \"PowerPath devices are open.\"`"
                    echo "`eval_gettext \"Please close these devices and then reissue \\$script_name stop.\"`"

                    /bin/false
                    rc_check
                fi
                #
                ## Restart Naviagent/Hostagent if we stopped it earlier
                #
                if [ $g_navi_running -ne 0 ]; then
                    restart_navi $g_navi_running $g_naviloc
                fi
            else

                create_marker_file_and_udev_rule

                /bin/false  ## not ok_to_stop
            fi
        fi
        rc_check
        rc_status -v
        ;;
    check_service_start)
        #
        # Check if PowerPath has indeed started properly.  If it has not,
        # then call 'systemctl stop' so that the status of the service is set
        # to 'inactive'.  Otherwise, if a 'systemctl start' is done again, 
        # systemd will not call '/etc/init.d/PowerPath start' as the status
        # of the service is already 'active'.
        #
        if [ "$SYSTEMD_SUPPORT" == "yes" ]; then
            lsmod | grep -w '^emcp' > /dev/null
            if [ $? -ne 0 ]; then
                #
                # Create marker file so that 'systemctl stop' will
                # exit immediately without doing anything.
                # 
                touch /tmp/.pp_systemctl_check

                #
                # The purpose of this command is just to set the status of the
                # service to 'inactive'.
                #
                nohup systemctl stop PowerPath.service &
                sleep 1
            fi
        fi

        /bin/true
        rc_check
        ;;
    check_service_stop)
        #
        # Check if PowerPath has indeed stopped properly.  If it has not,
        # then call 'systemctl start' so that the status of the service is set
        # to 'active'.  Otherwise, if a 'systemctl stop' is done again, 
        # systemd will not call '/etc/init.d/PowerPath stop' as the status
        # of the service is already 'inactive'.
        #
        if [ "$SYSTEMD_SUPPORT" == "yes" ]; then
            lsmod | grep -w '^emcp' > /dev/null
            if [ $? -eq 0 ]; then
                #
                # Create marker file so that 'systemctl start' will
                # exit immediately without doing anything.
                # 
                touch /tmp/.pp_systemctl_check

                #
                # The purpose of this command is just to set the status of the
                # service to 'active'.
                #
                nohup systemctl start PowerPath.service &
                sleep 1
            fi
        fi

        /bin/true
        rc_check
        ;;
    *)
        var=$0
        echo "`eval_gettext \"Usage: \\$var {start|stop}\"`"
        ;;
esac

rc_exit
