#! /bin/ksh
# ident "@(#)tunenfs	1.32    97/10/01 SMI"
# Copyright 1995 Sun Microsystems, Inc. All Rights Reserved
 
#
# Script to tune the Netra NFS server. Tunes the following variables based
# on amount of installed memory:
#
#	- ufs_ninode, ncsize, and buhwm (in /etc/system)
#	- number of nfs server threads
#
# Based on amount of installed memory, NVRAM, stripe unit sizes,
# number and types of filesystems, and server platform, the size of stable
# memory region used by PrestoServe is tuned as well. The variables are:
# (found in /platform/sun4u/kernel/drv/rootnex.conf)
#
#	- stabmem-reg1-size, stabmem-reg2-size for NetraNFS 150s
#	- nvram-reg1-size, nvram-reg2-size for other platforms.
#
# Usage:
#
#	tunenfs [ -c ] [-r rootdir ] [ -p ] [ -l ]
#
# Options:
#
#	c: run without actually changing any config file
#	p: prints presto tuning info *when* presto is
#	   under-tuned. Nothing is printed otherwise.
#	l: lists attributes set by tunenfs, their current values,
#	   and their new values if tunenfs is run. Option -c is
#	   automatically set.
#
# Option -p Output
# 
# 	When the stable memory or NVRAM to be used by presto is
# 	under-tuned, the -p option causes an output of the format:
# 
# 	$1: Either INSUFFICIENT or SUBOPTIMAL.
# 	    - INSUFFICIENT: memory or NVRAM configuration does not meet
# 	      minimum requirement. Presto might not load successfully.
# 	    - SUBOPTIMAL: memory or NVRAM configuration not enough to support
# 	      optimal tuning of Presto.
# 	
# 	$2: Either memory, nvram, or dumpdev. Refers to the resource
# 	    that's lacking .'dumpdev' (on Netra NFS 150s only) indicates
# 	    that the dump partition for presto is too small.
# 
# 	$3: Amount in MBytes of resource $2 desired.
# 
# 	$4: Amount in MBytes of resource $2 needs to be added to bring the
# 	    configuration out of state $1.
# 
# 	So for exmaple, 
# 
# 		INSUFFICIENT nvram 32 16
# 
# 	means that NVRAM configuration on the server is 16M below the
# 	minimum requirement of 32M, and presto might have problem loading.
#
# Option -l Output
#	
#	A table is unconditionally printed. Each row has three fields,
#	namely $1=name of attribute, $2=current value of $1, $3=new value
#	of $1 tunenfs would assign. Note that no attribute is modified
#	with this option.
#
# exit status codes:
#
#       0 ==> System already tuned for installed memory.
#       1 ==> tuneables changed for installed memory. Requires a reboot.
#       2 ==> Error exit
#
# Note: This version of tunenfs *does not* scale Presto upper size wrt. to
# number of filesystems, number of disks, or stripe unit size. We use
# experimentally determined optimal values for Presto upper on 150s, and
# simply use up all NVRAM available on non-150s.
#

MODULE=nfs/tune-nfs
. /opt/netra/SUNWnetra/lib/netra.globals

netra_read_locale tunenfs_bin.msgs

ROOT_DIR=""
checkopt=0
printopt=0
unknown_opt=false

# Extract the options that are passed
#
while getopts ":r:lcp" opt;do
case $opt in
        c) checkopt=1 ;;
	p) printopt=1 ;;
	l) listopt=1 
	   checkopt=1
	   ;;
        r) ROOT_DIR=$OPTARG;;
        *) unknown_opt=true ;;
esac
done
if [[ $unknown_opt = *true* ]]
then
        print "$usage_msg"
        exit 2
fi

BACKUP_SYSTEM=/var/tmp/system
BACKUP_ROOTNEX=/var/tmp/rootnex.conf

pid=$$
TMP_SYSTEM="/tmp/$pid.sys"
TMP_ROOTNEX="/tmp/$pid.root"
/usr/bin/touch $TMP_SYSTEM
/usr/bin/touch $TMP_ROOTNEX
 
