#!/bin/sh
#
# ktune		ktune sysctls
#
# chkconfig: - 05 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/ktune
else
    echo -n $"$0: /etc/sysconfig/ktune does not exist."; warning; echo
    exit 5
fi

VAR_SUBSYS_KTUNE=/var/lock/subsys/ktune
SAVE_FILE="/var/run/ktune.save"
SYSCTL_LOAD="/sbin/sysctl -q -e -p "
SYSCTL_GET="/sbin/sysctl -e"

KERNEL_ELEVATOR="cfq"
/bin/fgrep -q 'elevator=' /proc/cmdline
CMDLINE_ELEVATOR=$?
SYS_BLOCK_SDX=$(eval ls ${ELEVATOR_TUNE_DEVS} 2>/dev/null)

start() {
    ret=0

    # Do not save and apply if there is no config file.
    if [ -r "$SYSCTL" ]; then
	keys=$(cat "$SYSCTL" | grep -v '^#' | grep '=' | cut -d "=" -f 1)

	# save current sysctl settings for ktune keys
	echo -n $"Saving current sysctl settings: "
	rm -f "$SAVE_FILE"
	ret_s=0
	for key in $keys; do
	    $SYSCTL_GET $key >> "$SAVE_FILE"
	    let ret_s+=$?
	done
	if [ $ret_s -eq 0 ]; then
	    success; echo
	else
	    failure; echo; ret=1
	fi

	# apply ktune settings
	echo -n $"Applying ktune sysctl settings from $SYSCTL: "
	$SYSCTL_LOAD "$SYSCTL"
	if [ $? -eq 0 ]; then
	    success; echo
	else
	    failure; echo; ret=1
	fi

	# apply general sysctl settings
	if [ -r "$SYSCTL_POST" ]; then
	    echo -n $"Applying sysctl settings from $SYSCTL_POST: "
	    $SYSCTL_LOAD "$SYSCTL_POST"
	    if [ $? -eq 0 ]; then
		success; echo
	    else
		failure; echo; ret=1
	    fi
	fi
    fi


    # if no elevator on command line, apply the ktune elevator
    if [ -n "$ELEVATOR" -a -n "$SYS_BLOCK_SDX" -a !$CMDLINE_ELEVATOR ]; then
	ret_e=0
	echo -n $"Applying ${ELEVATOR} elevator: "
	for i in $SYS_BLOCK_SDX; do
	    dev="${i%%/queue*}"; echo -n "${dev##*/} "
	    echo "$ELEVATOR" > "$i" 2>/dev/null;
	    let ret_e+=$?
	done
	if [ $ret_e -eq 0 ]; then
	    success; echo
	else
	    failure; echo; ret=1
	fi
    fi
    
    touch $VAR_SUBSYS_KTUNE
    return $ret
}

stop() {
    ret=0

    if [ -r "$SAVE_FILE" ]; then
	echo -n $"Reverting to saved sysctl settings: "
	$SYSCTL_LOAD "$SAVE_FILE"
	if [ $? -eq 0 ]; then
	    success; echo
	else
	    failure; echo; ret=1
	fi
	/bin/rm -f "$SAVE_FILE"
    fi

    # if no elevator on command line, apply the default elevator
    if [ -n "$ELEVATOR" -a -n "$SYS_BLOCK_SDX" -a !$CMDLINE_ELEVATOR ]; then
	ret_e=0
	echo -n $"Reverting to ${KERNEL_ELEVATOR} elevator: "
	for i in $SYS_BLOCK_SDX; do
	    dev="${i%%/queue*}"; echo -n "${dev##*/} "
	    echo "$KERNEL_ELEVATOR" > "$i" 2>/dev/null;
	    let ret_e+=$?
	done
	if [ $ret_e -eq 0 ]; then
	    success; echo
	else
	    failure; echo; ret=1
	fi
    fi

    rm -f $VAR_SUBSYS_KTUNE
    return $ret
}

status() {
    if [ ! -f "$VAR_SUBSYS_KTUNE" ]; then
	echo $"ktune stettings are not applied."
	return 3
    fi

    echo $"Current ktune sysctl stettings:"
    keys=$(cat "$SYSCTL" | grep -v '^#' | grep '=' | cut -d "=" -f 1)
    for key in $keys; do
	$SYSCTL_GET $key
    done

    if [ -n "$ELEVATOR" -a -n "$SYS_BLOCK_SDX" -a !$CMDLINE_ELEVATOR ]; then
	echo
	echo $"Current elevator stettings:"
	for i in $SYS_BLOCK_SDX; do
	    echo -n "${i}:"; cat "$i"
	done
    fi

    return 0
}

case "$1" in
    start)
	[ -f "$VAR_SUBSYS_KTUNE" ] && exit 0
	start
	RETVAL=$?
	;;
    stop)
	[ -f "$VAR_SUBSYS_KTUNE" ] || exit 0
	stop
	RETVAL=$?
	;;
    restart|force-reload)
	[ -f "$VAR_SUBSYS_KTUNE" ] && stop
	start
	RETVAL=$?
	;;
    condrestart|try-restart)
	[ -f "$VAR_SUBSYS_KTUNE" ] || exit 0
	stop
	start
	RETVAL=$?
	;;
    status)
	status
	RETVAL=$?
	;;
    *)
	echo $"Usage: $0 {start|stop|restart|condrestart|status}"
	RETVAL=2
	;;
esac

exit $RETVAL
