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


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

SLES11_HOST=no
RHEL6_HOST=no

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


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
}

case "$1" in 
     start)
        fcoe_found="no"
        if [ -f /etc/init.d/fcoe ]; then
            #
            # Wait for upto 30 seconds for atleast some of
            # the FCoE ports to be initialized
            #    
            for (( i=0; i<6; i++ ))
            do
                fcoe_ports=`/usr/sbin/fcoeadm -i | grep "Symbolic Name" | wc -l`
                if [ ${fcoe_ports} -gt 0 ]; then
                    fcoe_found="yes"
                    break; 
                fi
                sleep 5
            done

            if [ ${fcoe_found} == "yes" ]; then
                sleep 10
                #
                # Wait for upto 150 seconds for all the FCoE ports to
                # become online
                #
                for (( i=0; i<30; i++ ))
                do
                    fcoe_ports=`/usr/sbin/fcoeadm -i | grep "Symbolic Name" | wc -l`
                    fcoe_ports_online=`/usr/sbin/fcoeadm -i | grep State | grep Online | wc -l`
                    if [ ${fcoe_ports} -eq ${fcoe_ports_online} ]; then
                        break; 
                    fi
                    sleep 5
                done
            
                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/fcoemon.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 # fcoe_found==yes
        fi

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

            if [ -d /var/lock/subsys ] ; then
                touch /var/lock/subsys/PowerFcoe
            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
            
            # Run "mount -a" to mount FCoE mount points in /etc/fstab on RHEL6
            if [ "$RHEL6_HOST" == "yes" ]; then
                 if [ "`grep -c '_netdev' /etc/fstab 2>/dev/null`" -gt 0 ]; then
                    mount -a 2>/dev/null
                 fi
            fi
        fi
        ;;

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

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

     *)
        ;;
esac