ETC_SYSTEM=${ROOT_DIR}/etc/system
ROOTNEX_CONF=${ROOT_DIR}/platform/sun4u/kernel/drv/rootnex.conf
NFS_RC=${ROOT_DIR}/etc/init.d/nfs.server
MD_CF=${ROOT_DIR}/etc/opt/SUNWmd/md.cf
NETRA_FSTAB=${ROOT_DIR}/etc/opt/netra/common/netra_fstab
NETRA_IS150=/opt/netra/SUNWsysc/netra_is150

METASTAT=${ROOT_DIR}/usr/opt/SUNWmd/sbin/metastat
SAVECONF=${ROOT_DIR}/opt/netra/services/SUNWsmA/lib/sm_save_conf
NVADM=${ROOT_DIR}/usr/sbin/nvadm

(( MAX_PROM_RETAIN=122 ))
(( MIN_MEM_SIZE=64 ))
(( MIN_NVRAM_SIZE=32 ))
(( MEM_STABMEM_RATIO=3 ))

(( meg=1024*1024 ))
if [[ -f "$NETRA_IS150" ]]; then
    (( on150 = 1 ))
else
    (( on150 = 0 ))
fi

#
# Function to round up a given quantity in bytes to megabytes.
# Usage: roundup_to_meg <quantity>
#
roundup_to_meg() {
	(( quotient = $1/meg ))
	(( remainder = $1%meg ))

	if (( remainder == 0 )); then
	    print $quotient
	else
	    print $(( quotient + 1 ))
	fi	    
}			

#
# Function to print tunenfs output.
# Only prints when the server is undertuned, and is so because of lack of
# memory (on NetraNFS 150s), NVRAM (on non-150s), or dump partition for
# stable memory is too small (on 150s).
# For format of output, see beginning of this script.
#
tunenfs_report() {

	if [[ $problem = underconf_memory ]]; then
	    print INSUFFICIENT memory $MIN_MEM_SIZE \
		 $(( MIN_MEM_SIZE - mem_size ))
	elif [[ $problem = underconf_nvram ]]; then
	    print INSUFFICIENT nvram $MIN_NVRAM_SIZE \
		$(( MIN_NVRAM_SIZE - installed_nvram ))
	else	    

	    case $problem in
		'memory')	(( to_add = diff * MEM_STABMEM_RATIO )) ;;
		'nvram')	(( to_add = diff )) ;;
		'dumpdev')	(( to_add = stabmem_rqd - dumpdev_size )) ;;
		*)		return ;;
	    esac

	    to_add_meg=`roundup_to_meg $to_add`
	    stabmem_rqd_meg=`roundup_to_meg $stabmem_rqd`

	    print SUBOPTIMAL $problem $stabmem_rqd_meg $to_add_meg

	fi
}
 
#
# Define property name for amount of non-volatile memory allocation to
# presto upper and lower. Different on 150 and non-150.
#
if (( on150 )); then
	nvmem1_prop_name="stabmem-reg1-size"
	nvmem2_prop_name="stabmem-reg2-size"
	unwanted_prop="nvmem"
else
	nvmem1_prop_name="nvmem-reg1-size"
	nvmem2_prop_name="nvmem-reg2-size"
	unwanted_prop="stabmem"
fi
dumpdev_prop_name="stabmem-dumpdev"
dumpdev_name="/dev/dsk/c0t0d0s3"

let mem_size=`/usr/sbin/prtconf -v  | /usr/bin/grep "^Memory size:" |\
/usr/bin/sed 's/[a-z,A-Z,:, ]//g'`

#
# On non-150 platforms, sum up amount of NVRAM on all installed NVRAM hw,
# using the nvadm command.
#
if (( !on150 )); then
    if [[ -x $NVADM ]]; then
	sum=`$NVADM -v | /usr/bin/nawk '
	    BEGIN { installed=0; avail=0; }
	    $1~/^nvram/ && $4=="0x0" { installed += $2; avail += $3 }
	    END { print installed " " avail; }
	'`
	(( installed_nvram=`/usr/bin/echo $sum | awk ' { print $1 } '`/meg ))
	(( nvram_size=`/usr/bin/echo $sum | awk ' { print $2 } '` ))
    else
	(( installed_nvram=0 ))
	(( nvram_size=0 ))
    fi	
