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

SLES11_HOST=no
RHEL55_HOST=no
RHEL6_HOST=no
RESET_DM_DEPS=no
PPME_SUPPORT=yes

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

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

if [ -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
        RHEL6_HOST=yes
        TMP_MODP=/tmp/powerpath.conf
        PP_MODP=/etc/modprobe.d/powerpath.conf
    fi

    if [ "${rhel_version}" == "5" ]; then
        if [ ${rhel_update} -ge 5 ]; then
            RHEL55_HOST=yes
        fi
    fi
fi

if [ "$SLES11_HOST" == "yes" -o "$RHEL6_HOST" == "yes" ]; then
    RESET_DM_DEPS=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

    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/SuSE-release ]
    then
        VENDOR=suse
    elif [ -f /etc/asianux-release ]
    then
        VENDOR=asianux
    elif [ -f /etc/redhat-release ]
    then
        VENDOR=redhat
    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
        ;;
        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
}

#-----------------------------------------------------------
# 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 re-issue 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"
}

#-----------------------------------------------------------------
# Start 32 bit Emulation library before powerpath startup for ia64
#-----------------------------------------------------------------
start_32bit_emulation()
{
    /etc/init.d/ia32el start  > /dev/null 2>&1
    rc_check

    if rc_status ; then
        echo -e "`eval_gettext \"\nSuccessfully started 32-bit emulation library\"`"
    else
        echo -e "`eval_gettext \"\nError in starting 32-bit emulation library\"`"
    fi
}

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

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

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

    if [ ! -e /etc/powermt.custom ]; 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 > /dev/null 2>&1

        if ! /sbin/powermig info -all | 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
    fi
    rc_check

    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
}

#-----------------------------------------------------------
# Check for naviagent and powermt before stopping PowerPath.
#-----------------------------------------------------------
ok_to_stop()
{
    ps -C naviagent >> /dev/null
    if [ $? -eq 0 ]; then
        echo "`eval_gettext \"Navisphere agent is running.\"`"
        echo  "`eval_gettext \"Please stop the agent and then re-issue \\$script_name stop.\"`"
        return 1
    fi

    ps -C powermt >> /dev/null
    if [ $? -eq 0 ]; then
        echo "`eval_gettext \"The powermt command is running.\"`"
        echo "`eval_gettext \"Please stop powermt and then re-issue \\$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 re-issue \\$script_name stop.\"`"
                return 1
            fi
        fi
    fi

    #
    # Stopping stordaemons. They will restart automatically.
    #
    if [ -x /usr/storapi/storbin/stordaemon ]; then
        /usr/storapi/storbin/stordaemon shutdown all > /dev/null 2>&1;
    fi

    if pp_stop_lvm ; then
        return 0
    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

    case `uname -p` in
        ppc64*) lookupname=.kallsyms_lookup_name ;;
             *) lookupname=kallsyms_lookup_name ;;
    esac

    klh=`fgrep -w $lookupname /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
#
# All the powermt command except powermt version requires
# root authorization. The check_registration command is 
# more appropriate than powermt display, as powermt display
# executes 'emcpmgr map' internally.
powermt check_registration > /dev/null 2>&1
rc_status
if [ $? -eq 4 ]; 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: \"`"

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

                    #
                    # Start the PowerPath Management daemon.
                    #
                    if [ -f /etc/opt/emcpower/emcp_mond.conf ]; then
                        $PPBASE/emcp_mond.sh start
                    else
                        pp_logger PowerPath: Missing PowerPath Management Daemon configuration file=/etc/opt/emcpower/emcp_mond.conf
                    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}
                    echo "`eval_gettext \"Stopping PowerPath Management Daemon...\"`"
                    pp_logger PowerPath: Stopping PowerPath Management Daemon...
                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 -elf | grep emcp_xcryptd | grep -v grep > /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 re-issue \\$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
            else
                /bin/false  ## not ok_to_stop
            fi
        fi
        rc_status -v
        ;;
    *)
        var=$0
        echo "`eval_gettext \"Usage: \\$var {start|stop}\"`"
        ;;
esac

rc_exit
