#!/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


: ${VOLROOT_DIR:=$__VXVM_ROOT_DIR}
. ${VOL_SCRIPTS_LIB:-$VOLROOT_DIR/usr/lib/vxvm/lib}/vxcommon

voladm_help_push yorn.help
voladm_menu_push VolumeManager/Unroot

set_OS_variables

# Get info and set variables regarding the device slices and tags
eval `vxparms`

# The OPT partition tag is set to the "unused" state
V_OPT=$V_UNUSED

#
# unroot_check - Check that no other object(s) but those removed by
# this script exist on the disk whose dm name is passed in as an
# argument.
#
# The approved list of objects is: rootvol swapvol usr var opt home
#
unroot_check()
{
	disk_dm=$1
	disk=`vxprint -F "%da_name" $disk_dm`

	#
	# Check for any volumes on this disk other than the approved list
	#
	vxprint -g "rootdg" -vn \
		-e "aslist.aslist.sd_dm_name==\"$disk_dm\"" | \
		 egrep -v \
		"^rootvol$|^swapvol$|^usr$|^var$|^opt$|^home$"> $tmpfile1

	#
	# Check for any unassociated plexes on this disk
	#
	vxprint -g "rootdg" -pn \
		-e "!assoc && aslist.sd_dm_name==\"$disk_dm\"" > $tmpfile2

	#
	# Check for any unassociated subdisks on this disk that do not
	# belong to this list of exceptions: ...G ...Priv ...-B0
	#
	vxprint -g "rootdg" -n -sd -e "!assoc && sd_dm_name==\"$disk_dm\"" | \
	    egrep -v "^${disk_dm}G\$|^${disk_dm}Priv\$|^${disk_dm}-B0\$"> $tmpfile3

	if [ -s $tmpfile1 -o -s $tmpfile2 -o -s $tmpfile3 ]; then
export tmpfile1 tmpfile2 tmpfile3 disk; ewritemsg -M vxvmshm:168 \
"ERROR: The vxunroot operation cannot proceed unless the 
	following objects on $disk are (re)moved."
		(echo ""; cat $tmpfile1 $tmpfile2 $tmpfile3; echo "") | \
		voladm_display
		quit 10
	fi

	return 0
}

# return the offset of the public region of a given disk in pub_off 
get_pub_offset() {

	[ -z "$VOL_PUB_SLICE_TAG" ] && 	VOL_PUB_SLICE_TAG=0xe
	disk=$1
	dogi_device_rawpath $disk rawpath
	$PRTVTOC -f $tmpfile1 $rawpath
	exec 3<&0 < $tmpfile1
	pub_off=0

	while read slice tag flags start size
	do
		if [ "$tag" = "$VOL_PUB_SLICE_TAG" ] ; then
			pub_off=$start
		fi
	done
	exec 0<&3 3<&-
}

