#!/bin/ksh -p
# $Id: //depot/stable/explorer/tools/vxvm#3 $
# 
# Explorer Version:
EXP_VERSION=4.0
#

# Source tools functions
. ${EXP_LIB}/exp_tools

# Run if specified to do so
script=${0##*/}
which_gate_exit storage $script default all

pkginfo -q SUNWvxvm; PKGSUNW=$?
pkginfo -q VRTSvxvm; PKGVXVM=$?
TEXT=`gettext "VxVM not installed"`
require "test \"$PKGSUNW\" -eq 0 -o \"$PKGVXVM\" -eq 0" "${TEXT}"

#
# VxVM info
#

# If Sun's VxVM is running, PKGSUNW will be set to 0.
# If Veritas's VxVM is running, PKGVXVM will be set to 0.
# Gotta check to MAKE SURE that the VRTS package, if installed, is
# a 2.x or 3.x flavor.  If customer bought VxVM from Veritas, it's
# possible that their VRTSvxvm package is a 2.x variety.  If it's
# a SUNW package, it MUST be 2.x.
if [ $PKGSUNW -eq 0 ]
then
   VM2X=0; VM3X=1
else
   VER=`pkginfo -l VRTSvxvm | egrep -e "VERSION:" | nawk '{print substr($2,1,1)}'`
   if [ "$VER" -eq "3" ]
   then
      VM2X=1; VM3X=0
   else
      VM2X=0; VM3X=1
   fi
fi

# At this point, if VxVM 2.x is running, VM2X will be 0.
# If VxVM 3.x is running, VM3X will be 0.
get_cmd "/usr/sbin/vxprint -Ath" disks/vxvm/vxprint-Ath
get_cmd "/usr/sbin/vxprint -th"  disks/vxvm/vxprint-th
get_cmd "/usr/sbin/vxprint -h"   disks/vxvm/vxprint-h
get_cmd "/usr/sbin/vxdg -q list" disks/vxvm/vxdg-q-list

# Starting in the 3.X version, a new option "-o alldgs" should be added
# to the vxdisk list command to gather more information about deported
# diskgroups.
if [ $VM2X -eq 0 ]
then
   get_cmd "/usr/sbin/vxdisk list"           disks/vxvm/vxdisk-list
else
   get_cmd "/usr/sbin/vxdisk -o alldgs list" disks/vxvm/vxdisk-list
   get_cmd "/usr/sbin/vxprint -thrL"         disks/vxvm/vxprint-thrL
   get_cmd "/usr/sbin/vxprint -hr"           disks/vxvm/vxprint-hr

   # Enclosure based naming may mask device name, get vxdisk list device
   /usr/sbin/vxdisk -o alldgs list | while read DISK_NAME DISK_TYPE DA_NAME DG_NAME DISK_STATE
   do
       # Skip header
       [ "${DISK_NAME}" = "DEVICE" ] && continue

       # Disk may be offline, so skip
       [ "${DISK_STATE}" = "offline" ] && continue

       get_cmd "/usr/sbin/vxdisk list ${DISK_NAME}" disks/vxvm/disks/vxdisk_list=${DISK_NAME}
   done

   # Collect logs in VxVM 3.x
   get_dir "/var/opt/vmsa/logs" disks/vxvm/logs/
fi

# recursive listings of the /dev/vx/ directory
get_cmd "/usr/bin/ls -lR /dev/vx" disks/vxvm/ls-lR_dev_vx

# list the disk groups
/usr/sbin/vxdg -q list | while read DG_NAME DG_STATE DG_ID
do
    if [ "$DG_STATE" =  "disabled" ]
    then
	# dg is disabled, process next
	continue
    fi

    # list free space that can be used  for allocating subdisks
    get_cmd "/usr/sbin/vxdg -g $DG_NAME free" disks/vxvm/disk_groups/vxdg-g_free=$DG_NAME

    # List the contents of disk group and its configuration
    get_cmd "/usr/sbin/vxdg list $DG_NAME" disks/vxvm/disk_groups/vxdg_list=$DG_NAME

    # list the volumes
    get_cmd "/usr/sbin/vxprint -vng $DG_NAME" disks/vxvm/disk_groups/vxprint-vng=$DG_NAME

    # store volume list in VOL_LIST, I need this so all volumes
    # are listed on a single line
    VOL_LIST=`/usr/sbin/vxprint -vng $DG_NAME`
    # Turn newlines into spaces
    VOL_LIST=`echo ${VOL_LIST} | sed -e 's,\n, ,g'`

    # this output will eliminate the dg and dm definitions, this is
    # the format vxmake expects when recreating the private area for a
    # lost disk group.  This command is different depending on the
    # version of VxVM being used.
    if [ $VM2X -eq 0 ]
    then
       get_cmd "/usr/sbin/vxprint -hmQqg $DG_NAME $VOL_LIST" \
	  disks/vxvm/disk_groups/vxprint-hmQqg_4vxmk=$DG_NAME
    else
       get_cmd "/usr/sbin/vxprint -rmvg $DG_NAME $VOL_LIST"  \
	  disks/vxvm/disk_groups/vxprint-rmv_4vxmk=$DG_NAME
    fi

    # this output lists also the dg and dm definitions, this is the
    # format vxprint expects when playing back the output later.
    if [ $VM2X -eq 0 ]
    then
       get_cmd "/usr/sbin/vxprint -hmQqg $DG_NAME" \
	   disks/vxvm/disk_groups/vxprint-hmQqg=$DG_NAME
    else
       # For 3.x, this takes two commands.....
       get_cmd "/usr/sbin/vxprint -mdg   $DG_NAME" \
	   disks/vxvm/disk_groups/vxprint-mvrGg=$DG_NAME
       get_cmd "/usr/sbin/vxprint -mvrGg $DG_NAME" \
	   disks/vxvm/disk_groups/vxprint-mvrGg=$DG_NAME
    fi

    #
    # We need to determine 'privpaths' for vxprivutil
    #
    # Get the name of the first disk with configuration data, eg: c1t0d0s7
    # only list the disks with 'clean' copies on them.
    # exit in awk stops after the first line, i.e. disk
    CONF_DISK=`/usr/sbin/vxdg list $DG_NAME | egrep -e "state=clean" | \
		nawk '$1 == "config" && $2 == "disk" {print $3; exit} '`

    # Dump the disk's config and determine the path to its private region.
    PRIV_PATH=`/usr/sbin/vxdisk list ${CONF_DISK} | \
	nawk '$1 == "privpaths:" {sub(/block=/, "", $2); print $2; exit}'`

    if [ "$PRIV_PATH" = "" ]
    then
	# Couldn't find privpaths:, so get pubpaths: instead. In some cases
	# like using a dummy slice for rootdg 'privpaths:' may be missing.
	PRIV_PATH=`/usr/sbin/vxdisk list ${CONF_DISK} | \
	    nawk '$1 == "pubpaths:" {sub(/block=/, "", $2); print $2; exit}'`
    fi

    if [ "$PRIV_PATH" != "" ]
    then
	get_cmd "/etc/vx/diag.d/vxprivutil dumpconfig ${PRIV_PATH}" \
	    disks/vxvm/disk_groups/vxprivutil-dumpconfig=${DG_NAME}
    fi
done

#
# Files to check for hot relocation
#    /etc/default/vxassist is collected in etc.info
#
get_file /etc/vx/vxrelocd             etc/vx
get_file /etc/rc2.d/S95vxvm-recover   etc/vx
get_file /etc/vfstab.prevm            etc/vx

# Gather the /etc/vx/volboot file
get_file /etc/vx/volboot	      etc/vx

get_dir  /etc/vx/elm                  etc/vx/elm
get_dir  recursive /etc/vx/reconfig.d etc/vx/reconfig.d
get_dir  recursive /var/vxvm          etc/vx/var

# Sum of libraries
get_cmd "/usr/bin/sum /etc/vx/slib/* /usr/lib/libc.so.1 /usr/lib/libthread.so.1" etc/vx/sum
