#!/sbin/sh
#
# Copyright (c) 1999, by Sun Microsystems, Inc.
# All rights reserved.
#
#ident "@(#)dumpvp_setup	1.4     99/04/13"
#

# do not attempt this script if the Volume Manager libraries are not
# present on the system

if [ ! -x /usr/lib/vxvm/voladm.d/lib/vxadm_lib.sh ]
then
	exit 0
fi

PATH=/usr/lib/vxvm/bin:/sbin:/usr/sbin:/usr/bin
 
. ${VOLADM_LIB:-/usr/lib/vxvm/voladm.d/lib}/vxadm_lib.sh
 
set_OS_variables

#
# define used commands
#
U4FTCTL="/platform/SUNW,Ultra-4FT/lib/u4ftctl"
SCSICTL="/usr/platform/SUNW,Ultra-4FT/SUNWcms/lib/scsictl"

#
# find the disk device used for this boot
#
cookie=`$U4FTCTL find / chosen`

# get its path
path=`$U4FTCTL prop_get_strings $cookie "bootpath" | \
					sed -e "s/disk/sd/" | \
					sed -e "s/\"$//" | \
					sed -e "s/^\"/\/devices/"`

# get the chosen device name
chosen_disk=`ls -l /dev/dsk/ | grep $path | sed -e "s/^.* c/c/" | \
					sed -e "s/s.*$//"`

# do not start if the upgrade_start script was run and the upgrade_finish
# has not be run yet, because /etc/vx/slib does not have right libraries
# till the upgrade_finish is run

if [ -f /VXVM2.5-UPGRADE/.start_runed ]
then
	exit 0
fi

# if the volume manager hasn't yet been initialized, don't bother
# starting vold.  The install-db file indicates that initialization
# has NOT yet happened.

if [ -f "/etc/vx/reconfig.d/state.d/install-db" ]
then
	exit 0
fi

# look for volumes that should be started early
firstswap=
dumpvol=
exec < /etc/vfstab
while read dev rdev mpoint fstype more
do
	isdumpdev=
	case $mpoint in
	/usr/kvm | /var | /var/adm )
		;;
	*)	case $fstype in
		swap )	[ -z "$firstswap" ] && isdumpdev=yes
			firstswap=yes
			;;
		*)	continue;;
		esac
		;;
	esac
	case $dev in
	/dev/vx/dsk/rootdg/*)
		vol="`expr "\$dev" : '/dev/vx/dsk/rootdg/\(.*\)'`"
		;;
	/dev/vx/dsk/*/*)
		vol="`expr "\$dev" : '/dev/vx/dsk/[^/]*/\(.*\)'`"
		;;
	/dev/vx/dsk/*)
		vol="`expr "\$dev" : '/dev/vx/dsk/\(.*\)'`"
		;;
	*)	continue
	esac
	[ -n "$isdumpdev" ] && dumpvol=$vol
done
exec < /dev/null

# For Solaris, if the first swap device is a volume, then it will
# be used as the dump device.  Since VxVM does not currently support
# volumes as dump devices, use a strange behavior in the Solaris
# kernel to make an underlying partition device into the dump device.
# The behavior is that the first swap device added to the system
# becomes the dump device, even if that device is later removed.

[ -n "$dumpvol" ] && {
	vxprint -se \
		"sd_pl_offset=0 &&
		 assoc.assoc=\"$dumpvol\" && len >= assoc.assoc.len" \
		-F "%path %dev_offset %len" |
	{
	    found=
	    while [ -z "$found" ] && read path devoffset sdlen
	    do
		# get the disk offset of the subdisk
		dogi_path_to_slice $path base_slice
		[ -n "$base_slice" ] || continue
		dogi_slice_rawpath $base_slice base_rawpath
		STAT_PSTARTSEC=
		STAT_DISK_TYPE=
		eval "`vxparms -s $base_rawpath 2> /dev/null`"
		[ -n "$STAT_PSTARTSEC" ] || continue
		[ "X$STAT_DISK_TYPE" = Xdisk ] || continue
		diskoffset=`expr $devoffset + $STAT_PSTARTSEC`

		# locate a partition that matches the beginning of that
		# subdisk, and that is not longer than the subdisk
		dogi_slice_to_device $base_slice base_device
		for slice_num in 1 0 2 3 4 5 6 7
		do
			dogi_device_slice $base_device $slice_num try_slice 
			dogi_slice_rawpath $try_slice try_rawpath
			STAT_PSTARTSEC=
			STAT_PNUMSEC=
			eval "`vxparms -s $try_rawpath`"
			[ -n "$STAT_PSTARTSEC" -a -n "$STAT_PNUMSEC" ] ||
				continue
			[ $STAT_PSTARTSEC -eq $diskoffset ] || continue
			[ $STAT_PNUMSEC -le $sdlen ] || continue
			[ -n "$base_device" ] || continue
			[ "$base_device" = "$chosen_disk" ] || continue
			# set the dump device
			dogi_slice_blkpath $try_slice dumpdev
			found=yes
			echo \
"dumpvp_setup: Setting partition $dumpdev as the dump device."
			rdumpdev=`echo $dumpdev | sed -e "s/dsk/rdsk/"`
			$SCSICTL SETDUMP $rdumpdev $dumpdev
			break
		done
	    done
	    [ -n "$found" ] || {
		echo \
"WARNING: No suitable partition from $dumpvol to set as the dump device."
	    }
	}
}