# sliceinfo varprefix daname sliceno -- get information on the given slice
sliceinfo()
{
	_prefix=$1
	dogi_whole_slice $2 whole_slice || return
	dogi_slice_rawpath $whole_slice rawpath
	set `vxpartinfo "\$rawpath" $3 2> /dev/null`
	if [ $# -ne 5 ] 
	then
		return 1
	fi
	eval "${_prefix}_tag=$2"
	eval "${_prefix}_flags=$3"
	eval "${_prefix}_offset=$4"
	eval "${_prefix}_len=$5"
	return 0
}

# is_free_slice daname sliceno tag -- determine if a slice is
# in use or has been picked already
is_free_slice()
{
	list_member $1:$2 $slices_pending && return 1
	sliceinfo _slc "$1" $2 || return 1
	[ $# -gt 2 -a "X$_slc_tag" = "X$3" ] && return 0
	[ "X$_slc_tag" = X0x0 ] && return 0
	return 1
}

# find_slice varname "trylist" da tag -- Used to find a free slice
# on the disk
find_slice()
{
	for tslice in $2
	do
		is_free_slice $3 $tslice $4 && {
			eval "$1=\$tslice"
			append_list slices_pending "$3:$tslice"
			return 0
		}
	done
	eval "$1="
	return 1
}

#
# DMP:
# dmpdisk_to_subpath: Function to check which subpath was specified
# in the /etc/vfstab and put that subpath correctly for the mount
# point entries.  If this information is not available from reconfig.d
# directory then use any of the active subpaths, hopefully system should
# work after vxunroot. If there is only one subpath for the dmp root
# device then use that directly. If not DMP device, use the name
# directly.
#

dmpdisk_to_subpath()
{
	dmpdisk=$1

	vxdisk list $dmpdisk  > $tmpfile1
	grep '^numpaths' $tmpfile1 > $tmpfile2 2> /dev/null

	if [ $? -ne 0 ]
	then
		eval "${2}=$dmpdisk"
		return 0
	fi

	exec 3<&0 < $tmpfile2
	read numpaths num
	exec 0<&3 3<&-

	if [ $num = 1 ]
	then
		eval "${2}=$dmpdisk"
		return 0
	fi

	vxdisk list $dmpdisk  > $tmpfile1
	grep "^c[0-9]" $tmpfile1 > $tmpfile2 2> /dev/null
	exec 3<&0 < $tmpfile2
	while read line; do
		subpath=`echo $line | cut -f 1 -d " "`
		state=`echo $line | cut -f 2 -d " "`
		if [ X"$state" = Xstate=enabled ]
		then
			break
		fi
	done
	exec 0<&3 3<&-
	
	if [ X"$subpath" != X ]
	then
		dogi_slice_to_device $subpath subpathdev
		eval "${2}=$subpathdev"
		return 0
	fi

	grep '^#rename' $DISKDIR/$dmpdisk/newpart > $tmpfile1 2> /dev/null
	if [ $? -eq 0 ]
	then
		exec 3<&0 < $tmpfile1
		read rename subpath voldmpdev
		exec 0<&3 3<&-
		dogi_slice_to_device $subpath subpathdev
		eval "${2}=$subpathdev"
		return 0
	fi

	eval "${2}=$dmpdisk"
	return 0
}

#
# Build the sliceno list, which is assigned back to argument #1.
#
# This function is intended to be call before find_slice(). The slice list
# that this function creates dictate the search order of free slices in
# find_slice() function. For example, a slice list of "7 3 4 5 1 6" indicates
# that slice 7 will be searched first for free slice, follow by slice 3, then
# 4, and so on.
# 
# The sliceno list is built based on the volume name (i.e. argument #2) and
# knowledge of pre-encapsulated parition information (located in the commented
# section of /etc/vfstab). With the goal of putting slices back in the same
# place as pre-encapsulation, we build a list with the first entry matching
# the slice number this volume had at pre-encapsulation. For example, if at
# pre-encapsulation, /opt was in slice #5, then the sliceno list will have "5"
# in the beginning of the list. That is, the returned list will be
# "5 7 3 4 1 6"
#
# Input:
#  $2 = volume name, e.g. "opt"
#  $3 = the default slice number list
#
# Output:
#  $1 = the newly modified slice number list
#
build_sliceno_list()
{
	# Pre-set the slice list to supplied preferred list. We will default
	# to this list in case any error occurs.
	preset_sliceno_list="$3"

	# Initialize pre-encapsulated sliceno to nothing. Currently, we have
	# not found one yet.
	preencapsulated_sliceno=

	if [ ! -s $SYSROOT/etc/vfstab ]
	then
		# Actually, if /etc/vfstab is NOT found, we should have bigger
		# problems elsewhere. Anyway, we can not process further so
		# just return original pre-set sliceno list
		eval "$1=\"$preset_sliceno_list\""
		return
	fi
		
	# Pre-encapsulated slice information are stored in vfstab
	# with a "#NOTE:" prefix.
	preencap_sliceentry=`cat $SYSROOT/etc/vfstab \
		| egrep "^#NOTE: volume $2" | egrep "encapsulated partition"`

	# If that vfstab entry is missing, then just return the preset
	# sliceno list
	if [ ! -n $preencap_sliceentry ]
	then
		eval "$1=\"$preset_sliceno_list\""
		return
	fi

	# Extract sliceno from device name. For example, the sliceno of
	# "c0t0d0s4" is 4.
	preencapsulated_sliceno=`echo $preencap_sliceentry \
		| awk '{ print substr($NF, length($NF), 1) }'`

	# Make sure we have a valid slice number
	case $preencapsulated_sliceno in
		[3-7])	;;
		1)	;;
		*)	
			# Invalid slice number. Just return pre-set list
			#  instead.
			eval "$1=\"$preset_sliceno_list\""
			return
	esac

	# Remove the sliceno from the pre-set list. Later on, we will
	# add the sliceno at the beginning of the list, which will have
	# the net effect of moving the sliceno to the beginning.
	tmplist=`echo $preset_sliceno_list \
		| sed s/$preencapsulated_sliceno//g | sed s/"  "/" "/g`

	# Prefix the pre-set list with a sliceno. This causes this sliceno to
	#  be searched FIRST when running the find_slice() function.
	new_slicelist="$preencapsulated_sliceno $tmplist"

	eval "${1}=\"$new_slicelist\""
	return
}


tempdisk=

# Find all subdisks that are not log subdisks for a plex.

vnames=`vxprint -F "%vname" -s\
    -e "(!sd_is_log)" | sort -u`
 
# If no root volume then we can exit now.
echo "$vnames" | grep -w rootvol 2>&1 > /dev/null
if [ $? -ne 0 ]
then
	CMD=$0
        export CMD; egettxt "$CMD: No rootvol found." \
        vxvmshm:991 $CMD  >&2
        quit 1

fi

# Initialize list of slices

slices_pending=

# Find if swap, usr, var, opt, and home are also on volumes

swap_is_vol=
usr_is_vol=
var_is_vol=
opt_is_vol=
home_is_vol=
echo $vnames | grep -w swapvol 2>&1 > /dev/null
if [ $? -eq 0 ]
then
	swap_is_vol=yes
fi
echo $vnames | grep -w usr 2>&1 > /dev/null
if [ $? -eq 0 ]
then
	usr_is_vol=yes
fi
echo $vnames | grep -w var 2>&1 > /dev/null
if [ $? -eq 0 ]
then
	var_is_vol=yes
