#!/sbin/sh -
# @(#)src/sol/cmd/vxvm/voladm/vxcvminstall.sh	1.2.21.9 09/29/00 09:45:45 - 
#ident "@(#)unixvm:src/sol/cmd/vxvm/voladm/vxcvminstall.sh 1.2.21.9"

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

# cvminstall.sh - This script will be used on systems running VCS.
#		  It  reads the existing VCS configuration file
#		  /etc/VRTSvcs/conf/config/main.cf to get the cluster
#		  configuration information, i.e. the names of the nodes
#		  in the cluster. 
#		  It prompts the user for -
#			- the vxconfigd and vxkmsgd port numbers.
#			- the network address space of his/her choice.	
#			- the number of the private networks and the 
#			  names of the interfaces on each of the nodes.

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

ewritemsg  -M vxvmshm:0 "
To configure cluster agents for Volume Manager in VCS environment
please use /usr/lib/vxvm/bin/vxcvmconfig script instead of the 
vxcvminstall script."
exit 0 

VXDCTL=/usr/sbin/vxdctl
# Path of the VCS configuration file.
VCS_CONF_DIR=/etc/VRTSvcs/conf/config
VCS_CONF_FILE=$VCS_CONF_DIR/main.cf
VCS_SAMPLE_DIR=/etc/VRTSvcs/conf/sample_cvm/
VCS_CVMTYPES_FILE=$VCS_CONF_DIR/CVMTypes.cf
VCS_CVMTYPES_SAV=$VCS_CONF_DIR/CVMTypes.cf.save
VCS_CONF_SAV=$VCS_CONF_DIR/main.cf.save

PATH=$PATH:/opt/VRTSvcs/bin:/usr/lib/vxvm/bin


LLTSTAT=/sbin/lltstat


# Cluster configuration info
VCS_MAX_NODEID=${VCS_MAX_NODEID-32}
VCS_MIN_NODEID=${VCS_MIN_NODEID-0}
CLUSTERNAME=
NODENAMES=
VOLD_PORT=
KMSG_PORT=

# VCSSYS - File configured to contain the local system name if uname -n
# returns domain-qualified system name.
VCSSYS=/etc/VRTSvcs/conf/sysname

# LICKLOCAL IP Address
LINK_LCL_IP="169.254.65."
IPFORMAT="^[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$"

# Lock file to avoid two cvminstall running concurrently.
CVMINST_LOCK_DIR=$VROOT_DIR/var/spool/locks
CVMINST_LOCK_FILE=$CVMINST_LOCK_DIR/.cvminstall.lock

# Temporary files.
tmpfile=${TMPDIR:-/tmp}/cvminst.tmp

op_sys=`uname`
[ $op_sys = "SunOS" ] &&  RSH=/usr/bin/rsh

#  Messages
EXISTMSG=" ERROR:$VCS_CONF_FILE already contains CVM group."
INVALIP=" Invalid IP address. 
   Enter the address in the standard Internet \"dot notation.\" "
INVALPORT=" Invalid Port Number. Port numbers below 1024 are not allowed.
	Re-enter the port number."
INVALNIC=" Invalid network interface." 

INVALCONF=" ERROR: Invalid VCS Configuration." 
ONLINE_CVM="Do you want to online cvm service group ? [y,n]"

SYSERR="ERROR: /etc/VRTSvcs/conf/sysname is missing on the local node.
VCS does not accept domaind-qualified system names.
Create /etc/VRTSvcs/conf/sysname file and put in it the name which VCS will
use for the local system."

RUNERR="ERROR: VCS is not running on the local node.
VCS needs to be running on the system for this utility to execute."

TYPEWRN="
	WARNING: CVMTypes.cf already exists.
	Before overwriting the existing CVMTypes.cf, it will be 
	moved to $VCS_CVMTYPES_SAV."


cvminst_lock_cleanup() {
	rm -f $CVMINST_LOCK_FILE			
}

tmpfiles_cleanup() {
	rm -f $tmpfile
}


clr_screen() {
	tput clear
}

#Read the configuration file
read_conf() {
	exec 5<&0 < $VCS_CONF_FILE
	while  read key_word arg1 arg2	
	do
		case $key_word in
		'#'* | '') 	#  Ignore comments, empty lines
				continue ;;
		esac
		
		if [ "$key_word" = "cluster" ]
		then
			#Cluster definition
			CLUSTERNAME=$arg1
		fi		
		if [ "$key_word" = "system" ]
		then
			#Node definition
			NODENAMES="$NODENAMES $arg1"
		fi
		if [ "$key_word" = "group" ]
		then
			if [ "$arg1" = "cvm" ]
			then
				ewritemsg  -M vxvmshm:893 "$EXISTMSG" 
				quit 1 
			fi
		fi
	done
	exec 0<&5 5<&-
}

