#!/bin/sh
# $Id: vxr5check.sh,v 1.5.45.2 2001/09/13 17:50:48 dstowe Exp $
#ident "$Source: /project/unixvm-cvs/src/common/cmd/vxvm/support/vxr5check.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

#
# vxr5check:
#
# Utility to verify raid-5 volume data and parity.  The vxr5check utility
# will read the raid-5 data for each stripe, generate parity and compare 
# this parity to the existing parity. 
#	
#	usage: vxr5check [-i|v|c] [-g diskgroup] volume_name
#
# 	[ -i ]	Optional argument.  If -i is specified, then vxr5check will 
#		verify the raid-5 volume on a incremental per-stripe basis.  
#		If a parity mismatch is detected vxr5check will stop on that 
#		stripe, and an 	error message will be issued against that 
#		stripe.  Not specifying this option will cause the entire 
#		raid-5 	volume to be verified.  If a parity mismatch is 
#		detected vxr5check will stop, and an error message will be 
#		issued 	against the volume.  Not specifying the -i option 
#		is faster than running vxr5check with the -i option.
#	[ -v ]	Optional arguement. The -v option is only valid with the 
#               -i option (i.e. vxr5check -iv volume_name).  The -v option 
#               is for verbose output on the incremental vxr5check operation.
#	[ -c ]	Optional arguement. The -c flag will pass this flag onto 
#		vxr5vrfy; vxr5vrfy will continue the raid-5 data-parity 
#		check even if data-parity mismatches are encountered.
#	[-g diskgroup]     - specify a diskgroup (default is rootdg)
# 	volume_name        - specify a raid-5 volume 
#
#	
# Stick the vxr5vrfy program in /usr/lib/vxvm/bin
# 

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

usage()
{
	egettxt "Usage:  vxr5check [-i|v|c] [-g diskgroup] raid5_volume_name " vxvmshm:1433
	echo $ECHOOPT ""
	exit 1
}

if [ $# -lt 1 ]
then
        usage
        exit 1
fi

# default diskgroup to rootdg
diskgroup=rootdg
verbose=no
inc=no
cont=no

while getopts :g:civV c
do
        case $c in
        v|V)	verbose=yes 		;;
        i)	inc=yes 		;;
        c)	cont=yes 		;;
        g)	diskgroup=$OPTARG 	;;
        ?)	usage; exit 1		;;
        esac
done

shift `expr $OPTIND - 1`
if [ $# -ne 1 ] 
then
	usage	
	exit 1
fi
volume_name=$1

if [ -z "$volume_name" ]
then
	usage 
	exit 1
fi

vxprint -g "$diskgroup" > /dev/null 2>&1
if [ "$?" -ne 0 ] 
then
	export diskgroup
	egettxt "vxr5check: ERROR $diskgroup is not a disk group name" vxvmshm:1461
	exit 1
fi

vxprint -v "$volume_name" > /dev/null 2>&1
if [ "$?" -ne 0 ] 
then
	export volume_name
	egettxt "vxr5check: ERROR $volume_name is not a volume name" vxvmshm:1463
	exit 1
fi

vxprint -g "$diskgroup" -v "$volume_name" > /dev/null 2>&1
if [ "$?" -ne 0 ] 
then
	export volume_name diskgroup
	egettxt "vxr5check: ERROR $volume_name is not a volume name in disk group $diskgroup" vxvmshm:1464
	exit 1
fi

volume_type=`vxprint -g "$diskgroup" -F"%use_type" -v $volume_name 2>&1`
if [ "$volume_type" != "raid5" ]
then
	export volume_name
	egettxt "vxr5check: ERROR $volume_name is not a RAID-5 volume" vxvmshm:1462
	exit 1
fi

vol_len=`vxprint -g "$diskgroup" -F"%len" -v "$volume_name"`
 
num_r5_cols=`vxprint -thq -g "$diskgroup" -v "$volume_name" |  grep "^pl" | grep RAID | awk '{ print $8 }' | awk -F/ '{print $1}'`

stripe_unit_size=`vxprint -thq -g "$diskgroup" -v "$volume_name" |  grep "^pl" | grep RAID | awk '{ print $8 }' | awk -F/ '{print $2}'`

r5_data_cols=`expr $num_r5_cols - 1`
col_span=`expr $vol_len / $r5_data_cols`

opt_continue=""
if [ "$cont" = "yes" ]
then
	opt_continue="-c"
fi

if [ "$inc" = "yes" ]
then
	start_col_offset=0
	stripe=0
	if [ "$verbose" = "yes" ] 
	then
		export volume_name
		egettxt "stripe increment vxr5check $volume_name" vxvmshm:1449
		egettxt "verification in process..." vxvmshm:1450
		__msg=`egettxt "\tstripe passed = " vxvmshm:1438`
		echo $ECHOOPT "$__msg\c"
	else
		__msg=`egettxt "verification in process... " vxvmshm:1451`
		echo $ECHOOPT "$__msg\c"
	fi
	while [ $start_col_offset -lt $col_span ]	
	do
		stripe=`expr $stripe + 1`
		/usr/lib/vxvm/bin/vxr5vrfy -i $stripe_unit_size $opt_continue $diskgroup/$volume_name $start_col_offset $stripe_unit_size > /dev/null 2>&1
		if [ "$?" -eq 0 ]
		then
			[ "$verbose" = "yes" ] && echo $ECHOOPT " $stripe \c"
		else
			export volume_name
			egettxt "\nvxr5check: ERROR in the RAID-5 volume $volume_name" vxvmshm:1436
			export stripe
			egettxt "Verification failed at stripe $stripe" vxvmshm:1435
			egettxt "Initiate RAID-5 error recovery procedures" vxvmshm:1432
			echo $ECHOOPT ""
			exit 1
		fi
		start_col_offset=`expr $start_col_offset + $stripe_unit_size`
	done
	if [ "$verbose" = "yes" ] 
	then
		echo $ECHOOPT "\n"
		export volume_name
		egettxt "vxr5check: RAID-5 volume $volume_name - verification passed" vxvmshm:1465
		echo $ECHOOPT ""
	else
		egettxt "passed" vxvmshm:1445
	fi
else
	export volume_name
	__msg=`egettxt "complete volume vxr5check $volume_name" vxvmshm:1439`
	[ "$verbose" = "yes" ] && echo $ECHOOPT $__msg
	__msg=`egettxt "verification in process... " vxvmshm:1451`
	echo $ECHOOPT "$__msg\c"

	/usr/lib/vxvm/bin/vxr5vrfy $opt_continue $diskgroup/$volume_name $col_span > /dev/null 2>&1
	if [ "$?" -ne 0 ]
	then
		if [ "$verbose" = "yes" ] 
		then
			export volume_name
			egettxt "\nvxr5check: ERROR in the RAID-5 volume $volume_name" vxvmshm:1436
			egettxt "Verification failed" vxvmshm:1434
			egettxt "Initiate RAID-5 error recovery procedures" vxvmshm:1432
			echo $ECHOOPT ""
		else
			egettxt "failed" vxvmshm:1441
		fi
		exit 1
	else 
		if [ "$verbose" = "yes" ] 
		then	
			export volume_name
			egettxt "\nvxr5check: RAID-5 volume $volume_name - verification passed" vxvmshm:1437
			echo $ECHOOPT ""
		else
			egettxt "passed" vxvmshm:1445
		fi
	fi
fi

exit 0
