#!/usr/bin/ksh -p
# *****************************************************************************
#   (C) Copyright 2000-2008 Hewlett-Packard Development Company, L.P.
#   HPVM: $Id: hpvmupgrade 8227 2008-11-04 20:45:28Z spider $ - HP CONFIDENTIAL
# *****************************************************************************
#	hpvmupgrade - Check guests for 11.23 to 11.31 
#       	      upgrade difficulties
# ******************************************************
#
#	This utility can be run on either an 11.23 based 
#       HPVM server or an 11.31 based HPVM server. Each 
#       configured guest is checked for potential booting
#       problems.  The problems found can be due to 11.23
#       upgrade incompatibilities or general guest configuration
#       issues. The output from running this utility can 
#       be found in the logfile /var/opt/hpvm/common/hpvmupgrade.date
#
PATH=$PATH:/opt/hpvm/bin:/usr/sbin:/usr/ucb:/usr/bin:/sbin:/usr/contrib/bin
logfile=/var/opt/hpvm/common/hpvmupgrade
alias rm=/usr/bin/rm
alias cat=/usr/bin/cat
alias awk=/usr/bin/awk
alias sed=/usr/bin/sed
alias grep=/usr/bin/grep
alias date=/usr/bin/date
alias test=/usr/bin/test
alias tail=/usr/bin/tail
alias uname=/usr/bin/uname
alias ioscan=/usr/sbin/ioscan
alias mount=/usr/sbin/mount
alias vginfo=/usr/sam/lbin/vginfo
alias machinfo=/usr/contrib/bin/machinfo
alias hpvminfo=/opt/hpvm/bin/hpvminfo
alias hpvmstatus=/opt/hpvm/bin/hpvmstatus
alias hpvmmodify=/opt/hpvm/bin/hpvmmodify
alias hpvmdevmgmt=/opt/hpvm/bin/hpvmdevmgmt
#
# multipath subsystem utilities
# 
alias autopath=/sbin/autopath
alias securepath=/sbin/securepath
alias powerpath=/sbin/powermt

typeset Noopt=true
typeset Force=false 
typeset Examine=false
#
#
# If hpvmstatus is not present assume HPVM is not present
#
test -x /opt/hpvm/bin/hpvmstatus ||
{
   echo "ERROR: This system does not have HPVM installed."
   exit 0
}
hpvminfo -M | grep -q ":host:" || 
{
   echo "ERROR: This system must be an HPVM host."
   exit 0
}
#
# determine our starting release
#	11.23 or 11.31 or 11.41
release=`uname -r | sed 's/.*11./11./'`
#
# Process options
#
while getopts :evf opt ; do
    case ${opt} in
      (f) # Force the whatever
        Force=true
      ;;
      (e) # Examine current upgrade situation
        Examine=true
        Noopt=false
      ;;
      (v) # Print version of utility
	echo "Version A.04.00.00"
        Noopt=false
        exit 0
      ;;
      ([?]) # Ignore anything else
        :
      ;;
    esac
done
if ${Noopt} 
then
    echo "Usage: hpvmupgrade -e  to examine the current system"
    exit 1
fi
#
# Get agile device list
#
if [[ ${release} = "11.31" ]]
then
    agiledevs=`ioscan -N | grep "path for" | awk '{ print $NF}'`
fi

UPTMP="/var/opt/hpvm/common/upgradetmp"
HPAPDEVPATH="/hpap/rdsk"
DMPDEVPATH="/dev/vx/rdmp"
DATETAG=`date +%y.%m.%d.%T`
LOGFILE="$logfile.$DATETAG"
NOW=`date`

HFSDIRS=$(mount -p | grep -w hfs | awk '{print $2}' | grep -v -w /)

echo "***********************************************" >>$LOGFILE
echo "**** 11.31 HPVM guest compatibility report ****" >>$LOGFILE
echo "****   $NOW        ****" >>$LOGFILE
echo "***********************************************" >>$LOGFILE

for guest in `hpvmstatus -M | awk '{FS=":";print $1}'`
do
    echo "Processing guest $guest"
    echo "***********************************************" >>$LOGFILE
    echo "            REPORT FOR GUEST $guest    " >>$LOGFILE
#
#   If guest is not already running run hpvmmodify -s 
#   to get HPVM start checks for this guest
#
    ERRNUM=1
    hpvmmodify -s -P $guest -l hpvm_upgrade_label 2>&1 | grep "Warning" >$UPTMP
    if test -s $UPTMP 
    then
	ERRNUM=`tail -n 1 $UPTMP | sed 's/://' | awk '{print $2}'`
	ERRNUM=`expr $ERRNUM + 1`
	cat $UPTMP >>$LOGFILE
	rm $UPTMP
    fi
#
# get the disk list (beware of really large lists)
#
    guestdisks=
    for i in `hpvmstatus -M -P $guest |  sed 's@;lv;/@ /@g'| sed 's@;disk;/@ /@g' | sed 's@;attach;/@ /@g' | sed 's@;file;/@ /@g' |sed 's@:@ @g' | sed 's@,@ @g'`
    do
