#!/bin/ksh
# $Revision: 1.5.2.2 $
# Veritas VOL command for the Sunfiler CLI
PATH=$PATH:/usr/bin:/usr/sbin

loaded=`/usr/bin/pkginfo VRTSvxvm 2>/dev/null`

if [[ -z $loaded ]]; then
	/usr/bin/echo "Veritas Volume Manager is not installed on this system."
	/usr/bin/echo "You must have Veritas to run the vol command."
	exit 1
fi

case $1 in

'create')

	if [[ -z $3 ]]; then
		/usr/bin/echo "Usage: vol $1 <name> <size>"
		exit 1
	fi
	
	# Don't let them create a volume called nfs_dg1 or rootdg

	if [[ $2 = "nfs_dg1" || $2 = "rootdg" ]]; then
		/usr/bin/echo "Please use a different volume name"
		exit 1
	fi

	# Do not allow them to start the name with a /

	if [[ $2 = +(/)* ]]; then
		/usr/bin/echo "Please do not start the name with /"
		exit 1
	fi

	# Check to see if the volume already exists

	/usr/sbin/vxprint $2 > /tmp/mess 2>&1

	err_mess=`/usr/bin/cat /tmp/mess | /usr/bin/grep ERROR`

	if [[ -z $err_mess ]]; then
		/usr/bin/echo "Volume already exists"
		exit 1
	fi

	/usr/bin/rm /tmp/mess

	# Check to make sure the directory name doesn't already exist

	if [[ -a /$2 ]]; then
		/usr/bin/echo "The directory /$2 already exists."
		exit 1
	fi

	# Check that the value entered for size is a number

	if [[ $3 != +([0-9]) ]]; then
		/usr/bin/echo "Please enter a number for the size"
		exit 1
	fi

	# Finding out which disks correspond to the purples

	disks=`/usr/sbin/luxadm probe | /usr/bin/grep /dev/rdsk | /usr/bin/awk -F: '{print$2}' | /usr/bin/sed -e 's/^\/dev\/rdsk\///' | /usr/bin/sed -e 's/s[0-7]//' `

	if [[ -z $disks ]]; then
		/usr/bin/echo "You do not have any disks that correspond to the purples."
		/usr/bin/echo "Please check all your connections and then rerun the vol command."
		exit 1
	fi

	/usr/bin/printf "Checking for and initializing new storage\n"
	/usr/bin/printf "Please wait ..."
	for x in $disks
	do
		# Checking to see if disk is already in veritas control, and if not, initializing it.

		vol_disk=`/usr/sbin/vxdisk list | /usr/bin/grep $x | /usr/bin/grep online |grep -v invalid`

		if [[ -z $vol_disk ]]; then
			/usr/bin/printf "."
			/etc/vx/bin/vxdisksetup -i $x > /dev/null 2>&1
		fi

		new_vol_disk=`/usr/sbin/vxdisk list | /usr/bin/grep $x | /usr/bin/grep online | grep -v invalid`

		# Checking to see if the disk group nfs_dg1 exists.  If not, creating it.
		# Also, checking to see if the disk is in nfs_dg1 and if not, adding it to the disk group.

		dg_exists=`/usr/sbin/vxdg list | /usr/bin/grep nfs_dg1`

		in_dg=`/usr/sbin/vxdisk list | /usr/bin/grep $x | /usr/bin/grep nfs_dg1`

		if [[ -n $new_vol_disk && -z $in_dg ]]; then
			if [[ -z $dg_exists ]]; then
				/usr/sbin/vxdg init nfs_dg1 $x
			else
				/usr/sbin/vxdg -g nfs_dg1 adddisk $x
			fi
		fi
	done
	/usr/bin/printf "\n"

        # Size Checks

	if (( $3<1 )); then
		/usr/bin/echo "Please give a size of at least 1"
		exit 1
	fi

        maxsize=`/usr/sbin/vxassist -g nfs_dg1 maxsize | /usr/bin/grep "Mb)" | /usr/bin/sed -e 's/.*(\([0-9]*\)Mb)/\1/' `

	/usr/sbin/vxassist -g nfs_dg1 maxsize layout=mirror > /tmp/mess 2>&1
	err_mess=`/usr/bin/cat /tmp/mess | /usr/bin/grep ERROR`
	/usr/bin/rm /tmp/mess

	if [[ -z $err_mess ]]; then
	        formirror=`/usr/sbin/vxassist -g nfs_dg1 maxsize layout=mirror  | /usr/bin/grep "Mb)" | /usr/bin/sed -e 's/.*(\([0-9]*\)Mb)/\1/' `
	else
		let formirror=0
	fi

        if (( $3>$maxsize )); then
                /usr/bin/echo "The maximum size volume you may create is $maxsize."
		if [[ -z $err_mess ]]; then
                	/usr/bin/echo "If you would like to create a mirrored volume,"
                	/usr/bin/echo "the size must be less than or equal to $formirror."
		fi
		exit 1
        fi

	if [[ -z $err_mess ]]; then
	        if (( $3>$formirror )); then
       	         /usr/bin/echo "Warning: The volume will not be able to be mirrored."
       	         /usr/bin/echo "The size must be less than or equal to $formirror to mirror the volume."
       	         /usr/bin/echo "Would you still like to create the volume with a size of $3 (Y or N)? \c"

       	         done=false
       	         while [[ $done = false ]]; do
       	                 done=true
       	                 read answer
       	                 case $answer in
       	                 y | Y)
       	                         ;;
       	                 n | N)
       	                         /usr/bin/echo "Volume will not be created."
       	                         exit 1
       	                         ;;
       	                 *)
       	                         /usr/bin/echo "Please answer Y or N: \c"
       	                         done=false
       	                         ;;
       	                 esac
       	         done
		fi
       	 fi
	
	/usr/bin/echo "Creating volume, please stand by...\c"

	# Creating the veritas volume

	/usr/sbin/vxassist -g nfs_dg1 make $2 $3m layout=nostripe

	sectors=`/usr/sbin/vxprint -l $2 | /usr/bin/grep "^info" | /usr/bin/awk -F= '{print $2}' `

	# Creating the filesystem, directory, and mounting it

	/usr/sbin/mkfs -F ufs /dev/vx/rdsk/nfs_dg1/$2 $sectors > /tmp/mess 2>&1
	/usr/bin/rm /tmp/mess

	/usr/bin/mkdir /$2

	/sbin/mount -F ufs -o logging /dev/vx/dsk/nfs_dg1/$2 /$2

	# Adding the newly mounted directory to the /etc/vfstab file 

	/usr/bin/sed '$a\
/dev/vx/dsk/nfs_dg1/'$2'	/dev/vx/rdsk/nfs_dg1/'$2'	/'$2'	ufs	2	yes	logging' /etc/vfstab > /tmp/newfstab
	/usr/bin/mv /tmp/newfstab /etc/vfstab

	# Finding out which Array # to add to the filer configuration file

	#x=1
	#arrayexists=`/usr/bin/cat /opt/SUNWsfadm/cfg/filer | /usr/bin/grep Array | /usr/bin/awk '{print$2}' | /usr/bin/grep $x`

	#while [[ -n $arrayexists ]]; do
        #	let x=$x+1
        #	arrayexists=`/usr/bin/cat /opt/SUNWsfadm/cfg/filer | /usr/bin/grep Array | /usr/bin/awk '{print$2}' | /usr/bin/grep $x`
	#done

	# Editing the filer configuration file

	/usr/bin/sed '$a\
ARRAY='$2':/'$2'' /opt/SUNWsfadm/cfg/filer > /tmp/newfiler
	/usr/bin/mv /tmp/newfiler /opt/SUNWsfadm/cfg/filer

	/usr/bin/echo "DONE"

        ;;