fi

#
# Stripe size is used as a parameter in determing how much non-volatile
# memory Presto needs (lower only), since Presto buffer sizes
# depend on stripe unit size. Note that stripe sizes may be reduced on
# non-150 platforms to lower Presto requirement for NVRAM.
# Note that stripe size is in unit of blocks.
# Also, we assume that all metadevices have the same stripe size.
#
if [[ -f $MD_CF ]]; then
	stripe_size=$(/usr/bin/grep -v "^[	]*#" $MD_CF |\
	/usr/bin/grep "\-i" | /usr/bin/head -1 |\
	/usr/bin/sed -e 's/^.*-i //' -e 's/b.*$//')
fi
stripe_size=${stripe_size:-16}

#
# Determine system-wide RAID level. Assume same RAID level for all FS.
#
let raid_level=5
if [[ -f $MD_CF ]]; then
	/usr/bin/grep -v "^[	]*#" $MD_CF |\
	/usr/bin/grep "\-m" >/dev/null 2>&1
	if [[ $? == 0 ]]; then
		let raid_level=10
	fi
fi

#
# set_stabmem2_per_md:
# determines how much stable memory will be needed by each type of
# metadevice. "combo" refers to either an internal disk array (for 150
# platform only) or an external combo, and "ssatray" refers to metadevices
# built on an SSA tray. They have different requirement because they have
# different number of disks contained.
# Note that Presto upper size is independent of size of filesystem.
#
set_stabmem2_per_md()
{
	(( stabmem1_per_combo=0 ))

	case $stripe_size in
	32)	(( stabmem2_per_combo=10*meg ))
		(( stabmem2_per_ssatray=6*meg ))
		;;
	16|*)	(( stabmem2_per_combo=5*meg ))
		(( stabmem2_per_ssatray=3*meg ))
		;;
	esac

}