get_ports() {
	ewritemsg  -M vxvmshm:0 "
	Enter the port numbers for the daemons 
	vxconfigd and vxkmsgd."

	voladm_help_push cvmport.menu.help
	# Prompt the user for the vxconfigd port number
	while true
	do
		_riput=`egettxt "Port number for vxconfigd:" vxvmshm:940`
		default=4500
		_opt="<port_number>"	
		read_input "$_riput" "$_opt" $default
		if [ $input -le 1024 ]
		then
			ewritemsg  -M vxvmshm:896 "$INVALPORT" 
			continue
		fi
		break
	done
	VOLD_PORT=$input

	# Prompt the user for the vxkmsgd port number
	
	while true
	do
		_riput=`egettxt "Port number for vxkmsgd:" vxvmshm:941`
		default=4501
		_opt="<port_number>"	
		read_input "$_riput" "$_opt" $default
		if [ $input -le 1024 ]
		then
			ewritemsg  -M vxvmshm:896 "$INVALPORT" 
			continue
		fi
		break
	done
	KMSG_PORT=$input
	voladm_help_pop
	voladm_continue -n
}

# Check that the node ids being entered are unique.

chk_id() { 
	chkid=$1
	nodearg=$2
	for i in $NODEID
	do
		if [ $chkid = $i ]
		then
			ewritemsg  -M vxvmshm:0 "
		Node ID $chkid for $nodearg is already being used by 
		some other node. Two nodes in a cluster
		cannot have same node IDs."
		get_nid_usr $nodearg

		fi
	done
	if [ $chkid -lt $VCS_MIN_NODEID -o $chkid -gt $VCS_MAX_NODEID ]
	then
		ewritemsg  -M vxvmshm:0 "
		Node ID $chkid for $nodearg does not fall within 
		the range of valid node IDs. 
		The maximum allowed node ID is $VCS_MAX_NODEID 
		and minimum is $VCS_MIN_NODEID."
		get_nid_usr $nodearg
	fi
	eval "nodeid_$nodearg=$chkid"
	eval "NODEID=\"\$NODEID \$nodeid_$nodearg\""
	clr_screen
}
			

# rsh to each node in the cluster and get the node ids from the
# /etc/llttab file. If there are some errors in doing rsh or 
# reading the info from the llttab file, then call get_nid_usr to
# prompt the user to enter  the node id. 

get_nodeid() {
	nodearg=$1
	$RSH $nodearg $LLTSTAT -c > $tmpfile 2>&1
	if [ $? -eq 0 ]
	then
		nodeid=`cat $tmpfile | grep "node:" | cut -d ":" -f2`
		chk_id $nodeid $nodearg
		return 0
	fi
	ewritemsg  -M vxvmshm:0 "
	Unable to get the node ID for the node $nodearg using the $LLTSTAT
	command.
	Enter the ID for the node $nodearg."
	get_nid_usr $nodearg
}
get_nid_usr() {
	nodearg=$1
	while true
	do
		voladm_help_push cvmid.menu.help
		_riput=`egettxt "Node ID for $nodearg:" vxvmshm:937`
		_opt="<node_id>"	
		read_input "$_riput" "$_opt"
		voladm_help_pop
		case $input in
		"")     ewritemsg -M vxvmshm:891 " Invalid Input"
			continue;;
		esac
		break
	done
	chk_id $input $nodearg
}

get_num_nic() {
	voladm_help_push cvmnic.menu.help
	ewritemsg  -M vxvmshm:0 "
	Enter the number of potential private networks 
	between the nodes."
	while true
	do
		_riput=`egettxt "Number of private networks :" vxvmshm:938`
		_opt="<num>"	
		read_input "$_riput" "$_opt"
		NUM_NIC=$input
		case $input in
		"")     ewritemsg -M vxvmshm:891 " Invalid Input"
			continue;;
		esac
		if [ $input -le 0 ]	
		then
			ewritemsg  -M vxvmshm:0 "
		Invalid number of private networks.
		Specify the correct number."
			continue
		fi
		break
	done
	voladm_help_pop
}
# get the information for the network interfaces on the private network.