'display')

	# Default is ALL volumes

	if [[ -z $2 ]]; then
		/usr/sbin/vxprint -pt
	else
		if [[ $2 = "nfs_dg1" || $2 = "rootdg" ]]; then
			/usr/bin/echo "Not a valid volume name"
			exit 1
		fi

		# Do not allow them to start the name with a /

		if [[ $2 = +(/)* ]]; then
			/usr/bin/echo "Please do not start the name with /"
			exit 1
		fi

		# Check to see if the volume exists

		/usr/sbin/vxprint $2 > /tmp/mess 2>&1
		err_mess=`/usr/bin/cat /tmp/mess | /usr/bin/grep ERROR`

		if [[ -n $err_mess ]]; then
			/usr/bin/echo "Volume does not exist"
			exit 1
		fi
		/usr/bin/rm /tmp/mess

		/usr/sbin/vxprint -vt $2
	fi
	;;


'expand')

	if [[ -z $3 ]]; then
		/usr/bin/echo "Usage: vol $1 <name> <size>"
		exit 1
	fi

        # Do not allow them to start the name with a /

        if [[ $2 = +(/)* ]]; then
                /usr/bin/echo "Please do not start the name with /"
                exit 1
        fi

	# Check to make sure the volume exists

	/usr/sbin/vxprint $2 > /tmp/mess 2>&1
	err_mess=`/usr/bin/cat /tmp/mess | /usr/bin/grep ERROR`

	if [[ -n $err_mess ]]; then
		/usr/bin/echo "Volume does not exist"
		exit 1
	fi
	/usr/bin/rm /tmp/mess

	if [[ $2 = "nfs_dg1" || $2 = "rootdg" ]]; then
		/usr/bin/echo "Not a valid volume name"
		exit 1
	fi

	# Do not allow them to expand rootvol or swapvol

	if [[ $2 = "rootvol" || $2 = "swapvol" ]]; then
		/usr/bin/echo "You are not allowed to expand rootvol or swapvol."
		exit 1
	fi

	# Check that the value entered for size is a number or +number

	if [[ $3 != +([0-9]) && $3 != "+"+([0-9]) ]]; then
		/usr/bin/echo "Please enter a number for the size"
		exit 1
	fi

	if (( $3 == 0 )); then
		/usr/bin/echo "Invalid size"
		exit 1
	fi

	# Check if it's a grow to or grow by size value

	grow=`/usr/bin/echo $3 | /usr/bin/grep +`

	if [[ -z $grow ]]; then

		# Grow the volume TO this size

		# Find the max size that the volume can grow to

		maxsize=`/usr/sbin/vxassist -g nfs_dg1 maxgrow $2 | /usr/bin/grep "Mb)" | /usr/bin/sed -e 's/.*(\([0-9]*\)Mb)/\1/' `

		currentsectors=`/usr/sbin/vxprint -l $2 | /usr/bin/grep "^info" | /usr/bin/awk -F= '{print $2}' `

		let currentMb='currentsectors/2048'

		if (( $3<=$maxsize && $3>currentMb )); then
			/etc/vx/bin/vxresize -g nfs_dg1 $2 $3m
		else
			/usr/bin/echo "Please enter a size greater than the current size of $currentMb and less than or equal to $maxsize"
			exit 1
		fi
	else
		# Grow the volume BY this size

		maxgrow=`/usr/sbin/vxassist -g nfs_dg1 maxgrow $2 | /usr/bin/awk '{print$7}'`

		let maxgrow='maxgrow/2048'

		if (( $3<=$maxgrow && $3>0 )); then
			/etc/vx/bin/vxresize -g nfs_dg1 $2 $3m
		else
			/usr/bin/echo "You can only grow the volume by $maxgrow"
			exit 1
		fi
	fi
        ;;


