#!/sbin/sh -

# @(#)src/scripts/vxvm/solaris/upgrade/upgrade_finish.sh	1.1.2.1 07/22/97 11:30:27 -  
#ident	"@(#)cvm:src/scripts/vxvm/solaris/upgrade/upgrade_finish.sh	1.1.2.1"

# Copyright(C)1996 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

# Script to complete an upgrade from VxVM 1.3/2.0 to VxVM 2.2.

PATH=${VXVM_UPGRADE_PATH:-/usr/sbin:/sbin:/usr/bin:/etc/vx/bin}:$PATH
SAVEDIR=/VXVM2.2-UPGRADE
undo_file=$SAVEDIR/restore.sh
converted=$SAVEDIR/converted.vols
upgrade_reconf=$SAVEDIR/online.plexes
reconfigfile=/etc/vx/reconfig.d/state.d/reconfig

VOL_GHOST_LEN=1
VOL_FULL_SLICE=s2
tab='	'
NOTHING_UPGRADED=no

tempf1=/tmp/vxupgrade_finish.$$a
tempf2=/tmp/vxupgrade_finish.$$b
tempf3=/tmp/vxupgrade_finish.$$c
tempf4=/tmp/vxupgrade_finish.$$d

# save /etc/vfstab
cp /etc/vfstab $tempf3 || exit 10

# save /etc/system
cp /etc/system $tempf4 || exit 10

# undo - undo what have been done before abnormal termination
undo()
{
	echo "
Restoring /etc/vfstab..."
	cp $tempf3 /etc/vfstab

	echo "
Restoring /etc/system..."
	cp $tempf4 /etc/system

	echo "
Upgrade operation is incomplete.  Consult the Volume Manager documentation 
if you need further help."
	sync
}

quit()
{
	[ $1 -eq 0 ] || undo
	rm -f $tempf1 $tempf2 $tempf3 $tempf4
	exit $1
}

trap 'quit 2' INT HUP QUIT TERM 

wait_continue()
{
	echo "\nHit RETURN to continue.\c"
	read ANS
}

yes_or_no()
{
	while :
	do
		echo "\n$1? [$2] \c"
		ANS=
		read ANS
		case $ANS in
		"")	ANS=$2; break;;
		n|no)	ANS=no; break;;
		y|yes)	ANS=yes; break;;
		*)	echo "\nPlease answer \"yes\" or \"no\"."
			continue;;
		esac
	done
	[ $ANS = yes ]
}

forceload_drv()
{
	file=/etc/system
	cp $file $file.sav
	drv=$1
	grep "^forceload: drv/$drv" $file 2>&1 > /dev/null
	if [ $? -eq 0 ] ; then
		return 0
	fi

	end_line=`grep -n vxvm_END $file`
	if [ -z "$end_line" ] ; then
	sed \
	-e '$ a\
* vxvm_START\
* vxvm_END' $file > $file.sav 
	cp $file.sav $file
	end_line=`grep -n vxvm_END $file`
	fi

	end_line=`expr "$end_line" : "\(.*\):.*"`
	end_line=`expr $end_line - 1`
	sed \
	-e ''$end_line' a\
forceload: 'drv/$drv'' $file > $file.sav
	cp $file.sav $file
}

get_drv_list() 
{
	dir_entry="`ls -ld \$1`"
	path=`expr "$dir_entry" : ".*\/devices\(.*\)"`
	[ -n "$path" ] || {
		echo "
get_drv_list: Invalid device path: $1"
		return 1
	}

	node=`basename $path`
	path=`dirname $path`

	while [ -n "$path" -a "X$node" != "X/" ]; do
		drv=`expr $node : "\(.*\)@.*"`
		echo $drv
		node=`basename $path`
		path=`dirname $path`
	done
}

echo \
"VERITAS Volume Manager VxVM 2.2 upgrade completion script

This script completes the process of upgrading your system from
previous releases to the VxVM 2.2 release of the VERITAS Volume Manager and
upgrading Solaris to 2.4 or later versions.
If you saved old configuration information using upgrade_start before
you removed the previous Volume Manager package, then you can run this 
script after adding the VxVM 2.2 package to restart the Volume Manager on 
your system  

NOTE: This can also be used to undo whatever upgrade_start script did"