fi
echo $vnames | grep -w opt 2>&1 > /dev/null
if [ $? -eq 0 ]
then
	opt_is_vol=yes
fi
echo $vnames | grep -w home 2>&1 > /dev/null
if [ $? -eq 0 ]
then
	home_is_vol=yes
fi

ewritemsg -M vxvmshm:947 \
"This operation will convert the following file systems from
  volumes to regular partitions: root swap usr var opt home"

# Assert that the count of plexes for the ROOT volume is exactly one.

for vname in rootvol
do
	vxprint -g rootdg -F "%pl_num" $vname > $tmpfile1
	if [ $? -ne 0 ]
	then
		export vname; ewritemsg -M vxvmshm:504 \
"WARNING: Cannot determine the count of plexes for the $vname volume"
	else
		if [ "X`cat $tmpfile1`" != X1 ]
		then
export tmpfile1 vname; ewritemsg -M vxvmshm:170 \
"ERROR: There are `cat $tmpfile1` plexes associated with volume $vname
         The vxunroot operation cannot proceed."
			quit 10
		fi
	fi
done
rm -f $tmpfile1

# Assert that the count of plexes for the SWAP volume is exactly one.

if [ ! -z "$swap_is_vol" ]
then
	vxprint -g rootdg -F "%pl_num" swapvol  > $tmpfile1
	if [ $? -ne 0 ]
	then
		ewritemsg -M vxvmshm:505 \
"WARNING: Cannot determine the count of plexes for the swap volume"
	else
		if [ "X`cat $tmpfile1`" != X1 ]
		then
export tmpfile1 ; ewritemsg -M vxvmshm:169 \
"ERROR: There are `cat $tmpfile1` plexes associated with the swap volume
         The vxunroot operation cannot proceed."
			quit 10
		fi
	fi
fi

# Assert that the count of plexes for the USR volume is exactly one.

if [ ! -z "$usr_is_vol" ]
then
	vxprint -g rootdg -F "%pl_num" usr  > $tmpfile1
	if [ $? -ne 0 ]
	then
		ewritemsg -M vxvmshm:506 \
"WARNING: Cannot determine the count of plexes for the usr volume"
	else
		if [ "X`cat $tmpfile1`" != X1 ]
		then
export tmpfile1 ; ewritemsg -M vxvmshm:171 \
"ERROR: There are `cat $tmpfile1` plexes associated with volume usr
         The vxunroot operation cannot proceed."
			quit 10
		fi
	fi
fi

# Assert that the count of plexes for the VAR volume is exactly one.

if [ ! -z "$var_is_vol" ]
then
	vxprint -g rootdg -F "%pl_num" var > $tmpfile1
	if [ $? -ne 0 ]
	then
		ewritemsg -M vxvmshm:623 \
"WARNING: Cannot determine the count of plexes for the var volume"
	else
		if [ "X`cat $tmpfile1`" != X1 ]
		then
export tmpfile1 ; ewritemsg -M vxvmshm:615 \
"ERROR: There are `cat $tmpfile1` plexes associated with volume var
         The vxunroot operation cannot proceed."
			quit 10
		fi
	fi
fi

# Assert that the count of plexes for the OPT volume is exactly one.

if [ ! -z "$opt_is_vol" ]
then
	vxprint -g rootdg -F "%pl_num" opt > $tmpfile1
	if [ $? -ne 0 ]
	then
		ewritemsg -M vxvmshm:507 \
"WARNING: Cannot determine the count of plexes for the opt volume"
	else
		if [ "X`cat $tmpfile1`" != X1 ]
		then
export tmpfile1 ; ewritemsg -M vxvmshm:172 \
"ERROR: There are `cat $tmpfile1` plexes associated with volume opt
         The vxunroot operation cannot proceed."
			quit 10
		fi
	fi
fi

# Assert that the count of plexes for the HOME volume is exactly one.

if [ ! -z "$home_is_vol" ]
then
	vxprint -g rootdg -F "%pl_num" home > $tmpfile1
	if [ $? -ne 0 ]
	then
		ewritemsg -M vxvmshm:694 \
"WARNING: Cannot determine the count of plexes for the home volume"
	else
		if [ "X`cat $tmpfile1`" != X1 ]
		then
export tmpfile1 ; ewritemsg -M vxvmshm:668 \
"ERROR: There are `cat $tmpfile1` plexes associated with volume home
         The vxunroot operation cannot proceed."
			quit 10
		fi
	fi
fi

# Find the disk on which rootvol resides

rootdisk=
rootdisk_dm=
good_root=
vxprint -g rootdg -hqt rootvol  > $tmpfile1
grep '^sd' $tmpfile1 > $tmpfile2
exec 3<&0 < $tmpfile2
read sd disksd pl rootdisk_dm rootstart rootlen var dash


# Omit ghost sd

sub=0
if [ $rootlen -eq $VOL_GHOST_LEN ] 
then
	read sd disksd pl rootdisk_dm rootstart rootlen var dash
	rootlen=`expr $rootlen + $VOL_GHOST_LEN` 
	sub=1