#
# adjust_stabmem_rqd:
# adjusts nvmem required by presto upper and lower. In case of too little
# nvmem, the current algorithm tries to cut down stabmem1_rqd as much as
# possible before cutting down stabmem2_rqd.
# In case of too much stabmem available on non-150s, we give the extra
# stabmem to Presto upper, since on these platforms, stabmem1_per_* is 
# determined assuming a small amount of NVRAM (see set_stabmem_per_md).
#
adjust_stabmem_rqd()
{
	#
	# For 150s, we assume that 1/MEM_STABMEM_RATIO of total installed
	# memory could be used for Presto. We also need to make sure the
	# remaining memory is enough for the system to work normally,
	# assumed to be 64M.
	# For non-150s, assume ALL NVRAM could be used for Presto.	
	#	
	if (( on150 )); then
		(( stabmem_avail=mem_size/MEM_STABMEM_RATIO ))
		problem="memory"

		#
		# prom_retain() has problem with stabmem > 128M,
		# experimentally determined to be 122M max.
		#
		if (( stabmem_avail > MAX_PROM_RETAIN )); then
			(( stabmem_avail = MAX_PROM_RETAIN ));
			problem="prom_retain()"
		fi

		if (( mem_size < MIN_MEM_SIZE )); then
			(( stabmem_avail=0 ))
			problem="underconf_memory"
		fi
		(( stabmem_avail*=meg ))
	else
		(( stabmem_avail=nvram_size ))
		problem="nvram"

		if (( installed_nvram < MIN_NVRAM_SIZE )); then
			problem="underconf_nvram"
		fi
	fi

	#
	# Make sure we can dump everything in nvmem to the dump device
	# when we have to do so.
	#
        dumpdev=`/usr/bin/fgrep "$dumpdev_prop_name=" $ROOTNEX_CONF | \
		/usr/bin/grep -v '^#' | /usr/bin/grep -v '^[ 	]*#' | \
		/usr/bin/head -1 | /usr/bin/awk -F"=" '{ print $2 }' | \
		/usr/bin/sed 's/[\"\; 	]//g' | \
		/usr/bin/sed 's/\/dsk/\/rdsk/'`

	#
	# If dumpdev property is not defined (say on non-150s), it imposes
	# no additinoal constraint.
	# We assume the dump device, if exists, is always on slice 3.
	#
	if [[ $dumpdev != "" ]]; then
		dumpdev_size=`/usr/sbin/prtvtoc -h $dumpdev |\
			/usr/bin/awk '$1=="3" { printf "%ld", $5*512; }'`

		if (( dumpdev_size<stabmem_avail )); then
			(( stabmem_avail=dumpdev_size ))
			problem="dumpdev"
		fi
	fi

	(( stabmem_rqd=stabmem1_rqd+stabmem2_rqd ))
	if (( stabmem_avail<stabmem_rqd )); then
		#
		# The strategy is to cut presto upper requirement until a
		# min. of 2meg (so that the driver can still be loaded),
		# then pass on the rest of the cutting to presto lower.
		#
		(( diff=stabmem_rqd-stabmem_avail ))
		(( stabmem1_rqd-=diff ))

		if (( stabmem1_rqd<0 )); then
			(( stabmem2_rqd+=stabmem1_rqd ))
			(( stabmem1_rqd=0 ))
		fi

		if (( stabmem2_rqd<0 )); then
			(( stabmem2_rqd=0 ))
		fi

		if (( stabmem2_rqd>=4*meg && stabmem1_rqd<2*meg )); then
			(( stabmem1_rqd+=2*meg ))
			(( stabmem2_rqd-=2*meg ))
		fi

		if [[ $printopt = 1 ]]; then
			tunenfs_report
		fi

	elif (( !on150 && stabmem_avail>stabmem_rqd )); then
		(( stabmem1_rqd += stabmem_avail-stabmem_rqd ))
	fi
}

#
# This function determines how many SSA RSM trays are in use.  It considers a
# tray to be in use when it finds a disk in a tray.
#
tunenfs_ssa_check()
{
	controller=0
	MAX_CONTROLLER=127
	while [ $controller -le MAX_CONTROLLER -a $num_ext_fs -gt 0 ]
	do
		if /usr/sbin/ssaadm display c${controller} > /dev/null 2>&1
		then
			target=0
			MAX_TARGET=5
			while [ target -le MAX_TARGET -a $num_ext_fs -gt 0 ]
			do
				disk=0
				MAX_DISK=6
				while [ disk -le MAX_DISK -a $num_ext_fs -gt 0 ]
				do
				  if /usr/sbin/ssaadm display \
				  /dev/rdsk/c${controller}t${target}d${disk}s0 \
				  > /dev/null 2>&1
				  then
				    (( stabmem2_rqd+=stabmem2_per_ssatray ))
				    (( num_ext_fs-=1 ))
				    break
				  fi
				  (( disk+=1 ))
				done
				(( target+=1 ))
			done
		fi
		(( controller+=1 ))
	done
}

#
# Set tuning parameters for a single file system on the internal disk array
#
if ((mem_size>=512))
then
	(( ninode_rqd=72000 ))
	(( ncsize_rqd=72000 ))
	(( bufhwm_rqd=48000 ))
	(( nfsd_rqd=96 ))
	(( stabmem1_rqd=72*meg ))
elif ((mem_size>=384))
then
	(( ninode_rqd=64000 ))
	(( ncsize_rqd=64000 ))
	(( bufhwm_rqd=36000 ))
	(( nfsd_rqd=64 ))
	(( stabmem1_rqd=72*meg ))
elif ((mem_size>=256))
then
	(( ninode_rqd=48000 ))
	(( ncsize_rqd=48000 ))
	(( bufhwm_rqd=24000 ))
	(( nfsd_rqd=64 ))
	(( stabmem1_rqd=48*meg ))