wait_continue

pkginfo -q VRTSvxvm || pkginfo -q SUNWvxvm || {
	echo "
The VERITAS Volume Manager package has not yet been added or readded
to your system.  Please add the VxVM 2.2 package and run this
script again."
	quit 0
}

echo "
Verifying installation of VxVM 2.2 ... "
installed=
[ -d /etc/vx/slib ] || [ -d /etc/vx/bin ] ||{
	echo "  /etc/vx/slib directory not found."
	echo "
The VxVM 2.2 package does not appear to have been fully installed.
Please re-add the package.  If you need further help, please consult
the Volume Manager documentation."
	quit 0
}

if [ ! -f $SAVEDIR/.start_runed ]
then
	[ ! -f /etc/vx/reconfig.d/state.d/install-db ] && {	
		echo "  /etc/vx/reconfig.d/state.d/install-db file not found."
		echo "
The volume manager appears to have been upgraded or completely
reinstalled already.  If this is an error, please consult the Volume Manager 
documentation for further help."
		quit 0
	}
fi

[ -f /etc/vx/volboot ] && {
	echo "  /etc/vx/volboot exists."
	installed=yes
}

[ -d /etc/vx/reconfig.d ] && {
 	echo "  /etc/vx/reconfig.d exists."
}
if [ -n "$installed" ]
then
	echo "
Some files appear to have been restored already.  This may be
because you interrupted an early attempt to restore the configuration
or you used the upgrade_start script to upgrade just Solaris.  If this is not
the case, you should probably not continue."
	yes_or_no "Continue restoring the configuration" no || {
		echo "
Operation of the Volume Manager has not been restored.  Consult the 
Volume Manager documentation if you need further help."
		quit 0
	}
fi

failed_restore()
{
	echo "
Operation of the volume manager could not be restored.  You may just
need to reboot your system and try again.  If you have already
rebooted your system, consult the Volume Manager documentation for
further help."
	quit 1
}

storage_array=
if pkginfo -q SUNWssadv && prtconf | grep 'SUNW,pln, instance ' > /dev/null
then
	storage_array=yes
elif [ -c /dev/ff ]; then
	storage_array=yes
fi

if [ "$storage_array" != "yes" ]
then	
echo "\nLicensing information:"
echo "    System host ID: `/usr/ucb/hostid`"
echo "    Host type: `prtconf | sed -n -e '/^$/{' -e n -e p -e q -e '}'`"
echo ""

vxserial -p
if [ $? -eq 0 ]
then
	echo "
Some VxVM 2.2 licenses are already installed."
	while :
	do
		yes_or_no "Do you wish to enter another license key" \
			no || break;
		vxserial -c || continue
	done	
else
		echo "
There is no valid license key installed on your machine.  You will need to
get or restore a license before the volume manager can be reinitialized.
"
		yes_or_no "Do you wish to enter a license key" yes || quit 0
		while :
		do
			vxserial -c || continue
			yes_or_no "Do you wish to enter another license key" \
				no || break;
		done	
fi	
fi

SAVEDIR=/VXVM2.2-UPGRADE

[ -d $SAVEDIR ] || {

	# hmmm... Can't find any saved state files.

	echo "
No files were saved by the update_start script.  If you forgot to run
upgrade_start before removing the previous Volume Manager package, 
this script can probably still restore operation of VxVM.  Do not 
attempt to restore operation of VxVM if you do not have any disks 
under Volume Manager control."

	yes_or_no "Restore operation of VxVM" no || {
		echo "
Refer to the VxVM Installation Guide for instructions on installing
the Volume Manager."
		quit 0
	}

	echo "
Attempting to reinitialize the Volume Manager ..."

	# starting I/O daemons proves that VxVM 2.2 is at least
	# marginally functional
	echo "  Starting I/O daemons ... \c"
	vxiod set 10 || { echo failed.; failed_restore; }
	echo started.

	# start vxconfigd so that the volboot file can be rebuilt
	# and so on.
	echo "  Starting vxconfigd ... \c"
	vxdctl -k stop 2> /dev/null
	sleep 2
	vxconfigd -m disable 2> /dev/console || {
		echo failed.
		vxdctl -k stop 2> /dev/null
		failed_restore
	}

	# create the volboot file so that the volume manager can
	# come up reasonably.
	echo "  Creating the /etc/vx/volboot file ... \c"
	vxdctl init || {
		echo failed.
		vxdctl -k stop 2> /dev/null
		failed_restore
	}
	echo "created."

	# enable the volume manager to import all disk groups.
	echo "  Enable the volume manager ... \c"
	vxdctl enable || {
		echo failed.
		vxdctl -k stop 2> /dev/null
		failed_restore
	}
	echo enabled.

	# the volume manager is now operational, remove the install-db
	# file and start the volumes
	rm -f /etc/vx/reconfig.d/state.d/install-db
	echo "  Starting volumes ... \c"
	vxrecover -s -b || {
		echo "\
Some volumes could not be started.  The Volume Manager appears to
be installed correctly, so this may not be a problem.  If there is
no apparent reason why some of your volumes should be unstartable,
consult the Volume Manager documentation for further help."
		quit 0
	}
	echo "started."

	echo "
The Volume Manager has been successfully restored."
	quit 0
}