#
#	    Does this device exist?
#
	if echo $i | grep -q  ^\/ 
	then 
	    if [[ -s "$i" || -c "$i" || -h "$i" ]]
	    then 
	        echo $i | grep -q -e "/common/efi" -e "/common/patterns.vmmpat" || {
		    guestdisks="$guestdisks $i"
                }
            fi
        fi
    done
    if test -n "$guestdisks" 
    then
#
#	Make grep arguements to search for devices
#
        echo "guestdisks = $guestdisks"
	grepargs="-w "
	for i in $guestdisks
        do
	    if hpvmdevmgmt -l gdev:$i |grep -q ",DEVTYPE=FILE[,:]"
	    then
		for dir in $HFSDIRS
		do
		    if echo $i | grep -q "^$dir"
		    then
	                echo "ERROR $ERRNUM: File $s is contained in hfs filesystem $dir." >>$LOGFILE
	                ERRNUM=`expr $ERRNUM + 1`
			break
                    fi
                done 
	    else 
                test -c $i || {
                    echo "    Warning $ERRNUM: $i is not a character special device" >>$LOGFILE
	            ERRNUM=`expr $ERRNUM + 1`
                }
	        test -h $i && {
		    echo "    Warning $ERRNUM: $i is a symbolic link" >>$LOGFILE
	            ERRNUM=`expr $ERRNUM + 1`
                }
                echo $i | grep -q "$DMPDEVPATH" && {
		    echo "    Warning $ERRNUM: $i is a Veritas multipath device" >>$LOGFILE
	            ERRNUM=`expr $ERRNUM + 1`
                }
                echo $i | grep -q "$HPAPDEVPATH" && {
		    echo "    Warning $ERRNUM: $i is a Secure Path/Autopath device" >>$LOGFILE
	            ERRNUM=`expr $ERRNUM + 1`
                }
#
#               Check if this is a legacy device if this system is at 11.31
#
                if test -n "$agiledevs"
                then
		    dev=`echo $i | sed 's@.*/@@' | sed 's@_p.*@@'`
		    echo $agiledevs | grep -qw $dev || {
                        echo "    Warning $ERRNUM: $i legacy device in use" >>$LOGFILE
	                ERRNUM=`expr $ERRNUM + 1`
                    }
                fi
#
#	    Convert guest volume backing devices to block devices
#
                hpvmdevmgmt -l gdev:$i | grep -q ",DEVTYPE=LV[,:]" && 
                {
		    basedevname=`echo $i| sed 's@.*/r@@'`
		    basedir=${i%/*}
		    volname=`echo $basedir/$basedevname`
		    VOLDEVS=`vginfo  -l $volname -t LVM |grep /dev/ | awk '{FS=":";i=NF-2;print $i}' | sed 's/@@.*//' | sed 's/,/ /g'`
		    if test -z "$VOLDEVS"
		    then
		        VOLDEVS=`vginfo  -l $volname -t VxVM |grep /dev/ | awk '{FS=":";i=NF-2;print $i}' | sed 's/@@.*//' | sed 's/,/ /g'`
                    fi
		    if test -n "$VOLDEVS"
		    then
		        DISKS=`echo $VOLDEVS|sed 's@/dev/dsk/@ -e @g'`
                    else
		        DISKS=`echo $i| sed 's@.*/@ -e @'`
                    fi
		    grepargs=`echo "$grepargs $DISKS"`
                }
            fi
        done
	autodevs=
	securedevs=
	powerdevs=
	volumedevs=
#
#      If the autopath utility exists use it to check devs
#
	if test -x autopath  
	then 
	    autodevs=`autopath display | grep $grepargs`
        fi
	test -n "$autodevs" && {
	    echo "ERROR $ERRNUM: Autopath devices in use: $autodevs" >>$LOGFILE
	    ERRNUM=`expr $ERRNUM + 1`
        }
#
#      If the securepath utility exists use it to check devs
#
	if test -x securepath  
	then 
	    securedevs=`securepath display | grep $grepargs`
        fi
	test -n "$securedevs" && {
	    echo "ERROR $ERRNUM: Securepath devices in use: $securedevs" >>$LOGFILE
	    ERRNUM=`expr $ERRNUM + 1`
        }
#
#      If the powerpath utility exists use it to check devs
#
	if test -x powerpath  
	then 
	    powerdevs=`powerpath display dev=all | grep $grepargs`
        fi
	test -n "$powerdevs" && {
	    echo "ERROR $ERRNUM: Powerpath devices in use: $powerdevs" >>$LOGFILE
	    ERRNUM=`expr $ERRNUM + 1`
        }
    fi
    if [[ $ERRNUM -eq 1 ]] 
    then 
        echo "Found no problems with $guest    " >>$LOGFILE
    fi
done
echo "***********************************************" >>$LOGFILE
echo "******* HPVM guest upgrade log complete *******" >>$LOGFILE
echo "***********************************************" >>$LOGFILE
if `grep -q -e ERROR -e Warning $LOGFILE`
then
    echo "Check file $LOGFILE for hpvmupgrade results."
else
    echo "No serious guest configuration problems were found."
    rm $LOGFILE
fi