elif ((mem_size>=128))
then
	(( ninode_rqd=24000 ))
	(( ncsize_rqd=24000 ))
	(( bufhwm_rqd=24000 ))
	(( nfsd_rqd=16 ))
	(( stabmem1_rqd=32*meg ))
elif ((mem_size>=64))
then
	(( ninode_rqd=12000 ))
	(( ncsize_rqd=12000 ))
	(( bufhwm_rqd=24000 ))
	(( nfsd_rqd=16 ))
	(( stabmem1_rqd=4*meg ))
else
	(( ninode_rqd=12000 ))
	(( ncsize_rqd=12000 ))
	(( bufhwm_rqd=24000 ))
	(( nfsd_rqd=16 ))
	(( stabmem1_rqd=2*meg ))
fi

#
# On non-150's, due to limited number of SBus slots for NVRAM,
# the requirement for NVRAM needs to be reduced.
#
if (( !on150 )); then
	if (( mem_size > 512 )); then
		(( stabmem1_rqd=32*meg ))
	else
		(( stabmem1_rqd=16*meg ))
	fi
fi

#
# Determines amount of stable memory for Presto lower needed by each type
# of metadevice.
#
set_stabmem2_per_md	

if (( raid_level==5 )) && [ -s ${NETRA_FSTAB} ]; then
	#
	# We depend on netra_fstab listing at least as many file systems
	# as there are external disk arrays (plus a fs for internal disks
	# in the case of a breadbox system).
	#
	# Note that if an external disk array is attached, the tuning 
	# algorithms size for a system with 512MB of memory regardless of
	# the amount of memory actually installed.
	#
	(( num_fs=`/usr/bin/cat ${NETRA_FSTAB} |\
		/usr/bin/egrep -ve '(^#|^[	]*#|^[	]*$)' |\
		/bin/wc | /usr/bin/awk '{ print $1 }'` ))

	#
	# On non-150 platforms, there is no internal disk array. All
	# filesystems are external, either in external combo, or in
	# SSAs. Note that in case of 150, we need to assign initial
	# tuning values for the internal disk array.
	#
	if (( on150 )); then
		(( num_ext_fs=num_fs-1 ))
		(( stabmem2_rqd=stabmem2_per_combo ))
	else
		(( num_ext_fs=num_fs ))
		(( stabmem2_rqd=0 ))
	fi

	if [ $num_ext_fs -gt 0 ]; then
		#
		# Adjust tuning parameters for file systems on external 
		# disk arrays.
		#

		# Check for SSAs	
		tunenfs_ssa_check

		if [ $num_ext_fs -gt 0 ]
		then
			# Our search for SSAs didn't account for all the file 
			# systems, so add tuning for the leftover
			# filesystems, which has to be external combos.
			(( stabmem2_rqd+=num_ext_fs*stabmem2_per_combo ))
		fi
	fi
elif (( raid_level==5 )); then
	#
	# tunenfs is called before any metadevice is created.
	# Assume minimum for all platforms, i.e. one combo.
	#
	(( stabmem2_rqd=stabmem2_per_combo ))
else
	#
	# No use of Presto lower except on RAID-5 systems.
	#
	(( stabmem2_rqd=0 ))
fi

#
# Make sure we don't ask for too much stable memory for
# PrestoServe. If so, adjust in some way.
#
adjust_stabmem_rqd


#
# Ensure consistency of properties wrt. platform,
# if we're not running with -c option.
#
if [[ "$checkopt" != 1 ]];
then
	# Remove incorrect properties
	if /usr/bin/grep -v "^[	]*#" $ROOTNEX_CONF |\
		/usr/bin/grep "$unwanted_prop" >/dev/null 2>&1;
	then
		/usr/bin/cat $ROOTNEX_CONF | \
		/usr/bin/grep -v "^[    ]*[^#]*$unwanted_prop" > $TMP_ROOTNEX

		/usr/bin/cp $TMP_ROOTNEX $ROOTNEX_CONF >/dev/null
		/usr/bin/rm -f $TMP_ROOTNEX >/dev/null
		/usr/bin/touch $TMP_ROOTNEX
		to_save=1
	fi

	# Add dump device property on 150's, if not exist
	if (( on150 )) &&
		! /usr/bin/grep -v "^[	]*#" $ROOTNEX_CONF |\
		/usr/bin/grep "$dumpdev_prop_name" >/dev/null;
	then
		echo "${dumpdev_prop_name}=\"${dumpdev_name}\";">>$ROOTNEX_CONF
		to_save=1
	fi

	if [[ $to_save = 1 ]]; then
		$SAVECONF >/dev/null 2>&1    
	fi	