get_nic() {
	nodearg=$1
	voladm_help_push cvmnicf.menu.help
	ewritemsg  -M vxvmshm:0 "
	Enter the network interface on the private networks 
	for node $nodearg.
	You have to enter only one interface per network. 
	Enter the interface for the primary network first."
	i=0
	nic=
	while [ $i -lt $NUM_NIC ]	
	do
		while true
		do
			_riput=`egettxt \
			 	"What is the network interface on network $i?"\
				vxvmshm:965` 
			_opt="<nic>"
			read_input "$_riput" "$_opt"	
			if [ "X$input" = "X" ]
			then
				ewritemsg  -M vxvmshm:895 "$INVALNIC"
				continue
			fi
			break
		done
			
		nic="$nic $input"
		eval "lastnic_$nodearg=\"$input\""
		i=`expr $i + 1`
	done
	voladm_help_pop
	eval "nodenic_$nodearg=\"$nic\""
}	

# Use LINKLOCAL network address space as default.
use_default() {
	ewritemsg  -M vxvmshm:0 "
	Using LINKLOCAL network address space as default.
	LINKLOCAL IP addresses have following format: 169.254.X.X.
	"
	vip_ctr=1
	bip_ctr=101
	
	for i in $NODENAMES	
	do
		eval "nodevip_$i=\$LINK_LCL_IP\$vip_ctr"	
		eval "n_nic=\"\$nodenic_$i\""
		k=0
		for j in  $n_nic
		do
			eval "nodebip_$i$k=\$LINK_LCL_IP\$bip_ctr"	
			bip_ctr=`expr $bip_ctr + 1`
			k=`expr $k + 1`
		done
		vip_ctr=`expr $vip_ctr + 1`
	done
	return 0
}
get_ip() {
	ewritemsg  -M vxvmshm:0 "
	Specify whether you would like to provide the virtual
	and base IP addresses for the network interfaces on the
	private network for all the nodes in the cluster."

	voladm_help_push cvmip.menu.help		
	while true 
	do
		_riput=`egettxt "Would you like to specify the IP addresses?" vxvmshm:967`
		_opt="y,n"	
		read_input "$_riput" "$_opt"
		ch=$input
		
		if [ "$input" = "n" -o "$input" = "N" ]
		then
			voladm_help_pop
			use_default
			return 0
		fi

		if [ "$input" != "y" -a "$input" != "Y" ]
		then
			ewritemsg  -M vxvmshm:892 " Invalid input."
			continue 
		fi
		break
	done
	voladm_help_pop
	for node in $NODENAMES
	do
		clr_screen
		get_nodeip $node
	done
	return 0
}

get_nodeip() {
	nodearg=$1
	voladm_help_push cvmipf.menu.help
	while true
	do
		_riput=`egettxt \
			 "What is the virtual IP address for node $nodearg ?"\
			vxvmshm:966` 
		_opt="<ip>"
		read_input "$_riput" "$_opt"	
		echo $input | grep $IPFORMAT > /dev/null 2>&1
		if [ $? -ne 0 ]
		then
			ewritemsg  -M vxvmshm:894 "$INVALIP"
			continue
		fi
		break
	done	
	eval "nodevip_$nodearg=\$input"
	eval "n_nic=\"\$nodenic_$nodearg\""	
	k=0
	for i in  $n_nic
	do
		while true
		do
			_riput=`egettxt \
				 "What is the base IP addr for the NIC $i on the node $nodearg?" vxvmshm:964` 
			_opt="<ip>"
			read_input "$_riput" "$_opt"	
			echo $input | grep $IPFORMAT > /dev/null 2>&1
			if [ $? -ne 0 ]
			then
				ewritemsg  -M vxvmshm:894 "$INVALIP"
				continue
			fi
			break
		done
		eval "nodebip_$nodearg$k=\$input"
		k=`expr $k + 1`
			
	done
	voladm_help_pop
}

delete_grp() {
	ewritemsg  -M vxvmshm:0 "
	Deleting the cvm service group."
	hares -delete cvm_clus > /dev/null 2>&1 
	hares -delete cvmports > /dev/null 2>&1 
	hares -delete cvmmnic > /dev/null 2>&1 
	for i in $NODENAMES
	do
		hares -delete $i > /dev/null 2>&1 
	done
	hagrp -delete cvm > /dev/null 2>&1 
	haconf -dump -makero
}

