Rev 58 | Blame | Compare with Previous | Last modification | View Log | RSS feed
#!/bin/sh## ktune ktune sysctls## chkconfig: - 27 99# description: Applies and reverts ktune sysctl settings## config: /etc/sysctl.ktune# config: /etc/sysconfig/ktune## Source function library.. /etc/init.d/functions# Source ktune configureation.if [ -f /etc/sysconfig/ktune ] ; then. /etc/sysconfig/ktuneelseecho -n $"$0: /etc/sysconfig/ktune does not exist."; warning; echoexit 5fiVAR_SUBSYS_KTUNE=/var/lock/subsys/ktuneSAVE_FILE="/var/run/ktune.save"SYSCTL_SET="/sbin/sysctl -q -w "SYSCTL_GET="/sbin/sysctl -e"RESTART=KERNEL_ELEVATOR="cfq"/bin/fgrep -q 'elevator=' /proc/cmdlineCMDLINE_ELEVATOR=$?if [ -n "$ELEVATOR_TUNE_DEVS" ]; thenSYS_BLOCK_SDX=for file in $(eval LANG=C /bin/ls -1d "${ELEVATOR_TUNE_DEVS}" 2>/dev/null); doif ! expr match $(readlink -f "$file") '^/sys/.*/queue/scheduler$' >/dev/null; thenprintf $"$0: '%s' is not a scheduler control file." "$file"warning; echoelseSYS_BLOCK_SDX="$SYS_BLOCK_SDX $file"fidoneelseSYS_BLOCK_SDX=fideclare -a KTUNE_FILESif [ -r "$SYSCTL" ]; thenKTUNE_FILES[0]="$SYSCTL"fiif [ "x$USE_KTUNE_D" = "xyes" ]; thenwhile read file; do[ -r "$file" ] || continueKTUNE_FILES[${#KTUNE_FILES[*]}]="${file}"done <<EOF$(LANG=C /bin/ls -1 /etc/ktune.d/*.conf 2>/dev/null)EOFfifunction is_int{[ ! -z "${1##*[^0-9]*}" ]}load_file() {local file=$1local save=$2local curr_valueret_s=0errors=# apply ktune settingswhile read line; do[ "${line:0:1}" == "#" -o "${line:0:1}" == ";" ] && continue[ -z "$line" ] && continuekey=$(echo $line | awk '{ match($0,"[ \t]*=[ \t]*") && $0=substr($0,1,RSTART-1); print $0}')value=$(echo $line | awk '/=/ { match($0,"[ \t]*=[ \t]*"); $0=substr($0,RSTART+RLENGTH); print $0}')curr_value=""if [ "${value:0:1}" = '>' ]; thenvalue=${value:1}curr_value=$($SYSCTL_GET -n "$key")fiif [ "${value:0:1}" = '$' ]; thenvalue=${value:1}value=${!value}fiis_int "$curr_value" && is_int "$value" && [ "$value" -lt "$curr_value" ] && value="$curr_value"[ $save -ne 0 ] && save_value=$($SYSCTL_GET "$key")setting=$keyif [ "$value" ]; then[ -n "$value" ] && setting+="=${value}"error="$($SYSCTL_SET "${setting}" 2>&1)"ret=$?if [ $save -ne 0 -a $ret -eq 0 ] && ! grep -q "^${key} =" $SAVE_FILE 2>/dev/null; thenecho "$save_value" >> "$SAVE_FILE"fi[ -n "$error" ] && errors+="${error}\n"let ret_s+=$retfidone < "$file"if [ $ret_s -eq 0 ]; thensuccess; echoelsefailure; echo; ret=1echo -e "$errors" | while read line; do echo -e " ${line}"; donefi}call_script() {local script=$1shiftif [ -x "$script" ]; thenecho -n "Calling '$script $@': "("$script" "$@")ret=$?if [ $ret -eq 0 ]; thensuccess; echoelsefailure; echo; ret=1fifi}load_sysctl() {# Remove save file/bin/rm -f "$SAVE_FILE"if [ ${#KTUNE_FILES[@]} -gt 0 ]; then# Apply general sysctl settings beforehandfor f in $SYSCTL_PRE; doif [ -r "$f" ]; thenecho $"Applying sysctl settings from $f"sysctl -e -p "$f" >/dev/null 2>&1fidoneecho $"Applying ktune sysctl settings:"# Apply ktune files.for file in "${KTUNE_FILES[@]}"; doecho -n $"$file: "load_file "$file" 1call_script "${file%.conf}.sh" startdone# Apply general sysctl settings afterwardsfor f in $SYSCTL_POST; doif [ -r "$f" ]; thenecho $"Applying sysctl settings from $f"sysctl -e -p "$f" >/dev/null 2>&1fidonefi}revert_sysctl() {if [ -r "$SAVE_FILE" ]; thenif [ "$KTUNE_REVERT_SYSCTL" = 1 ]; thenecho -n $"Reverting to saved sysctl settings: "load_file "$SAVE_FILE" 0fi/bin/rm -f "$SAVE_FILE"fiif [ ${#KTUNE_FILES[@]} -gt 0 ]; thenfor file in "${KTUNE_FILES[@]}"; docall_script "${file%.conf}.sh" stop ${RESTART:+restart}donefi}start() {[ "$EUID" != "0" ] && exit 4ret=0# if no elevator on command line, apply the ktune elevatorif [ -n "$ELEVATOR" -a -n "$SYS_BLOCK_SDX" -a $CMDLINE_ELEVATOR -ne 0 ]; thenret_e=0echo -n $"Applying ${ELEVATOR} elevator: "for i in $SYS_BLOCK_SDX; dodev="${i%%/queue*}"; echo -n "${dev##*/} "echo "$ELEVATOR" > "$i" 2>/dev/null;let ret_e+=$?doneif [ $ret_e -eq 0 ]; thensuccess; echoelsefailure; echo; ret=1fifiload_sysctl/bin/touch $VAR_SUBSYS_KTUNEreturn $ret}reload() {[ "$EUID" != "0" ] && exit 4revert_sysctlload_sysctl}stop() {[ "$EUID" != "0" ] && exit 4ret=0revert_sysctl# if no elevator on command line, apply the default elevatorif [ -n "$ELEVATOR" -a -n "$SYS_BLOCK_SDX" -a $CMDLINE_ELEVATOR -ne 0 ]; thenret_e=0echo -n $"Reverting to ${KERNEL_ELEVATOR} elevator: "for i in $SYS_BLOCK_SDX; dodev="${i%%/queue*}"; echo -n "${dev##*/} "echo "$KERNEL_ELEVATOR" > "$i" 2>/dev/null;let ret_e+=$?doneif [ $ret_e -eq 0 ]; thensuccess; echoelsefailure; echo; ret=1fifi/bin/rm -f $VAR_SUBSYS_KTUNEreturn $ret}status() {[ "$EUID" != "0" ] && exit 4if [ ! -f "$VAR_SUBSYS_KTUNE" ]; thenecho $"ktune settings are not applied."return 3fiecho $"Current ktune sysctl settings:"if [ -e $SAVE_FILE ]; thenkeys=$(awk '/^[#;]/ { next } /=/ { match($1,"=") && $1=substr($1,1,RSTART-1); print $1 }' "$SAVE_FILE")for key in $keys; do$SYSCTL_GET $keydoneelseecho $"None"fiif [ -n "$ELEVATOR" -a -n "$SYS_BLOCK_SDX" -a $CMDLINE_ELEVATOR -ne 0 ]; thenechoecho $"Current elevator settings:"for i in $SYS_BLOCK_SDX; doecho -n "${i}: "; cat "$i"donefireturn 0}case "$1" instart)[ -f "$VAR_SUBSYS_KTUNE" ] && exit 0startRETVAL=$?;;stop)[ -f "$VAR_SUBSYS_KTUNE" ] || exit 0stopRETVAL=$?;;reload)[ -f "$VAR_SUBSYS_KTUNE" ] && reloadRETVAL=$?;;restart|force-reload)[ -f "$VAR_SUBSYS_KTUNE" ] && stopstartRETVAL=$?;;condrestart|try-restart)[ -f "$VAR_SUBSYS_KTUNE" ] || exit 0RESTART=1stopstartRETVAL=$?;;status)statusRETVAL=$?;;*)echo $"Usage: $0 {start|stop|restart|condrestart|status}"RETVAL=2;;esacexit $RETVAL