#!/sbin/sh -
# $Id: vxdarestore.sh,v 1.1.45.5 2001/10/10 02:24:58 aniljt Exp $
#ident  "$Source: /project/unixvm-cvs/src/sol/cmd/vxvm/support/vxdarestore.sh,v $"

# Copyright (c) 2001 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

add_volume_to_list()
{
	found=0
	for volume_plex_dg in $volume_dg_list
	do
		volume=`echo $volume_plex_dg | cut -d":" -f1`
		plex=`echo $volume_plex_dg | cut -d":" -f2`
		dg=`echo $volume_plex_dg | cut -d":" -f3`

		if [ "$1" = "$volume" ] && [ "$2" = "$plex" ] && \
					[ "$3" = "$dg" ]
		then
			found=1
			break
		fi
	done

	[ $found -eq 0 ] && { volume_dg_list="$volume_dg_list $1:$2:$3"; }

}

get_new_daname()
{
	simple_da=$1
	newda_ret=

	# get the slice number of the failed record
	dogi_slice_num $simple_da simple_slice

	if [ "$simple_slice" = "s2" ]
	then
		simple_slice=""
	fi

	# get the new name using vxdmpadm
	newda=`vxdmpadm getdmpnode nodename=$simple_da 2>/dev/null | tail +3 | \
						awk '{ print $1; }'`

	# if newda has a slice part, get rid of it
	dogi_name_is_device $newda
	if [ $? -eq 0 ]
	then
		dogi_name_is_slice $newda
		if [ $? -eq 0 ]
		then
			dogi_slice_to_device $newda newda_ret
			newda_ret="$newda_ret$simple_slice"
		fi
	fi

	eval "${2}=$newda_ret"
}

define_simple_disk()
{
	simple_disk_da=$1
	newda=$2

	offset=`vxdisk list $simple_disk_da 2>/dev/null | grep "^info:" | \
			awk '{ print $2; }' | cut -d"=" -f2`

	[ "X" = "X$offset" ] && { offset=1; }

	comment=`vxdisk list $simple_disk_da 2>/dev/null | grep "^comment:" | \
			cut -d":" -f2`
			
	state=
	vxdisk list $simple_disk_da 2>/dev/null | grep "^flags:" | \
				grep "offline" > /dev/null
	[ $? -eq 0 ] && { state=offline; }

	export newda; ewritemsg -M vxvmshm:1386 "defining simple disk da $newda . . ."
	dogi_slice_num $simple_disk_da _simple_slice_num 
	if [ "$_simple_slice_num" = "s2" ]
	then
		vxdisk rm $newda 2>/dev/null 1>&2
	fi

	vxdisk -f define $newda type=simple privoffset=$offset comment="$comment" $state
}

define_nopriv_disk()
{
	nopriv_da=$1
	newda=$2

	args=`vxdisk list $nopriv_da 2>/dev/null | grep "^info:" | \
			awk '{ print $2; }' | sed 's/,/ /g'`
	
	state=
	vxdisk list $nopriv_da 2>/dev/null | grep "^flags:" | \
				grep "offline" > /dev/null
	[ $? -eq 0 ] && { state=offline; }

	export newda; ewritemsg -M vxvmshm:1385 "defining nopriv disk da $newda . . ."
	dogi_slice_num $nopriv_da _nopriv_slice_num 
	if [ "$_nopriv_slice_num" = "s2" ]
	then
		vxdisk rm $newda 2>/dev/null 1>&2
	fi

	vxdisk -f define $newda type=nopriv $args $state
}

# if vxconfigd is not in "ENABLED" mode, return with error
vold_mode=`/usr/sbin/vxdctl mode`
if [ "$vold_mode" != "mode: enabled" ]
then
	ewritemsg -M vxvmshm:1404 "\
The Volume Manager Configuration Daemon (vxconfigd) has to be running in
  enabled mode for this operation to proceed. Currently, vxconfigd is not
  in enabled mode."
	exit 2
fi

# if the current naming scheme is not enclosure based, do not proceed
if [ ! -f /etc/vx/.newnames ]
then
	ewritemsg -M vxvmshm:1371 "\
This script can be used only if VxVM is using enclosure based naming scheme.
  Currently, VxVM is using c#t#d#s# naming scheme. Use the vxdiskadm
  utility to change the naming scheme and rerun this script."
	exit 1
fi

# Get the list of simple disk records which are not in online state
da_list=`vxdisk list | \
	awk '($2 == "simple" || $2 == "nopriv") { print $1":"$2; }'`

failed_da_dg_list=

for da_datype in `echo $da_list`
do
	simple_da=`echo $da_datype | cut -d":" -f1`
	type=`echo $da_datype | cut -d":" -f2`

	get_new_daname $simple_da newdaname

	[ "X" = "X$newdaname" ] && continue
	[ "X$simple_da" = "X$newdaname" ] && continue

	if [ "$type" = "simple" ]
	then
		define_simple_disk $simple_da $newdaname
	else
		define_nopriv_disk $simple_da $newdaname
	fi

	# if there is an entry in the volboot file for this device,
	# change that as well

	vxdctl list 2>/dev/null | awk '$1 == "disk" { print $2; }' | \
					grep $simple_da >/dev/null
	if [ $? -eq 0 ]
	then
		export simple_da newdaname; ewritemsg -M vxvmshm:1388 "\
replacing $simple_da with $newdaname in the volboot file . . ."
		vxdctl add disk $newdaname
		vxdctl rm disk $simple_da
	fi

	# for simple disks, running vxreattach is enough

	if [ "$type" = "simple" ]
	then
		/etc/vx/bin/vxreattach -r $newdaname
		vxdisk rm $simple_da
		continue
	fi

	# This section is for handling "nopriv" disks (because vxreattach
	# doesn't handle "nopriv" disks).
	# devices in imported dgs will be shown as "failed was" in the
	# output of "vxdisk list". For these, we do a "vxdg adddisk"
	# operation

	vxdisk list | grep "failed was" | grep $simple_da >/dev/null
	if [ $? -eq 0 ]
	then
		dg=`vxdisk list | grep "failed was" | grep $simple_da | \
							awk '{ print $4; }'`
		dm=`vxdisk list | grep "failed was" | grep $simple_da | \
							awk '{ print $3; }'`
		vxdg -g $dg -k adddisk $dm=$newdaname 2>/dev/null
		plex=`vxprint -g $dg -st 2>/dev/null | grep "^sd" | \
				grep $newdaname | awk '{ print $3; }'`
		if [ "X" != "X$plex" ]
		then
			volume=`vxprint -g $dg -pt 2>/dev/null | grep "^pl" | \
					grep $plex | awk '{ print $3; }'`
			add_volume_to_list $volume $plex $dg
		fi
	fi

	vxdisk rm $simple_da
done

# get the affected plexes back to ENABLED state

[ -z "$volume_dg_list" ] && exit 0

for vol_dg in `echo $volume_dg_list`
do
	volume=`echo $vol_dg | cut -d":" -f1`
	plex=`echo $vol_dg | cut -d":" -f2`
	dg=`echo $vol_dg | cut -d":" -f3`

	volume_state=`vxprint -g $dg -v $volume | grep "^v" | awk '{print $4;}'`
	if [ "X$volume_state" = "XENABLED" ]
	then
		export plex volume dg; ewritemsg -M vxvmshm:1387 "\
getting plex $plex (volume $volume, dg $dg) back to ENABLED state . . ."
		vxplex -g $dg det $plex 2>/dev/null
		vxplex -g $dg att $volume $plex 2>/dev/null
	fi
done

exit 0