fi
rootstart=`vxprint -F  "%dev_offset" $disksd`
rootstart=`expr $rootstart - $sub`
rootdisk=`vxprint -F "%da_name" $rootdisk_dm`
dogi_slice_to_device $rootdisk rootdisk
#rootdisk=`expr ${rootdisk} : '\(.*\)s.'`
exec 0<&3 3<&-

if [ -z "$rootdisk" ]
then
	ewritemsg -M vxvmshm:164 \
	"ERROR: Cannot determine the rootdisk."
	quit 1
fi

# Find out one good path of the rootdisk so that its vtoc can be read..
dmpdisk_to_subpath $rootdisk good_root

get_pub_offset $good_root
rootstart=`expr $rootstart + $pub_off`

# Find the disk on which swapvol resides

swapdisk_dm=
swapdisk=
good_swap=
if [ ! -z "$swap_is_vol" ]
then
	vxprint -g rootdg -hqt swapvol  > $tmpfile1
	grep '^sd' $tmpfile1 > $tmpfile2
	exec 3<&0 < $tmpfile2
	read sd disksd pl swapdisk_dm swapstart swaplen var dash
	sub=0
	if [ $swaplen -eq $VOL_GHOST_LEN ]
	then
		read sd disksd pl swapdisk_dm swapstart swaplen var dash
		swaplen=`expr $swaplen + $VOL_GHOST_LEN` 
		sub=1
	fi
	swapstart=`vxprint -F  "%dev_offset" $disksd`
	swapstart=`expr $swapstart - $sub`
	swapdisk=`vxprint -F "%da_name" $swapdisk_dm`
	dogi_slice_to_device $swapdisk swapdisk
	#swapdisk=`expr ${swapdisk} : '\(.*\)s.'`
	exec 0<&3 3<&-
	if [ -z "$swapdisk" ]
	then
		ewritemsg -M vxvmshm:165 \
		"ERROR: Cannot determine the swapdisk."
		quit 1
	fi

# Get any good path to the swap disk
dmpdisk_to_subpath $swapdisk good_swap

	get_pub_offset $good_swap
	swapstart=`expr $swapstart + $pub_off`
fi


# Find the disk on which usr resides

usrdisk_dm=
usrdisk=
good_usr=
if [ ! -z "$usr_is_vol" ]
then
	vxprint -g rootdg -hqt usr  > $tmpfile1
	grep '^sd' $tmpfile1 > $tmpfile2
	exec 3<&0 < $tmpfile2
	sub=0
	read sd disksd pl usrdisk_dm usrstart usrlen var dash
	if [ $usrlen -eq $VOL_GHOST_LEN ]
	then
		read sd disksd pl usrdisk_dm usrstart usrlen var dash
		usrlen=`expr $usrlen + $VOL_GHOST_LEN` 
		sub=1
	fi
	usrstart=`vxprint -F  "%dev_offset" $disksd`
	usrstart=`expr $usrstart - $sub`
	usrdisk=`vxprint -F "%da_name" $usrdisk_dm`
	dogi_slice_to_device $usrdisk usrdisk
	#usrdisk=`expr ${usrdisk} : '\(.*\)s.'`
	exec 0<&3 3<&-
	if [ -z "$usrdisk" ]
	then
		ewritemsg -M vxvmshm:166 \
		"ERROR: Cannot determine the usrdisk."
		quit 1
	fi

	# Get any good path to the user disk
	dmpdisk_to_subpath $usrdisk good_usr

	get_pub_offset $good_usr
	usrstart=`expr $usrstart + $pub_off`
fi

# Find the disk on which var resides

vardisk_dm=
vardisk=
good_var=
if [ ! -z "$var_is_vol" ]
then
	vxprint -g rootdg -hqt var  > $tmpfile1
	grep '^sd' $tmpfile1 > $tmpfile2
	exec 3<&0 < $tmpfile2
	sub=0
	read sd disksd pl vardisk_dm varstart varlen var dash
	if [ $varlen -eq $VOL_GHOST_LEN ]
	then
		read sd disksd pl vardisk_dm varstart varlen var dash
		varlen=`expr $varlen + $VOL_GHOST_LEN` 
		sub=1
	fi
	varstart=`vxprint -F  "%dev_offset" $disksd`
	varstart=`expr $varstart - $sub`
	vardisk=`vxprint -F "%da_name" $vardisk_dm`
	dogi_slice_to_device $vardisk vardisk
	#vardisk=`expr ${vardisk} : '\(.*\)s.'`
	exec 0<&3 3<&-
	if [ -z "$vardisk" ]
	then
		ewritemsg -M vxvmshm:614 \
		"ERROR: Cannot determine the vardisk."
		quit 1
	fi
	# Get any good path to the var disk
	dmpdisk_to_subpath $vardisk good_var
	get_pub_offset $good_var
	varstart=`expr $varstart + $pub_off`
fi

# Find the disk on which opt resides

