#!/bin/sh
###############################################################################
#
#          Dell Inc. PROPRIETARY INFORMATION
# This software is supplied under the terms of a license agreement or
# nondisclosure agreement with Dell Inc. and may not
# be copied or disclosed except in accordance with the terms of that
# agreement.
#
# Copyright (c) 2010 Dell Inc. All Rights Reserved.
#
# Module Name: md_coexistence_util.sh
# Module Version: 2.0.0.4
# Environment: Linux
#
#
# Abstract/Purpose:
#
#   This script will perform the necessary tasks to allow coexistence of
#   PowerVault MD3000/i storage arrays with newer Device Mapper based PowerVault
#   storage arrays (ex. MD32xx/i and MD36xxi Series). Run the script with
#   the "install" parameter to make the necessary modifications.  If at some
#   point in the future access to the PowerVault MD3000/i storage array(s)
#   will no longer be required, run the script with the "uninstall" parameter
#   to return the host to appropriate configuration for Device Mapper only.
#
#   This script may also be run with the "status" parameter to display a status
#   report which indicates if the current host has all required components at a
#   minimum level required for coexistence support for the listed PowerVault
#   MD models.  It also displays the current driver versions and timings (if
#   applicable).
#
#
# Parameters:
#
#   <action> - One of the following actions:
#     install - Applies modifications to the host necessary to support
#               coexistence of certain PowerVault Modular Disk storage array
#               models based on passed model list arguments.
#				Array arguments have to be seperated by comma
#               They can be one or several of the followings:
#               md3000, md3000i, md32xx, md32xxi, md36xxi, md36xxf
#     interactive - Applies modifications to the host necessary to support
#               coexistence of certain PowerVault Modular Disk storage array
#               models based on user prompt input.
#     uninstall - Reverts the host to a state suitable for only Device Mapper
#                 based PowerVault storage array operation.
#     status - Displays coexistence status.
#
#
# Returns:
#   0 - Success
#   1 - Invalid usage
#  
#   [install]
#   2 - Invalid launch location for the install mode.  Must be launched from
#       the PowerVault Modular Disk resource media
#   3 - Resource media not installed
#   4 - User aborted
#
#   [uninstall]
#   4 - User aborted
###############################################################################

# Return codes.
RETCODE_SUCCESS=0
RETCODE_INVALID_USAGE=1
RETCODE_INVALID_INSTALL_LAUNCH_LOCATION=2
RETCODE_RESOURCE_MEDIA_NOT_INSTALLED=3
RETCODE_USER_ABORTED=4

MPP_VERSION=""
MIN_MPP_VER="09.03.0C06.0452.2-1dkms"
SCSI_DH_VER=""
MIN_SCSI_DH_VER_RHEL_5_4="1.4.1.2-3dkms"
MIN_SCSI_DH_VER_RHEL_5_5="1.4.2.1-3dkms"
MIN_SCSI_DH_VER_RHEL_6_0="1.6.1.1-1dkms"
MIN_SCSI_DH_VER_SLES_10_3="1.3.1.2-3dkms"
MIN_SCSI_DH_VER_SLES_11_0="1.5.1.2-3dkms"
MIN_SCSI_DH_VER_SLES_11_1="1.5.2.1-3dkms"
FAST_FAILOVER_REPLACEMENT_TIMEOUT="30"
FAST_FAILOVER_NOOP_OUT_TIMEOUT="15"
COEX_REPLACEMENT_TIMEOUT="144"
COEX_NOOP_OUT_TIMEOUT="30"
RESP=""
RESP_LOWERCASE=""
REBOOT_NEEDED=""

# Set RPM path.
PATH_TO_RPMS="$(dirname $0)/resources"

MD3000=""
MD3000I=""
MD32XX=""
MD32XXI=""
MD36XXf=""

###############################################################################
# Gets information about the Linux distribution and checks if it is supported.
###############################################################################
function CheckLinuxDist
{
  echo -n "Checking Linux distribution..."
  local IDS_UNSUPPORTED_DISTRO="The current linux distribution is not supported."

  # Redhat distros.
  if [ -e /etc/redhat-release ]; then 
    LINUX_DISTRO=rhel
    LINUX_KERNEL_REV=$(uname -r)
    LINUX_ARCH=$(uname -i)
    LINUX_REV=$(cat /etc/redhat-release | sed 's/.*release //' | sed 's/ .*//')
    LINUX_REV_MAJOR=$(echo $LINUX_REV | sed 's/[\.][0-9]*//')
    LINUX_REV_MINOR=$(echo $LINUX_REV | sed 's/[0-9]*[\.]*//')
    if [ -z $LINUX_REV_MINOR ]; then
      LINUX_REV_MINOR=0
    fi
    echo "$LINUX_DISTRO $LINUX_REV"

    # Check for unsupported revs.
    if [ $LINUX_REV_MAJOR -lt 5 ]; then
      echo "$IDS_UNSUPPORTED_DISTRO"
      exit $RETCODE_UNSUPPORTED_DISTRO
    elif [ $LINUX_REV_MAJOR -eq 5 -a $LINUX_REV_MINOR -lt 4 ]; then
      echo "$IDS_UNSUPPORTED_DISTRO"
      exit $RETCODE_UNSUPPORTED_DISTRO
    fi

  # SuSE distros.
  elif [ -e /etc/SuSE-release ]; then 
    LINUX_DISTRO=sles
    LINUX_KERNEL_REV=$(uname -r)
    LINUX_ARCH=$(uname -i)
    LINUX_REV_MAJOR=$(cat /etc/SuSE-release | tr '\n' ' ' | sed 's/.*VERSION = //' | sed 's/ .*//')
    LINUX_REV_MINOR=$(cat /etc/SuSE-release | tr '\n' ' ' | sed 's/.*PATCHLEVEL = //' | sed 's/ .*//')
    LINUX_REV=$LINUX_REV_MAJOR.$LINUX_REV_MINOR
    echo "$LINUX_DISTRO $LINUX_REV"

    # Check for unsupported revs.
    if [ $LINUX_ARCH != "x86_64" ]; then
      echo "$IDS_UNSUPPORTED_DISTRO"
      exit $RETCODE_UNSUPPORTED_DISTRO
    elif [ $LINUX_REV_MAJOR -lt 10 ]; then
      echo "$IDS_UNSUPPORTED_DISTRO"
      exit $RETCODE_UNSUPPORTED_DISTRO
    elif [ $LINUX_REV_MAJOR -eq 10 -a $LINUX_REV_MINOR -lt 3 ]; then
      echo "$IDS_UNSUPPORTED_DISTRO"
      exit $RETCODE_UNSUPPORTED_DISTRO
    fi
  else
    echo "$IDS_UNSUPPORTED_DISTRO"
    exit $RETCODE_UNSUPPORTED_DISTRO
  fi
}


