#!/bin/sh
# @(#)src/sol/cmd/vxvm/support/vxr5check.sh	1.2.21.4 09/19/00 14:12:45 - Copyright (c) 1999 VERITAS Software Corp.
#ident "@(#)unixvm:src/sol/cmd/vxvm/support/vxr5check.sh 1.2.21.4"

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

usage()
{
	echo "Usage:  vxr5check [-i|v|c] [-g diskgroup] raid5_volume_name \n"
	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

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

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

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

volume_type=`/usr/sbin/vxprint -g "$diskgroup" -F"%use_type" -v $volume_name 2>&1`
if [ "$volume_type" != "raid5" ]
then
	echo "vxr5check: ERROR $volume_name is not a RAID-5 volume"
	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 '{ FS="/"; print $1 }'`

stripe_unit_size=`vxprint -thq -g "$diskgroup" -v "$volume_name" |  grep "^pl" | grep RAID | awk '{ print $8 }' | awk '{ FS="/"; 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
		echo "stripe increment vxr5check $volume_name"
		echo "verification in process..."
		echo "\tstripe passed = \c"
	else
		echo "verification in process... \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 " $stripe \c"
		else
			echo "\nvxr5check: ERROR in the RAID-5 volume $volume_name"
			echo "Verification failed at stripe $stripe"
			echo "Initiate RAID-5 error recovery procedures\n"
			exit 1
		fi
		start_col_offset=`expr $start_col_offset + $stripe_unit_size`
	done
	if [ "$verbose" = "yes" ] 
	then
		echo "\n"
		echo "vxr5check: RAID-5 volume $volume_name - verification passed\n"
	else
		echo "passed"
	fi
else
	[ "$verbose" = "yes" ] && echo "complete volume vxr5check $volume_name"
	echo "verification in process... \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
			echo "\nvxr5check: ERROR in the RAID-5 volume $volume_name"
			echo "Verification failed"
			echo "Initiate RAID-5 error recovery procedures\n"
		else
			echo "failed"
		fi
		exit 1
	else 
		if [ "$verbose" = "yes" ] 
		then	
			echo "\nvxr5check: RAID-5 volume $volume_name - verification passed\n"
		else
			echo "passed"
		fi
	fi
fi

exit 0