optdisk_dm=
optdisk=
good_opt=
if [ ! -z "$opt_is_vol" ]
then
	vxprint -g rootdg -hqt opt  > $tmpfile1
	grep '^sd' $tmpfile1 > $tmpfile2
	exec 3<&0 < $tmpfile2
	sub=0
	read sd disksd pl optdisk_dm optstart optlen var dash
	if [ $optlen -eq $VOL_GHOST_LEN ]
	then
		read sd disksd pl optdisk_dm optstart optlen var dash
		optlen=`expr $optlen + $VOL_GHOST_LEN` 
		sub=1
	fi
	optstart=`vxprint -F  "%dev_offset" $disksd`
	optstart=`expr $optstart - $sub`
	optdisk=`vxprint -F "%da_name" $optdisk_dm`
	dogi_slice_to_device $optdisk optdisk
	#optdisk=`expr ${optdisk} : '\(.*\)s.'`
	exec 0<&3 3<&-
	if [ -z "$optdisk" ]
	then
		ewritemsg -M vxvmshm:167 \
		"ERROR: Cannot determine the optdisk."
		quit 1
	fi
	# Get a good path to the opt disk
	dmpdisk_to_subpath $optdisk good_opt
	get_pub_offset $good_opt
	optstart=`expr $optstart + $pub_off`
fi

# Find the disk on which home resides

homedisk_dm=
homedisk=
good_home=
if [ ! -z "$home_is_vol" ]
then
	vxprint -g rootdg -hqt home  > $tmpfile1
	grep '^sd' $tmpfile1 > $tmpfile2
	exec 3<&0 < $tmpfile2
	sub=0
	read sd disksd pl homedisk_dm homestart homelen var dash
	if [ $homelen -eq $VOL_GHOST_LEN ]
	then
		read sd disksd pl homedisk_dm homestart homelen var dash
		homelen=`expr $homelen + $VOL_GHOST_LEN` 
		sub=1
	fi
	homestart=`vxprint -F  "%dev_offset" $disksd`
	homestart=`expr $homestart - $sub`
	homedisk=`vxprint -F "%da_name" $homedisk_dm`
	dogi_slice_to_device $homedisk homedisk
	#homedisk=`expr ${homedisk} : '\(.*\)s.'`
	exec 0<&3 3<&-
	if [ -z "$homedisk" ]
	then
		ewritemsg -M vxvmshm:666 \
		"ERROR: Cannot determine the homedisk."
		quit 1
	fi
	# Get a good path to the home disk
	dmpdisk_to_subpath $homedisk good_home
	get_pub_offset $good_home
	homestart=`expr $homestart + $pub_off`
fi

# Find root slice on root disk
# Must send the active root disk path because we are doing a prtvtoc on it 

dogi_device_rawpath $good_root rootrpath
$PRTVTOC -f $tmpfile1 $rootrpath
exec 3<&0 < $tmpfile1
rootno=
while read slice tag flags start size
do
	case $slice in
	[0-9])	sliceno=$slice;;
	'#'*)	continue;;
	esac
	if [ $start -eq $rootstart ] && [ $size -eq $rootlen ]
	then
		rootno=$sliceno
	fi
done
exec 0<&3 3<&-

# Find swap slice on swap disk

if [ ! -z "$swap_is_vol" ]
then
	dogi_device_rawpath $good_swap swaprpath
	$PRTVTOC -f $tmpfile1 $swaprpath
	exec 3<&0 < $tmpfile1
	swapno=
	while read slice tag flags start size
	do
		case $slice in
		[0-9])	sliceno=$slice;;
		'#'*)	continue;;
		esac
		if [ $start -eq $swapstart ] && [ $size -eq $swaplen ]
		then
			swapno=$sliceno
		fi
	done
	exec 0<&3 3<&-
fi

# Find usr slice on usr disk
mkusrpart=
if [ ! -z "$usr_is_vol" ]
then
	dogi_device_rawpath $good_usr usrrpath
	$PRTVTOC -f $tmpfile1 $usrrpath
	exec 3<&0 < $tmpfile1
	usrno=
	while read slice tag flags start size
	do
		case $slice in
		[0-9])	sliceno=$slice;;
		'#'*)	continue;;
		esac
		if [ $start -eq $usrstart ] && [ $size -eq $usrlen ]
		then
			usrno=$sliceno
		fi
	done
	exec 0<&3 3<&-
	if [ -z "$usrno" ]
	then
		sliceno_list=
		build_sliceno_list sliceno_list "usr" "6 7 3 4 5 1"
		find_slice usrno "$sliceno_list" $usrdisk $V_USR
		mkusrpart=yes
	fi
fi

# Find var slice on var disk
mkvarpart=
if [ ! -z "$var_is_vol" ]
then
	dogi_device_rawpath $good_var varrpath
	$PRTVTOC -f $tmpfile1 $varrpath
	exec 3<&0 < $tmpfile1
	varno=
	while read slice tag flags start size
	do
		case $slice in
		[0-9])	sliceno=$slice;;
		'#'*)	continue;;
		esac
		if [ $start -eq $varstart ] && [ $size -eq $varlen ]
		then
			varno=$sliceno
		fi
	done
	if [ -z "$varno" ]
	then
		sliceno_list=
		build_sliceno_list sliceno_list "var" "7 3 4 5 1 6"
		find_slice varno "$sliceno_list" $vardisk $V_VAR
		mkvarpart=yes
	fi
	exec 0<&3 3<&-