write_nicinfo() {
	
	hares -add cvmmnic CVMMultiNIC cvm > /dev/null 2>&1
	if [ $? -ne 0 ]
	then
		ewritemsg  -M vxvmshm:0 "
		Error: Cannot add CVMMultiNIC resource in cvm group"	
		delete_grp
		quit 1
	fi
	hares -modify cvmmnic Enabled 1
	if [ $? -ne 0 ]
	then
		ewritemsg  -M vxvmshm:0 "
		Error: Cannot set Enabled attribute for CVMMultiNIC "
		delete_grp
		quit 1
	fi
	hares -modify cvmmnic Critical 0 
	if [ $? -ne 0 ]
	then
		ewritemsg  -M vxvmshm:0 "
		Error: Cannot set Critical attribute for CVMMultiNIC "
		delete_grp
		quit 1
	fi
	
	hares -local cvmmnic CVMDevice 
	if [ $? -ne 0 ]
	then
		ewritemsg  -M vxvmshm:0 "
		Error: Cannot make CVMDevice attribute for CVMMultiNIC local"
		delete_grp
		quit 1
	fi
	for i in $NODENAMES
	do
		eval "nnic=\$nodenic_$i"
		niclist=
		k=0
		for j in $nnic
		do
			eval "nbip=\$nodebip_$i$k"
			k=`expr $k + 1`
			niclist="$niclist $j $nbip"
		done
		hares -modify cvmmnic CVMDevice -add $niclist -sys $i 
		if [ $? -ne 0 ]
		then
			ewritemsg  -M vxvmshm:0 "
			Error: Cannot set CVMDevice attribute for CVMMultiNIC "
			delete_grp
			quit 1
		fi
	done
	addrlist=
	for i in $NODENAMES
	do
		eval "nvip=\$nodevip_$i"
		addrlist="$addrlist $i $nvip"	
	done

	hares -modify cvmmnic CVMAddress -add $addrlist 
	if [ $? -ne 0 ]
	then
		ewritemsg  -M vxvmshm:0 "
		Error: Cannot set CVMAddress attribute for CVMMultiNIC "
		delete_grp
		quit 1
	fi
	hares -modify cvmmnic ActivePvtNet 1 
	if [ $? -ne 0 ]
	then
		ewritemsg  -M vxvmshm:0 "
		Error: Cannot set ActivePvtNet attribute for CVMMultiNIC "
		delete_grp
		quit 1
	fi
	hares -modify cvmmnic CVMNetMask 0xffffff00 
	if [ $? -ne 0 ]
	then
		ewritemsg  -M vxvmshm:0 "
		Error: Cannot set CVMNetMask attribute for CVMMultiNIC "
		delete_grp
		quit 1
	fi
	hares -modify cvmmnic CVMIfconfigTwice 1 
	if [ $? -ne 0 ]
	then
		ewritemsg  -M vxvmshm:0 "
		Error: Cannot set CVMIfconfigTwice attribute for CVMMultiNIC "
		delete_grp
		quit 1
	fi
	hares -modify cvmmnic CVMArpDelay 5 
	if [ $? -ne 0 ]
	then
		ewritemsg  -M vxvmshm:0 "
		Error: Cannot set CVMArpDelay attribute for CVMMultiNIC "
		delete_grp
		quit 1
	fi

}
write_nodeinfo() {
	for i in $NODENAMES
	do
		eval "nid=\$nodeid_$i"
		eval "nnic=\$nodenic_$i"
		eval "nvip=\$nodevip_$i"
	
		hares -add $i CVMNodeInfo cvm > /dev/null  2>&1
		if [ $? -ne 0 ]
		then
			ewritemsg  -M vxvmshm:0 "
			Error: Cannot add CVMNodeInfo resource in cvm group"	
			delete_grp
			quit 1
		fi
		hares -modify $i Enabled 1
		if [ $? -ne 0 ]
		then
			ewritemsg  -M vxvmshm:0 "
			Error: Cannot set Enabled attribute for CVMNodeInfo "
			delete_grp
			quit 1
		fi
		hares -modify $i Critical 0
		if [ $? -ne 0 ]
		then
			ewritemsg  -M vxvmshm:0 "
			Error: Cannot set Critical attribute for CVMNodeInfo "
			delete_grp
			quit 1
		fi
		hares -modify $i NodeName $i
		if [ $? -ne 0 ]
		then
			ewritemsg  -M vxvmshm:0 "
			Error: Cannot set NodeName attribute for CVMNodeInfo "
			delete_grp
			quit 1
		fi
		hares -modify $i NodeId $nid
		if [ $? -ne 0 ]
		then
			ewritemsg  -M vxvmshm:0 "
			Error: Cannot set NodeId attribute for CVMNodeInfo "
			delete_grp
			quit 1
		fi
		hares -modify $i IPAdrr "$nvip" 
		if [ $? -ne 0 ]
		then
			ewritemsg  -M vxvmshm:0 "
			Error: Cannot set IPAdrr attribute for CVMNodeInfo "
			delete_grp
			quit 1
		fi
	
	done
}

