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


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

RHEL6_HOST=no
RHEL5_HOST=no
SLES10_HOST=no
OL55_HOST=no
OL6_HOST=no
CXS6_HOST=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

#----------------------------------------------------------------
# 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.  Hence to update the partition
# table, 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
}

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
}

#
# Check if a dm device of a LVM volume is contained in a holders directory.
#
# Parameter 1 - holders directory
#           2 - the dm device of the mounted LVM volume
# Return values 0 - The the dm device is not contained in the holders
#                   directory.
#               1 - The dm device is contained in the holders directory.
#
check_holders()
{
    local holders_path=$1
    local dm=$2

    for h in $holders_path/* ; do
        [ "${h##*/}" != "*" ] || break
        [ "${h##*/}" = "${dm##*/}" ] && return 1
     done
     return 0
}

#
# Check for a pseudo device's holders' holders or partitions' holders
# to see if the pseudo device contains the dm device of a mounted
# LVM volume.
#
# Parameter 1 - pseudo device name
#           2 - whether the psdudo device is a partition(1: yes 0: no)
#           3 - the dm device of the mounted LVM volume
# Return values 0 - The pseudo device doesn't contain the dm device.
#               1 - The pseudo device contains the dm device.
#
check_holders_holders()
{
    local dev=$1
    local is_partition=$2
    local dm=$3

    if [ $is_partition -eq 1 ] ; then
        empty_string="emcpower*"
        parent_path="/sys/devices/virtual/block/$dev/$empty_string"
    else
        empty_string="*"
        parent_path="/sys/devices/virtual/block/$dev/holders/$empty_string"
    fi

    for p in $parent_path ; do
        [ "${p##*/}" != "$empty_string" ] || break

        if [ $is_partition -eq 1 ] ; then
            holders_path="$p/holders/"
            g_partition_dev_name="${p##*/}"
        else
            holders_path="/sys/devices/virtual/block/"${p##*/}"/holders/"
        fi
        check_holders $holders_path $dm
        if [ $? -eq 1 ] ; then
            [ $is_partition -eq 1 ] && g_partition_dev_name="${p##*/}"
            return 1
        fi
     done
     return 0
}

#
# If umount fails and the mounted device is a LVM volume, then
# reset the dm table for dm devices associated with an iSCSI
# pseudo device to use pseudo device's first active native
# path to prevent open-iscsi:stop from logging out the iSCSI
# session the native path belongs to.
#
# parameter 1 - the pseudo device or pseudo device partition
#
reset_dm_table_to_use_native()
{
    local dev=$1
    local partition_no="${dev##*[a-z]}"
    local dev_without_partition="${dev%$partition_no}"
    local pseudo_major=`grep -e "$dev$" /proc/partitions | \
        sed -e "s/^ *\([0-9]*\) *[0-9]* *[0-9]* *$dev$/\1/"`""
    local pseudo_minor=`grep -e "$dev$" /proc/partitions | \
        sed -e "s/^ *[0-9]* *\([0-9]*\) *[0-9]* *$dev$/\1/"`
    local pseudo_major_minor="$pseudo_major:$pseudo_minor"
    local native_dev_names=$(powermt display dev=$dev_without_partition | \
        egrep 'active|standby|unlic|asb:iopf' | grep -v unknown | \
        sed -ne 's:\(.*\) \(sd[a-z]*\) \(.*\):\2:p')
    local table_file="/tmp/poweriscsi_table_file"

    for n in $(echo $native_dev_names)
    do
        first_native_path="$n$partition_no"
        break
    done

    [ "$first_native_path" != "" ] || return


    native_major=`grep -e "$first_native_path$" /proc/partitions | \
        sed -e "s/^ *\([0-9]*\) *[0-9]* *[0-9]* *$first_native_path$/\1/"`""
    native_minor=`grep -e "$first_native_path$" /proc/partitions | \
        sed -e "s/^ *[0-9]* *\([0-9]*\) *[0-9]* *$first_native_path$/\1/"`
    native_major_minor="$native_major:$native_minor"

    DM_LIST=`dmsetup ls 2>/dev/null | grep -v "No devices found" | \
        sed -e 's/^\(\S*\) *.*/\1/'`
    DM_MAJOR=`cat /proc/devices|grep device-mapper|awk '{print $1}'`

    for DM_DEV in $DM_LIST
    do
        OLD_TABLE=`dmsetup table $DM_DEV 2>/dev/null`
        NEW_TABLE=`echo "$OLD_TABLE" | \
            sed -e "s/$pseudo_major_minor/$native_major_minor/g"`

        if [ "$OLD_TABLE" != "$NEW_TABLE" ] ; then
            echo "$NEW_TABLE" > $table_file
            dmsetup suspend $DM_DEV 2>/dev/null
            dmsetup reload $DM_DEV $table_file 2>/dev/null
            dmsetup resume $DM_DEV 2>/dev/null
            rm $table_file 2>/dev/null
        fi
    done
}

#
# Unmount any mount points associated with a pseudo device that
# are using the specific transport protocol
#
# Parameter 1 - an pseudo device such as emcpowera
# Parameter 2 - a transport protocol (i.e. iSCSI, FCoE)
#
umount_if_mounted()
{
    local dev=$1
    local protocol=$2
    local d m x islink holder_path mount_link

    cat /proc/mounts | sed -ne '/^\/dev\/.*/p' | while read d m x; do
        islink=0
        if [ -L "$d" ] ; then
            # Mounted device is LVM volume such as /dev/mapper/vg01-lvol0.
            # Use readlink to get its dm device, such /dev/dm-1.
            d=$(readlink -f $d)
            islink=1
        fi
        [ -b "$d" ] || continue
        [ "$m" != "/" ] || continue  # Skip root mount point

        is_mounted=0
        g_partition_dev_name=""

        if [ $islink -eq 0 ] ; then
            #
            # The mounted device can be /dev/emcpowera, /dev/emcpowera2,
            # /dev/sda and /dev/sda2, etc.
            #
            # Strip the path and partition from the mount device.
            #
            d="${d##*/}"
            d="${d%"${d##*[a-z]}"}"

            if [ "$dev" = "$d" ]; then
               is_mounted=1
            else # Check native path as well
                /sbin/powermt display dev="${dev##*/}" | \
                   grep $protocol | grep "$d" > /dev/null 2>&1
                if [ $? -eq 0 ] ; then
                    is_mounted=1
                fi
            fi
        else
            #
            # Mounted device is a LVM volume. Check for $dev's holders and
            # holders' holders(i.e., in the case of mirrored LVM volumes)
            # to see if $dev contains the dm device of the LVM volume.
            #
            # Check for partitions' holders as well because volume can be
            # created with partitions.
            #
            check_holders "/sys/devices/virtual/block/$dev/holders/" $d && \
                check_holders_holders $dev 0 $d && \
                check_holders_holders $dev 1 $d
            is_mounted=$?
        fi

        if [ $is_mounted -eq 1 ] ; then
            if  ! umount $m 2> /dev/null  && [ $islink -eq 1 ] ; then
                #
                # If umount fails and the mounted device is a LVM volume,
                # then reset the dm table for dm devices associated with this
                # pseudo device to use pseudo device's first active native
                # path to prevent open-iscsi:stop from logging out the FCoE
                # session this the native path belongs to.
                #
                if [ "$g_partition_dev_name" != "" ] ; then
                    reset_dm_table_to_use_native $g_partition_dev_name
                else
                    reset_dm_table_to_use_native $dev
                fi
            fi
        fi
    done
}

#
# Check if a device-mapper device is mounted by UUID and maps to a
# LVM logical volume which uses EMC PowerPath pseudo device names.
# If a match is found unmount the file system which corresponds to
# the device-mapper device.
#
# Parameter 1 - protocol string to search for (i.e. iSCSI, FCoE)
#
umount_if_mounted_by_uuid()
{
    local protocol=$1
    #
    # Loop through the file systems that are currently mounted on
    # device-mapper devices.
    #
    cat /proc/mounts | grep -e "^\/dev\/dm-" | awk '{print $1}' \
        | while read filesystem;
    do
        #
        # Use the block ID command to get the UUID for the
        # device-mapper device.
        #
        dm_dev_uuid=`/sbin/blkid $filesystem | awk 'BEGIN {FS = "\""} \
            ; {print $2}'`
        #
        # For each device-mapper volume still mounted loop through the
        # LVM volume groups and check if it has an EMC PowerPath pseudo
        # device.
        #
        vgdisplay -c | awk 'BEGIN {FS = ":"} ; {print $1}' | while read vg;
        do
            pp_dev=`vgdisplay -v $vg 2>/dev/null | grep -e "PV Name" \
                | grep -e "emcpower" | awk '{print $3}'`
            if [ "$pp_dev" != "" ] ; then
                #
                # Get the EMC PowerPath pseudo device name from the
                # LVM physical volume.
                #
                pp_pseudo=`echo $pp_dev | awk 'BEGIN {FS = "/"} \
                    ; {print $3}'`
                #
                # strip the partition 
                #
                pp_pseudo="${pp_pseudo##*/}"
                pp_pseudo="${pp_pseudo%"${pp_pseudo##*[a-z]}"}"
                #
                # Check if the EMC PowerPath pseudo device matches
                # the specified transport protocol
                #
                is_match=`/sbin/powermt display dev=$pp_pseudo \
                    | grep -e $protocol`
                if [ "$is_match" != "" ] ; then
                    #
                    # We have a LVM volume group that has an EMC PowerPath
                    # pseudo device. Check to see if our current
                    # device-mapper device UUID matches a LVM logical
                    # volume UUID from this volume group.
                    #
                    vgdisplay -v $vg 2>/dev/null | grep -e "LV Name" \
                        | awk '{print $3}' | while read lv;
                    do
                        #
                        # Use the block ID command to get the UUID for the
                        # LVM logical volume.  Then compare the current
                        # device-mapper UUID with the LVM logical volume
                        # UUID.
                        #
                        lvm_uuid=`/sbin/blkid $lv | awk 'BEGIN {FS = "\""} \
                            ; {print $2}'`
                        if [ "$dm_dev_uuid" = "$lvm_uuid" ] ; then
                            #
                            # We have matching UUIDs. Search the fstab file
                            # for the mount point for unmounting.
                            #
                            fs_to_unmount=`grep -e $dm_dev_uuid /etc/fstab \
                                | awk '{print $2}'`
                            /bin/umount $fs_to_unmount
                            #
                            # Deactivate the volume group
                            #
                            /sbin/vgchange -a n $vg > /dev/null 2>&1
                        fi
                    done
                fi
            fi
        done
    done
}

# main #

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

    if [ "${host_os}" == "10" ]; then
        SLES10_HOST=yes
    fi
elif [ -f /etc/oracle-release ]; then
    if [ $(uname -r | grep -c uek) -ne 0 ]
    then
        oracle_version=`sed '/^#/d;s/^.*release \([0-9]\)[0-9.]*/\1/' \
                                              /etc/oracle-release`
        oracle_update=`sed '/^#/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" ]; then
          OL6_HOST=yes
        fi
    else
        rhel_version=`sed '/^#/d;s/^.*release \([0-9]\)[0-9.]* .*/\1/' \
                                               /etc/redhat-release`
        if [ "${rhel_version}" == "6" ]; then
            RHEL6_HOST=yes
        elif [ "${rhel_version}" == "5" ]; then
            RHEL5_HOST=yes
        fi
    fi
elif [ -f /etc/redhat-release ]; then
    citrix_release=`cat /etc/redhat-release|grep -v "^#"|\
            grep "XenServer release"`
    if [ -n "$citrix_release" ]
    then
        CXS6_HOST=yes
    else
        rhel_version=`sed '/^#/d;s/^.*release \([0-9]\)[0-9.]* .*/\1/' \
                                            /etc/redhat-release`

        if [ "${rhel_version}" == "6" ]; then
            RHEL6_HOST=yes
        elif [ "${rhel_version}" == "5" ]; then
            RHEL5_HOST=yes
        fi
    fi
fi

if  [ "$RHEL5_HOST" == "yes"  -o  "$SLES10_HOST" == "yes" \
      -o "$OL55_HOST" == "yes" ]; then
        RESET_DM_DEPS=no
fi

case "$1" in
     start)
        fcoe_found="no"
        if [ -f /etc/init.d/fcoe -o -f /etc/init.d/boot.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

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

            # Run "mount -a" to mount FCoE mount points in /etc/fstab on
            # RHEL6 and OL6
            if [ "$RHEL6_HOST" == "yes" -o "$OL6_HOST" == "yes" -o \
                 "$CXS6_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 -o -f /etc/init.d/boot.fcoe ]; then
            #
            # Umount all mount points associated with each PP FCoE device.
            #
            for dev in /dev/emcpower?* ; do
                [ "${dev##*/}" != "emcpower?*" ] || break
                if [ "`expr \"${dev##*/}\" : \
                    \"emcpower[a-z][a-z]*[0-9][0-9]*\"`" != "0" ] ; then
                    continue # skip partitions
                fi
                /sbin/powermt display dev="${dev##*/}" | \
                    grep FCoE > /dev/null 2>&1
                if [ $? -eq 0 ] ; then
                    umount_if_mounted "${dev##*/}" FCoE
                fi
            done
            #
            # Check of existing mounted file systems to see if there
            # are device-mapper devices mounted by UUID which use
            # EMC PowerPath pseudo devices.
            #
            umount_if_mounted_by_uuid FCoE

            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