fi

# Find opt slice on opt disk
mkoptpart=
if [ ! -z "$opt_is_vol" ]
then
	dogi_device_rawpath $good_opt optrpath
	$PRTVTOC -f $tmpfile1 $optrpath
	exec 3<&0 < $tmpfile1
	optno=
	while read slice tag flags start size
	do
		case $slice in
		[0-9])	sliceno=$slice;;
		'#'*)	continue;;
		esac
		if [ $start -eq $optstart ] && [ $size -eq $optlen ]
		then
			optno=$sliceno
		fi
	done
	if [ -z "$optno" ]
	then
		sliceno_list=
		build_sliceno_list sliceno_list "opt" "7 3 4 5 1 6"
		find_slice optno "$sliceno_list" $optdisk $V_OPT
		mkoptpart=yes
	fi
	exec 0<&3 3<&-
fi

# Find home slice on home disk
mkhomepart=
if [ ! -z "$home_is_vol" ]
then
	dogi_device_rawpath $good_home homerpath
	$PRTVTOC -f $tmpfile1 $homerpath
	exec 3<&0 < $tmpfile1
	homeno=
	while read slice tag flags start size
	do
		case $slice in
		[0-9])	sliceno=$slice;;
		'#'*)	continue;;
		esac
		if [ $start -eq $homestart ] && [ $size -eq $homelen ]
		then
			homeno=$sliceno
		fi
	done
	if [ -z "$homeno" ]
	then
		sliceno_list=
		build_sliceno_list sliceno_list "home" "7 3 4 5 1 6"
		find_slice homeno "$sliceno_list" $homedisk $V_HOME
		mkhomepart=yes
	fi
	exec 0<&3 3<&-
fi

if [ -z "$rootno" ]
then
	export rootdisk; ewritemsg -M vxvmshm:146 \
"Disk $rootdisk does not have a suitable ROOT partition to use.
  Perhaps the ROOTVOL plex is not the original one created by 
  vxrootmir or not the original one created when the rootdisk
  was encapsulated; or perhaps the ROOT partition no longer 
  exists on this disk.

  The vxunroot operation cannot proceed."
	quit 10
fi

unroot_check $rootdisk_dm

if [ ! -z "$swap_is_vol" ] && [ -z "$swapno" ]
then
	export swapdisk; ewritemsg -M vxvmshm:147 \
"Disk $swapdisk does not have a suitable SWAP partition to use.
  Perhaps the SWAPVOL plex is not the original one created by 
  vxrootmir or not the original one created when the rootdisk
  was encapsulated; or perhaps the SWAP partition no longer 
  exists on this disk.

  The vxunroot operation cannot proceed."
	quit 10
fi

unroot_check $swapdisk_dm

if [ ! -z "$usr_is_vol" ] && [ -z "$usrno" ]
then
	export usrdisk; ewritemsg -M vxvmshm:148 \
"Disk $usrdisk does not have a suitable USR partition to use.
  Perhaps the USR plex is not the original one created by 
  vxrootmir or not the original one created when the rootdisk
  was encapsulated; or perhaps the USR partition no longer 
  exists on this disk.

  The vxunroot operation cannot proceed."
	quit 10
fi

[ ! -z "$usr_is_vol" ] &&  unroot_check $usrdisk_dm

if [ ! -z "$var_is_vol" ] && [ -z "$varno" ]
then
	export vardisk; ewritemsg -M vxvmshm:611 \
"Disk $vardisk does not have a suitable VAR partition to use.
  Perhaps the VAR plex is not the original one created by 
  vxrootmir or not the original one created when the rootdisk
  was encapsulated; or perhaps the VAR partition no longer 
  exists on this disk.

  The vxunroot operation cannot proceed."
	quit 10
fi

[ ! -z "$var_is_vol" ] &&  unroot_check $vardisk_dm

if [ ! -z "$opt_is_vol" ] && [ -z "$optno" ]
then
	export optdisk; ewritemsg -M vxvmshm:149 \
"Disk $optdisk does not have a suitable OPT partition to use.
  Perhaps the OPT plex is not the original one created by 
  vxrootmir or not the original one created when the root
  was encapsulated; or perhaps the OPT partition no longer 
  exists on this disk.

  The vxunroot operation cannot proceed."
	quit 10
fi

[ ! -z "$opt_is_vol" ] &&  unroot_check $optdisk_dm

if [ ! -z "$home_is_vol" ] && [ -z "$homeno" ]
then
	export homedisk; ewritemsg -M vxvmshm:904 \
"Disk $homedisk does not have a suitable HOME partition to use.
  Perhaps the HOME plex is not the original one created by 
  vxrootmir or not the original one created when the root
  was encapsulated; or perhaps the HOME partition no longer 
  exists on this disk.

  The vxunroot operation cannot proceed."
	quit 10
fi

[ ! -z "$home_is_vol" ] &&  unroot_check $homedisk_dm

if [ -n "$mkusrpart" ]
then
	dogi_device_rawpath $usrdisk usrpath
	vxpartadd $usrpath $usrno $V_USR 0x200 $usrstart $usrlen
	if [ $? -ne 0 ]
	then
	ewritemsg -M vxvmshm:620 \