fi

#
# Get the current tuning parameters.
#
ninode_cur=`/usr/bin/grep 'set[ 	]*ufs_ninode=' $ETC_SYSTEM | \
		/usr/bin/grep -v '^#' | /usr/bin/grep -v '^[ 	]*#' | \
		/usr/bin/head -1 | /usr/bin/awk -F"=" '{ print $2 }' | \
		/usr/bin/sed 's/[ 	]//g'`
ncsize_cur=`/usr/bin/grep 'set[ 	]*ncsize=' $ETC_SYSTEM | \
		/usr/bin/grep -v '^#' | /usr/bin/grep -v '^[ 	]*#' | \
		/usr/bin/head -1 | /usr/bin/awk -F"=" '{ print $2 }' | \
		/usr/bin/sed 's/[ 	]//g'`
bufhwm_cur=`/usr/bin/grep 'set[ 	]*bufhwm=' $ETC_SYSTEM | \
		/usr/bin/grep -v '^#' | /usr/bin/grep -v '^[ 	]*#' | \
		/usr/bin/head -1 | /usr/bin/awk -F"=" '{ print $2 }' | \
		/usr/bin/sed 's/[ 	]//g'`

nfsd_cur=$(/usr/bin/nawk '$0 ~ /.usr.lib.nfs.nfsd/ {print $NF; exit}' $NFS_RC)

stabmem1_cur=""
stabmem2_cur=""
if [ -f ${ROOTNEX_CONF} ]
then
        stabmem1_cur=`/usr/bin/fgrep "$nvmem1_prop_name=" $ROOTNEX_CONF | \
		/usr/bin/grep -v '^#' | /usr/bin/grep -v '^[ 	]*#' | \
		/usr/bin/head -1 | /usr/bin/awk -F"=" '{ print $2 }' | \
		/usr/bin/sed 's/;//g' | /usr/bin/sed 's/[ 	]//g'`

	#
	# $nvmem2_prop_name may be the second property on this line of
	# the rootnex.conf file.
	#
	stabmem2_test=`/usr/bin/fgrep "$nvmem2_prop_name=" $ROOTNEX_CONF | \
		/usr/bin/grep -v '^#' | /usr/bin/grep -v '^[ 	]*#' | \
		/usr/bin/head -1 | /usr/bin/awk '{ print $2 }' | \
		/usr/bin/sed 's/;//g' | /usr/bin/sed 's/[ 	]//g'`
	stabmem2_tmp=`echo $stabmem2_test | /usr/bin/awk -F"=" '{ print $1 }'`
	
	if [ "$stabmem2_tmp" = "$nvmem2_prop_name" ]; then
		stabmem2_cur=`echo $stabmem2_test | \
			/usr/bin/awk -F"=" '{ print $2 }'`
	else
		stabmem2_cur=`/usr/bin/fgrep "$nvmem2_prop_name=" \
		$ROOTNEX_CONF | \
		/usr/bin/grep -v '^#' | /usr/bin/grep -v '^[ 	]*#' | \
		/usr/bin/head -1 | /usr/bin/awk -F"=" '{ print $2 }' | \
		/usr/bin/sed 's/;//g' | /usr/bin/sed 's/[ 	]//g'`
	fi
fi

#
# Print listing of attributes set by tunenfs, both current and new values
#

