#!/bin/sh
# *****************************************************************************
# restrict_hpvmhost_devices 
#
# (C) Copyright 2000-2008 Hewlett-Packard Development Company, L.P.
# @(#) $Revision: 7880 $
#
# Tool to add disks and logical volumes used by the VM Host system to the
# Integrity VM device database as restricted devices, protecting them from
# use as storage by virtual machines.
#
# *****************************************************************************

CACHEDIR=/var/tmp/hpvmcachedir
MGMTDBBACKUP=/var/opt/hpvm/common/hpvm_mgmtdb.autordevbk
MGMTDB=/var/opt/hpvm/common/hpvm_mgmtdb
HPVMDEVMGMT=/opt/hpvm/bin/hpvmdevmgmt
ADDDEVCMD="${HPVMDEVMGMT} -a rdev:"
CHKDEVCMD="${HPVMDEVMGMT} -l rdev:"
MODDEVCMD="${HPVMDEVMGMT} -m rdev:"
CMD="hpvmhostrdev"
DISKLISTOPT="-rf"

# Overkill: Modify path to get right grep, cut, echo, etc.
PATH=/usr/bin:/usr/sbin:${PATH}

# *****************************************************************************
usage ()
{
  echo "  Usage: ${CMD} [-D|-E|-d|-h|-r|-u]"

  echo >&$1 ""
  echo >&$1 "      -D : Disable automatic restriction of host device use."
  echo >&$1 "      -E : Enable automatic restriction of host device use."
  echo >&$1 "      -d : Delete automatically added restricted devices."
  echo >&$1 "      -h : Help"
  echo >&$1 "      -r : Recover HPVM mgmt database"
  echo >&$1 "      -u : Do not update device cache (do not force HW scan)"
  echo >&$1 ""
  echo >&$1 "  ${CMD}: Unable to continue."

} # usage()

# Add a single device ($RDEV) as an rdev
addAutoRdev ()
{
  # Check to see if device is already an rdev.
  if [[ -n ${RDEV} ]]; then
    ${CHKDEVCMD}${RDEV}  > /dev/null 2>&1
    RETVAL=$?
    # If not, then add it
    if [[ ${RETVAL} != 0 ]]; then
       ${ADDDEVCMD}${RDEV}:attr:AUTORDEV=YES  > /dev/null 2>&1
    else
      # See if existing entry is an AUTORDEV
      ${CHKDEVCMD}${RDEV} | cut -d ':' -f 2 | grep "AUTORDEV=NO" > /dev/null 2>&1
      RETVAL=$?
      # If it is already an AUTORDEV, reset its attribute value to YES
      # Otherwise leave it alone
      if [[ ${RETVAL} -eq 0 ]]; then
        ${MODDEVCMD}${RDEV}:attr:AUTORDEV=YES  > /dev/null 2>&1
      fi
    fi
  fi
} # addAutoRdev

# Remove all automatically added rdev entries.
removeAutoRdevs () {
  ${HPVMDEVMGMT} -l rdev:AUTORDEV=NO > /dev/null 2>&1
  RETVAL=$?
  if [[ $RETVAL -eq 0 ]]; then
    # No, not very performance-wise to run hpvmdevmgmt -l rdev... twice, but it
    # seems that pipe may hang if output of hpvmdevmgmt -l rdev is empty...
    RDEVLIST=$(${HPVMDEVMGMT} -l rdev:AUTORDEV=NO  | cut -d ':' -f 1) > /dev/null 2>&1
    for I in ${RDEVLIST}; do
      ${HPVMDEVMGMT} -d rdev:$I
    done
  fi

  ${HPVMDEVMGMT} -l rdev:AUTORDEV=YES > /dev/null 2>&1
  RETVAL=$?
  if [[ $RETVAL -eq 0 ]]; then
    RDEVLIST=$(${HPVMDEVMGMT} -l rdev:AUTORDEV=YES  | cut -d ':' -f 1) > /dev/null 2>&1
    for I in ${RDEVLIST}; do
      ${HPVMDEVMGMT} -d rdev:$I
    done
  fi
  
} # removeAutoRdevs


# *****************************************************************************
# Backup current devmgmtdb
backupDevmgmtdb () {
	cp -f -p ${MGMTDB} ${MGMTDBBACKUP} 
}


# *****************************************************************************
# Recover devmgmtdb
recoverDevmgmtdb () {
	# Probably should do a straight copy from backup, but mv is atomic...
	cp -f -p ${MGMTDBBACKUP} ${MGMTDBBACKUP}.tmp
	mv -f ${MGMTDBBACKUP}.tmp ${MGMTDB} 
}


# *****************************************************************************
#
# Main
#
# *****************************************************************************
if [[ (! -x /usr/sam/lbin/lvlist ) || ( ! -x /usr/sam/lbin/disklist ) ]] ; then
  echo "  Error: System management utilities in /usr/sam/lbin not installed."
  echo "  Unable to add host storage to Integrity VM device database."
  exit -1
