#!/bin/ksh
#
#ident	"@(#)disk_info.sh	1.8	04/12/15 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Provides useful info for all available disks devices
# Provides info about HBA controllers on the system
# By Svetlana Kostyuchenko
#

# Run as root
id | grep "uid=0(root)" > /dev/null 2>&1
if [ $? -ne 0 ]
then
        echo
        echo "WARNING: You must be root to run this script."
        echo " Try again"
        exit 1
fi

master=/platform/i86pc/boot/solaris/devicedb/master; export master
P=`pwd`; export P
rm /tmp/format.out > /dev/null 2>&1
rm /tmp/vendor.out > /dev/null 2>&1
rm /tmp/vendor.out1 > /dev/null 2>&1

#Get Vendor and Product info for each IDE disks
(
cat /var/adm/messages* | grep Vendor | grep Product | awk -F: '{ print $4,$5 }' | awk -F] '{ print $2 }' | awk -Fk '{ print $2 }' | sort | uniq
) 1> /tmp/vendor.out 2>/dev/null

#Merge devpath, Vendor and Product info for each IDE disks
diskno=`wc -l /tmp/vendor.out | awk '{ print $1 }'`
i=1
while [ $i -le $diskno ]
do
	diskinfo=`sed -n $i'p' /tmp/vendor.out`
	index=`echo $diskinfo  | awk '{ print $1 }'`
	devpath=`cat /var/adm/messages* | grep "cmdk$index is" |awk -F: '{ print $4 }' | awk -F] '{ print $2 }' | awk '{ print $3 }' | uniq`
	vendor=`echo $diskinfo | awk -F"'" '{ print $2 }'`
	product=`echo $diskinfo | awk -F"'" '{ print $4 }'`
	echo "$devpath:$vendor:$product">>/tmp/vendor.out1 2>/dev/null
	i=`expr $i + 1`
done

# Get disk list
cd /
(
prtvtoc /dev/rdsk/c*d*p0 |grep "partition map" | awk ' { print $2 } ' 
) 1> /tmp/disklist 2> /dev/null

echo
echo "		Disk Device List	"
echo

# Get disk detailed info
for i in `cat /tmp/disklist`
do (
format -e $i <<EOF |tail -8 |sed 's/format> //g' | sed 's/Current //' | sed 's/quit/------------------------/g' >>/tmp/format.out 2> /dev/null
current
inq
q
EOF
) 2> /dev/null
disk=`cat /tmp/format.out | grep $i`
if [ -z "$disk" ]
then
	continue
fi
echo $i

i=`echo $i | awk -F"/" '{ print $4 }'`
sd=`echo $i | grep t`
if [ -n "$sd" ]
then
	continue
fi

j=`wc -l /tmp/format.out | awk '{ print $1 }'`
j=`expr $j - 1`
devpath=`sed -n $j'p' /tmp/format.out`
vendor=`cat /tmp/vendor.out1 | grep $devpath | awk -F: '{ print $2 }'`
product=`cat /tmp/vendor.out1 | grep $devpath | awk -F: '{ print $3 }'`
if [ -n "$vendor" ]
then
	echo "Vendor:   $vendor" >>/tmp/format.out
fi
if [ -n "$product" ]
then
	echo "Product:  $product" >>/tmp/format.out
fi
done

#<Begin> Modified by Martin Mei for bug 4477446, replace "<",">" with "(",")" for html protocol
egrep -v '(volname|<cmd>|inquiry|save)' /tmp/format.out | sed 's/</(/g' | sed 's/>/)/g' > /tmp/format.out1
#<end> modification of Martin Mei

cat /tmp/format.out1
echo

# Determine hba controllers (sd and cmdk target controllers)

egrep '(sd|cmdk)' /tmp/format.out1 > /tmp/format.out2 2>/dev/null
if [ $? -eq 0 ]
then
echo "		HBA Controllers:	"
echo
prtconf -pv | grep model > /tmp/format.out7 2>/dev/null
egrep -i '(SCSI|IDE|controller|Symbios)' /tmp/format.out7 > /tmp/format.out8 2>/dev/null
egrep -iv '(floppy|VGA|class|net)' /tmp/format.out8 > /tmp/format.out9 2>/dev/null
cat /tmp/format.out9 | sed '/^/s/      //g'
echo
fi
#sh $P/pcmcia.sh
#echo "----------------------------------------------------------------------"

# Cleanup
rm /tmp/disklist /tmp/format.out*
rm /tmp/vendor.out > /dev/null 2>&1
rm /tmp/vendor.out1 > /dev/null 2>&1
cd $P