stabmem1_rqd_hex=`echo $stabmem1_rqd | /usr/bin/awk '{ printf "0x%x", $1 }'`
stabmem2_rqd_hex=`echo $stabmem2_rqd | /usr/bin/awk '{ printf "0x%x", $1 }'`

if [[ $listopt = 1 ]]; then
    print $nvmem1_prop_name '\t' ${stabmem1_cur:-0x0} '\t' $stabmem1_rqd_hex
    print $nvmem2_prop_name '\t' ${stabmem2_cur:-0x0} '\t' $stabmem2_rqd_hex
    print ufs_ninode '\t\t' $ninode_cur '\t\t' $ninode_rqd
    print ncsize '\t\t\t' $ncsize_cur '\t\t' $ncsize_rqd
    print bufhwm '\t\t\t' $bufhwm_cur '\t\t' $bufhwm_rqd
fi

#
# Decide whether we need to change any tuning values in the config files.
#

if [[   $ninode_cur = $ninode_rqd && \
        $ncsize_cur = $ncsize_rqd && \
        $bufhwm_cur = $bufhwm_rqd && \
        $stabmem1_cur = $stabmem1_rqd_hex && \
        $stabmem2_cur = $stabmem2_rqd_hex && \
	$nfsd_cur = $nfsd_rqd ]]
then
        diff_status=0
        exit ${diff_status}
else
        diff_status=1
 
        #
        # If checkopt is set, exit (with status) without changing config files.
	#
        if [[ $checkopt = 1 ]]
        then
                exit $diff_status
        fi
fi

#
# If any stabmem_cur is zero, it's possible that Presto has not been
# successfuly loaded before -- do reconfig boot, so to make sure Presto
# logical devices will be created.
#
if [[ "${stabmem1_cur:-0x0}" = "0x0" && "$stabmem1_rqd_hex" != "0x0" ]] ||
	[[ "${stabmem2_cur:-0x0}" = "0x0" && "$stabmem2_rqd_hex" != "0x0" ]];
then
	touch /reconfigure
fi

#
# tuning will change tunable values. Advise system reboot on main HTML page.
#
netra_main_page -r
 
ninode_rqd_line=`echo $ninode_rqd | \
	/usr/bin/awk '{ printf "set ufs_ninode=%s\n", $1 }'`
ncsize_rqd_line=`echo $ncsize_rqd | \
	/usr/bin/awk '{ printf "set ncsize=%s\n", $1 }'`
bufhwm_rqd_line=`echo $bufhwm_rqd | \
	/usr/bin/awk '{ printf "set bufhwm=%s\n", $1 }'`
stabmem1_rqd_line=`echo $nvmem1_prop_name $stabmem1_rqd_hex | \
	/usr/bin/awk '{ printf "%s=%s;\n", $1, $2 }'`
stabmem2_rqd_line=`echo $nvmem2_prop_name $stabmem2_rqd_hex | \
	/usr/bin/awk '{ printf "%s=%s;\n", $1, $2 }'`
 