add_types() {
	#
	# Install CVM resource type definitions into VCS.
	#

	haconf -makerw

	hatype -add CVMMultiNIC
	haattr -add -static CVMMultiNIC RegList -string -keylist
	hatype -modify CVMMultiNIC RegList -add ActivePvtNet
	hatype -modify CVMMultiNIC MonitorTimeout 120
	hatype -modify CVMMultiNIC LogLevel error
	hatype -modify CVMMultiNIC FaultOnMonitorTimeouts 4
	hatype -modify CVMMultiNIC RestartLimit 100
	hatype -modify CVMMultiNIC ArgList -add CVMDevice
	hatype -modify CVMMultiNIC ArgList -add CVMAddress
	hatype -modify CVMMultiNIC ArgList -add ActivePvtNet
	hatype -modify CVMMultiNIC ArgList -add CVMNetMask
	hatype -modify CVMMultiNIC ArgList -add CVMIfconfigTwice
	hatype -modify CVMMultiNIC ArgList -add CVMArpDelay
	hatype -modify CVMMultiNIC ArgList -add CVMRouteOptions
	haattr -add CVMMultiNIC CVMDevice -string -assoc
	haattr -add CVMMultiNIC CVMAddress -string -assoc
	haattr -add CVMMultiNIC ActivePvtNet -integer -scalar 0
	haattr -add CVMMultiNIC CVMNetMask
	haattr -add CVMMultiNIC CVMIfconfigTwice -integer -scalar
	haattr -add CVMMultiNIC CVMArpDelay -integer -scalar 1
	haattr -add CVMMultiNIC CVMRouteOptions
	hatype -modify CVMMultiNIC SourceFile "./CVMTypes.cf"

	hatype -add CVMCluster
	hatype -modify CVMCluster OnlineTimeout 400
	hatype -modify CVMCluster LogLevel error
	hatype -modify CVMCluster FaultOnMonitorTimeouts 4
	hatype -modify CVMCluster ArgList -add CVMClustName
	haattr -add CVMCluster CVMClustName 
	hatype -modify CVMCluster SourceFile "./CVMTypes.cf"

	hatype -add CVMNodeInfo
	hatype -modify CVMNodeInfo LogLevel error
	hatype -modify CVMNodeInfo FaultOnMonitorTimeouts 4	
	hatype -modify CVMNodeInfo ArgList -add NodeName
	hatype -modify CVMNodeInfo ArgList -add NodeId
	hatype -modify CVMNodeInfo ArgList -add IPAdrr
	hatype -modify CVMNodeInfo NameRule resource.NodeName
	haattr -add CVMNodeInfo NodeName -string
	haattr -add CVMNodeInfo NodeId -integer -scalar 999
	haattr -add CVMNodeInfo IPAdrr -string
	hatype -modify CVMNodeInfo SourceFile "./CVMTypes.cf"

	hatype -add CVMPortInfo
	hatype -modify CVMPortInfo LogLevel error
	hatype -modify CVMPortInfo FaultOnMonitorTimeouts 4
	hatype -modify CVMPortInfo ArgList -add PortConfigd
	hatype -modify CVMPortInfo ArgList -add PortKmsgd
	haattr -add CVMPortInfo PortConfigd -integer
	haattr -add CVMPortInfo PortKmsgd -integer
	hatype -modify CVMPortInfo SourceFile "./CVMTypes.cf"

	hatype -add CVMDiskGroup
	hatype -modify CVMDiskGroup LogLevel error
	hatype -modify CVMDiskGroup FaultOnMonitorTimeouts 4
	haattr -add -static CVMDiskGroup RegList -string -keylist
	hatype -modify CVMDiskGroup RegList -add CVMActivation
	hatype -modify CVMDiskGroup ArgList -add CVMDiskGroup
	hatype -modify CVMDiskGroup ArgList -add CVMActivation
	haattr -add CVMDiskGroup CVMDiskGroup
	haattr -add CVMDiskGroup CVMActivation
	hatype -modify CVMDiskGroup SourceFile "./CVMTypes.cf"

	hatype -add CVMVolume
	hatype -modify CVMVolume LogLevel error
	hatype -modify CVMVolume FaultOnMonitorTimeouts 4
	hatype -modify CVMVolume ArgList -add CVMVolume
	hatype -modify CVMVolume ArgList -add CVMDiskGroup
	hatype -modify CVMVolume NameRule 'resource.CVMDiskGroup + "_" + resource.CVMVolume'
	haattr -add CVMVolume CVMVolume
	haattr -add CVMVolume CVMDiskGroup
	hatype -modify CVMVolume SourceFile "./CVMTypes.cf"

	if [ -f $VCS_CVMTYPES_FILE ]
	then
		ewritemsg  -M vxvmshm:0 "$TYPEWRN"
		mv $VCS_CVMTYPES_FILE $VCS_CVMTYPES_SAV
	fi
	haconf -dump -makero

}

