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


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

SLES10_HOST=no
RHEL5_HOST=no
OL55_HOST=no
SLES11SP2_HOST=no

# By default, the dm dependencies should be reset to pseudo devices
# except for older platforms such as SLES10, RHEL5 and OL55
#
# Note: During shutdown, we will reset dm dependencies for involved
# devices to native devices if we fail to unmount iSCSI LVM volumes.
# 
RESET_DM_DEPS=yes

# Used to pass mounted pseudo partition device name from
# function check_holders_holders.
#
g_partition_dev_name=""

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 an iSCSI pseudo device.
# Parameters 1 - an iSCSI pseudo device such as emcpowera
#
iscsi_umount_if_mounted()
{
    local dev=$1
    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 iSCSI | 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 iSCSI
                # 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.
#
iscsi_umount_if_mounted_by_uuid()
{
    #
    # 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 is iSCSI
                #
                is_iscsi=`/sbin/powermt display dev=$pp_pseudo \
                    | grep -e iSCSI`
                if [ "$is_iscsi" != "" ] ; 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`

    patch_level=`awk '/PATCHLEVEL/ {patch=$3}
                  END {print patch}' /etc/SuSE-release`

    if [ "${host_os}" == "10" ]; then
        SLES10_HOST=yes
    elif [ "${host_os}" == "11" ] && [ "${patch_level}" == "2" ]; then
        SLES11SP2_HOST=yes
    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
    fi
elif [ -f /etc/redhat-release ]; then
    rhel_version=`sed 's/^.*release \([0-9]\)[0-9.]* .*/\1/' \
                                        /etc/redhat-release`
    if [ "${rhel_version}" == "5" ]; then
        RHEL5_HOST=yes
    fi
fi

if  [ "$RHEL5_HOST" == "yes"  -o  "$SLES10_HOST" == "yes" \
      -o "$OL55_HOST" == "yes" ]; then
        RESET_DM_DEPS=no
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

            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

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

      stop)
        if [ -f /etc/init.d/iscsi -o -f /etc/init.d/open-iscsi ]; then
            if [ "$SLES11SP2_HOST" == "yes" -o \
                "$SLES10_HOST" == "yes" ]; then
                # Umount all mount points associated with each PP iSCSI 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 iSCSI > /dev/null 2>&1
                    if [ $? -eq 0 ] ; then
                        iscsi_umount_if_mounted "${dev##*/}"
                    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.
                #
                iscsi_umount_if_mounted_by_uuid
            fi

            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
