#!/bin/sh
#
# ###################################################################
# Copyright (c) 2005-2011, EMC Corporation. All Rights Reserved.
#
# /etc/init.d/PowerIscsi
# This file is a part of EMC PowerPath.
#
# chkconfig: 35 24 00
# description: Configures iSCSI devices during boot.
#
# ###################################################################
#
### BEGIN INIT INFO
# Provides:          PowerIscsi
# Required-Start:
# Required-Stop:
# Default-Start:     3 5
# Default-Stop:
# Description:       PowerIscsi -- Configures iSCSI devices during boot.
### END INIT INFO


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

SLES11_HOST=no
RHEL6_HOST=no

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
}

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`
    if [ "${rhel_version}" == "6" ]; then
        RHEL6_HOST=yes
    fi
fi

RESET_DM_DEPS=no
if [ "$SLES11_HOST" == "yes" -o "$RHEL6_HOST" == "yes" ]; then
    RESET_DM_DEPS=yes
fi



case "$1" in 
     start)
        if [ -f /etc/init.d/iscsi -o -f /etc/init.d/open-iscsi ]; then
            if [ -x /sbin/udevadm ]; then
                /sbin/udevadm settle --timeout=180
            else
                if [ -x /sbin/udevsettle ]; then
                    /sbin/udevsettle --timeout=180
                else
                    if [ -e /etc/SuSE-release -a -s /var/run/iscsid.pid ]; then
                       sleep 10
                       for (( cnt=0; $cnt < 360; cnt++ )); do
                          hw=`ps ax | grep -w hwscan |grep -v grep| wc -l`
                          if [ $hw -eq 0 ]; then
                              sleep 6
                              hw=`ps ax | grep -w hwscan |grep -v grep| wc -l`
                              if [ $hw -eq 0 ]; then
                                    break;
                              fi
                          fi
                          sleep 10
                       done
                    fi 
                fi
            fi

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

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

        if [ -f /etc/init.d/iscsi -o -f /etc/init.d/open-iscsi ]; then
            /sbin/powermt save >  /dev/null 2>&1

            if [ -d /var/lock/subsys ] ; then
                touch /var/lock/subsys/PowerIscsi
            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
        fi
        ;;

      stop)
        if [ -f /etc/init.d/iscsi -o -f /etc/init.d/open-iscsi ]; then
           hbano=`powermt display | grep iSCSI | awk '{ print $1 }'`
           for i in $hbano; do
               powermt remove force hba=$i
           done

           if [ -e /var/lock/subsys/PowerIscsi ]; then
              rm /var/lock/subsys/PowerIscsi
           fi
        fi
        ;;

     *)
        ;;
esac