"Unable to create partition for usr volume"
		quit 10
	fi
fi

if [ -n "$mkvarpart" ]
then
	dogi_device_rawpath $vardisk varpath
	vxpartadd $varpath $varno $V_VAR 0x200 $varstart $varlen
	if [ $? -ne 0 ]
	then
	ewritemsg -M vxvmshm:621 \
"Unable to create partition for var volume"
		quit 10
	fi
fi
	

if [ -n "$mkoptpart" ]
then
	dogi_device_rawpath $optdisk optpath
	vxpartadd $optpath $optno $V_OPT 0x200 $optstart $optlen
	if [ $? -ne 0 ]
	then
	ewritemsg -M vxvmshm:619 \
"Unable to create partition for opt volume"
		quit 10
	fi
fi


if [ -n "$mkhomepart" ]
then
	dogi_device_rawpath $homedisk homepath
	vxpartadd $homepath $homeno $V_HOME 0x200 $homestart $homelen
	if [ $? -ne 0 ]
	then
	ewritemsg -M vxvmshm:948 \
"Unable to create partition for home volume"
		quit 10
	fi
fi

# We already have the active path in $good_root
#dmpdisk_to_subpath $rootdisk tempdisk
dogi_device_slice $good_root $rootno rootslice
dogi_slice_rawpath $rootslice rootrpath
dogi_slice_blkpath $rootslice rootbpath

export rootno rootslice swapno rootdisk swapdisk;  ewritemsg -M vxvmshm:278 \
"Replace volume rootvol with $rootslice.

  This operation will require a system reboot.  If you choose to
  continue with this operation, system configuration will be updated 
  to discontinue use of the volume manager for your root and swap
  devices."

voladm_yorn -M vxvmshm:162 "Do you wish to do this now" y || quit 10

[ -d $STATEDIR ] || volmkdirs

rm -f $rmvols
rm -f $rmvmdisks

cd $SAVEDIR
ewritemsg -M vxvmshm:287 "Restoring kernel configuration..."

#
# Replace ROOT partition stuff from /etc/vfstab ...
#
doit sed -e "s;^/dev/vx/dsk/rootvol\\([	 ]\\);${rootbpath}\\1;" \
	 -e "s;/dev/vx/rdsk/rootvol\\([	 ]\\);${rootrpath}\\1;" \
		< $SYSROOT/etc/vfstab > $tmpfile1 || quit 10

doit mv $tmpfile1 $SYSROOT/etc/vfstab || quit 10

#
# Remove ROOT partition stuff from /etc/system ...
# NOTE: don't remove any and all "rootdev" lines, just ours ...
# NOTE: restore any "rootdev" lines that we commented out ...
#
grep -v rootdev_is_volume $SYSROOT/etc/system > $tmpfile1
doit mv $tmpfile1 $SYSROOT/etc/system || quit 10

grep -v "^rootdev:/pseudo/vxio@0:0" $SYSROOT/etc/system > $tmpfile1
doit mv $tmpfile1 $SYSROOT/etc/system || quit 10

restore_commented_rootdev $SYSROOT/etc/system

echo rootvol >> $rmvols
echo $rootdisk_dm >> $rmvmdisks

#
# Replace SWAP partition stuff from /etc/vfstab ...
#
if [ ! -z "$swap_is_vol" ]
then
	#dmpdisk_to_subpath $swapdisk tempdisk
	dogi_device_slice $good_swap $swapno swapslice
	dogi_slice_blkpath $swapslice swapbpath
	doit sed -e "s;^/dev/vx/dsk/swapvol\\([	 ]\\);${swapbpath}\\1;" \
		< $SYSROOT/etc/vfstab > $tmpfile1 || quit 10

	doit mv $tmpfile1 $SYSROOT/etc/vfstab || quit 10
	echo swapvol >> $rmvols
	(grep $swapdisk_dm $rmvmdisks 2>&1 ) > /dev/null
	if [ $? -ne 0 ] ; then
		echo $swapdisk_dm >> $rmvmdisks
	fi
fi

#
# Replace USR partition stuff from /etc/vfstab ...
#
if [ ! -z "$usr_is_vol" ]
then
	#dmpdisk_to_subpath $usrdisk tempdisk
	dogi_device_slice $good_usr $usrno usrslice
	dogi_slice_rawpath $usrslice usrrpath
	dogi_slice_blkpath $usrslice usrbpath
	doit sed -e "s;^/dev/vx/dsk/usr\\([	 ]\\);${usrbpath}\\1;" \
	 -e "s;/dev/vx/rdsk/usr\\([	 ]\\);${usrrpath}\\1;" \
		< $SYSROOT/etc/vfstab > $tmpfile1 || quit 10

	doit mv $tmpfile1 $SYSROOT/etc/vfstab || quit 10
	echo usr >> $rmvols
	(grep $usrdisk_dm $rmvmdisks 2>&1 ) > /dev/null
	if [ $? -ne 0 ] ; then
		echo $usrdisk_dm >> $rmvmdisks
	fi
fi