/usr/bin/cat $ETC_SYSTEM |
while read line
do
        line_printed="no"
        if [[ $line = *set*ufs_ninode=* && \
              $line != \#*set*ufs_ninode=* ]]
        then
                if ((mem_size>=64))
                then
                        print "$ninode_rqd_line" >> $TMP_SYSTEM
                else
                        print "$line" >> $TMP_SYSTEM
                fi
                line_printed="yes"
        fi
        if [[ $line = *set*ncsize=* && \
              $line != \#*set*ncsize=* ]]
        then
                if ((mem_size>=64))
                then
                        print "$ncsize_rqd_line" >> $TMP_SYSTEM
                else
                        print "$line" >> $TMP_SYSTEM
                fi
                line_printed="yes"
        fi
        if [[ $line = *bufhwm=* && \
              $line != \#*bufhwm=* ]]
        then
                if ((mem_size>=64))
                then
                        print "$bufhwm_rqd_line" >> $TMP_SYSTEM
                else
                        print "$line" >> $TMP_SYSTEM
                fi
                line_printed="yes"
        fi
        if [[ $line_printed = no ]]
        then
                print "$line" >> $TMP_SYSTEM
        fi
done
 
#
# Check to see if entries for ninode, ncsize and bufhwm *DO NOT* exist in
# the /etc/system file. If they do not exist create new entries.
#
/usr/bin/fgrep "set ufs_ninode=" $TMP_SYSTEM | /usr/bin/grep -v "^#" > /dev/null
if [[ $? -ne 0 ]]
then
        if ((mem_size>=64))
        then
                print "$ninode_rqd_line" >> $TMP_SYSTEM
        fi
fi
 
/usr/bin/fgrep "set ncsize=" $TMP_SYSTEM | /usr/bin/grep -v "^#" > /dev/null
if [[ $? -ne 0 ]]
then
       if ((mem_size>=64))
       then
                print "$ncsize_rqd_line" >> $TMP_SYSTEM
       fi
 
fi
 
/usr/bin/fgrep "set bufhwm=" $TMP_SYSTEM | /usr/bin/grep -v "^#" > /dev/null
if [[ $? -ne 0 ]]
then
        if ((mem_size>=64))
        then
                print "$bufhwm_rqd_line" >> $TMP_SYSTEM
        fi
fi
 
# Set the stable memory region size for the Presto Upper cache in the
# rootnex.conf file.
#
 
if [ -f ${ROOTNEX_CONF} ]
then
	stabmem_printed=FALSE
        /usr/bin/cat $ROOTNEX_CONF |
        while read entire_line
        do
                if [[ $entire_line = *$nvmem1_prop_name=* &&
			$entire_line != \#*$nvmem1_prop_name=* ||
                	$entire_line = *$nvmem2_prop_name=* &&
			$entire_line != \#*$nvmem2_prop_name=* ]]
                then
			if [ $stabmem_printed = TRUE ]; then
				continue
			fi

                        if ((mem_size>=64))
                        then
				# Print all stabmem parameters, grouping them 
				# together.
                        	echo "$stabmem1_rqd_line" >>$TMP_ROOTNEX
                        	echo "$stabmem2_rqd_line" >>$TMP_ROOTNEX
				stabmem_printed=TRUE
                        else
                                echo "$entire_line" >> $TMP_ROOTNEX
                        fi
		else
			print "$entire_line" >> $TMP_ROOTNEX
                fi
        done
 
	#
	# Handle the case where there were no stabmem-regX-size properties
	# defined in the rootnex.conf file.
	#
	if [ "$stabmem1_cur" = "" -a "$stabmem2_cur" = "" ]
	then
                if ((mem_size>=64))
                then
			echo "$stabmem1_rqd_line" >> $TMP_ROOTNEX
			echo "$stabmem2_rqd_line" >> $TMP_ROOTNEX
		fi
	fi
fi
 
/usr/bin/cp $ETC_SYSTEM $BACKUP_SYSTEM > /dev/null
/usr/bin/cp $TMP_SYSTEM $ETC_SYSTEM
/usr/bin/rm -rf $TMP_SYSTEM >/dev/null
 
if [ -f ${ROOTNEX_CONF} ]
then
        /usr/bin/cp $ROOTNEX_CONF $BACKUP_ROOTNEX > /dev/null
        /usr/bin/cp $TMP_ROOTNEX $ROOTNEX_CONF
        /usr/bin/rm -rf $TMP_ROOTNEX > /dev/null
fi
 
# Make sure TMP_ROOTNEX is removed as its been touched in the
# very beginning
#
/usr/bin/rm -rf $TMP_ROOTNEX > /dev/null

# Tune the number of nfs daemons
#
if [[ $nfsd_cur != $nfsd_rqd ]]
then
	/usr/bin/ed $NFS_RC << END_NFSRC >/dev/null 2>&1
/^..*usr.lib.nfs.nfsd..*$/s//		\/usr\/lib\/nfs\/nfsd -a $nfsd_rqd/
w
q
END_NFSRC
fi

$SAVECONF >/dev/null 2>&1 &
exit ${diff_status}
