#! /bin/sh
# /etc/init.d/lupin-sysctl: Set kernel variables needed to improve
# robustness against hard reboot when / is loop-mounted.
#
# Written by Colin Watson <cjwatson@ubuntu.com> based on information from
# Agostino Russo <agostino.russo@gmail.com> and the procps init script by
# Elrond <Elrond@Wunder-Nett.org>.
#
### BEGIN INIT INFO
# Provides:		lupin-sysctl
# Required-Start:	mountkernfs procps
# Required-Stop:
# Default-Start:	S
# Default-Stop:
# Short-Description:	Kernel variables for loop-mount installations
### END INIT INFO

set -e

[ -r /etc/default/rcS ] || exit 0
. /etc/default/rcS
. /lib/lsb/init-functions

PATH="/sbin:$PATH"

type sysctl >/dev/null 2>&1 || exit 0
[ -f /etc/fstab ] || exit 0

exec 9<&0 </etc/fstab

host_device=
while read DEV MTPT FSTYPE OPTS REST
do
	case "$DEV" in
		""|\#*)
			continue
			;;
	esac
	case "$OPTS" in
		loop|*,loop|loop,*|*,loop,*)
			if [ "$MTPT" = / ]; then
				host_device="$DEV"
			fi
			;;
	esac
done

exec 0<&9 9<&-

[ -n "$host_device" ] || exit 0

case $1 in
	start|restart|force-reload)
		if [ "$VERBOSE" = yes ]; then
			quiet=
			log_begin_msg "Setting kernel variables for loop-mounting..."
		else
			quiet=-q
		fi
		CODE=0
		sysctl $quiet -w vm.dirty_background_ratio=0 || CODE=$?
		sysctl $quiet -w vm.dirty_ratio=40 || CODE=$?
		sysctl $quiet -w vm.dirty_expire_centisecs=2 || CODE=$?
		sysctl $quiet -w vm.dirty_writeback_centisecs=2 || CODE=$?
		if [ "$VERBOSE" = yes ]; then
			log_end_msg $CODE
		fi
		;;
	stop)
		;;
	*)
		echo "Usage: $0 {start|stop|force-reload|restart}"
		exit 1
		;;
esac

exit 0