# Add the information obtained from the user to the VCS
# configuration.
write_conf() {

	# Add the CVMTypes into the VCS system
	add_types
	nodes_list=""
	nindx=0
	for i in $NODENAMES
	do
		nodes_list="$nodes_list $i $nindx"
	done


	# Add the information obtained from the user to
	# the VCS configuration. 
	haconf -makerw 
	hagrp -add cvm > /dev/null 2>&1 
	if [ $? -ne 0 ]
	then
		ewritemsg  -M vxvmshm:0 "
		Error: Adding cvm service group"	
		delete_grp
		quit 1
	fi
	hagrp -modify cvm SystemList $nodes_list 
	if [ $? -ne 0 ]
	then
		ewritemsg  -M vxvmshm:0 "
		Error: Cannot set SystemList attribute for cvm group"	
		delete_grp
		quit 1
	fi
	hagrp -modify cvm AutoStartList $NODENAMES 
	if [ $? -ne 0 ]
	then
		ewritemsg  -M vxvmshm:0 "
		Error: Cannot set AutoStartList attribute for cvm group"	
		delete_grp
		quit 1
	fi
	hagrp -modify cvm Parallel 1
	if [ $? -ne 0 ]
	then
		ewritemsg  -M vxvmshm:0 "
		Error: Cannot set Parallel attribute for cvm group"	
		delete_grp
		quit 1
	fi
	hagrp -modify cvm AutoFailOver 0
	if [ $? -ne 0 ]
	then
		ewritemsg  -M vxvmshm:0 "
		Error: Cannot set AutoFailOver attribute for cvm group"	
		delete_grp
		quit 1
	fi

	# Add information for CVMCluster resrc
	
	hares -add cvm_clus CVMCluster cvm > /dev/null 2>&1
	if [ $? -ne 0 ]
	then
		ewritemsg  -M vxvmshm:0 "
		Error: Cannot add CVMCluster resource in cvm group"	
		delete_grp
		quit 1
	fi
	hares -modify cvm_clus Enabled 1
	if [ $? -ne 0 ]
	then
		ewritemsg  -M vxvmshm:0 "
		Error: Cannot set Enabled attribute for CVMCluster "	
		delete_grp
		quit 1
	fi
	hares -modify cvm_clus Critical 0 
	if [ $? -ne 0 ]
	then
		ewritemsg  -M vxvmshm:0 "
		Error: Cannot set Critical attribute for CVMCluster "	
		delete_grp
		quit 1
	fi
	hares -modify cvm_clus CVMClustName $CLUSTERNAME
	if [ $? -ne 0 ]
	then
		ewritemsg  -M vxvmshm:0 "
		Error: Cannot set CVMClustName attribute for CVMCluster"	
		delete_grp
		quit 1
	fi
	
	write_nodeinfo

	# Add CVMPortInfo resource	

	hares -add cvmports CVMPortInfo cvm > /dev/null 2>&1
	if [ $? -ne 0 ]
	then
		ewritemsg  -M vxvmshm:0 "
		Error: Cannot add CVMPortInfo resource in cvm group"	
		delete_grp
		quit 1
	fi
	hares -modify cvmports Enabled 1
	if [ $? -ne 0 ]
	then
		ewritemsg  -M vxvmshm:0 "
		Error: Cannot set Enabled attribute for CVMPortInfo "
		delete_grp
		quit 1
	fi
	hares -modify cvmports Critical 0 
	if [ $? -ne 0 ]
	then
		ewritemsg  -M vxvmshm:0 "
		Error: Cannot set Critical attribute for CVMPortInfo "
		delete_grp
		quit 1
	fi
	hares -modify cvmports PortConfigd $VOLD_PORT
	if [ $? -ne 0 ]
	then
		ewritemsg  -M vxvmshm:0 "
		Error: Cannot set PortConfigd attribute for CVMPortInfo "
		delete_grp
		quit 1
	fi
	hares -modify cvmports PortKmsgd $KMSG_PORT
	if [ $? -ne 0 ]
	then
		ewritemsg  -M vxvmshm:0 "
		Error: Cannot set PortKmsgd attribute for CVMPortInfo "
		delete_grp
		quit 1
	fi
	write_nicinfo	

	hares -link cvm_clus cvmmnic 
	if [ $? -ne 0 ]
	then
		ewritemsg  -M vxvmshm:0 "
		Error: Cannot link CVMCluster to CVMMultiNIC "
		delete_grp
		quit 1
	fi

	# Commit the new configuration 
	haconf -dump -makero
	if [ $? -ne 0 ]
	then
		ewritemsg  -M vxvmshm:0 "
		ERROR: Adding CVM Configuration to VCS system"
		haconf -makerw
		delete_grp
		quit 1
	fi

	cmmit=`haclus -value DumpingMembership`
	if [ $cmmit -ne 0 ]
	then
		ewritemsg  -M vxvmshm:0 "
	Waiting for VCS to add the new configuration."

	fi
	while [ $cmmit -ne 0 ]
	do
		sleep 1
		cmmit=`haclus -value DumpingMembership`
	done

	# Validate the contents of main.cf
	hacf -verify $VCS_CONF_DIR
	if [ $? -ne 0 ]
	then
		ewritemsg  -M vxvmshm:979 "$INVALCONF"
	fi

	voladm_continue
	clr_screen
	ewritemsg  -M vxvmshm:0 "
	You will now be prompted to enter whether you want to online
	cvm service group on the systems in the cluster.
	If you choose to do so, then vxcvminstall will attempt
	to online the group on all the systems in the cluster. 
	It will not online the group on systems which do not 
	have VCS running.
	"
	cvm_on=0
	while true
	do
		echo "$ONLINE_CVM \c"
		read online_cvm 
		case $online_cvm in 
		"y" | "Y") cvm_on=1
			   break;;	
		"n" | "N") cvm_on=0
			   break;;	
		*) ewritemsg  -M vxvmshm:0 "Invalid input".
			   continue
		esac
	done
	if [ $cvm_on -eq 1 ]
	then
		for i in $NODENAMES
		do
			# Check if VCS is running. 
			sys_stat=`hasys -display $i -attribute SysState | \
					grep "SysState"  2>/dev/null`
			echo $sys_stat | grep "RUNNING" > /dev/null 2>&1
			if [  $? -ne 0 ]
			then
				ewritemsg  -M vxvmshm:0 "
			VCS is not running on system $i. Hence
			not onlining the cvm group on it. 
			You will need to autoenable the cvm group
			before onlining it on the system."

				continue
			fi
			hagrp -autoenable cvm -sys $i
			if [ $? -ne 0 ]
			then
				ewritemsg  -M vxvmshm:0 "
				ERROR: Autoenabling the cvm group on node $i"
			else
				hagrp -online cvm -sys $i 
				if [ $? -ne 0 ]
				then
					ewritemsg  -M vxvmshm:0 "
				ERROR: Onlining the cvm group on node $i"

				fi
			fi
		done
	else
		ewritemsg  -M vxvmshm:0 "	
		You will need to autoenable the cvm group
		before onlining it on the systems."
	fi
	

}