'mirror')

	if [[ -z $2 ]]; then
		/usr/bin/echo "Usage: vol $1 <name>"
		exit 1
	fi

	if [[ $2 = +(/)* ]]; then
		/usr/bin/echo "Please do not start the name with /"
		exit 1
	fi

	# Check to make sure the volume exists

	/usr/sbin/vxprint $2 > /tmp/mess 2>&1
	err_mess=`/usr/bin/cat /tmp/mess | /usr/bin/grep ERROR`

	if [[ -n $err_mess ]]; then
		/usr/bin/echo "Volume does not exist"
		exit 1
	fi
	/usr/bin/rm /tmp/mess

	if [[ $2 = "nfs_dg1" || $2 = "rootdg" ]]; then
		/usr/bin/echo "Not a valid volume name"
		exit 1
	fi

	# Check to make sure that the volume isn't already mirrored

	mirror=`/usr/sbin/vxprint | /usr/bin/grep ''$2''-02`
	if [[ -n $mirror ]]; then
		/usr/bin/echo "Volume is already mirrored"
		exit 1
	fi

	# Check to make sure that I have enough space to mirror the volume

	currentsize=`/usr/sbin/vxprint -l $2 | /usr/bin/grep "^info" | /usr/bin/awk -F= '{print $2}' `

	freespace=`/usr/sbin/vxassist -g nfs_dg1 -p maxsize`

	if (( $currentsize>$freespace )); then
		/usr/bin/echo "You do not have enough space to mirror the volume $2"
		exit 1		
	else
		# Mirroring the volume
		/usr/sbin/vxassist -g nfs_dg1 mirror $2
	fi
        ;;