###############################################################################
# Prompts user for input using the passed message.  If a series of forward
# slash delimited characters are detected between square brackets, the characters
# will be the valid values that the user must enter.  All comparisons are
# performed as lowercase so uppercase entries will still work.
#
# Parameters:
#   1 - Prompt Message
#
# Returns:
#   Nothing.  User entry will be in global RESP variable and the user entry
#   converted to lowercase will be in the global RESP_LOWERCASE variable.
###############################################################################
function Prompt
{
  MSG="$1"
  VALID_VALUES=

  # Extract list of valid values from bracketed expression if there is one.
  echo "$MSG" | grep "\[.*\]" >/dev/null 2>&1
  if [ $? -eq 0 ]; then
    VALID_VALUES=$(echo $MSG | sed "s/.*\[\(.*\)\].*/\1/" | sed "s/\// /")
  fi

  # No list of valid valus was passed.
  if [ -z "$VALID_VALUES" ]; then
    # Prompt and get response.
    echo -n "$MSG"
    read RESP

  # List of valid values was passed.
  else
    IS_VALID="false"
    while [ 1 ]; do
      # Prompt and get response.
      echo -n "$MSG"
      read RESP

      # Check if entered value is valid.
      for VALID_VALUE in $VALID_VALUES; do
        RESP_LOWERCASE=$(echo "$RESP" | tr '[:upper:]' '[:lower:]')
        VALID_VALUE=$(echo "$VALID_VALUE" | tr '[:upper:]' '[:lower:]')
        if [ "$RESP_LOWERCASE" == "$VALID_VALUE" ]; then
          IS_VALID="true"
          break
        fi
      done

      if [ "$IS_VALID" == "true" ]; then break; fi
      echo "Invalid value entered."
    done
  fi

  # Remove leading/trailing spaces and set lowercase version.
  RESP=$(echo "$RESP" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//')
  RESP_LOWERCASE=$(echo "$RESP" | tr '[:upper:]' '[:lower:]')
}


###############################################################################
# Updates the the passed original file with the passed modified file
# if they are different.
#
# Parameters:
#   1 - File To Modify
#   2 - Modified File
#
# Returns:
#  0 - File udpated
#  1 - File NOT updated
###############################################################################
function CompareAndUpdateFile
{
  local FILE_TO_MODIFY="$1"
  local FILE_MODIFIED="$2"

  # Update file if needed.
  echo -n "Checking if update is needed for $FILE_TO_MODIFY..."
  IGNORE=$(diff "$FILE_TO_MODIFY" "$FILE_MODIFIED")
  if [ $? -eq 0 ]; then
    echo "no"
    rm -f "$FILE_MODIFIED"
    return 1
  else
    echo "yes"
    echo -n "Updating $FILE_TO_MODIFY..."
    cp -f "$FILE_MODIFIED" "$FILE_TO_MODIFY"
    rm -f "$FILE_MODIFIED"
    echo "Done"
  fi
}


###############################################################################
# Returns whether or not the DM driver (scsi_dh_rdac) is installed and of the
# minimum required version.
#
# Returns:
#  0 - DM is installed and at an appropriate level
#  1 - DM is not installed or version is too low
###############################################################################
function TestDMSupport
{
  # Choose min DM version based on supported distros.
  local MIN_SCSI_DH_VER=""
  if [ "$LINUX_DISTRO" == "rhel" ]; then
    if [ "$LINUX_REV" == "5.4" ]; then
      MIN_SCSI_DH_VER=$MIN_SCSI_DH_VER_RHEL_5_4
    elif [ "$LINUX_REV" == "5.5" ]; then
      MIN_SCSI_DH_VER=$MIN_SCSI_DH_VER_RHEL_5_5
    elif  [ "$LINUX_REV" == "5.6" ]; then
      MIN_SCSI_DH_VER=$MIN_SCSI_DH_VER_RHEL_5_6
	elif [ "$LINUX_REV" == "5.7" ]; then
      MIN_SCSI_DH_VER=$MIN_SCSI_DH_VER_RHEL_5_7
	elif [ "$LINUX_REV" == "6.0" ]; then
      MIN_SCSI_DH_VER=$MIN_SCSI_DH_VER_RHEL_6_0  
    fi
  elif [ "$LINUX_DISTRO" == "sles" ]; then
    if [ "$LINUX_REV" == "10.4" ]; then
      MIN_SCSI_DH_VER=$MIN_SCSI_DH_VER_SLES_10_4
    elif [ "$LINUX_REV" == "11.0" ]; then
      MIN_SCSI_DH_VER=$MIN_SCSI_DH_VER_SLES_11_0
    elif [ "$LINUX_REV" == "11.1" ]; then
      MIN_SCSI_DH_VER=$MIN_SCSI_DH_VER_SLES_11_1
    fi
  fi

  # DM driver present and high enough for supported distros?
  if [ ! -z "$MIN_SCSI_DH_VER" ]; then
    local PKG_NAME="scsi_dh_rdac"
    INSTALLED_PKG_VERSION=$(rpm -q "$PKG_NAME" --qf "%{VERSION}-%{RELEASE}" 2>/dev/null)
    if [ $? -ne 0 ]; then return 1; fi
    if [ "$INSTALLED_PKG_VERSION" \< "$MIN_SCSI_DH_VER" ]; then return 1; fi
  else
    # Native DM present?
    if [ -z "$SCSI_DH_VERSION" ]; then return 1; fi
  fi

  return 0
}

###############################################################################
# Displays message and exits if the install mode of the script is not run from
# the MD resource media
###############################################################################
function CheckResourceMediaLaunch
{
  if [ ! -d "$PATH_TO_RPMS" -o ! -d "$PATH_TO_RPMS/linuxrdac" ] ||
     [ ! -d "$PATH_TO_RPMS/mptlinux" -o ! -d "$PATH_TO_RPMS/mpt2sas" ]; then
    echo ""
    echo "The install option must be run from the PowerVault Modular Disk resource media." 
    exit $RETCODE_INVALID_INSTALL_LAUNCH_LOCATION
  fi
}


###############################################################################
# Display message and exits if the MD resource media host components aren't
# installed.
###############################################################################
function CheckResourceMediaInstalled
{
  local CHECKS_FAILED=""

  # DM version present and at a high enough version?
  #TestDMSupport
  #if [ $? -ne 0 ]; then
    #CHECKS_FAILED="true"
  #fi
  
  # DKMS present?
  local PKG_NAME="dkms"
  INSTALLED_PKG=$(rpm -q "$PKG_NAME" 2>/dev/null)
  if [ $? -ne 0 ]; then
    CHECKS_FAILED="true"
  fi

  # SM appears installed?
  if [ ! -d /var/opt/SM ]; then
    CHECKS_FAILED="true"
  fi
  
  # SMAgent present? 
  local PKG_NAME="SMagent"
  INSTALLED_PKG=$(rpm -q "$PKG_NAME" 2>/dev/null)
  if [ $? -ne 0 ]; then
    CHECKS_FAILED="true"
  fi

  if [ "$CHECKS_FAILED" == "true" ]; then
    echo "Coexistence support requires the latest MD resource media host components be"
    echo "installed prior to execution.  Some of the required host components are either"
    echo "not installed or not of a high enough version to support coexistence for the"
    echo "the current host.  Install the latest MD resource media Host components."
    exit $RETCODE_RESOURCE_MEDIA_NOT_INSTALLED
  fi
}


###############################################################################
# Updates the iSCSI replacement and noop out timeouts with the passed values.
#
# Parameters:
#   1 - New replacement timeout
#   2 - New noop out timeout
#
# Returns:
#  0 - File udpated
#  1 - File NOT updated
###############################################################################
function UpdateTimings
{
  local NEW_REPLACEMENT_TIMEOUT=$1
  local NEW_NOOP_OUT_TIMEOUT=$2

  # Specify open-iscsi conf file.
  local FILE_TO_MODIFY="/etc/iscsi/iscsid.conf"
  local FILE_MODIFIED="/etc/iscsi/iscsid.conf.dell_modified"

  # Strip old entries and modify conf file.
  awk '
    /^# Begin Dell MD[0-9x ]* Modification/ {modification_depth=modification_depth+1; next;}
    /^# End Dell MD[0-9x ]* Modification/ {modification_depth=modification_depth-1; next;}
    modification_depth>0 {if (substr($0,1,1) == "#") {sub("^# ?", ""); print $0;} next;}
    {print $0};
  ' <"$FILE_TO_MODIFY" | awk -v replacement_timeout=$NEW_REPLACEMENT_TIMEOUT -v noop_out_timeout=$NEW_NOOP_OUT_TIMEOUT '
    function begin_modify () {print "# Begin Dell MD Modification";}
    function end_modify () {print "# End Dell MD Modification";}
    /^node.conn\[0\].timeo.noop_out_timeout.*=.*/ {
      nt_found = 1;
      if ($3 != noop_out_timeout) {
        begin_modify();
        print "# "$0;
        print "node.conn[0].timeo.noop_out_timeout = " noop_out_timeout;
        end_modify();
      } else {
        print $0;
      }
      next;
    }
    /^node.session.timeo.replacement_timeout.*=.*/ {
      rt_found = 1;
      if ($3 != replacement_timeout) {
        begin_modify();
        print "# "$0;
        print "node.session.timeo.replacement_timeout = " replacement_timeout;
        end_modify();
      } else {
        print $0;
      }
      next;
    }
    {print $0};
    END {
      if (nt_found != 1 || rt_found != 1) {
        begin_modify();
        if (nt_found != 1) {
          print "node.conn[0].timeo.noop_out_timeout = " noop_out_timeout;
        }
        if (rt_found != 1) {
          print "node.session.timeo.replacement_timeout = " replacement_timeout;
        }
        end_modify();
      }
    }
  ' >"$FILE_MODIFIED"
  
  # Update file if needed.
  CompareAndUpdateFile "$FILE_TO_MODIFY" "$FILE_MODIFIED"
  return $?
}


###############################################################################
# Gets driver versions and iSCSI initiator timings.
###############################################################################
function GetConfigurationDetails
{
  MPP_VERSION=$(modinfo -F version mppVhba 2>/dev/null | sed 's/\([0-9]*\.[0-9]*\.[0-9A-Z]*\.[0-9]*\).*/\1/')
  SCSI_DH_VERSION=$(modinfo -F version scsi_dh_rdac 2>/dev/null)
  MPT_SAS_VERSION=$(modinfo -F version mptsas 2>/dev/null)
  MPT2_SAS_VERSION=$(modinfo -F version mpt2sas 2>/dev/null)
  if [ -e /sbin/iscsid ]; then
    ISCSI_VERSION=$(/sbin/iscsid --version)
    ISCSI_VERSION=${ISCSI_VERSION##* }
    local ISCSID_CONF_FILE="/etc/iscsi/iscsid.conf"
    if [ -e "$ISCSID_CONF_FILE" ]; then
      local REPLACEMENT_TIMEOUT_PATTERN="^node.session.timeo.replacement_timeout[[:space:]]*=[[:space:]]*"
      for LINE in $(grep "$REPLACEMENT_TIMEOUT_PATTERN" "$ISCSID_CONF_FILE" | sed "s/$REPLACEMENT_TIMEOUT_PATTERN//"); do
        REPLACEMENT_TIMEOUT="$LINE"
      done
      local NOOP_OUT_TIMEOUT_PATTERN="^node.conn\[0\].timeo.noop_out_timeout[[:space:]]*=[[:space:]]*"
      for LINE in $(grep "$NOOP_OUT_TIMEOUT_PATTERN" "$ISCSID_CONF_FILE" | sed "s/$NOOP_OUT_TIMEOUT_PATTERN//"); do
        NOOP_OUT_TIMEOUT="$LINE"
      done
    fi
  fi
}


###############################################################################
# Returns whether or not the MPP driver (linuxrdac) is installed and of the
# minimum required version.
#
# Returns:
#  0 - MPP is installed and at an appropriate level
#  1 - MPP is not installed or version is too low
###############################################################################
function TestMPPSupport
{
  # MPP driver present and high enough?
  local PKG_NAME="linuxrdac"
  INSTALLED_PKG_VERSION=$(rpm -q "$PKG_NAME" --qf "%{VERSION}-%{RELEASE}" 2>/dev/null)
  if [ $? -ne 0 ]; then return 1; fi
  if [ "$INSTALLED_PKG_VERSION" \< "$MIN_MPP_VER" ]; then return 1; fi

  return 0
}

###############################################################################
# Returns whether or not the minimum necessary components to support MD3000
# are installed.
#
# Returns:
#  0 - All components are installed and at appropriate levels
#  1 - Updates are required
###############################################################################
function TestMD3000Support
{
  # SAS 5/e adapter driver present?
  if [ "$LINUX_DISTRO" == "sles" -a "$LINUX_REV" == "10.3" ]; then
    local PKG_NAME="mptlinux"
    INSTALLED_PKG=$(rpm -q "$PKG_NAME" 2>/dev/null)
    if [ $? -ne 0 ]; then return 1; fi
  fi

  # MPP driver present and high enough version?
  TestMPPSupport
  if [ $? -ne 0 ]; then return 1; fi

  return 0
}


###############################################################################
# Returns whether or not the minimum necessary components to support MD3000i
# are installed.
#
# Returns:
#  0 - All components are installed and at appropriate levels
#  1 - Updates are required
###############################################################################
function TestMD3000iSupport
{
  # MPP driver present and high enough version?
  TestMPPSupport
  if [ $? -ne 0 ]; then return 1; fi

  # iSCSI initiator present? 
  if [ ! -e /sbin/iscsid ]; then return 1; fi

  # Coexistence timings set?
  if [ "$REPLACEMENT_TIMEOUT" != "$COEX_REPLACEMENT_TIMEOUT" ]; then return 1; fi
  if [ "$NOOP_OUT_TIMEOUT" != "$COEX_NOOP_OUT_TIMEOUT" ]; then return 1; fi

  return 0
}


###############################################################################
# Returns whether or not the minimum necessary components to support MD32xx
# are installed.
#
# Returns:
#  0 - All components are installed and at appropriate levels
#  1 - Updates are required
###############################################################################
function TestMD32xxSupport
{
  # 6Gbps SAS adapter driver present?
  if [ "$LINUX_DISTRO" == "rhel" -a "$LINUX_REV" == "5.4" ] ||
     [ "$LINUX_DISTRO" == "sles" -a "$LINUX_REV" == "10.3" ] ||
     [ "$LINUX_DISTRO" == "sles" -a "$LINUX_REV" == "11.0" ]; then
    local PKG_NAME="mpt2sas"
    INSTALLED_PKG=$(rpm -q "$PKG_NAME" 2>/dev/null)
    if [ $? -ne 0 ]; then return 1; fi
  fi

  # DM driver present and high enough version?
  TestDMSupport
  if [ $? -ne 0 ]; then return 1; fi

  return 0
}


###############################################################################
# Returns whether or not the minimum necessary components to support MD32xxi
# are installed.
#
# Returns:
#  0 - All components are installed and at appropriate levels
#  1 - Updates are required
###############################################################################
function TestMD32xxiSupport
{
  # iSCSI initiator present? 
  if [ ! -e /sbin/iscsid ]; then return 1; fi

  # DM driver present and high enough version?
  TestDMSupport
  if [ $? -ne 0 ]; then return 1; fi
 
  return 0
}


###############################################################################
# Returns whether or not the minimum necessary components to support MD36xxi
# are installed.
#
# Returns:
#  0 - All components are installed and at appropriate levels
#  1 - Updates are required
###############################################################################
function TestMD36xxiSupport
{
  # iSCSI initiator present? 
  if [ ! -e /sbin/iscsid ]; then return 1; fi

  # DM driver present and high enough version?
  TestDMSupport
  if [ $? -ne 0 ]; then return 1; fi
 
  return 0
}


###############################################################################
# Installs the SAS5/e adapter driver (mptlinux).
#
# Returns:
#  0 - Installation succeeded
#  1 - There are no packages to install for the current distro
#  2 - Installation failed
###############################################################################
function InstallSAS5E
{
  # Choose path based on supported distros.
  local RPM_PATH=""
  if [ "$LINUX_DISTRO" == "sles" ]; then
    if [ "$LINUX_REV" == "10.3" ]; then
      RPM_PATH="$PATH_TO_RPMS/mptlinux/sles10sp3"
    fi
  fi

  # Exit if RPM path doesn't exist.
  if [ ! -d "$RPM_PATH" ]; then return 1; fi

  # Exit if RPM package doesn't exist.
  local PKG_NAME="mptlinux"
  local RPM_FILE=$(ls "$RPM_PATH/$PKG_NAME-"*.noarch.rpm 2>/dev/null)
  if [ ! -e "$RPM_FILE" ]; then return 1; fi

  # Install or update depending on current status.
  INSTALLED_PKG=$(rpm -q "$PKG_NAME" 2>/dev/null)
  if [ $? -ne 0 ]; then
    echo "Installing SAS 5/e adapter driver ($PKG_NAME)..."
    rpm -ihv "$RPM_FILE"
    REBOOT_NEEDED="true"
  else
    local RPM_PKG=$(rpm -qp "$RPM_FILE" 2>/dev/null)
    if [ "$RPM_PKG" \> "$INSTALLED_PKG" ]; then
      echo "Updating SAS 5/e adapter driver ($PKG_NAME)..."
      rpm -e "$PKG_NAME"
      rpm -ihv "$RPM_FILE"
      REBOOT_NEEDED="true"
    else
      echo "SAS 5/e adapter driver ($PKG_NAME) does not need to be updated."
    fi
  fi
}


###############################################################################
# Installs the 6Gbps SAS adapter driver (mpt2sas).
#
# Returns:
#  0 - Installation succeeded
#  1 - There are no packages to install for the current distro
#  2 - Installation failed
###############################################################################
function InstallSAS6Gbps
{
  # Choose path based on supported distros.
  local RPM_PATH=""
  if [ "$LINUX_DISTRO" == "rhel" ]; then
    if [ "$LINUX_REV" == "5.4" ]; then
      RPM_PATH="$PATH_TO_RPMS/mpt2sas/rhel54"
    fi
  elif [ "$LINUX_DISTRO" == "sles" ]; then
    if [ "$LINUX_REV" == "10.3" ]; then
      RPM_PATH="$PATH_TO_RPMS/mpt2sas/sles10sp3"
    elif [ "$LINUX_REV" == "11.0" ]; then
      RPM_PATH="$PATH_TO_RPMS/mpt2sas/sles11"
    fi
  fi
  
  # Exit if RPM path doesn't exist.
  if [ ! -d "$RPM_PATH" ]; then return 1; fi

  # Exit if RPM package doesn't exist.
  local PKG_NAME="mpt2sas"
  local RPM_FILE=$(ls "$RPM_PATH/$PKG_NAME-"*.noarch.rpm 2>/dev/null)
  if [ ! -e "$RPM_FILE" ]; then return 1; fi

  # Install or update depending on current status.
  INSTALLED_PKG=$(rpm -q "$PKG_NAME" 2>/dev/null)
  if [ $? -ne 0 ]; then
    echo "Installing 6Gbps SAS adapter driver ($PKG_NAME)..."
    rpm -ihv "$RPM_FILE"
    REBOOT_NEEDED="true"
  else
    local RPM_PKG=$(rpm -qp "$RPM_FILE" 2>/dev/null)
    if [ "$RPM_PKG" \> "$INSTALLED_PKG" ]; then
      echo "Updating 6Gbps SAS adapter driver ($PKG_NAME)..."
      rpm -e "$PKG_NAME"
      rpm -ihv "$RPM_FILE"
      REBOOT_NEEDED="true"
    else
      echo "6Gpbs SAS adapter driver ($PKG_NAME) does not need to be updated."
    fi
  fi
}


###############################################################################
# Installs the MPP failover driver (linuxrdac).
#
# Returns:
#  0 - Installation succeeded
#  1 - There are no packages to install for the current distro
#  2 - Installation failed
###############################################################################
function InstallMPP
{
  # Choose path based on supported distros.
  local RPM_PATH=""
  if [ "$LINUX_DISTRO" == "rhel" ]; then
    if [ "$LINUX_REV_MAJOR" == "5" ]; then
      RPM_PATH="$PATH_TO_RPMS/linuxrdac/rhel5"
	elif [ "$LINUX_REV_MAJOR" == "6" ]; then
      RPM_PATH="$PATH_TO_RPMS/linuxrdac/rhel6"
	fi
  elif [ "$LINUX_DISTRO" == "sles" ]; then
    if [ "$LINUX_REV_MAJOR" == "10" ]; then
      RPM_PATH="$PATH_TO_RPMS/linuxrdac/sles10"
    elif [ "$LINUX_REV_MAJOR" == "11" ]; then
      RPM_PATH="$PATH_TO_RPMS/linuxrdac/sles11"
    fi
  fi

  # Exit if RPM path doesn't exist.
  if [ ! -d "$RPM_PATH" ]; then return 1; fi

  # Exit if RPM package doesn't exist.
  local PKG_NAME="linuxrdac"
  local RPM_FILE=$(ls "$RPM_PATH/$PKG_NAME-"*.noarch.rpm 2>/dev/null)
  if [ ! -e "$RPM_FILE" ]; then return 1; fi

  # Install or update depending on current status.
  INSTALLED_PKG=$(rpm -q "$PKG_NAME" 2>/dev/null)
  if [ $? -ne 0 ]; then
    echo "Installing MPP driver ($PKG_NAME)..."
    rpm -ihv "$RPM_FILE"
    REBOOT_NEEDED="true"
  else
    local RPM_PKG=$(rpm -qp "$RPM_FILE" 2>/dev/null)
    if [ "$RPM_PKG" \> "$INSTALLED_PKG" ]; then
      echo "Updating MPP driver ($PKG_NAME)..."
      rpm -e "$PKG_NAME"
      rpm -ihv "$RPM_FILE"
      REBOOT_NEEDED="true"
    else
      echo "MPP driver ($PKG_NAME) does not need to be updated."
    fi
  fi
}


###############################################################################
# Uninstalls the MPP failover driver (linuxrdac).
###############################################################################
function UninstallMPP
{
  # Uninstall if needed.
  local PKG_NAME="linuxrdac"
  INSTALLED_PKG=$(rpm -q "$PKG_NAME" 2>/dev/null)
  if [ $? -eq 0 ]; then
    echo "Uninstalling MPP driver ($PKG_NAME)..."
    rpm -e "${PKG_NAME}"
    REBOOT_NEEDED="true"
  fi
}

function GetArrayModelList
{
	case "$1" in
	  interactive)
  		echo ""
  		echo "###############################################################################"
  		echo "The following questions will be used to determine which, if any, additional"
  		echo "packages and system modifications are needed to support coexistence of the"
  		echo "specified PowerVault Modular Disk storage array models."
  		echo "###############################################################################"
  		echo ""
		  # Prompt for models.
		  Prompt "Will this host be attached to an MD3000 (SAS) [y/n]? "
		  if [ "$RESP_LOWERCASE" == "y" ]; then MD3000="true"; fi
		  Prompt "Will this host be attached to an MD3000i (iSCSI) [y/n]? "
		  if [ "$RESP_LOWERCASE" == "y" ]; then MD3000I="true"; fi
		  Prompt "Will this host be attached to an MD32xx (SAS) [y/n]? "
		  if [ "$RESP_LOWERCASE" == "y" ]; then MD32XX="true"; fi
		  Prompt "Will this host be attached to an MD32xxi or MD36xxi (iSCSI) [y/n]? "
		  if [ "$RESP_LOWERCASE" == "y" ]; then MD32XXI="true"; fi
		  Prompt "Will this host be attached to an MD36xxf (Fibre Channel) [y/n]? "
		  if [ "$RESP_LOWERCASE" == "y" ]; then MD36XXf="true"; fi
		  ;;
	  install)
		  # parse the parameter argument list seperated by comma
		  echo "Passed arrays are: " $2;
		  for array in ${2//,/ }; do
		  	  if [ "$array" == "md3000" ]; then MD3000="true"; fi
			  if [ "$array" == "md3000i" ]; then MD3000I="true"; fi
			  if [ "$array" == "md32xx" ]; then MD32XX="true"; fi
			  if [ "$array" == "md32xxi" ]; then MD32XXI="true"; fi
			  if [ "$array" == "md36xxi" ]; then MD32XXI="true"; fi
			  if [ "$array" == "md36xxf" ]; then MD36XXf="true"; fi
		  done
	  esac
}


###############################################################################
# Prompts the user for configuration details and installs the necessary
# packages and system modifications to enable coexistence accordingly.
#
# Returns:
#  0 - No updates are required
#  1 - Updates are required
###############################################################################
function InstallCoexistence
{
  # Exit if there is nothing to do.
  if [ -z "$MD3000" -a -z "$MD3000I" -a -z "$MD32XX" -a -z "$MD32XXI" -a -z "$MD36XXf" ]; then
    echo ""
    echo "No additional packages or system modifications are needed."
    exit $RETCODE_SUCCESS
  fi

  # Choose actions based upon selections.
  local INSTALL_SAS5E=""
  local INSTALL_SAS6GBPS=""
  local INSTALL_MPP=""
  local CHANGE_TIMEOUTS=""
  local USE_SHARED_TIMEOUTS=""
  if [ "$MD3000" == "true" -o "$MD3000I" == "true" ]; then INSTALL_MPP="true"; fi
  if [ "$MD3000" == "true" -a "$LINUX_DISTRO" == "sles" ]; then
    if [ "$LINUX_REV" == "10.3" ]; then INSTALL_SAS5E="true"; fi
  fi
  if [ "$MD3000I" == "true" -o "$MD32XXI" == "true" ]; then CHANGE_TIMEOUTS="true"; fi
  if [ "$MD3000I" == "true" ]; then USE_SHARED_TIMEOUTS="true"; fi
  if [ "$MD32XX" == "true" ]; then
    if [ "$LINUX_DISTRO" == "rhel" -a "$LINUX_REV" == "5.4" ] ||
       [ "$LINUX_DISTRO" == "sles" -a "$LINUX_REV" == "10.3" ] ||
       [ "$LINUX_DISTRO" == "sles" -a "$LINUX_REV" == "11.0" ]; then
      INSTALL_SAS6GBPS="true"
    fi
  fi

  # Exit if there is nothing to do.  
  if [ -z "$INSTALL_SAS5E" -a -z "$INSTALL_SAS6GBPS" -a -z "$INSTALL_MPP" -a -z "$CHANGE_TIMEOUTS" ]; then
    echo ""
    echo "No additional packages or system modifications are needed."
    exit $RETCODE_SUCCESS
  fi

  # Display summary of actions and prompt to continue.
  echo ""
  echo "The following actions will be performed:"
  if [ "$INSTALL_SAS5E" == "true" ]; then echo " * Install or update SAS 5/e adapter driver (mptlinux)"; fi
  if [ "$INSTALL_SAS6GBPS" == "true" ]; then echo " * Install or update 6Gbps SAS adapter driver (mpt2sas)"; fi
  if [ "$INSTALL_MPP" == "true" ]; then echo " * Install or update MPP driver (linuxrdac)"; fi
  if [ "$CHANGE_TIMEOUTS" == "true" ]; then
    if [ "$USE_SHARED_TIMEOUTS" == "true" ]; then
      echo " * Update iSCSI initiator timings to coexistence values"
    else
      echo " * Update iSCSI initiator timings to fast failover values"
    fi
  fi
  echo ""

  # Install / Update SAS 5/e.
  if [ "$INSTALL_SAS5E" == "true" ]; then
    InstallSAS5E
  fi

  # Install / Update 6Gbps SAS.
  if [ "$INSTALL_SAS6GBPS" == "true" ]; then
    InstallSAS6Gbps
  fi

  # Install / Update MPP.
  if [ "$INSTALL_MPP" == "true" ]; then
    InstallMPP
  fi

  # Update iSCSI timings.
  if [ "$CHANGE_TIMEOUTS" == "true" ]; then
    if [ "$USE_SHARED_TIMEOUTS" == "true" ]; then
      echo "Updating iSCSI initiator timings to coexistence values..."
      UpdateTimings $COEX_REPLACEMENT_TIMEOUT $COEX_NOOP_OUT_TIMEOUT
    else
      echo "Updating iSCSI initiator timings to fast failover values..."
      UpdateTimings $FAST_FAILOVER_REPLACEMENT_TIMEOUT $FAST_FAILOVER_NOOP_OUT_TIMEOUT
    fi
  fi
  
  # Copy utility to system if it's newer than an existing version or not already present.
  local SCRIPT_INSTALL_PATH="/usr/local/bin"
  if [ -d "$SCRIPT_INSTALL_PATH" ]; then
    local SCRIPT_NAME=$(basename $0)
    local INSTALLED_SCRIPT_VER=""
    local CURRENT_SCRIPT_VER=""
    local MODULE_VERSION_PATTERN="^#[[:space:]]*Module Version:[[:space:]]*"
    if [ -e "$SCRIPT_INSTALL_PATH/$SCRIPT_NAME" ]; then
      INSTALLED_SCRIPT_VER=$(grep "$MODULE_VERSION_PATTERN" "$SCRIPT_INSTALL_PATH/$SCRIPT_NAME" | sed "s/$MODULE_VERSION_PATTERN//")
    fi
    CURRENT_SCRIPT_VER=$(grep "$MODULE_VERSION_PATTERN" "$0" | sed "s/$MODULE_VERSION_PATTERN//")
    if [ -z "$INSTALLED_SCRIPT_VER" -o "$CURRENT_SCRIPT_VER" \> "$INSTALLED_SCRIPT_VER" ]; then
      cp "$0" "$SCRIPT_INSTALL_PATH/"
    fi
  fi
  
  # Reboot notification.
  if [ "$REBOOT_NEEDED" == "true" ]; then
    echo ""
    echo "*** A system reboot is required to complete driver installation. ***"
  fi
}


###############################################################################
# Uninstalls MPP and adjusts timings to fast failover settings if MD3000
# and MD3000i arrays will no longer be attached.
###############################################################################
function UninstallCoexistence
{
  #echo ""
  #echo "###############################################################################"
  #echo "IMPORTANT:"
  #echo "The uninstall mode only needs to be run if you will no longer be connecting"
  #echo "this host to either an MD3000 or MD3000i."
  #echo ""
  #echo "If you wish to disconnect from all MD storage arrays, run the overall storage"
  #echo "software uninstaller."
  #echo ""
  #echo "If you wish to disconnect from either an MD32xx, MD32xxi, or MD36xxi, no"
  #echo "further system modifications are required."
  #echo "###############################################################################"
  #echo ""

  # Flags for user selections.  
  local PERFORM_UNINSTALL=""

  # Only uninstall mpp and adjust timings.  Other devices may be using SAS HBAs and/or initiator.

  # Display summary of actions 
  echo ""
  echo "The following actions will be performed:"
  echo " * Remove MPP Driver (linuxrdac)"
  if [ ! -z "$ISCSI_VERSION" ]; then
    echo " * Update iSCSI initiator timings to fast failover values"
  fi
  echo ""

  UninstallMPP

  # Update iSCSI timings.
  if [ ! -z "$ISCSI_VERSION" ]; then
    echo "Updating iSCSI initiator timings to fast failover values..."
    UpdateTimings $FAST_FAILOVER_REPLACEMENT_TIMEOUT $FAST_FAILOVER_NOOP_OUT_TIMEOUT
  fi

  # Reboot notification.
  if [ "$REBOOT_NEEDED" == "true" ]; then
    echo ""
    echo "*** A system reboot is required to complete driver uninstallation. ***"
  fi
}


###############################################################################
# Displays coexistence status information. 
###############################################################################
function DisplayStatus
{
  # Test MD3000 support.
  local MD3000_SUPPORT="Updates Needed"
  TestMD3000Support
  if [ $? -eq 0 ]; then
    MD3000_SUPPORT="Coexistence Supported"
  fi
  
  # Test MD3000i support.
  local MD3000I_SUPPORT="Updates Needed"
  TestMD3000iSupport
  if [ $? -eq 0 ]; then
    MD3000I_SUPPORT="Coexistence Supported"
  fi
  
  # Test MD32xx support.
  local MD32XX_SUPPORT="Updates Needed"
  TestMD32xxSupport
  if [ $? -eq 0 ]; then
    MD32XX_SUPPORT="Coexistence Supported"
  fi
  
  # Test MD32xxi support.
  local MD32XXI_SUPPORT="Updates Needed"
  TestMD32xxiSupport
  if [ $? -eq 0 ]; then
    MD32XXI_SUPPORT="Coexistence Supported"
  fi

  # Test MD36xxi support.
  local MD36XXI_SUPPORT="Updates Needed"
  TestMD36xxiSupport
  if [ $? -eq 0 ]; then
    MD36XXI_SUPPORT="Coexistence Supported"
  fi
  
  # MPP (linuxrdac).
  local MPP_VERSION_STRING="$MPP_VERSION"
  if [ -z "$MPP_VERSION" ]; then
    MPP_VERSION_STRING="Not Installed"
  fi
  
  # DM (scsi_dh_rdac).
  local SCSI_DH_VERSION_STRING="$SCSI_DH_VERSION"
  if [ -z "$SCSI_DH_VERSION" ]; then
    SCSI_DH_VERSION_STRING="Not Installed"
  fi
  
  # SAS 5/e (mptlinux).
  local MPT_SAS_VERSION_STRING="$MPT_SAS_VERSION"
  if [ -z "$MPT_SAS_VERSION" ]; then
    MPT_SAS_VERSION_STRING="Not Installed"
  fi

  # 6Gbps SAS HBA (mpt2sas).
  local MPT2_SAS_VERSION_STRING="$MPT2_SAS_VERSION"
  if [ -z "$MPT2_SAS_VERSION" ]; then
    MPT2_SAS_VERSION_STRING="Not Installed"
  fi
  
  # iSCSI Initiator.
  local ISCSI_VERSION_STRING="$ISCSI_VERSION"
  if [ -z "$ISCSI_VERSION" ]; then
    ISCSI_VERSION_STRING="Not Installed"
  fi
  
  # Replacement Timeout.
  local REPLACEMENT_TIMEOUT_STRING="$REPLACEMENT_TIMEOUT"
  if [ -z "$REPLACEMENT_TIMEOUT" ]; then
    REPLACEMENT_TIMEOUT_STRING="Not Set"
  fi

  # Noop Out Timeout.
  local NOOP_OUT_TIMEOUT_STRING="$NOOP_OUT_TIMEOUT"
  if [ -z "$NOOP_OUT_TIMEOUT" ]; then
    NOOP_OUT_TIMEOUT_STRING="Not Set"
  fi

  echo ""
  echo "###############################################################################"
  echo "PowerVault Modular Disk Storage Array Coexistence Support Summary:"
  echo ""
  echo " This summary indicates which PowerVault MD models this host has the minimum"
  echo " components required to connect with in a coexistence scenario."
  echo ""
  echo " MD3000 (SAS): $MD3000_SUPPORT"
  echo " MD3000i (iSCSI): $MD3000I_SUPPORT"
  echo " MD32xx (SAS): $MD32XX_SUPPORT"
  echo " MD32xxi (iSCSI): $MD32XXI_SUPPORT"
  echo " MD36xxi (iSCSI): $MD36XXI_SUPPORT"
  echo ""
  echo "Configuration Details:"
  echo " MD3000 / MD3000i Failover Driver (linuxrdac): $MPP_VERSION_STRING"
  echo " Device Mapper Failover Driver (scsi_dh_rdac): $SCSI_DH_VERSION_STRING"
  echo " SAS 5/e Adapter Driver (mptsas): $MPT_SAS_VERSION_STRING"
  echo " 6Gbps SAS Adapter Driver (mpt2sas): $MPT2_SAS_VERSION_STRING"
  echo " iSCSI Initiator: $ISCSI_VERSION_STRING"
  if [ ! -z "$ISCSI_VERSION" ]; then
    echo " iSCSI Replacement Timeout: $REPLACEMENT_TIMEOUT_STRING"
    echo " iSCSI Noop Out Timeout: $NOOP_OUT_TIMEOUT_STRING"
  fi
  echo "###############################################################################"
}


###############################################################################
# Main Execution Starts Here
###############################################################################

case "$1" in
  interactive)
    CheckLinuxDist
    CheckResourceMediaLaunch
    GetConfigurationDetails
    CheckResourceMediaInstalled
    GetArrayModelList $@
    InstallCoexistence
    ;;

  install)
    CheckLinuxDist
    #CheckResourceMediaLaunch
    GetConfigurationDetails
    #CheckResourceMediaInstalled
    GetArrayModelList $@
    InstallCoexistence 
    ;;

  uninstall)
    CheckLinuxDist
    GetConfigurationDetails
    UninstallCoexistence
    ;;

  status)
    CheckLinuxDist
    GetConfigurationDetails
    DisplayStatus
    ;;

  *)
    echo "Usage: $(basename $0) {install/interactive/uninstall/status}"
    echo " install"
    echo "   Applies modifications to the host necessary to support coexistence"
    echo "   of certain PowerVault Modular Disk storage array models based on passing array model parameters."
    echo ""
    echo " interactive"
    echo "   Applies modifications to the host necessary to support coexistence"
    echo "   of certain PowerVault Modular Disk storage array models based on user input."
    echo ""
    echo " uninstall"
    echo "   Reverts the host to a state suitable for only Device Mapper based"
    echo "   PowerVault storage array operation."
    echo ""
    echo " status"
    echo "   Displays coexistence status."
    exit $RETCODE_INVALID_USAGE
    ;;
esac