verify_info() {
	clr_screen
	voladm_help_push cvmver.menu.help
	ewritemsg  -M vxvmshm:0 " 
	------- Following is the summary of the information: ------
		Cluster 	: $CLUSTERNAME
		Nodes   	:$NODENAMES
		vxconfigd port 	: $VOLD_PORT
		kmsg port 	: $KMSG_PORT
	-----------------------------------------------------------"
	voladm_continue
	for i in $NODENAMES
	do
		clr_screen
		eval "nnic=\$nodenic_$i"
		eval "nid=\$nodeid_$i"
		eval "nvip=\$nodevip_$i"
		nstr=
		k=0
		for j in $nnic
		do
			eval "nbip=\$nodebip_$i$k"
			k=`expr $k + 1`
			nstr1="Base IP Addr for $j	: $nbip \n\t\t"
			nstr="$nstr$nstr1"
		done
		ewritemsg  -M vxvmshm:0 "
	--------------- Information for Node $i: ------------------
		NodeID	 		: $nid
		Virtual IP Addr		: $nvip 
	        $nstr
	-----------------------------------------------------------"
	voladm_continue
	done
	if voladm_yorn -M vxvmshm:211 "Is this correct" y
	then
		# User said yes
		voladm_help_pop
		ewritemsg  -M vxvmshm:0 "
	 Entering CVM Configuration to the VCS system. The existing
	 $VCS_CONF_FILE will be copied to 
	 $VCS_CONF_SAV."
	 cp $VCS_CONF_FILE $VCS_CONF_SAV
		write_conf
		return 0
	else
		voladm_help_pop
		# User has problems.	
		ewritemsg  -M vxvmshm:0 "
	Rerun the vxcvminstall program with the correct information."
		return 0
	fi
		
		
}

