#!/bin/sh
#
# ###################################################################
# Copyright (c) 2002-2012, 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-5.7.1.01.00"

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

# 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=`grep "XenServer release" /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}" == "11" ]; then
        SLES11_HOST=yes
    elif [ "${host_os}" == "10" ]; then
        SLES10_HOST=yes
    fi
elif [ -f /etc/ovs-release ]; then
    ovs_version=`sed 's/^.*release \([0-9]\)[0-9.]*/\1/' \
                                        /etc/ovs-release`
    if [ "${ovs_version}" == "3" ]; then
        PPME_SUPPORT=no
    fi
elif [ -f /etc/oracle-release ]; then
    oracle_version=`sed 's/^.*release \([0-9]\)[0-9.]*/\1/' \
                                        /etc/oracle-release`

    oracle_update=`sed '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" ]; then
        TMP_MODP=/tmp/powerpath.conf
        PP_MODP=/etc/modprobe.d/powerpath.conf
    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 's/^.*release \([0-9]\)[0-9.]* .*/\1/' \
                                        /etc/redhat-release`

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

    if [ "${rhel_version}" == "6" ]; 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
#-------------------------------------
# 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

    export RPM_INSTALL_PREFIX
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
        VENDOR=oracle
    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 -a "$RUNLEVEL" = "S" ]
    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
}

#--------------------------
# Active LVM volume groups.
#--------------------------
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
}

#----------------------------------------------------------------
# For encrypted devices, for which keys were not received on time
# because of RKM Client issues, the partition tables may not have
# been updated during powermg config.  Also, in a BFS setup on
# newer kernels, since the devices are already configured in
# initrd/initramfs, the partition tables on any encrypted device
# will also not be updated.  Hence to update the partition table
# for such devices, send BLKRRPART to all the encrypted devices
# for which keys becomes available.
#----------------------------------------------------------------
rescan_partitions_on_encrypted_devs()
{
    . /etc/emc/bin/emcp_setup.sh

    #
    # Wait for upto three minutes for the key states of
    # all the encrypted devices to be updated.
    #
    for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
    do
        /sbin/powervt xcrypt -info -dev all 2>&1 |
            grep -q "no key is available"
        if [ $? -eq 1 ]; then
            break
        fi
        sleep 10
    done

    #
    # For each encrypted device and for which the key is now
    # available, issue a BLKRRPART to update the partition
    # table.
    #
    for dev in `/sbin/powervt xcrypt -info -dev all 2>&1 |
                grep "is encrypted" |
                sed -e 's/\(emcpower.*\) is encrypted.*/\1/'`
    do
        /sbin/powervt xcrypt -info -dev $dev -refresh 2>&1 |
            grep -q "no key is available"
        if [ $? -eq 1 ]; then
            /sbin/blockdev --rereadpt /dev/${dev} > /dev/null 2>&1
        fi
    done
}

#-------------------------------------
# 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()
{
    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 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

    if [ -f /etc/emc/.xcrypt_cfg_done ]; then
        rescan_partitions_on_encrypted_devs 
    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
        echo "`eval_gettext \"Stopping Naviagent/Hostagent...\"`"
        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
        echo "`eval_gettext \"Restarting Naviagent/Hostagent...\"`"
        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" ]; 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"`
    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=`echo $retry | awk '{print $1 - 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

    echo "`eval_gettext \"PowerPath could not unload PowerPath modules.\"`"
    uerr=1
    return $uerr
}

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

}

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

rc_reset

#
# Check root authorization
#
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
rc_reset

script_name=PowerPath
ext=$PPBASE/bin/.drivers_ext
mgr=$PPBASE/bin/.drivers_mgr

case "$1" in
    start)

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

        check_dm_multipath_config

        if rc_status; then
            #
            # On SuSE, hostname is not available at this point during startup
            # and hence xcryptd daemon will fail to start. 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 -a "$RUNLEVEL" = "S" ]
            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

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

            #
            # Start the PowerPath Encryption daemon.
            #
            if [ -f /etc/emc/.xcrypt_cfg_done ]; then
                /etc/emc/bin/xcryptd_adm enable
            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 snmpd service will not have
                    # started. Instead the daemon will be started when
                    # the emcp_mond.sh service starts and by then, the
                    # snmpd service, if configured, should also have
                    # started.
                    #

                    if [ "$RUNLEVEL" != "S" \
                        -a "$UPSTART_EVENTS" != "startup" ]; then
                            /etc/init.d/emcp_mond.sh start
                    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
                echo "`eval_gettext \"PowerPath: unable to load PowerPath modules.\"`"
                unload_drivers
            fi
        fi
        rc_status -v
        ;;

    stop)
        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
            #
            # 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
                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
                    kill -usr1 ${mond_pid}
                fi

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

                /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
                    #
                    # Stop the PowerPath Encryption daemon, if its running.
                    #
                    ps -C emcp_xcryptd > /dev/null 2>&1
                    if [ $? -eq 0 ] ; then
                        /etc/emc/bin/xcryptd_adm disable
                    fi

                    unload_drivers
                    rc_check
                    if [ $? -eq 0 ] ; then
                        #
                        # Remove any stale pseudo device entries in /dev
                        #
                        rm -f /dev/emcpower*
                    else
                        echo "`eval_gettext \"PowerPath could not unload PowerPath modules.\"`"
                    fi
                else
                    echo "`eval_gettext \"PowerPath devices are open.\"`"
                    echo "`eval_gettext \"Please close these devices and then reissue \\$script_name stop.\"`"
                    /etc/opt/emcpower/emcpmgr map -p > /dev/null 2>&1

                    #
                    # Start the PowerPath Encryption daemon.
                    #
                    if [ -f /etc/emc/.xcrypt_cfg_done ]; then
                        /etc/emc/bin/xcryptd_adm enable
                    fi

                    /sbin/powermt config > /dev/null 2>&1
                    /sbin/powermt load > /dev/null 2>&1
                    /bin/false
                fi
                #
                ## Restart Naviagent/Hostagent if we stopped it earlier
                #
                if [ $g_navi_running -ne 0 ]; then
                    echo "`eval_gettext \"Restarting Naviagent/Hostagent.\"`"
                    restart_navi $g_navi_running $g_naviloc
                fi
            else
                /bin/false  ## not ok_to_stop
            fi
        fi
        rc_status -v
        ;;
    *)
        var=$0
        echo "`eval_gettext \"Usage: \\$var {start|stop}\"`"
        ;;
esac

rc_exit