# normal upgrade path

# restore saved files

echo "
The following operation will modify the /etc/system file.  The old /etc/system
file will be saved in $SAVEDIR/upgrade_finish_system.SAV"

wait_continue
cp /etc/system $SAVEDIR/upgrade_finish_system.SAV || quit 100
sync

file=/etc/system
if [ -f $SAVEDIR/root_was_volume ]
then
	grep "vxvm_END" $file 1>&2 > /dev/null
	if [ $? -ne 0 ]
	then
		echo "
The vxvm block in /etc/system is unexpectedly removed or corrupted."
		quit 1
	else
		[ -f $converted ] || {
			echo "
Some of the saved files in the upgrade start script are unexpectedly removed."
			quit 1
		}
		rootdisk=`awk '$1=="rootvol" {print $3}' $converted`
		[ -n $rootdisk ] || {
			echo "
Unexpected inconsistency in $converted"
			quit 1
		}

		# check if the user has upgraded the OS or VxVM
		# if not just write the saved system file back
		# This  will ensure that we do not write anything
		# new to the system file as the system files for VxVM
		# are different for Solaris 2.3 and later versions.
		# The else part of the compound statement creates the
		# appropriate system files if the user actually upgraded
		# VxVM to 2.1 or higher versions
		# This was written particularly if the user runs
		# upgarde_start accidently on a system running VxVM 1.3
		# on solaris 2.3, this code segment will ensure that everything
		# is put back as it was before.

		OS_VERSION_BEFORE_UPGRADE=`awk '{print $3}' $SAVEDIR/OS_info_before_upgrade`
		
		VM_VERSION_BEFORE_UPGRADE=\ `eval grep VERSION $SAVEDIR/VxVM_info_before_upgrade | awk '{print $2}'`

		OS_VERSION_AFTER_UPGRADE=`eval uname -r`

		pkg=`eval pkginfo | grep vxvm | awk '{print $2}'`

		VM_VERSION_AFTER_UPGRADE=`eval pkginfo -l $pkg | \
						grep VERSION | awk '{print $2}'`

		if [ $OS_VERSION_BEFORE_UPGRADE = $OS_VERSION_AFTER_UPGRADE ]&& \
		[ $VM_VERSION_BEFORE_UPGRADE = $VM_VERSION_AFTER_UPGRADE ] 
		then
			NOTHING_UPGRADED=yes
			cp $SAVEDIR/upgrade_start_system.SAV /etc/system
		else

			#
			# cp the correct driver 
			# 

			OS_VERS=`eval uname -r`
			OS_VEND=`eval uname -s` 

			if [ ! -s /kernel/drv/vxio ]
			then	
				echo "could not find /kernel/drv/vxio"
				quit
			fi 

			get_drv_list /dev/dsk/$rootdisk > $tempf1 
			if [ -s $tempf1 ]
			then
				forceload_drv vxio
				forceload_drv vxspec
				cat $tempf1 | while read drv
						do
							forceload_drv $drv
						done
			else
				echo "Cannot determine root disk drivers"
				quit 1
			fi
			sed -e 's;* vxvm_END;rootdev:/pseudo/vxio@0:0\
set vxio:vol_rootdev_is_volume=1\
* vxvm_END;' \
			< $file > $file.SAV || quit 100
			mv $file.SAV $file || quit 100	
		fi
	fi
