#!/bin/ksh
#
#pragma ident   "@(#)lib.sh 1.7     05/06/15 SMI"
#
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# check whether the slices are ready for test

err_arg=1
err_lack=2
err_unknown=3
err_cannotumount=4
err_reset=5

hostname=`hostname`

set -A filters
filters[0]="reserved_filter"
filters[1]="size_filter"
reserved=""
disk_required=1
disk_count=0
reserved_disks='/$| /usr$| /opt$| /var$| /export/home*'
tech=""
remove=0

get_mp() {

	typeset devicename
	typeset slices

	if [ $# -ne 1 ]; then
	        echo "Usage: $0 device_name"
	        exit 1
	fi
	devicename=$1
	# remove directory name
	devicename=`echo $devicename | sed -e "s/\/dev\/.*dsk\///" \
	        | sed -e "s/[^0-9][0-9][0-9]*$//"`
	
	slices=`mount | grep /dev/dsk/$devicename | awk '{print $1}'`
	
	if [ -z $slices ]
	then
	        exit 1
	else
		for i in $slices
		do
			echo "$i<br>"
		done
	fi
}

get_reserved() {
	df -k | egrep "${reserved_disks}" |\
 		cut "-d " -f 1 |\
		sed -e 's;\/dev\/dsk\/;;'\
    		-e 's;s.$;;' |\
		sort | uniq
}

reserved_filter() {
	typeset disks
	typeset disk
	disk=$1
	disks=`get_reserved`
        disk=`echo $disk | sed -e 's;\/dev\/.*dsk\/;;'\
                 -e 's;s[0-9][0-9]*;;'`
	echo ${disks} | grep ${disk} > /dev/null
	return $?
}

usbdisk_filter() {
	typeset disk
	disk=$1
	/opt/SUNWstaf/prgs/usb_check.sh $disk $hostname
	ret=$?
	if [ $ret -eq 0 ]
	then
		return 1   # It is under USB controller
	else
		return 0
	fi
}

size_filter() {
	typeset disk
        disk=`echo $1 | sed -e 's;\/dev\/[r]*dsk\/;;' -e 's;s.$;p0;'`
	disk="/dev/rdsk/$disk"
	machinesize=`rsh -l root $hostname fdisk -g $disk | sed -n "3p" | awk '
	BEGIN {
	}   
	NR == 1 { ncyl = $2
          nhead = $5
          nsect = $6
          secsiz = $7
          next
       		  }
	END {
	printf "%s", ncyl*nhead*nsect*secsiz/(1024*1024*1024)
	}'` 
	echo "${machinesize}GB"
	if [ $machinesize -lt 20 ]
	then
		return 0
	else
		return 1
	fi
}

get_all() {

	hds=`rsh -l root $hostname prtvtoc /dev/rdsk/c*d*s2 2> /dev/null | grep "partition map" \
     		| awk '{print $2}'`
	for hd in $hds
	do
		echo $hd
	done
}

get_avail() {
	typeset i
	typeset j
	typeset disks
	typeset disk_count

	disk_count=0

		disks=`get_all`
		for i in $disks
		do
			j=0; remove=0
			while [ $j -lt ${#filters[*]} ]
			do
				ret=`${filters[$j]} $i`
				if [ $? -eq 0 ]
				then
					remove=1
					break
				fi
				let "j = $j + 1"
			done
			if [ $remove -eq 0 ]
			then
				let "disk_count = $disk_count + 1"
				echo $i
			fi
		done
	
	if [ $disk_count -lt $disk_required ]
	then
		return $err_lack
	fi
}


# detect whether the device has proper slices
# slice 0,1,2,3 should be there, the sizes of them should be equal.
check_slices() {

	if [ $# -ne 1 ]; then
		echo "Usage: $0 device_name"
		return 1
	fi
	
	devicename=$1
	devicename=`echo $devicename | sed -e "s/\/dev\/.*dsk\///" \
	        | sed -e "s/[^0-9][0-9][0-9]*$/s2/"`
	
	ret=`rsh -l root $hostname prtvtoc /dev/dsk/$devicename | grep -v '^*' | awk '
		BEGIN { 
			sector_count = 0
			tag = 0
			first_sector_count = 0
		}
		{
			slice_number = $1
			tag = $2
			sector_count = $5
		}
	        NR == 1 {
			first_sector_count = $5
			if (slice_number != 0) {
				print "!Error"
				exit 1
			} else if (tag != 0) {
				print "!Error"
				exit 3
			}
			next
		}
	        NR == 2 {
			if (slice_number != 1) {
				print "!Error"
				exit 1
			} else if (sector_count != first_sector_count) {
				print "!Error"
				exit 2
			} else if (tag != 0) {
				print "!Error"
				exit 3
			}
			next
		}
	        NR == 3 {
			if (slice_number != 2) {
				print "!Error"
				exit 1
			} else if (sector_count != first_sector_count) {
				print "!Error"
				exit 2
			} else if (tag != 0) {
				print "!Error"
				exit 3
			}
			next
		}
	        NR == 4 {
			if (slice_number != 3) {
				print "!Error"
				exit 1
			} else if (sector_count < first_sector_count) {
				print "!Error"
				exit 2
			} else if (tag != 0) {
				print "!Error"
				exit 3
			}
			next
		}
		END {
		}'`
	
	echo $ret | grep "Error" >/dev/null
	return ! $?
}

# detect whether the specified slice is mounted.
check_mount() {

	if [ $# -ne 1 ]; then
		echo "Usage: $0 slice_name"
		return 1
	fi
	
	device=`echo $1 | sed -e "s/\/dev\/.*dsk\///"`
	
	echo $device | grep "s[0-9][0-9]*$" > /dev/null
	if [ $? -ne 0 ] # not a slice
	then
		echo "!Error: Enter a slice name."
		return 1
	fi
	
	mount | grep "/dev/dsk/$device" > /dev/null
	if [ $? -eq 0 ]
	then
		mount | grep "/dev/dsk/$device" | awk '{print $1}'
	fi
}

# Mount slice 0, 2, unmount slice 1,3
# If slice 0/2 is already mounted on the directory other than /test.cXtXdXsX,
# exit error
# If slice 1/3 is already mounted, exit error

mount_them() {

	set -A mount_device
	set -A umount_device
	mount_device[0]="s0"
	mount_device[1]="s2"
	umount_device[0]="s1"
	umount_device[1]="s3"
	
	if [ $# -ne 1 ]
	then
	        echo "Usage: $0 device_name"
		return 1
	fi
	
	device=`echo $1 | sed -e "s/\/dev\/.*dsk\///" \
		| sed -e "s/[^0-9][0-9][0-9]*$//"`
	
	# mount slices cXtXdXs0 and cXtXdXs2
	i=0
	while [ $i -lt ${#mount_device[*]} ]
	do
		mount_point="/test.${device}${mount_device[$i]}"
		mount_device[$i]="/dev/dsk/${device}${mount_device[$i]}"
		output=`check_mount ${mount_device[$i]}`
		if [ $? -ne 0 ]
		then
			return 1
		elif [ -z "$output" ] # is not mounted
		then
			ret=`rsh -l root $hostname mount ${mount_device[$i]} ${mount_point} 2>&1`
			if [ ! -z "$ret" ]
			then
				echo "<p><b>!Error: Cannot mount ${mount_device[$i]}.</b>"
				echo "$ret"
				return 1
			fi
		else # the slice is mounted on the directory other than /test.cXtXXX
			echo $output | grep ${mount_point} > /dev/null
			if [ $? -ne 0 ]
			then
				echo "<p><b>!Error: ${mount_device[$i]} is busy.</b>"
				return 1
			fi
		fi
		let "i = $i + 1"
	done
	
	i=0
	while [ $i -lt ${#umount_device[*]} ]
	do
		umount_device[$i]="${device}${umount_device[$i]}"
		output=`check_mount ${umount_device[$i]}`
		if [ $? -ne 0 ]
		then
			return 1
		elif [ -z $output ] # is mounted
		then
			let "i = $i + 1"
		else
			echo "<p><b>!Error: ${umount_device[$i]} is busy.</b>"
			return 1
		fi
	done
}

umount_them() {


	if [ $# -ne 1 ]
	then
	        echo "Usage: $0 device_name"
		return $err_arg
	fi
	
	device=`echo $1 | sed -e "s/\/dev\/.*dsk\///" \
		| sed -e "s/[^0-9][0-9][0-9]*$//"`

	# mount slices cXtXdXs0 and cXtXdXs2
	j=0
	while [ $j -lt 4 ]
	do
		mp=`check_mount ${device}s${j}`
		echo "$mp" | grep "\/" > /dev/null
		if [ $? -eq 0 ]
		then
			ret=`(rsh -l root $hostname "umount $mp") 2>&1`
			if [ ! -z "$ret" ]
			then
				echo "<p><b>!Error: Cannot umount $mp.</b>"
				echo "<br>$ret"
				return ${err_cannotumount}
			fi
		fi
		let "j = $j + 1"
	done
}