'remove')

	if [[ -z $2 ]]; then
		/usr/bin/echo "Usage: vol $1 <name>"
		exit 1
	fi

	if [[ $2 = +(/)* ]]; then
		/usr/bin/echo "Please do not start the name with /"
		exit 1
	fi

	# Check to make sure the volume exists

	/usr/sbin/vxprint $2 > /tmp/mess 2>&1
	err_mess=`/usr/bin/cat /tmp/mess | /usr/bin/grep ERROR`

	if [[ -n $err_mess ]]; then
		/usr/bin/echo "Volume does not exist"
		exit 1
	fi
	/usr/bin/rm /tmp/mess

	if [[ $2 = "nfs_dg1" || $2 = "rootdg" ]]; then
		/usr/bin/echo "Not a valid volume name"
		exit 1
	fi

	# Do not allow them to delete rootvol or swapvol

	if [[ $2 = "rootvol" || $2 = "swapvol" ]]; then
		/usr/bin/echo "You are not allowed to delete rootvol or swapvol"
		exit 1
	fi

	# Unmount the volume

	mounted=`/usr/sbin/df | /usr/bin/grep $2`
	if [[ -n $mounted ]]; then
		/usr/sbin/umount /$2 > /tmp/mess 2>&1
		busy=`/usr/bin/cat /tmp/mess | /usr/bin/grep busy`
		if [[ -n $busy ]]; then
			/usr/bin/echo "The volume appears to still be in use."
			/usr/bin/echo "Are you sure that you want to remove the volume (Y or N)? \c"
			done=false
			while [[ $done = false ]]; do
				done=true
				read answer
				case $answer in
				y | Y)
					/usr/sbin/umount -f /$2
					;;
				n | N)
					/usr/bin/echo "The volume will not be removed."
					exit 1
					;;
				*)
					/usr/bin/echo "Please answer Y or N: \c"
					done=false
					;;
				esac
			done
		fi
	fi

	/usr/bin/rm /tmp/mess

	# Remove the volume from /opt/SUNWsfadm/cfg/filer 

	/usr/bin/sed '/'$2'/d' /opt/SUNWsfadm/cfg/filer > /tmp/filer
	/usr/bin/mv /tmp/filer /opt/SUNWsfadm/cfg/filer

	# Remove the volume from /etc/vfstab

	/usr/bin/sed '/'$2'/d' /etc/vfstab > /tmp/vfstab
	/usr/bin/mv /tmp/vfstab /etc/vfstab

	# Remove the veritas volume

	/usr/sbin/vxedit -g nfs_dg1 -rf rm $2

	/usr/bin/rm -r /$2

	/usr/sbin/vxprint $2 > /tmp/mess 2>&1
	err_mess=`/usr/bin/cat /tmp/mess | /usr/bin/grep ERROR`

	if [[ -n $err_mess ]]; then
		/usr/bin/echo "The volume was successfully removed."
	fi

	/usr/bin/rm /tmp/mess

	;;


*)
        /usr/bin/echo "Usage: vol { create | display | expand | mirror | remove }"
        exit 1
        ;;
esac
