#!/bin/ksh
#
# Copyright (c) 1996 Sun Microsystems, Inc.
#
# ident   "@(#)finddevices.sh 1.12	01/03/28 SMI"
#
# This simple script will scan the /dev/rdsk directory and produce
# a unique list of controllers in the form of cX. For each
# of these controllers it will do an ssacli/ssaadm inq to see if its
# a pluto. If not it is just skipped, if it is the serial number
# is saved. 
#
# We then need to scan the /dev/rdsk directory and get the physical
# path for the pluto controllers we have found. We then save these
# to a file.
#

#################################################################################

function get_ssa_list
{
  Tmp=${1:-/var/opt/SUNWcluster/run/Tmp.$$}
  /bin/rm -f ${Tmp}

  cd /dev/rdsk
  ctl=$(/bin/ls | sed 's/t.*$//' | uniq)
  for i in ${ctl};
  do
    sn=$(${SSAADM} inq ${i} 2>/dev/null | grep 'Serial Number')
    if [[ $? -eq 0 ]]; then
      serial_num=$(echo ${sn} | sed 's/^Serial Number[ ]*//')
      echo ${i}:${serial_num}
      cnums=`/bin/ls ${i}t*`
      for c in ${cnums}
      do
	if [[ -h ${c} ]]; then
	  /bin/ls -l ${c}  | sed -e 's/^.*->//' | \
	   sed -e 's/^.*\.\.\/\.\.//' -e 's%^\(.*\)/\(.*\)%\1:ctlr%' >> ${Tmp}
	  break
	fi
      done
    fi
  done
  if [[ -f ${Tmp} ]]; then
    /bin/rm -f ${Tmp}
  fi
}

#################################################################################

 
# This function finds the root device of the system, whether it is encapsulated
# by the volume manager or not. It sets two variables which are used by other
# functions:
# rootdev = address of the root disk of the form cXtYdZs2
 
function get_root_device
{
  rootdev=$(cat /etc/vfstab |  awk ' $3 == "/" && $1 !~ /#/ { print $2 } ')
  print "${rootdev}" | egrep '/dev/rdsk/c[0-9]+t[0-9]+d[0-9]s[0-9]' > /dev/null 2>&1
  if [[ $? -eq 0 ]]; then
    rootdev=$(print ${rootdev} | sed -e 's|/dev/rdsk/||')
    rootdev=$(print ${rootdev} | sed -e 's/s[0-9]/s2/')
  else
    print "${rootdev}" | egrep '/dev/vx/rdsk/rootvol' > /dev/null 2>&1
    if [[ $? -eq 0 ]]; then
      rootvol=$(print ${rootdev} | sed -e 's|/dev/vx/rdsk/||')
      plex=$(/usr/sbin/vxprint | grep rootvol | grep pl | head -1 | awk '{print $2}')
      dm=$(/usr/sbin/vxprint | grep ${plex} | grep sd | awk '{print $2}' | sed -e 's/-[A-Z0-9][0-9]//g' | uniq )
      rootdev=$(/usr/sbin/vxprint | grep ${dm} | grep dm | awk '{print $3}')
    fi
  fi
  echo ${rootdev}
}
 
######################################################################################

 
function get_disk_list
{
  typeset cwd

  cwd=$(/usr/bin/pwd)
  cd /dev/rdsk
  
  if [[ -n "$1" ]]; then
    c=$1
    for d in $(/bin/ls ${c}t*s2); do
      sid=$(${ECSSSA} inquiry ${d})
      if [[ -n "${sid}" ]]; then
	print ${d}:`echo ${sid}`
      fi
    done
    cd ${cwd}
    return 0
  fi
  
  ctls=$(/bin/ls | sed 's/t.*$//' | uniq)
  
  for c in ${ctls}
  do
    if [[ -f "${SSAADM}" ]]; then
      ${SSAADM} display ${c} > /dev/null 2>&1
      if [[ "$?" -eq 0 ]]; then
	continue
      fi
    fi
    for d in ` /bin/ls ${c}t*s2 2>/dev/null `
    do
      sid=$(${ECSSSA} inquiry ${d})
      if [[ -n "${sid}" ]]; then
	print ${d}:`echo ${sid}`
      fi
    done
  done
  cd ${cwd}
}

#################################################################################

if [[ $# -eq 0 ]]; then
  cmd=ssa
else
  cmd=$1
fi

SSAADM=/usr/sbin/ssaadm
ECSSSA=/opt/SUNWcluster/bin/scssa

case ${cmd} in
  ssa) get_ssa_list $2
       ;;
  disks) get_disk_list $2
	 ;;
  rootdev) get_root_device
	   ;;
  *) exit 1
esac
exit 0