fi

echo "
The following operation will modify the /etc/vfstab file.  The old /etc/vfstab
file will be saved in $SAVEDIR/upgrade_finish_vfstab.SAV."

wait_continue

cp /etc/vfstab $SAVEDIR/upgrade_finish_vfstab.SAV || quit 100
sync

if [ -f $SAVEDIR/upgrade_start_vfstab.SAV ]
then
	cp $SAVEDIR/upgrade_start_vfstab.SAV /etc/vfstab || quit 100
	sync
fi

echo "
Restoring saved VxVM state files..."

[ -f $SAVEDIR/etc/vx/volboot ] && {
	cp $SAVEDIR/etc/vx/volboot /etc/vx/volboot || failed_restore
	sync
}

[ -d $SAVEDIR/etc/vx/reconfig.d ] && {
	[ -d /etc/vx/reconfig.old ] && rm -rf /etc/vx/reconfig.old
	mv /etc/vx/reconfig.d /etc/vx/reconfig.old
	cp -r $SAVEDIR/etc/vx/reconfig.d /etc/vx/reconfig.d || failed_restore
	sync
}

# create the /etc/vx/reconfig.d/saveconf.d directory so that future vxunroot
# operation will work
mkdir -p /etc/vx/reconfig.d/saveconf.d || quit 100

sync

# restore eeprom boot-device alias
if [ -f $SAVEDIR/boot-device.SAV ]
then
	bootdev=
	cat $SAVEDIR/boot-device.SAV | \
		while read bootdev
		do
			if [ `expr "$bootdev" : ".*\.\.\."` -eq 0 ]
			then
				eeprom "$bootdev" || quit 1
			fi
		done
fi


# libraries libthread.so.1 and libc.so.1 are incompatible for
# Solaris 2.5 and 2.4, if the root is encapsulated and if a user
# does a upgrade of Solaris from 2.4 to 2.5 the libraries existing
# in /etc/vx/slib are not valid anymore. So we should copy 2.5 libraries
# from /usr/lib to /etc/vx/slib after checking if the OS was upgraded
# the above is just an example. The libraries should always be copied
# to ensure that the correct libraries are available at all times.

echo "Copying libthread and libc into /etc/vx/slib" 
cp /usr/lib/libthread.so.1 /etc/vx/slib
cp /usr/lib/libc.so.1 /etc/vx/slib


# restart the volume manager

echo "
Restarting the volume manager ..."

# starting I/O daemons proves that VxVM 2.2 is at least
# marginally functional
echo "  Starting I/O daemons ... \c"
vxiod set 10 || { echo failed.; failed_restore; }
echo started.

# start vxconfigd
echo "  Starting vxconfigd ... \c"
vxdctl -k stop 2> /dev/null
sleep 2
vxconfigd 2> /dev/console || { echo "failed."; failed_restore; }
echo started.

if [ -f $undo_file ]; then
	cp $undo_file $upgrade_reconf 
	# some work needs to be done by vxvm-reconfig so touch the reqd file
	if [ ! -f $reconfigfile ]; then
		touch $reconfigfile
	fi
fi

	 
# the volume manager is now operational
rm -f /etc/vx/reconfig.d/state.d/install-db
rm -f $SAVEDIR/.start_runed

if [ -f $SAVEDIR/root_was_volume ] && [ $NOTHING_UPGRADED = no ]
then
	echo "
VERITAS Volume Manager Release 2.2 has been successfully restarted on
your system.  You must reboot the machine now to restore the volumes in the
root disk."
	wait_continue
fi

echo "Starting volumes ... \c"
vxrecover -s -b || {
	echo "
Some volumes could not be started.  The Volume Manager appears to
be installed correctly, so this may not be a problem.  If there is
no apparent reason why some of your volumes should be unstartable,
consult the Volume Manager documentation for further help."
	quit 0
}
echo "started."

sync
if [ $NOTHING_UPGRADED = yes ]
then
	echo " Volume Manager restored "
else
	echo "
VERITAS Volume Manager Release 2.2 has been successfully restarted
on your system."
fi

quit 0




