#!/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: install_dm.sh
# Module Version: 1.0.1.9
# Environment: Linux
#
#
# Abstract/Purpose:
#
#   Installs Device Mapper RDAC driver and associated tools for multipath
#   support.
#
#
# Parameters:
#
#   rpm_path - The path to the base directory containing RPMS.  The script's
#              current directory will be used if not specified.
#
#
# Returns:
#
#   0 - The installation was successful or DM is already installed
#   1 - RPM path invalid
#   2 - Unsupported Linux distro
#   3 - Package dependencies failed
#   4 - Unable to install or update scsi_dh_rdac package
#   5 - multipath.conf file not accessible
#   6 - modprobe.conf file not accessible
#
###############################################################################

# Return codes.
RETCODE_SUCCESS=0
RETCODE_INVALID_RPM_PATH=1
RETCODE_UNSUPPORTED_DISTRO=2
RETCODE_DEPENDENCIES_FAILED=3
RETCODE_RDAC_INSTALL_FAILED=4
RETCODE_MULTIPATH_CONF_NOT_ACCESSIBLE=5
RETCODE_MODPROBE_CONF_NOT_ACCESSIBLE=6

# Get command line params.  Usage.
PATH_TO_RPMS="${1}"
if [ -n "${PATH_TO_RPMS}" -a ! -d "${PATH_TO_RPMS}" ]; then
  echo "Usage: $(basename $0) rpm_path"
  exit $RETCODE_INVALID_RPM_PATH
elif [ -z "${PATH_TO_RPMS}" ]; then
  PATH_TO_RPMS=$(dirname $0)
fi


###############################################################################
# Sets up messages printed during the install.
###############################################################################
function SetupStrings
{
  IDS_UNSUPPORTED_DISTRO="The current linux distribution is not supported."
  IDS_CHECKING_LINUX_DISTRO="Checking Linux distribution..."
  IDS_REQUIRED_PACKAGES="The following packages are required for DM installation and do not appear to be installed:"
  IDS_INSTALLATION_ABORTED="Installation aborted.  Unable to install"
  IDS_INSTALLATION_ABORTED_MODIFY="Installation aborted.  Unable to modify"
  IDS_INSTALLING="Installing"
  IDS_UPDATING="Updating"
  IDS_CHECK_UPDATE="Checking if update is needed for"
  IDS_UPDATE_YES="yes"
  IDS_UPDATE_NO="no"
  IDS_RAMDISK_REBUILD="Rebuilding ramdisk image"
  IDS_RAMDISK_SAVE="Saving old initrd as"
  IDS_RAMDISK_NEW="Making new initrd as"
  IDS_REBOOT_REQUIRED="*** A system reboot is required for driver installation to complete. ***"
}


###############################################################################
# Gets information about the Linux distribution and checks if it is supported.
###############################################################################
function CheckLinuxDist
{
  echo -n "${IDS_CHECKING_LINUX_DISTRO}"

  # 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
}


