#!/sbin/sh -
# %W% %G% %U% - %Q%
#ident "%Z%unixvm:%M% %I%"

# Copyright (c) 2000 VERITAS Software Corporation.  ALL RIGHTS RESERVED.
# UNPUBLISHED -- RIGHTS RESERVED UNDER THE COPYRIGHT
# LAWS OF THE UNITED STATES.  USE OF A COPYRIGHT NOTICE
# IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
# OR DISCLOSURE.
# 
# THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND
# TRADE SECRETS OF VERITAS SOFTWARE.  USE, DISCLOSURE,
# OR REPRODUCTION IS PROHIBITED WITHOUT THE PRIOR
# EXPRESS WRITTEN PERMISSION OF VERITAS SOFTWARE.
# 
#               RESTRICTED RIGHTS LEGEND
# USE, DUPLICATION, OR DISCLOSURE BY THE GOVERNMENT IS
# SUBJECT TO RESTRICTIONS AS SET FORTH IN SUBPARAGRAPH
# (C) (1) (ii) OF THE RIGHTS IN TECHNICAL DATA AND
# COMPUTER SOFTWARE CLAUSE AT DFARS 252.227-7013.
#               VERITAS SOFTWARE
# 1600 PLYMOUTH STREET, MOUNTAIN VIEW, CA 94043


# This startup script must be run after / and /usr are available,
# and makes other volumes available that are needed early in the
# Solaris boot sequence.  This script can operate while / and /usr
# are still readonly.

#
# Also, need to force C locale setting so that
# english text is processed.
#
LANG=C
export LANG


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

# 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 /VXVM3.0-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
startvols=
firstswap=
dumpvol=
exec < /etc/vfstab
while read dev rdev mpoint fstype more
do
	isdumpdev=
	case $dev in
	\#*)    continue
	        ;;
	esac

	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/[^/]*/\(.*\)'`"
		msg="\
vxvm: Volume $vol for mountpoint $mpoint is not in rootdg disk group;
	Cannot start the volume early"
		msg="`egettxt "\$msg" vxvmshm:519`"
		echo "$msg"
		;;
	/dev/vx/dsk/*)
		vol="`expr "\$dev" : '/dev/vx/dsk/\(.*\)'`"
		;;
	*)	continue
	esac
	startvols="$startvols $vol"
	[ -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" ] && [ ! -x /usr/sbin/dumpadm ] && {
	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
		old_device=$base_device
		path=`vxdisk list $base_device 2>/dev/null | \
			grep "^c[0-9]*t\{0,1\}[0-9]*d[0-9]*" | \
			awk '$2 == "state=enabled" {print $1}'`
		path=`echo $path`
		if [ "X$path" != "X" ]
		then
			base_device=`echo $path | cut -d "s" -f 1` 
		fi

		if [ -z "$base_device" ]
		then
			base_device=$old_device
		fi
		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

			# set the dump device
			dogi_slice_blkpath $try_slice dumpdev
			found=yes
			echo \
"vxvm: NOTE: Setting partition $dumpdev as the dump device."
			swap -a $dumpdev
			swap -d $dumpdev
			break
		done
	    done
	    [ -n "$found" ] || {
		echo \
"vxvm: WARNING: No suitable partition from $dumpvol to set as the dump device."
	    }
	}
}

# start those volumes
[ -n "$startvols" ] && {
	echo "VxVM starting special volumes ($startvols )..."
	vxrecover -n -s -g rootdg $startvols || {
	    egettxt "vxvm: WARNING: Some special volumes could not be started" \
		vxvmshm:575
	}
}
