#!/bin/sh # mkkernel # v1.6, 1995. # Tunes the kernel parameters listed in the file "./settings" # This file must have the format PARAMETER VALUE # eg : # MAXUP 2000 # shell, softscreen, etc. # Updates /etc/conf/mtune.d/* and /etc/conf/cf.d/mtune files if maximum # allowable values require updating; keeps default, min values as before trap 'echo "`basename $0` : Interrupted" ; exit 1' 1 2 15 echo " Kernel Tuning Utility" echo " Intel DRS/NX, Intel UnixWare, SPARC MPlus" echo echo " === S G Parker and D A Dallas ===" echo check_root() { if [ "$LOGNAME" -ne "root" ] then echo "You must be super-user to run this script." exit 1 fi } init_vars() { USAGE="Usage : mkkernel [-f ] [-e] [-l] [-y] [-v]" EXACT="NO" VERBOSE="NO" LIST="NO" REBUILD="NO" SETTINGSFILE=settings while getopts 'eylf:?' c do case $c in e) EXACT="YES" ;; f) SETTINGSFILE=$OPTARG;; y) REBUILD="YES" ;; v) VERBOSE="YES" ;; l) LIST="YES" ;; \?) echo $USAGE echo "Where contains desired values, -e tunes exactly to that value" echo " default is to tune to at least the given value ie, never tune down" exit 2;; esac done } check_settingsfile() { echo "Checking $SETTINGSFILE ..." if [ ! -r $SETTINGSFILE ] then echo "ERROR: Cannot read settings file $SETTINGSFILE" exit 1 fi } strip_comments() { rm /tmp/settings.$$ > /dev/null 2>&1 while i=`line` # Remove comments and do # blank lines, convert j=`echo $i|awk '{ print $1 }' | cut -c1` # all to upper case if [ "$j" != "#" ] && [ "$j" != "" ] then echo $i | tr '[a-z]' '[A-Z]' >> /tmp/settings fi done < $SETTINGSFILE SETTINGSFILE=/tmp/settings.$$ } check_arch() { echo "Checking `uname -n` architecture ..." BASE='/etc/conf' # Default to Intel ARCH="INTEL" if [ "`uname -p`" = "sparc" ] then case "`uname -v`" in ?MPlus.*) ARCH="MPLUS" ;; *) BASE='/etc/master.d' # SPARC non-MPlus ARCH="SPARC" # settings. ;; esac fi if [ "$ARCH" = "SPARC" ] then echo "This script is for SPARC MPlus and Intel machines only" echo "`uname -n` architecture is $ARCH" exit 1 fi echo "`uname -n` architecture is $ARCH" # Confirm to user } list_vars() { if [ "$LIST" = "YES" ] then echo "`uname -n`: Current kernel parameters" ls -l /stand/`kernel` while i=`line` do PARAM=`echo $i | awk '{ print $1 }'` VALUE=`echo $i | awk '{ print $2 }'` echo "$PARAM: " > /tmp/param.out /etc/conf/bin/idtune -g $PARAM >> /tmp/param.out 2>&1 # Skip if parameter cat /tmp/param.out|tr '\012' ' ' > /tmp/out.param if [ $? -ne "0" ] # does not exist then echo "`basename $0`: Skipping nonexistant parameter '$PARAM'" else PRESENT=`cat /tmp/out.param|awk '{ print $2 }'` if [ "$VALUE" -gt "$PRESENT" ] then echo " (> to $VALUE)" >> /tmp/out.param elif [ "$VALUE" -lt "$PRESENT" ] then echo " (< to $VALUE)" >> /tmp/out.param else echo " (= `basename $SETTINGSFILE`)" >> /tmp/out.param fi cat /tmp/out.param fi done < $SETTINGSFILE fi } check_lock() { if [ ! -f /tmp/mkkernel.lock ] then echo "ERROR : Another copy of mkkernel is running on `uname -n`" ps -fp `cat /tmp/mkkernel.lock` if [ "$?" -ne "0" ] then # ps failed to find it echo "Failed to find process `cat /tmp/mkkernel.lock`" echo "This process may have terminated abnormally" echo "Please remove /tmp/mkkernel.lock if you are sure" echo "that there is no competing process" exit 2 fi else echo $$ > /tmp/mkkernel.lock echo "Process lock granted. Continuing ..." fi } make_backups() { echo "Creating backups of kernel files ..." mkdir /tmp/sgp > /dev/null 2>&1 # Create backups mkdir /tmp/sgp/mtune.d > /dev/null 2>&1 mkdir /tmp/sgp/cf.d > /dev/null 2>&1 cp -r $BASE/mtune.d/* /tmp/sgp/mtune.d cp -r $BASE/cf.d/mtune /tmp/sgp/cf.d/mtune } do_the_stuff() { while i=`line` do PARAM=`echo $i | awk '{ print $1 }'` VALUE=`echo $i | awk '{ print $2 }'` /etc/conf/bin/idtune -g $PARAM > /dev/null 2>&1 # Skip if parameter if [ $? -ne "0" ] # does not exist then echo "`basename $0`: Skipping nonexistant parameter '$PARAM'" else # this is a big "else" ... current_max=`/etc/conf/bin/idtune -g $PARAM | cut -f4` if [ "$current_max" -lt "$VALUE" ] then # fix the file if [ "$VERBOSE" = "YES" ] then echo "Fixing file $filename : max was $current_max" fi filename=`grep "^$PARAM" $BASE/mtune.d/*|cut -d: -f1` if [ $? -ne "0" ] then echo "ERROR: Cannot find $PARAM in $BASE/mtune.d/" exit 1 fi linenumber=`grep -n "^$PARAM" $filename | cut -d: -f1|head -1` thisline=`expr $linenumber - 1` # all lines before PARAM nextline=`expr $linenumber + 1` # all lines after PARAM head -$thisline $filename > /tmp/steve.tunefile if [ $? -ne "0" ] then echo "ERROR: Cannot write to /tmp directory" exit 1 fi current_values=`grep "^$PARAM" $filename|head -1` current_default=`echo $current_values|awk '{ print $2 }'` current_min=`echo $current_values|awk '{ print $3 }'` echo "$PARAM $current_default $current_min $VALUE" >> /tmp/steve.tunefile tail +$nextline $filename >> /tmp/steve.tunefile mv /tmp/steve.tunefile $filename # Place the edited file if [ $? -ne "0" ] # in the right place. then echo "ERROR: Cannot move /tmp/steve.tunefile to $filename" exit 1 fi filename='/etc/conf/cf.d/mtune' # this also needs editing linenumber=`grep -n "^$PARAM" $filename | cut -d: -f1` if [ $? -ne "0" ] then echo "ERROR: Cannot find $PARAM in $filename" exit 1 fi thisline=`expr $linenumber - 1` # see above nextline=`expr $linenumber + 1` head -$thisline $filename > /tmp/steve.tunefile if [ $? -ne "0" ] then echo "ERROR: Cannot write to /tmp" exit 1 fi echo "$PARAM $VALUE 0 $VALUE %%INS%%" >> /tmp/steve.tunefile tail +$nextline $filename >> /tmp/steve.tunefile mv /tmp/steve.tunefile $filename # move edited file to position if [ $? -ne "0" ] then echo "ERROR: Cannot move /tmp/steve.tunefile to $filename" exit 1 fi /etc/conf/bin/idtune -f $PARAM $VALUE if [ "$?" -ne "0" ] then echo "Still cannot tune $PARAM to $VALUE." fi fi # fix the file current_value=`/etc/conf/bin/idtune -g $PARAM|cut -f1` if [ $current_value -lt $VALUE ] then TUNEME="YES" else TUNEME="NO" fi if [ "$EXACT" = "YES" ] then TUNEME="YES" fi if [ "$TUNEME" = "YES" ] then echo Tuning $PARAM from $current_value to $VALUE /etc/conf/bin/idtune -f $PARAM $VALUE else echo $PARAM is already tuned to $current_value : will not tune to $VALUE fi fi # the "else" part of whether the parameter exists done < $SETTINGSFILE rm -f /tmp/mkkernel.lock # remove process lock } rebuild_kernel() { if [ "$REBUILD" = "NO" ] then echo "Rebuild and copy new kernel? (y/n) \c" read RESPONSE case "$RESPONSE" in "y"|"Y"|"Yes"|"yes") REBUILD="YES" ;; *) REBUILD="NO" ;; esac else echo "Called with the -y option; will rebuild and copy new kernel" fi if [ "$REBUILD" = "YES" ] then /etc/conf/bin/idbuild -K && /etc/conf/bin/idcpunix echo "The kernel has been tuned and rebuilt. Please reboot the machine" echo " to start using this new kernel." echo echo "/tmp/sgp/* contains backups of the previous kernel files." echo "Please note : /tmp/* will be deleted on a system reboot." fi rm /tmp/files.edited > /dev/null 2>&1 } ############## Main ################################# check_root init_vars check_arch check_settings strip_comments list_vars check_lock make_backups do_the_stuff rebuild_kernel echo "Done."