fi

# Parse option(s)
while getopts "DEdhru" opt > /dev/null 2>&1
do
  case ${opt} in
    D)  # Disable automatic addition of AUTORDEV entries at HPVM startup
	ch_rc -a -p AUTORDEVENABLE=0 /etc/rc.config.d/hpvmconf
        RETVAL=$?
        exit ${RETVAL}
        ;;
    E)  # Enable automatic addition of AUTORDEV entries at HPVM startup
	ch_rc -a -p AUTORDEVENABLE=1 /etc/rc.config.d/hpvmconf
        RETVAL=$?
        exit ${RETVAL}
        ;;
    d)  # Delete all AUTORDEV entries
	backupDevmgmtdb 
        removeAutoRdevs
        exit 0
        ;;
    h)  # Show usage info, exit gracefully
        usage
        exit 0
        ;;
    r)	# Recover hpvm_mgmtdb
	recoverDevmgmtdb 
	exit 0
	;;
    u)  # Do not force an update of the cache, no forced HW scan
        DISKLISTOPT="-ru"
        ;;
    ?)  # getopts will set opt to "?" with unknown option string
        echo >&2 "${CMD}: Unrecognized option."
        usage
        exit -1
        ;;
  esac
done

# Before doing anything else, create a backup copy of hpvm_mgmtdb
backupDevmgmtdb 

# Change AUTORDEV attribute value to NO in preparation for addtion, then
# removal of any remaining entries that still have value of NO
${HPVMDEVMGMT} -l rdev:AUTORDEV=YES > /dev/null 2>&1
RETVAL=$?
if [[ $RETVAL -eq 0 ]]; then
  RDEVLIST=$(${HPVMDEVMGMT} -l rdev:AUTORDEV=YES  | cut -d ':' -f 1) > /dev/null 2>&1
  for I in ${RDEVLIST}; do
    ${HPVMDEVMGMT} -m rdev:$I:attr:AUTORDEV=NO
  done
fi

# Mark all volumes used for anything except HPVM as restricted devices
LVLIST=$(/usr/sam/lbin/lvlist -p)
for I in ${LVLIST}; do
  set - $(echo $I | awk -F '|' '{ print $1, $2, $3, $6 }')
  LVPATH=$1
  LVROOT=$2
  # Comparisons made only in uppercase
  typeset -u LVUSED=$3
  typeset -u LVTYPE=$4

  if [[ x${LVUSED} != "xUNUSED" && x${LVUSED} != "xHPVM" ]]
  then
    RDEV=""
    if [[ x${LVTYPE} = "xLVM" ]]; then
      RDEV=$(echo ${LVPATH} | sed -e s?${LVROOT}/?${LVROOT}/r?)
    elif [[ x${LVTYPE} = "xVXVM" ]]; then
      RDEV=$(echo ${LVPATH} | sed -e s?/dev/vx/dsk?/dev/vx/rdsk?)
    fi

    addAutoRdev

  fi
done

# Mark all LUNs used for anything except HPVM as restricted devices
[[ ! -d ${CACHEDIR} ]] && mkdir ${CACHEDIR}
DISKLIST=$(/usr/sam/lbin/disklist -c ${CACHEDIR} -p ${DISKLISTOPT} | \
   awk -F '|' \
    '{if ( toupper($4) != "UNUSED" && toupper($4) != "HPVM" && $3 != "" ) \
     { sub("/dsk/","/rdsk/",$3) ; sub("/disk/","/rdisk/",$3) ; print $3 } }')
for I in ${DISKLIST}; do
  # Some multipathing solutions, e.g., PVLinks, result in multiple paths per
  # entry.  These entries are comma-delimited.  Fortunately, hpvmdevmgmt is
  # smart enough to identify alternate paths, so only one path need be added
  # to the device database.  They are semicolon delimited in 11.31.
  RDEV=$(echo $I | cut -d ';' -f 1 )

  addAutoRdev

done

# Now clean out any stale entries (i.e., those with AUTORDEV=NO)

  ${HPVMDEVMGMT} -l rdev:AUTORDEV=NO > /dev/null 2>&1
  RETVAL=$?
  if [[ $RETVAL -eq 0 ]]; then
    # No, not very performance-wise to run hpvmdevmgmt -l rdev... twice, but if
    # there are no such rdevs, the output to stderr is difficult to hide when
    # assigning value to RDEVLIST..
    RDEVLIST=$(${HPVMDEVMGMT} -l rdev:AUTORDEV=NO  | cut -d ':' -f 1) > /dev/null 2>&1
    for I in ${RDEVLIST}; do
      ${HPVMDEVMGMT} -d rdev:$I
    done
  fi

exit 0