###############################################################################
# Checks if the packages required by the DM drivers are installed and
# displays an error and exits if not.
###############################################################################
function CheckPackageDeps
{
  local MISSING_PACKAGES=()

  # Ensure DKMS is installed.
  INSTALLED_DKMS_PKG=$(rpm -q dkms)
  if [ $? -ne 0 ]; then
    MISSING_PACKAGES[${#MISSING_PACKAGES[@]}]="dkms"
  fi

  # Ensure GCC is installed.
  #INSTALLED_GCC_PKG=$(rpm -q gcc)
  #if [ $? -ne 0 ]; then
  #  MISSING_PACKAGES[${#MISSING_PACKAGES[@]}]="gcc"
  #fi


  # Choose kernel source package based on Linux distro.
  #local KERNEL_SRC_PKG=""
  #if [ "${LINUX_DISTRO}" == "rhel" ]; then
  #  KERNEL_SRC_PKG=kernel-devel
  #elif [ "${LINUX_DISTRO}" == "sles" ]; then
  #  KERNEL_SRC_PKG=kernel-source
  #fi

  # Ensure kernel source is installed.
  #INSTALLED_KERNEL_SRC_PKG=$(rpm -q ${KERNEL_SRC_PKG})
  #if [ $? -ne 0 ]; then
  #  MISSING_PACKAGES[${#MISSING_PACKAGES[@]}]="${KERNEL_SRC_PKG}"
  #fi


  if [ ${#MISSING_PACKAGES[@]} -gt 0 ]; then
   echo "${IDS_REQUIRED_PACKAGES}"
   for element in ${MISSING_PACKAGES[@]}; do
     echo " * ${element}"
   done
   exit $RETCODE_DEPENDENCIES_FAILED
  fi
}


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

  # Update file if needed.
  echo -n "${IDS_CHECK_UPDATE} ${FILE_TO_MODIFY}..."
  IGNORE=$(diff "${FILE_TO_MODIFY}" "${FILE_MODIFIED}")
  if [ $? -eq 0 ]; then
    echo "${IDS_UPDATE_NO}"
    rm -f "${FILE_MODIFIED}"
  else
    echo "${IDS_UPDATE_YES}"
    echo "${IDS_UPDATING} ${FILE_TO_MODIFY}..."
    cp -f "${FILE_MODIFIED}" "${FILE_TO_MODIFY}"
    rm -f "${FILE_MODIFIED}"
  fi
}


###############################################################################
# Updates the SLES fixed disk storage policy if needed.
###############################################################################
function UpdateSLES11StoragePolicy
{
  local FILE_TO_MODIFY="/usr/share/hal/fdi/policy/10osvendor/99-storage-policy-fixed-drives.fdi"
  local FILE_MODIFIED="/usr/share/hal/fdi/policy/10osvendor/99-storage-policy-fixed-drives.fdi.dell_dm_modified"
  
  # Create file if it doesn't exist.
  if [ ! -e "${FILE_TO_MODIFY}" ]; then
    touch "${FILE_TO_MODIFY}"
  fi

  # Strip old entries and make modifications.
  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 {next;}
    {print $0};
   ' <"${FILE_TO_MODIFY}" | awk '
    function begin_modify () {print "<!-- Begin Dell MD Modification -->";}
    function end_modify () {print "<!-- End Dell MD Modification -->";}
    function add_match_entries () {
      if (deviceinfo_exists != 1) {print "<deviceinfo version=\"0.2\">";}
      if (device_exists != 1) {print "  <device>";}
      if (hotplug_set != 1) {
        print "    <match key=\"@block.storage_device:storage.hotpluggable\" bool=\"false\">";
        print "      <match key=\"@block.storage_device:storage.removable\" bool=\"false\">";
        print "        <merge key=\"volume.ignore\" type=\"bool\">true</merge>";
        print "      </match>";
        print "    </match>";
        hotplug_set=1;
      }
      if (device_exists != 1) {print "  </device>";}
      if (deviceinfo_exists != 1) {print "</deviceinfo>";}
    }
    /^.*<deviceinfo.*>/ {in_deviceinfo=1; deviceinfo_exists=1; print $0; next;}
    in_deviceinfo == 1 {if (match($0, "<device.*>") != 0) {in_device=1; device_exists=1; print $0; next;}}
    in_device==1 {
      if (match($0, "<match key=\"@block.storage_device:storage.hotpluggable\" bool=\"false\".*>") != 0) {
        hotplug_exists=1;
        print $0;
        next;
      }
    }
    /^.*<\/device>/ {if (hotplug_set != 1) {begin_modify(); add_match_entries(); end_modify();} print $0; in_device=0; next;}
    {print $0;}
    END {if (hotplug_set != 1) {begin_modify(); add_match_entries(); end_modify();}}
  ' >>"${FILE_MODIFIED}"

  # Update file if needed.
  CompareAndUpdateFile "${FILE_TO_MODIFY}" "${FILE_MODIFIED}"
}


###############################################################################
# Updates the RHEL 5.4 udev multipath rules if needed.
###############################################################################
function UpdateRHEL54UDevRules
{
  local FILE_TO_MODIFY="/etc/udev/rules.d/40-multipath.rules"
  local FILE_MODIFIED="/etc/udev/rules.d/40-multipath.rules.dell_dm_modified"

  # Create blank rules file if it doesn't exist.
  if [ ! -e "${FILE_TO_MODIFY}" ]; then
    touch "${FILE_TO_MODIFY}"
  fi

  # Strip old entries, comment current rules, and append modified rules.
  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 '
    function begin_modify () {print "# Begin Dell MD Modification";}
    function end_modify () {print "# End Dell MD Modification";}
    BEGIN { begin_modify(); }
    {print "# "$0;}
    END {
      end_modify();
      begin_modify();
      print "SUBSYSTEM!=\"block\", GOTO=\"end_mpath\"";
      print "KERNEL!=\"dm-[0-9]*\", GOTO=\"end_mpath\"";
      print "PROGRAM!=\"/sbin/mpath_wait %M %m\", GOTO=\"end_mpath\"";
      print "PROGRAM!=\"/sbin/dmsetup info -c --noheadings -j %M -m %m\", GOTO=\"end_mpath\"";
      print "RESULT!=\"*:*:*:*:*:*:*:mpath-*\", GOTO=\"kpartx_check\"";
      print "PROGRAM==\"/sbin/dmsetup info -c --noheadings -o name -j %M -m %m\", RESULT==\"?*\", NAME=\"%k\", SYMLINK=\"mpath/%c\", RUN+=\"/sbin/kpartx -a -p p /dev/mapper/%c\"";
      print "OPTIONS=\"last_rule\"";
      print "LABEL=\"kpartx_check\"";
      print "RESULT!=\"*:*:*:*:*:*:*:part*-mpath-*\", GOTO=\"end_mpath\"";
      print "PROGRAM==\"/sbin/dmsetup info -c --noheadings -o name -j %M -m %m\", RESULT==\"?*\", NAME=\"%k\", SYMLINK=\"mpath/%c\"";
      print "OPTIONS=\"last_rule\"";
      print "LABEL=\"end_mpath\"";
      end_modify();
    }
  ' >"${FILE_MODIFIED}"

  # Update file if needed.
  CompareAndUpdateFile "${FILE_TO_MODIFY}" "${FILE_MODIFIED}"
}


###############################################################################
# Updates the mutlipath.conf file with MD devices if needed.  Displays an error
# and exits if conf file is not accessible.
###############################################################################
function UpdateMultipathConf
{
  local FILE_TO_MODIFY=/etc/multipath.conf
  local FILE_MODIFIED=/etc/multipath.conf.dell_dm_modified

  # Copy default multipath conf file for SLES if it doesn't exist.
  if [ "${LINUX_DISTRO}" == "sles" -a ! -e "${FILE_TO_MODIFY}" ]; then
    local MULTIPATH_CONF_SYNTHETIC=/usr/share/doc/packages/multipath-tools/multipath.conf.synthetic
    if [ -e "${MULTIPATH_CONF_SYNTHETIC}" ]; then
      cp "${MULTIPATH_CONF_SYNTHETIC}" "${FILE_TO_MODIFY}"
    fi
  fi

  # Copy default multipath conf file for RHEL 6 if it doesn't exist.
  if [ "${LINUX_DISTRO}" == "rhel" -a "${LINUX_REV_MAJOR}" == "6" -a ! -e "${FILE_TO_MODIFY}" ]; then
    local MULTIPATH_CONF_SYNTHETIC=$(ls /usr/share/doc/device-mapper-multipath-*/multipath.conf.synthetic | head -n 1)
    if [ -e "${MULTIPATH_CONF_SYNTHETIC}" ]; then
      cp "${MULTIPATH_CONF_SYNTHETIC}" "${FILE_TO_MODIFY}"
    fi
  fi

  # Display error and exit if conf file can't be accessed.
  if [ ! -e "${FILE_TO_MODIFY}" ]; then
    echo "${IDS_INSTALLATION_ABORTED_MODIFY} ${FILE_TO_MODIFY}"
    exit $RETCODE_MULTIPATH_CONF_NOT_ACCESSIBLE
  fi
  
  # If the OS is RHEL 6.2 or RHEL 6.1
if [ "${LINUX_DISTRO}" == "sles" -a "${LINUX_REV}" == "11.2" ]; then
  # Strip old entries and modify conf file. Also adds functionality to support followover mode for ALUA.
	  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 '
		function begin_modify () {print "# Begin Dell MD Modification";}
		function end_modify () {print "# End Dell MD Modification";}
		function comment_line() {if ($1 != "#") {begin_modify(); print "# "$0; end_modify();} else {print $0;}}
		function add_blacklist_entries() {
		  if (universal_xport_set != 1) {
			print "	device {";
			print "		vendor  \"*\"";
			print "		product \"Universal Xport\"";
			print "	}";
		  }
		  if (md3000_set != 1) {
			print "	device {";
			print "		vendor  \"*\"";
			print "		product \"MD3000\"";
			print "	}";
		  }
		  if (md3000i_set != 1) {
			print "	device {";
			print "		vendor  \"*\"";
			print "		product \"MD3000i\"";
			print "	}";
		  }
		  if (virtual_disk_set != 1) {
			print "	device {";
			print "		vendor  \"*\"";
			print "		product \"Virtual Disk\"";
			print "	}";
		  }
		}
		function add_defaults_entries () {
		  if (ufn_set != 1) {print "	user_friendly_names yes";}
		  if (max_fds_set != 1) {print "	max_fds             8192";}
		  print "  polling_interval      5";
		}
		function add_devices_entries () {
		  print "	device {";
		  print "		vendor                \"DELL\"";
		  print "		product               \"MD32xxi\"";
		  print "		path_grouping_policy  group_by_prio";
		  print "		prio                  rdac";
		  print "		path_checker          rdac";
		  print "		path_selector         \"round-robin 0\"";
		  print "		hardware_handler      \"1 rdac\"";
		  print "		failback              immediate";
		  print "		features              \"2 pg_init_retries 50\"";
		  print "		no_path_retry         30";
		  print "		rr_min_io             100";
		  print "	}";
		  print "	device {";
		  print "		vendor                \"DELL\"";
		  print "		product               \"MD32xx\"";
		  print "		path_grouping_policy  group_by_prio";
		  print "		prio                  rdac";
		  print "		path_checker          rdac";
		  print "		path_selector         \"round-robin 0\"";
		  print "		hardware_handler      \"1 rdac\"";
		  print "		failback              immediate";
		  print "		features              \"2 pg_init_retries 50\"";
		  print "		no_path_retry         30";
		  print "		rr_min_io             100";
		  print "	}";
		  print "	device {";
		  print "		vendor                \"DELL\"";
		  print "		product               \"MD36xxi\"";
		  print "		path_grouping_policy  group_by_prio";
		  print "		prio                  rdac";
		  print "		path_checker          rdac";
		  print "		path_selector         \"round-robin 0\"";
		  print "		hardware_handler      \"1 rdac\"";
		  print "		failback              immediate";
		  print "		features              \"2 pg_init_retries 50\"";
		  print "		no_path_retry         30";
		  print "		rr_min_io             100";
		  print "	}";
			print "	device {";
		  print "		vendor                \"DELL\"";
		  print "		product               \"MD36xxf\"";
		  print "		path_grouping_policy  group_by_prio";
		  print "		prio                  rdac";
		  print "		path_checker          rdac";
		  print "		path_selector         \"round-robin 0\"";
		  print "		hardware_handler      \"1 rdac\"";
		  print "		failback              immediate";
		  print "		features              \"2 pg_init_retries 50\"";
		  print "		no_path_retry         30";
		  print "		rr_min_io             100";  
		  print "	}";
		}
		/^blacklist.*{/ {in_blacklist=1; blacklist_exists=1; print $0; next;}
		in_blacklist==1 {
		  if (match($0, "devnode \"[*]\"") != 0) {comment_line(); next;}
		  if (match($0, "product \"Universal Xport\"") != 0) {print $0; universal_xport_set=1; next;}
		  if (match($0, "product \"MD3000\"") != 0) {print $0; md3000_set=1; next;}
		  if (match($0, "product \"MD3000i\"") != 0) {print $0; md3000i_set=1; next;}
		  if (match($0, "product \"Virtual Disk\"") != 0) {print $0; virtual_disk_set=1; next;}
		}
		/^}/ {
		  if (in_blacklist == 1) {
			if (universal_xport_set != 1 || md3000_set != 1 || md3000i_set != 1 || virtual_disk_set != 1) {
			  begin_modify();
			  add_blacklist_entries();
			  end_modify();
			}
			print $0;
			in_blacklist=0;
			next;
		  }
		}
		/^defaults.*{/ {in_defaults=1; defaults_exists=1; print $0; next;}
		in_defaults==1 {
		  if (match($0, "user_friendly_names") != 0) {if ($2 != "yes") {comment_line();} else {print $0; ufn_set=1;} next;}
		  if (match($0, "max_fds") != 0) {if ($2 != 8192) {comment_line();} else {print $0; max_fds_set=1;} next;}
		}
		/^}/ {if (in_defaults == 1) {if (ufn_set != 1 || max_fds_set != 1) {begin_modify(); add_defaults_entries(); end_modify();} print $0; in_defaults=0; next;}}
		/^devices.*{/ {in_devices=1; devices_exists=1; print $0; next;}
		in_devices==1 {
		  if (match($0, "MD32xx") != 0) {print $0; devices_set=1; next;}
		}
		/^}/ {if (in_devices == 1) {if (devices_set != 1) {begin_modify(); add_devices_entries(); end_modify();} print $0; in_devices=0; next;}}
		{print $0};
		END {
		  if (blacklist_exists != 1 || defaults_exists != 1 || devices_exists != 1) {
			begin_modify();
			if (blacklist_exists != 1) {
			  print "blacklist {";
			  add_blacklist_entries();
			 print "}";
			}
			if (defaults_exists != 1) {
			  print "defaults {";
			  add_defaults_entries();
			  print "}";
			}
			if (devices_exists != 1) {
			  print "devices {";
			  add_devices_entries();
			  print "}"
			}
			end_modify();
		  }
		}
	  ' >"${FILE_MODIFIED}"
  # if the OS is any version of RHEL 6
elif [ "${LINUX_DISTRO}" == "rhel" -a "${LINUX_REV}" == "6.1" ] || [ "${LINUX_DISTRO}" == "rhel" -a "${LINUX_REV}" == "6.2" ] || [ "${LINUX_DISTRO}" == "rhel" -a "${LINUX_REV}" == "6.3" ]; then
	# Strip old entries and modify conf file. Also adds functionality to support followover mode for ALUA.
	  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 '
		function begin_modify () {print "# Begin Dell MD Modification";}
		function end_modify () {print "# End Dell MD Modification";}
		function comment_line() {if ($1 != "#") {begin_modify(); print "# "$0; end_modify();} else {print $0;}}
		function add_blacklist_entries() {
		  if (universal_xport_set != 1) {
			print "	device {";
			print "		vendor  \"*\"";
			print "		product \"Universal Xport\"";
			print "	}";
		  }
		  if (md3000_set != 1) {
			print "	device {";
			print "		vendor  \"*\"";
			print "		product \"MD3000\"";
			print "	}";
		  }
		  if (md3000i_set != 1) {
			print "	device {";
			print "		vendor  \"*\"";
			print "		product \"MD3000i\"";
			print "	}";
		  }
		  if (virtual_disk_set != 1) {
			print "	device {";
			print "		vendor  \"*\"";
			print "		product \"Virtual Disk\"";
			print "	}";
		  }
		}
		function add_defaults_entries () {
		  if (ufn_set != 1) {print "	user_friendly_names yes";}
		  if (max_fds_set != 1) {print "	max_fds             8192";}
		  print "  polling_interval      5";
		  print "  queue_without_daemon  no";
		}
		function add_devices_entries () {
		  print "	device {";
		  print "		vendor                \"DELL\"";
		  print "		product               \"MD32xxi\"";
		  print "		path_grouping_policy  group_by_prio";
		  print "		prio                  rdac";
		  print "		path_checker          rdac";
		  print "		path_selector         \"round-robin 0\"";
		  print "		hardware_handler      \"1 rdac\"";
		  print "		failback              immediate";
		  print "		features              \"2 pg_init_retries 50\"";
		  print "		no_path_retry         30";
		  print "		rr_min_io             100";
		  print "	}";
		  print "	device {";
		  print "		vendor                \"DELL\"";
		  print "		product               \"MD32xx\"";
		  print "		path_grouping_policy  group_by_prio";
		  print "		prio                  rdac";
		  print "		path_checker          rdac";
		  print "		path_selector         \"round-robin 0\"";
		  print "		hardware_handler      \"1 rdac\"";
		  print "		failback              immediate";
		  print "		features              \"2 pg_init_retries 50\"";
		  print "		no_path_retry         30";
		  print "		rr_min_io             100";
		  print "	}";
		  print "	device {";
		  print "		vendor                \"DELL\"";
		  print "		product               \"MD36xxi\"";
		  print "		path_grouping_policy  group_by_prio";
		  print "		prio                  rdac";
		  print "		path_checker          rdac";
		  print "		path_selector         \"round-robin 0\"";
		  print "		hardware_handler      \"1 rdac\"";
		  print "		failback              immediate";
		  print "		features              \"2 pg_init_retries 50\"";
		  print "		no_path_retry         30";
		  print "		rr_min_io             100";
		  print "	}";
			print "	device {";
		  print "		vendor                \"DELL\"";
		  print "		product               \"MD36xxf\"";
		  print "		path_grouping_policy  group_by_prio";
		  print "		prio                  rdac";
		  print "		path_checker          rdac";
		  print "		path_selector         \"round-robin 0\"";
		  print "		hardware_handler      \"1 rdac\"";
		  print "		failback              immediate";
		  print "		features              \"2 pg_init_retries 50\"";
		  print "		no_path_retry         30";
		  print "		rr_min_io             100";
		  print "	}";
		}
		/^blacklist.*{/ {in_blacklist=1; blacklist_exists=1; print $0; next;}
		in_blacklist==1 {
		  if (match($0, "devnode \"[*]\"") != 0) {comment_line(); next;}
		  if (match($0, "product \"Universal Xport\"") != 0) {print $0; universal_xport_set=1; next;}
		  if (match($0, "product \"MD3000\"") != 0) {print $0; md3000_set=1; next;}
		  if (match($0, "product \"MD3000i\"") != 0) {print $0; md3000i_set=1; next;}
		  if (match($0, "product \"Virtual Disk\"") != 0) {print $0; virtual_disk_set=1; next;}
		}
		/^}/ {
		  if (in_blacklist == 1) {
			if (universal_xport_set != 1 || md3000_set != 1 || md3000i_set != 1 || virtual_disk_set != 1) {
			  begin_modify();
			  add_blacklist_entries();
			  end_modify();
			}
			print $0;
			in_blacklist=0;
			next;
		  }
		}
		/^defaults.*{/ {in_defaults=1; defaults_exists=1; print $0; next;}
		in_defaults==1 {
		  if (match($0, "user_friendly_names") != 0) {if ($2 != "yes") {comment_line();} else {print $0; ufn_set=1;} next;}
		  if (match($0, "max_fds") != 0) {if ($2 != 8192) {comment_line();} else {print $0; max_fds_set=1;} next;}
		}
		/^}/ {if (in_defaults == 1) {if (ufn_set != 1 || max_fds_set != 1) {begin_modify(); add_defaults_entries(); end_modify();} print $0; in_defaults=0; next;}}
		/^devices.*{/ {in_devices=1; devices_exists=1; print $0; next;}
		in_devices==1 {
		  if (match($0, "MD32xx") != 0) {print $0; devices_set=1; next;}
		}
		/^}/ {if (in_devices == 1) {if (devices_set != 1) {begin_modify(); add_devices_entries(); end_modify();} print $0; in_devices=0; next;}}
		{print $0};
		END {
		  if (blacklist_exists != 1 || defaults_exists != 1 || devices_exists != 1) {
			begin_modify();
			if (blacklist_exists != 1) {
			  print "blacklist {";
			  add_blacklist_entries();
			 print "}";
			}
			if (defaults_exists != 1) {
			  print "defaults {";
			  add_defaults_entries();
			  print "}";
			}
			if (devices_exists != 1) {
			  print "devices {";
			  add_devices_entries();
			  print "}"
			}
			end_modify();
		  }
		}
	  ' >"${FILE_MODIFIED}"
	  
  # if the OS is any of the RHEL 5 versions
elif [ "${LINUX_DISTRO}" == "rhel" -a "${LINUX_REV_MAJOR}" == "5" ]; then
  # 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 '
    function begin_modify () {print "# Begin Dell MD Modification";}
    function end_modify () {print "# End Dell MD Modification";}
    function comment_line() {if ($1 != "#") {begin_modify(); print "# "$0; end_modify();} else {print $0;}}
    function add_blacklist_entries() {
      if (universal_xport_set != 1) {
        print "	device {";
        print "		vendor  \"*\"";
        print "		product \"Universal Xport\"";
        print "	}";
      }
      if (md3000_set != 1) {
        print "	device {";
        print "		vendor  \"*\"";
        print "		product \"MD3000\"";
        print "	}";
      }
      if (md3000i_set != 1) {
        print "	device {";
        print "		vendor  \"*\"";
        print "		product \"MD3000i\"";
        print "	}";
      }
      if (virtual_disk_set != 1) {
        print "	device {";
        print "		vendor  \"*\"";
        print "		product \"Virtual Disk\"";
        print "	}";
      }
    }
    function add_defaults_entries () {
      if (ufn_set != 1) {print "	user_friendly_names yes";}
      if (max_fds_set != 1) {print "	max_fds             8192";}
	  print "  polling_interval      5";
    }
    function add_devices_entries () {
      print "	device {";
      print "		vendor                \"DELL\"";
      print "		product               \"MD32xxi\"";
      print "		path_grouping_policy  group_by_prio";
      print "		path_checker          rdac";
      print "		path_selector         \"round-robin 0\"";
      print "		hardware_handler      \"1 rdac\"";
      print "		failback              immediate";
      print "		features              \"2 pg_init_retries 50\"";
      print "		no_path_retry         30";
      print "		rr_min_io             100";
      print "		prio_callout          \"/sbin/mpath_prio_rdac /dev/%n\"";
      print "	}";
      print "	device {";
      print "		vendor                \"DELL\"";
      print "		product               \"MD32xx\"";
      print "		path_grouping_policy  group_by_prio";
      print "		path_checker          rdac";
      print "		path_selector         \"round-robin 0\"";
      print "		hardware_handler      \"1 rdac\"";
      print "		failback              immediate";
      print "		features              \"2 pg_init_retries 50\"";
      print "		no_path_retry         30";
      print "		rr_min_io             100";
      print "		prio_callout          \"/sbin/mpath_prio_rdac /dev/%n\"";
      print "	}";
      print "	device {";
      print "		vendor                \"DELL\"";
      print "		product               \"MD36xxi\"";
      print "		path_grouping_policy  group_by_prio";
      print "		path_checker          rdac";
      print "		path_selector         \"round-robin 0\"";
      print "		hardware_handler      \"1 rdac\"";
      print "		failback              immediate";
      print "		features              \"2 pg_init_retries 50\"";
      print "		no_path_retry         30";
      print "		rr_min_io             100";
      print "		prio_callout          \"/sbin/mpath_prio_rdac /dev/%n\"";
      print "	}";
	    print "	device {";
      print "		vendor                \"DELL\"";
      print "		product               \"MD36xxf\"";
      print "		path_grouping_policy  group_by_prio";
      print "		path_checker          rdac";
      print "		path_selector         \"round-robin 0\"";
      print "		hardware_handler      \"1 rdac\"";
      print "		failback              immediate";
      print "		features              \"2 pg_init_retries 50\"";
      print "		no_path_retry         30";
      print "		rr_min_io             100";
      print "		prio_callout          \"/sbin/mpath_prio_rdac /dev/%n\"";  
      print "	}";
    }
    /^blacklist.*{/ {in_blacklist=1; blacklist_exists=1; print $0; next;}
    in_blacklist==1 {
      if (match($0, "devnode \"[*]\"") != 0) {comment_line(); next;}
      if (match($0, "product \"Universal Xport\"") != 0) {print $0; universal_xport_set=1; next;}
      if (match($0, "product \"MD3000\"") != 0) {print $0; md3000_set=1; next;}
      if (match($0, "product \"MD3000i\"") != 0) {print $0; md3000i_set=1; next;}
      if (match($0, "product \"Virtual Disk\"") != 0) {print $0; virtual_disk_set=1; next;}
    }
    /^}/ {
      if (in_blacklist == 1) {
        if (universal_xport_set != 1 || md3000_set != 1 || md3000i_set != 1 || virtual_disk_set != 1) {
          begin_modify();
          add_blacklist_entries();
          end_modify();
        }
        print $0;
        in_blacklist=0;
        next;
      }
    }
    /^defaults.*{/ {in_defaults=1; defaults_exists=1; print $0; next;}
    in_defaults==1 {
      if (match($0, "user_friendly_names") != 0) {if ($2 != "yes") {comment_line();} else {print $0; ufn_set=1;} next;}
      if (match($0, "max_fds") != 0) {if ($2 != 8192) {comment_line();} else {print $0; max_fds_set=1;} next;}
    }
    /^}/ {if (in_defaults == 1) {if (ufn_set != 1 || max_fds_set != 1) {begin_modify(); add_defaults_entries(); end_modify();} print $0; in_defaults=0; next;}}
    /^devices.*{/ {in_devices=1; devices_exists=1; print $0; next;}
    in_devices==1 {
      if (match($0, "MD32xx") != 0) {print $0; devices_set=1; next;}
    }
    /^}/ {if (in_devices == 1) {if (devices_set != 1) {begin_modify(); add_devices_entries(); end_modify();} print $0; in_devices=0; next;}}
    {print $0};
    END {
      if (blacklist_exists != 1 || defaults_exists != 1 || devices_exists != 1) {
        begin_modify();
        if (blacklist_exists != 1) {
          print "blacklist {";
          add_blacklist_entries();
         print "}";
        }
        if (defaults_exists != 1) {
          print "defaults {";
          add_defaults_entries();
          print "}";
        }
        if (devices_exists != 1) {
          print "devices {";
          add_devices_entries();
          print "}"
        }
        end_modify();
      }
    }
  ' >"${FILE_MODIFIED}"
  else
  # 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 '
    function begin_modify () {print "# Begin Dell MD Modification";}
    function end_modify () {print "# End Dell MD Modification";}
    function comment_line() {if ($1 != "#") {begin_modify(); print "# "$0; end_modify();} else {print $0;}}
    function add_blacklist_entries() {
      if (universal_xport_set != 1) {
        print "	device {";
        print "		vendor  \"*\"";
        print "		product \"Universal Xport\"";
        print "	}";
      }
      if (md3000_set != 1) {
        print "	device {";
        print "		vendor  \"*\"";
        print "		product \"MD3000\"";
        print "	}";
      }
      if (md3000i_set != 1) {
        print "	device {";
        print "		vendor  \"*\"";
        print "		product \"MD3000i\"";
        print "	}";
      }
      if (virtual_disk_set != 1) {
        print "	device {";
        print "		vendor  \"*\"";
        print "		product \"Virtual Disk\"";
        print "	}";
      }
    }
    function add_defaults_entries () {
      if (ufn_set != 1) {print "	user_friendly_names yes";}
      if (max_fds_set != 1) {print "	max_fds             8192";}
    }
    function add_devices_entries () {
      print "	device {";
      print "		vendor                \"DELL\"";
      print "		product               \"MD32xxi\"";
      print "		path_grouping_policy  group_by_prio";
      print "		prio                  rdac";
      print "		polling_interval      5";
      print "		path_checker          rdac";
      print "		path_selector         \"round-robin 0\"";
      print "		hardware_handler      \"1 rdac\"";
      print "		failback              immediate";
      print "		features              \"2 pg_init_retries 50\"";
      print "		no_path_retry         30";
      print "		rr_min_io             100";
      print "		prio_callout          \"/sbin/mpath_prio_rdac /dev/%n\"";
      print "	}";
      print "	device {";
      print "		vendor                \"DELL\"";
      print "		product               \"MD32xx\"";
      print "		path_grouping_policy  group_by_prio";
      print "		prio                  rdac";
      print "		polling_interval      5";
      print "		path_checker          rdac";
      print "		path_selector         \"round-robin 0\"";
      print "		hardware_handler      \"1 rdac\"";
      print "		failback              immediate";
      print "		features              \"2 pg_init_retries 50\"";
      print "		no_path_retry         30";
      print "		rr_min_io             100";
      print "		prio_callout          \"/sbin/mpath_prio_rdac /dev/%n\"";
      print "	}";
      print "	device {";
      print "		vendor                \"DELL\"";
      print "		product               \"MD36xxi\"";
      print "		path_grouping_policy  group_by_prio";
      print "		prio                  rdac";
      print "		polling_interval      5";
      print "		path_checker          rdac";
      print "		path_selector         \"round-robin 0\"";
      print "		hardware_handler      \"1 rdac\"";
      print "		failback              immediate";
      print "		features              \"2 pg_init_retries 50\"";
      print "		no_path_retry         30";
      print "		rr_min_io             100";
      print "		prio_callout          \"/sbin/mpath_prio_rdac /dev/%n\"";
      print "	}";
	    print "	device {";
      print "		vendor                \"DELL\"";
      print "		product               \"MD36xxf\"";
      print "		path_grouping_policy  group_by_prio";
      print "		prio                  rdac";
      print "		polling_interval      5";
      print "		path_checker          rdac";
      print "		path_selector         \"round-robin 0\"";
      print "		hardware_handler      \"1 rdac\"";
      print "		failback              immediate";
      print "		features              \"2 pg_init_retries 50\"";
      print "		no_path_retry         30";
      print "		rr_min_io             100";
      print "		prio_callout          \"/sbin/mpath_prio_rdac /dev/%n\"";  
      print "	}";
    }
    /^blacklist.*{/ {in_blacklist=1; blacklist_exists=1; print $0; next;}
    in_blacklist==1 {
      if (match($0, "devnode \"[*]\"") != 0) {comment_line(); next;}
      if (match($0, "product \"Universal Xport\"") != 0) {print $0; universal_xport_set=1; next;}
      if (match($0, "product \"MD3000\"") != 0) {print $0; md3000_set=1; next;}
      if (match($0, "product \"MD3000i\"") != 0) {print $0; md3000i_set=1; next;}
      if (match($0, "product \"Virtual Disk\"") != 0) {print $0; virtual_disk_set=1; next;}
    }
    /^}/ {
      if (in_blacklist == 1) {
        if (universal_xport_set != 1 || md3000_set != 1 || md3000i_set != 1 || virtual_disk_set != 1) {
          begin_modify();
          add_blacklist_entries();
          end_modify();
        }
        print $0;
        in_blacklist=0;
        next;
      }
    }
    /^defaults.*{/ {in_defaults=1; defaults_exists=1; print $0; next;}
    in_defaults==1 {
      if (match($0, "user_friendly_names") != 0) {if ($2 != "yes") {comment_line();} else {print $0; ufn_set=1;} next;}
      if (match($0, "max_fds") != 0) {if ($2 != 8192) {comment_line();} else {print $0; max_fds_set=1;} next;}
    }
    /^}/ {if (in_defaults == 1) {if (ufn_set != 1 || max_fds_set != 1) {begin_modify(); add_defaults_entries(); end_modify();} print $0; in_defaults=0; next;}}
    /^devices.*{/ {in_devices=1; devices_exists=1; print $0; next;}
    in_devices==1 {
      if (match($0, "MD32xx") != 0) {print $0; devices_set=1; next;}
    }
    /^}/ {if (in_devices == 1) {if (devices_set != 1) {begin_modify(); add_devices_entries(); end_modify();} print $0; in_devices=0; next;}}
    {print $0};
    END {
      if (blacklist_exists != 1 || defaults_exists != 1 || devices_exists != 1) {
        begin_modify();
        if (blacklist_exists != 1) {
          print "blacklist {";
          add_blacklist_entries();
         print "}";
        }
        if (defaults_exists != 1) {
          print "defaults {";
          add_defaults_entries();
          print "}";
        }
        if (devices_exists != 1) {
          print "devices {";
          add_devices_entries();
          print "}"
        }
        end_modify();
      }
    }
  ' >"${FILE_MODIFIED}"
fi
  # Update file if needed.
  CompareAndUpdateFile "${FILE_TO_MODIFY}" "${FILE_MODIFIED}"
}


###############################################################################
# Updates the SLES boot.local if needed.
###############################################################################
function UpdateSLESBootLocal
{
  local FILE_TO_MODIFY="/etc/init.d/boot.local"
  local FILE_MODIFIED="/etc/init.d/boot.local.dell_dm_modified"

  # Strip old entries, comment current entries, and append modified entries.
  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 '
    function begin_modify () {print "# Begin Dell MD Modification";}
    function end_modify () {print "# End Dell MD Modification";}
    {print $0;}
    END {
      begin_modify();
      print "modprobe scsi_dh;"
      print "modprobe scsi_dh_rdac;"
      print "modprobe dm-multipath;"
      print "sleep 10;"
      print "/etc/init.d/multipathd start;"
      end_modify();
    }
  ' >>"${FILE_MODIFIED}"

  # Update file if needed.
  CompareAndUpdateFile "${FILE_TO_MODIFY}" "${FILE_MODIFIED}"
}


###############################################################################
# Updates the SLES unsupport-modules if needed.
###############################################################################
function UpdateSLESUnsupportedModules
{
  local FILE_TO_MODIFY="/etc/modprobe.d/unsupported-modules"
  local FILE_MODIFIED="/etc/modprobe.d/unsupported-modules.dell_dm_modified"

  # Strip old entries, comment current rules, and append modified rules.
  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 '
    function begin_modify () {print "# Begin Dell MD Modification";}
    function end_modify () {print "# End Dell MD Modification";}
    function comment_line() {if ($1 != "#") {begin_modify(); print "# "$0; end_modify();} else {print $0;}}
    function add_aum_entries () {print "allow_unsupported_modules 1"}
	/^allow_unsupported_modules.*/ {if ($2 != 1) {comment_line();} else {print $0; aum_set=1;} next;}
    {print $0;}
    END {if (aum_set != 1) {begin_modify(); add_aum_entries(); end_modify();}}
  ' >>"${FILE_MODIFIED}"

  # Update file if needed.
  CompareAndUpdateFile "${FILE_TO_MODIFY}" "${FILE_MODIFIED}"
}


###############################################################################
# Updates the modprobe.conf file with MD devices if needed.  Displays an error
# and exits if conf file is not accessible.
###############################################################################
function UpdateModprobeConf
{
 if [ "${LINUX_DISTRO}" == "rhel" ]; then
  if [ ${LINUX_REV_MAJOR} -lt 6 ]; then
	local FILE_TO_MODIFY=/etc/modprobe.conf
	local FILE_MODIFIED=/etc/modprobe.conf.dell_dm_modified
  else # it is RHEL6 and above
	local FILE_TO_MODIFY=/etc/modprobe.d/dell_md.conf
	local FILE_MODIFIED=/etc/modprobe.d/dell_md_dm_modified	
  fi
 else # it is SLES
 	local FILE_TO_MODIFY=/etc/modprobe.conf
	local FILE_MODIFIED=/etc/modprobe.conf.dell_dm_modified  
 fi
  
  # Create conf file if it doesn't already exist.
  if [ ! -e "${FILE_TO_MODIFY}" ]; then
    touch "${FILE_TO_MODIFY}"
  fi
  
  # Display error and exit if conf file can't be accessed.
  if [ ! -e "${FILE_TO_MODIFY}" ]; then
    echo "${IDS_INSTALLATION_ABORTED_MODIFY} ${FILE_TO_MODIFY}"
    exit $RETCODE_MODPROBE_CONF_NOT_ACCESSIBLE
  fi


  # 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 '
    function begin_modify () {print "# Begin Dell MD Modification";}
    function end_modify () {print "# End Dell MD Modification";}
    function comment_line() {if ($1 != "#") {begin_modify(); print "# "$0; end_modify();} else {print $0;}}
    function add_scsi_dh_rdac_options () {
      print "options scsi_dh_rdac blacklist=\"DELL:MD3000,DELL:MD3000i\"";
    }
    /^options scsi_dh_rdac blacklist="DELL:MD3000,DELL:MD3000i"/ {scsi_dh_rdac_options_set=1; next;}
    /^options scsi_dh_rdac blacklist=/ {if (scsi_dh_rdac_options_set != 1) {comment_line(); next;}}
    {print $0};
    END {
      if (scsi_dh_rdac_options_set != 1) {
        begin_modify();
        add_scsi_dh_rdac_options();
        end_modify();
      }
    }
  ' >"${FILE_MODIFIED}"

  # Update file if needed.
  CompareAndUpdateFile "${FILE_TO_MODIFY}" "${FILE_MODIFIED}"
}


###############################################################################
# Rebuilds the initial ramdisk image for RHEL.
###############################################################################
function RebuildRHELRamDisk
{
  echo "${IDS_RAMDISK_REBUILD}..."
  if [ "${LINUX_REV_MAJOR}" == "6" ]; then
    local RD_IMAGE="/boot/initramfs-${LINUX_KERNEL_REV}.img"
  else
    local RD_IMAGE="/boot/initrd-${LINUX_KERNEL_REV}.img"
  fi
  if [ -e "${RD_IMAGE}" ]; then
    local RD_IMAGE_COPY="${RD_IMAGE}.dell_dm_old"
    echo "${IDS_RAMDISK_SAVE} ${RD_IMAGE_COPY}"
    cp -f "${RD_IMAGE}" "${RD_IMAGE_COPY}"
  fi
  echo "${IDS_RAMDISK_NEW} ${RD_IMAGE}"
  if [ "${LINUX_REV_MAJOR}" == "6" ]; then
    mkinitrd -f -v /boot/${RD_IMAGE} `uname -r` --allow-missing --preload scsi_dh_rdac
  else
    mkinitrd -f "${RD_IMAGE}" "${LINUX_KERNEL_REV}"
  fi
  REBOOT_NEEDED=TRUE
}


###############################################################################
# Installs RPMs with version checking.
#
# Parameters:
#   1 - Package Name
#   2 - RPM Suffix (e.g. x86_64.rpm)
#
# Returns:
#   0 - The package was installed
#   1 - Install was not needed
#   2 - The package install failed
###############################################################################
function InstallRPM
{
  local RETVAL=0

  # Install or update package if needed.
  local PKG_NAME="${1}"
  local RPM_SUFFIX="${2}"
  echo -n "${IDS_CHECK_UPDATE} ${PKG_NAME}..."
  local RPM_VERSION=$(basename $(ls "${RPM_PATH}/${PKG_NAME}-"*) | sed s/${PKG_NAME}-// | sed "s/.${RPM_SUFFIX}//")
  local INSTALLED_PKG_NAME=$(rpm -q ${PKG_NAME} | grep "${RPM_VERSION}")
  # Not installed.
  if [ -z "${INSTALLED_PKG_NAME}" ]; then
    # Install package.
    echo "${IDS_UPDATE_YES}"
    echo "${IDS_INSTALLING} ${PKG_NAME}..."
    rpm -ivh "${RPM_PATH}/${PKG_NAME}-${RPM_VERSION}.${RPM_SUFFIX}"
    if [ $? -ne 0 ]; then
      RETVAL=2
    fi
  # Installed.
  else
    echo "${IDS_UPDATE_NO}"
    RETVAL=1
  fi
  
  return $RETVAL
}


###############################################################################
# Installs or Updates DKMS RPMs.
#
# Parameters:
#   1 - Package Name
#
# Returns:
#   0 - The package was installed or updated
#   1 - No update was needed
#   2 - The package install failed
#   3 - The package update failed
###############################################################################
function InstallOrUpdateDKMSPackage
{
  local RETVAL=0

  # Install or update package if needed.
  local PKG_NAME="${1}"
  echo -n "${IDS_CHECK_UPDATE} ${PKG_NAME}..."
  INSTALLED_PKG_NAME=$(rpm -q ${PKG_NAME})
  # Not installed.
  if [ $? -ne 0 ]; then
    # Install package.
    echo "${IDS_UPDATE_YES}"
    echo "${IDS_INSTALLING} ${PKG_NAME}..."
    rpm -ivh "${RPM_PATH}/${PKG_NAME}-*.noarch.rpm"
    if [ $? -ne 0 ]; then
      RETVAL=2
    fi
  # Installed.
  else
    # Update the existing installation if applicable.
    if ! ls "${RPM_PATH}/${INSTALLED_PKG_NAME}"* &>/dev/null; then
      echo "${IDS_UPDATE_YES}"
      echo "${IDS_UPDATING} ${PKG_NAME}..."
      # Remove old package and then install the new one versus using -U
      # since the new package may not actually be an upgrade.
      rpm -e "${PKG_NAME}"
      rpm -ivh "${RPM_PATH}/${PKG_NAME}-*.noarch.rpm"
      if [ $? -ne 0 ]; then
        RETVAL=3
      fi
    else
      echo "${IDS_UPDATE_NO}"
      RETVAL=1
    fi
  fi
  
  return $RETVAL
}

###############################################################################
# Installs or Updates RPMs. 
# 
# Parameters:
#   1 - Package Name
#   2 - RPM Suffix (e.g. noarch.rpm)
#
# Returns:
#   0 - The package was installed or updated
#   1 - No update was needed
#   2 - The package install failed
#   3 - The package update failed
###############################################################################
function InstallOrUpdateRPM
{
  local RETVAL=0

  # Install or update package if needed.
  local PKG_NAME="${1}"
  local RPM_SUFFIX="${2}"
  echo -n "${IDS_CHECK_UPDATE} ${PKG_NAME}..."
  INSTALLED_PKG_NAME=$(rpm -q ${PKG_NAME})
  # Not installed.
  if [ $? -ne 0 ]; then
    # Install package.
    echo "${IDS_UPDATE_YES}"
    echo "${IDS_INSTALLING} ${PKG_NAME}..."
    rpm -ivh "${RPM_PATH}/${PKG_NAME}-[0-9]*.${RPM_SUFFIX}"
    if [ $? -ne 0 ]; then
      RETVAL=2
    fi
  # Installed.
  else
    # Update the existing installation if applicable.
    if ! ls "${RPM_PATH}/${INSTALLED_PKG_NAME}"* &>/dev/null; then
      echo "${IDS_UPDATE_YES}"
      echo "${IDS_UPDATING} ${PKG_NAME}..."
      rpm -Uvh "${RPM_PATH}/${PKG_NAME}-[0-9]*.${RPM_SUFFIX}"
      if [ $? -ne 0 ]; then
        RETVAL=3
      fi
    else
      echo "${IDS_UPDATE_NO}"
      RETVAL=1
    fi
  fi
  
  return $RETVAL
}


###############################################################################
# Installs or Updates the DKMS DM RDAC driver.
###############################################################################
function InstallRDAC
{
  # Install or update rdac driver if needed.
  InstallOrUpdateDKMSPackage "scsi_dh_rdac"
  if [ $? -eq 0 ]; then
    REBOOT_NEEDED=TRUE
  elif [ $? -ne 1 ]; then
    echo "${IDS_INSTALLATION_ABORTED} scsi_dh_rdac"
    exit $RETCODE_RDAC_INSTALL_FAILED
  fi
}


###############################################################################
# Handles DM installation for RHEL.
###############################################################################
function InstallRHEL
{
  # RHEL 5.4.
  if [ "${LINUX_REV}" == "5.4" ]; then
    # Install or update rdac driver if needed.
    RPM_PATH="${PATH_TO_RPMS}/rhel54"
    InstallRDAC

    # Update the udev rules if needed.
    UpdateRHEL54UDevRules

  # RHEL 5.5.
  elif [ "${LINUX_REV}" == "5.5" ]; then
    # Install or update rdac driver if needed.
    RPM_PATH="${PATH_TO_RPMS}/rhel55"
    InstallRDAC
	
  # RHEL 5.6.
  elif [ "${LINUX_REV}" == "5.6" ]; then
    # Install or update rdac driver if needed.
    RPM_PATH="${PATH_TO_RPMS}/rhel56"
    InstallRDAC
	
  # RHEL 5.7.
  #elif [ "${LINUX_REV}" == "5.7" ]; then
    # Install or update rdac driver if needed.
    #RPM_PATH="${PATH_TO_RPMS}/rhel57"
    #InstallRDAC
	
  # RHEL 6.0.
  elif [ "${LINUX_REV}" == "6.0" ]; then
    # Install or update libs for multipath if needed.
    RPM_PATH="${PATH_TO_RPMS}/rhel6"
    local RPM_SUFFIX="$(uname -i).rpm"
    InstallOrUpdateRPM "device-mapper-multipath-libs" "${RPM_SUFFIX}"

    # Install or update multipath if needed.
    InstallOrUpdateRPM "device-mapper-multipath" "${RPM_SUFFIX}"
	
	# Install or update rdac driver if needed.
    RPM_PATH="${PATH_TO_RPMS}/rhel6"
    InstallRDAC
	
  # RHEL 6.1.
  elif [ "${LINUX_REV}" == "6.1" ]; then
    # Install or update libs for multipath if needed.
    RPM_PATH="${PATH_TO_RPMS}/rhel61"
    local RPM_SUFFIX="$(uname -i).rpm"
    InstallOrUpdateRPM "device-mapper-multipath-libs" "${RPM_SUFFIX}"

    # Install or update multipath if needed.
    InstallOrUpdateRPM "device-mapper-multipath" "${RPM_SUFFIX}"
	
	# Install or update rdac driver if needed.
    RPM_PATH="${PATH_TO_RPMS}/rhel61"
    InstallRDAC
  
  # RHEL 6.2.
  elif [ "${LINUX_REV}" == "6.2" ]; then
    # Install or update libs for multipath if needed.
    RPM_PATH="${PATH_TO_RPMS}/rhel62"
    local RPM_SUFFIX="$(uname -i).rpm"
    InstallOrUpdateRPM "device-mapper-multipath-libs" "${RPM_SUFFIX}"

    # Install or update multipath if needed.
    InstallOrUpdateRPM "device-mapper-multipath" "${RPM_SUFFIX}"
	
	# Install or update rdac driver if needed.
    RPM_PATH="${PATH_TO_RPMS}/rhel62"
    InstallRDAC
	
	# RHEL 6.3.
  elif [ "${LINUX_REV}" == "6.3" ]; then
    # Install or update libs for multipath if needed.
    RPM_PATH="${PATH_TO_RPMS}/rhel63"
    local RPM_SUFFIX="$(uname -i).rpm"
    InstallOrUpdateRPM "device-mapper-multipath-libs" "${RPM_SUFFIX}"

    # Install or update multipath if needed.
    InstallOrUpdateRPM "device-mapper-multipath" "${RPM_SUFFIX}"
	
	# Install or update rdac driver if needed. Commenting this section out since there is no scsi_dh_rdac package for this OS. DM is native.
    #RPM_PATH="${PATH_TO_RPMS}/rhel63"
    #InstallRDAC
  fi

  # Update the multipath.conf file if necessary.
  UpdateMultipathConf
  
  # Update the modprobe.conf file if necessary.
  if [ ${LINUX_REV_MAJOR} -eq 5 -a ${LINUX_REV_MINOR} -lt 6 ]; then
     UpdateModprobeConf
  fi
  

  # Rebuild the ramdisk image.
  RebuildRHELRamDisk

  # Enable multipathd during boot up.
  chkconfig multipathd on

  # Update script to allow new devices to be dynamically detected.
  local RESCAN_SCRIPT_NAME="rescan_dm_devs"
  local RESCAN_INSTALL_PATH="/usr/sbin"
  local RESCAN_PATH="${PATH_TO_RPMS}/common"
  local INSTALLED_RESCAN_VER=""
  local MODULE_VERSION_PATTERN="^#[[:space:]]*Module Version:[[:space:]]*"
  if [ -e "$RESCAN_INSTALL_PATH/$RESCAN_SCRIPT_NAME" ]; then
    INSTALLED_RESCAN_VER=$(grep "$MODULE_VERSION_PATTERN" "$RESCAN_INSTALL_PATH/$RESCAN_SCRIPT_NAME" | sed "s/$MODULE_VERSION_PATTERN//")
  fi
  local RESCAN_VER=$(grep "$MODULE_VERSION_PATTERN" "$RESCAN_PATH/$RESCAN_SCRIPT_NAME" | sed "s/$MODULE_VERSION_PATTERN//")
  if [ -z "$INSTALLED_RESCAN_VER" -o "$RESCAN_VER" \> "$INSTALLED_RESCAN_VER" ]; then
    cp "$RESCAN_PATH/$RESCAN_SCRIPT_NAME" "$RESCAN_INSTALL_PATH/"
  fi
}


###############################################################################
# Handles DM installation for SLES.
###############################################################################
function InstallSLES
{
  # SLES 10 SP3.
  if [ "${LINUX_REV}" == "10.3" ]; then
    # Install or update rdac driver if needed.
    RPM_PATH="${PATH_TO_RPMS}/sles10sp3"
    InstallRDAC

    # Update boot.local if needed.
    UpdateSLESBootLocal

	  # SLES 10 SP4.
  elif [ "${LINUX_REV}" == "10.4" ]; then
    # Install or update rdac driver if needed.
    RPM_PATH="${PATH_TO_RPMS}/sles10sp4"
    InstallRDAC

    # Update boot.local if needed.
    UpdateSLESBootLocal
	
  # SLES 11 Gold.
  elif [ "${LINUX_REV}" == "11.0" ]; then
    RPM_PATH="${PATH_TO_RPMS}/sles11"
    local RPM_SUFFIX="$(uname -i).rpm"
    # Install or update module-init-tools package if needed.
    InstallOrUpdateRPM "module-init-tools" "${RPM_SUFFIX}"

    # Always install errata kernel so the old kernel isn't upgraded and removed.
    # Install kernel-default-base errata if needed.
    InstallRPM "kernel-default-base" "${RPM_SUFFIX}"

    # Install kernel-default errata if needed.
    InstallRPM "kernel-default" "${RPM_SUFFIX}"

    # Install or update multipath-tools package if needed.
    InstallOrUpdateRPM "multipath-tools" "${RPM_SUFFIX}"

    # Install or update udev package if needed.
    InstallOrUpdateRPM "udev" "${RPM_SUFFIX}"

    # Install or update kpartx package if needed.
    InstallOrUpdateRPM "kpartx" "${RPM_SUFFIX}"

    # Install or update rdac driver if needed.
    InstallRDAC
    
    # Ensure RDAC is installed into the new errata kernel since the package will only attempt to install into the
    # running kernel version.
    local NEW_KERNEL_MODULE="kernel-default"
    local NEW_KERNEL_VERSION=$(for ln in $(rpm -qlp "${RPM_PATH}/${NEW_KERNEL_MODULE}-"* | grep "/lib/modules"); do echo $ln | cut -d/ -f4; break; done)
    local RDAC_MODULE="scsi_dh_rdac"
    local RDAC_VERSION=$(basename $(ls "${RPM_PATH}/${RDAC_MODULE}-"*) | sed s/${RDAC_MODULE}-// | sed "s/-[0-9]*dkms.noarch.rpm//")
    local RDAC_INSTALLED=$(dkms status -m ${RDAC_MODULE} -v "${RDAC_VERSION}" -k "${NEW_KERNEL_VERSION}" | grep "installed " | wc -l)
    if [ "${RDAC_INSTALLED}" == "0" ]; then
      # Override weak install behavior if needed.
      dkms install -m "${RDAC_MODULE}" -v "${RDAC_VERSION}" -k "${NEW_KERNEL_VERSION}"
      REBOOT_NEEDED=TRUE
    fi

    # Update boot.local if needed.
    UpdateSLESBootLocal

    # Update unsupported-modules if needed.
    UpdateSLESUnsupportedModules

  # SLES 11.1.
  elif [ "${LINUX_REV}" == "11.1" ]; then
    # Install or update rdac driver if needed.
    RPM_PATH="${PATH_TO_RPMS}/sles11sp1"
    InstallRDAC

    # Update unsupported-modules if needed.
    UpdateSLESUnsupportedModules
  fi

  # Update fixed disk storage policy for SLES 11 if needed.
  if [ "${LINUX_REV_MAJOR}" == "11" ]; then
    UpdateSLES11StoragePolicy
  fi

  # Update the multipath.conf file if necessary.
  UpdateMultipathConf

  # Update the modprobe.conf file if necessary.
  if [ ${LINUX_REV_MAJOR} -eq 10 -a ${LINUX_REV_MINOR} -lt 4 ] || 
       [ ${LINUX_REV_MAJOR} -eq 11 -a ${LINUX_REV_MINOR} -lt 2 ]; then
    UpdateModprobeConf
  fi

  # Enable multipathd during boot up.
  chkconfig multipathd on

  # Copy script to allow new devices to be dynamically detected.
  if [ ! -e "/usr/sbin/rescan_dm_devs" ]; then
    cp "${PATH_TO_RPMS}/common/rescan_dm_devs" /usr/sbin
  fi
}


###############################################################################
# Main DM install routine.
###############################################################################
function InstallDM
{
  # Setup strings used during the installation.
  SetupStrings
  
  # Get Linux Distro and exit if unsupported.
  CheckLinuxDist

  # Ensure all package dependencies are available.
  CheckPackageDeps

  # Call the install logic for the appropriate distro.
  if [ "${LINUX_DISTRO}" == "rhel" ]; then
    InstallRHEL
  elif [ "${LINUX_DISTRO}" == "sles" ]; then
    InstallSLES
  fi
  
  # Reboot notification.
  if [ "${REBOOT_NEEDED}" == "TRUE" ]; then
    echo ""  
    echo "${IDS_REBOOT_REQUIRED}"
    echo ""  
  fi
}


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

# Kick off installation.
InstallDM