#
# Replace VAR partition stuff from /etc/vfstab ...
#
if [ ! -z "$var_is_vol" ]
then
	#dmpdisk_to_subpath $vardisk tempdisk
	dogi_device_slice $good_var $varno varslice
	dogi_slice_rawpath $varslice varrpath
	dogi_slice_blkpath $varslice varbpath
	doit sed -e "s;^/dev/vx/dsk/var\\([	 ]\\);${varbpath}\\1;" \
	 -e "s;/dev/vx/rdsk/var\\([	 ]\\);${varrpath}\\1;" \
		< $SYSROOT/etc/vfstab > $tmpfile1 || quit 10

	doit mv $tmpfile1 $SYSROOT/etc/vfstab || quit 10
	echo var >> $rmvols
	(grep $vardisk_dm $rmvmdisks 2>&1 ) > /dev/null
	if [ $? -ne 0 ] ; then
		echo $vardisk_dm >> $rmvmdisks
	fi
fi

#
# Replace OPT partition stuff from /etc/vfstab ...
#
if [ ! -z "$opt_is_vol" ]
then
	#dmpdisk_to_subpath $optdisk tempdisk
	dogi_device_slice $good_opt $optno optslice
	dogi_slice_rawpath $optslice optrpath
	dogi_slice_blkpath $optslice optbpath
	doit sed -e "s;^/dev/vx/dsk/opt\\([	 ]\\);${optbpath}\\1;" \
	 -e "s;/dev/vx/rdsk/opt\\([	 ]\\);${optrpath}\\1;" \
		< $SYSROOT/etc/vfstab > $tmpfile1 || quit 10

	doit mv $tmpfile1 $SYSROOT/etc/vfstab || quit 10
	echo opt >> $rmvols
	(grep $optdisk_dm $rmvmdisks 2>&1 ) > /dev/null
	if [ $? -ne 0 ] ; then
		echo $optdisk_dm >> $rmvmdisks
	fi
fi

#
# Replace HOME partition stuff from /etc/vfstab ...
#
if [ ! -z "$home_is_vol" ]
then
	#dmpdisk_to_subpath $0 tempdisk
	dogi_device_slice $good_home $homeno homeslice
	dogi_slice_rawpath $homeslice homerpath
	dogi_slice_blkpath $homeslice homebpath
	doit sed -e "s;^/dev/vx/dsk/home\\([	 ]\\);${homebpath}\\1;" \
	 -e "s;/dev/vx/rdsk/home\\([	 ]\\);${homerpath}\\1;" \
		< $SYSROOT/etc/vfstab > $tmpfile1 || quit 10

	doit mv $tmpfile1 $SYSROOT/etc/vfstab || quit 10
	echo home >> $rmvols
	(grep $homedisk_dm $rmvmdisks 2>&1 ) > /dev/null
	if [ $? -ne 0 ] ; then
		echo $homedisk_dm >> $rmvmdisks
	fi
fi


doit rm -f $rootfile $rootsvfile


doit rm -f /etc/vx/.dumpadm

#
# Change the default boot-device so that the system boots from the pathname
# which is there in /etc/vfstab.
#
good_root=${good_root}s${rootno}
if dogi_name_is_slice $good_root
then
	new_root=$good_root
else
	dogi_device_slice $good_root 2 new_root
fi

case $new_root in
	mc*)
		new_root_wo_ap=`echo $new_root | awk -Fm '{print $2}'`
		tmp_root_path=`ls -l /dev/dsk/$new_root_wo_ap | \
						awk '{print $NF}'`
		;;
	*)
		tmp_root_path=`ls -l /dev/dsk/$new_root | awk '{print $NF}'`
		;;
esac
root_path=`expr $tmp_root_path : '.*devices\(.*\)$'`
vxeeprom boot-device $root_path > /dev/null 2>&1

if [ $? != 0 ]
then
	export root_path; ewritemsg -M vxvmshm:874 \
	"Please set boot-device to $root_path before shutdown.
	 To do this type

	 eeprom set boot-device $root_path"
	echo ""
	quit 10
fi

#
# Remove unwanted comments with pre-encapsulation slice information
#
grep -v "^#NOTE: volume .* \(.*\) encapsulated partition" \
        $SYSROOT/etc/vfstab > $tmpfile1
doit mv $tmpfile1 $SYSROOT/etc/vfstab || quit 10

#
# Notify user
#
ewritemsg -M vxvmshm:78 \
"A shutdown is now required to install the new kernel.
  You can choose to shutdown now, or you can shutdown later, at your
  convenience."

voladm_yorn -M vxvmshm:163 "Do you wish to shutdown now" n
if [ $? -ne 0 ]
then
	ewritemsg -M vxvmshm:258 \
"Please shutdown before you perform any additional volume manager
  or disk reconfiguration.  To shutdown your system cd to / and type

  	shutdown -g0 -y -i6"
	echo ""
	quit 10
fi

# The following is not within scope for i18n so -M is unused
ewritemsg "shutdown -g0 -y -i6"
cd /
doit shutdown -g0 -y -i6 && { sleep 1200; quit 0; }

egettxt "The shutdown failed.  Consult your system administration guide." vxvmshm:390
quit 10