# Get the information regarding the nodes and the port numbers from
# the user

get_info() {
	clr_screen	
	ewritemsg  -M vxvmshm:890  "\
	The cluster configuration information read from the VCS
	configuration file is as follows.
		Cluster : $CLUSTERNAME
		Nodes   :$NODENAMES"
	ewritemsg  -M vxvmshm:0 "
	You will now be prompted to enter the information pertaining 
	to the cluster and the individual nodes."
	voladm_continue 
	clr_screen
	get_ports
	clr_screen
	get_num_nic
	for node in $NODENAMES
	do
		clr_screen
		get_nodeid $node
		get_nic $node
		voladm_continue -n
	done
	clr_screen
	get_ip
	voladm_continue -n
	verify_info
}	
main_menu() {
	
	#Display the Main Menu 
	voladm_menu_push CVM service group 
	voladm_help_push cvminst.menu.help
	voladm_menu <<-!
	============== Main Menu ================
	1 configure vxvmshm:910 Enter the Configuration for CVM Service Group

	
	!
	voladm_menu_pop
	voladm_help_pop
	case $VOLADM_MENU_SELECT in
	configure) get_info
		 ;;
	*)	 ewritemsg  -M vxvmshm:0 " 
		 Unrecognized command." 
	esac
	ewritemsg  -M vxvmshm:928 "Have A Nice Day!"
	quit 0
}
start_cvminst() {
	
	# Check if the cluster is already running. 
	clr_screen
	clust_run=`$VXDCTL -c mode 2>/dev/null`
	echo $clust_run | grep "cluster active" > /dev/null 2>&1
	if [ $? -eq 0 ] 
	then
		ewritemsg  -M vxvmshm:0 "
		ERROR: The local node is already a member of CVM cluster. 
		Shutdown the CVM Cluster before proceeding."
		quit 1
	fi
	
	# Check if the main.cf file exists
	if [ ! -f $VCS_CONF_FILE ] 
	then
		ewritemsg  -M vxvmshm:0 "
		ERROR: The VCS cluster configuration file:
		$VCS_CONF_FILE is missing. 

		Create the file with appropriate cluster 
		configuration information and rerun vxcvminstall."
		quit 1
	fi
	read_conf
	main_menu
}	

# Check if any other user is running cvminstall
if [ -f $CVMINST_LOCK_FILE ] 
then
	export CVMINST_LOCK_FILE
	ewritemsg  -M vxvmshm:0 " 
	The vxcvminstall script is already running and cannot run
	concurrently with another vxcvminstall. Try running it at a
	later time. If you are sure that no one else is running
	vxcvminstall, you can remove the file:
			$CVMINST_LOCK_FILE
	
	Removing this lock file allows you to run vxcvminstall."
	quit 1	
fi

localnode=`uname -n 2>/dev/null`
echo $localnode | grep "\." > /dev/null 2>&1
if [ $? -eq 0 ]
then
	# VCS does not accept domaind-qualified system names.
	# The file /etc/VRTSvcs/conf/sysname should contain
	# the local system name.

	if [ -s $VCSSYS ]
	then
		localnode=`cat $VCSSYS 2>/dev/null`
	else
		ewritemsg  -M vxvmshm:0 "$SYSERR"
		quit 1
	fi
fi
		

# Check if VCS is running. 
sys_stat=`hasys -display $localnode -attribute SysState | \
		grep "SysState"  2>/dev/null`
echo $sys_stat | grep "RUNNING" > /dev/null 2>&1
if [  $? -ne 0 ]
then
	ewritemsg  -M vxvmshm:0 "$RUNERR"
	quit 1
fi

add_cleanup cvminst_lock_cleanup tmpfiles_cleanup
2> /dev/null > $CVMINST_LOCK_FILE
if [ $? -ne 0 ]
then
	export CVMINST_LOCK_FILE
	ewritemsg  -M vxvmshm:0 "
	Cannot create lock file : $CVMINST_LOCK_FILE"
	
	quit 1
fi
start_cvminst
