1===============================================
2How to conserve battery power using laptop-mode
3===============================================
4
5Document Author: Bart Samwel (bart@samwel.tk)
6
7Date created: January 2, 2004
8
9Last modified: December 06, 2004
10
11Introduction
12------------
13
14Laptop mode is used to minimize the time that the hard disk needs to be spun up,
15to conserve battery power on laptops. It has been reported to cause significant
16power savings.
17
18.. Contents
19
20   * Introduction
21   * Installation
22   * Caveats
23   * The Details
24   * Tips & Tricks
25   * Control script
26   * ACPI integration
27   * Monitoring tool
28
29
30Installation
31------------
32
33To use laptop mode, you don't need to set any kernel configuration options
34or anything. Simply install all the files included in this document, and
35laptop mode will automatically be started when you're on battery. For
36your convenience, a tarball containing an installer can be downloaded at:
37
38	http://www.samwel.tk/laptop_mode/laptop_mode/
39
40To configure laptop mode, you need to edit the configuration file, which is
41located in /etc/default/laptop-mode on Debian-based systems, or in
42/etc/sysconfig/laptop-mode on other systems.
43
44Unfortunately, automatic enabling of laptop mode does not work for
45laptops that don't have ACPI. On those laptops, you need to start laptop
46mode manually. To start laptop mode, run "laptop_mode start", and to
47stop it, run "laptop_mode stop". (Note: The laptop mode tools package now
48has experimental support for APM, you might want to try that first.)
49
50
51Caveats
52-------
53
54* The downside of laptop mode is that you have a chance of losing up to 10
55  minutes of work. If you cannot afford this, don't use it! The supplied ACPI
56  scripts automatically turn off laptop mode when the battery almost runs out,
57  so that you won't lose any data at the end of your battery life.
58
59* Most desktop hard drives have a very limited lifetime measured in spindown
60  cycles, typically about 50.000 times (it's usually listed on the spec sheet).
61  Check your drive's rating, and don't wear down your drive's lifetime if you
62  don't need to.
63
64* If you mount some of your ext3/reiserfs filesystems with the -n option, then
65  the control script will not be able to remount them correctly. You must set
66  DO_REMOUNTS=0 in the control script, otherwise it will remount them with the
67  wrong options -- or it will fail because it cannot write to /etc/mtab.
68
69* If you have your filesystems listed as type "auto" in fstab, like I did, then
70  the control script will not recognize them as filesystems that need remounting.
71  You must list the filesystems with their true type instead.
72
73* It has been reported that some versions of the mutt mail client use file access
74  times to determine whether a folder contains new mail. If you use mutt and
75  experience this, you must disable the noatime remounting by setting the option
76  DO_REMOUNT_NOATIME to 0 in the configuration file.
77
78
79The Details
80-----------
81
82Laptop mode is controlled by the knob /proc/sys/vm/laptop_mode. This knob is
83present for all kernels that have the laptop mode patch, regardless of any
84configuration options. When the knob is set, any physical disk I/O (that might
85have caused the hard disk to spin up) causes Linux to flush all dirty blocks. The
86result of this is that after a disk has spun down, it will not be spun up
87anymore to write dirty blocks, because those blocks had already been written
88immediately after the most recent read operation. The value of the laptop_mode
89knob determines the time between the occurrence of disk I/O and when the flush
90is triggered. A sensible value for the knob is 5 seconds. Setting the knob to
910 disables laptop mode.
92
93To increase the effectiveness of the laptop_mode strategy, the laptop_mode
94control script increases dirty_expire_centisecs and dirty_writeback_centisecs in
95/proc/sys/vm to about 10 minutes (by default), which means that pages that are
96dirtied are not forced to be written to disk as often. The control script also
97changes the dirty background ratio, so that background writeback of dirty pages
98is not done anymore. Combined with a higher commit value (also 10 minutes) for
99ext3 or ReiserFS filesystems (also done automatically by the control script),
100this results in concentration of disk activity in a small time interval which
101occurs only once every 10 minutes, or whenever the disk is forced to spin up by
102a cache miss. The disk can then be spun down in the periods of inactivity.
103
104If you want to find out which process caused the disk to spin up, you can
105gather information by setting the flag /proc/sys/vm/block_dump. When this flag
106is set, Linux reports all disk read and write operations that take place, and
107all block dirtyings done to files. This makes it possible to debug why a disk
108needs to spin up, and to increase battery life even more. The output of
109block_dump is written to the kernel output, and it can be retrieved using
110"dmesg". When you use block_dump and your kernel logging level also includes
111kernel debugging messages, you probably want to turn off klogd, otherwise
112the output of block_dump will be logged, causing disk activity that is not
113normally there.
114
115
116Configuration
117-------------
118
119The laptop mode configuration file is located in /etc/default/laptop-mode on
120Debian-based systems, or in /etc/sysconfig/laptop-mode on other systems. It
121contains the following options:
122
123MAX_AGE:
124
125Maximum time, in seconds, of hard drive spindown time that you are
126comfortable with. Worst case, it's possible that you could lose this
127amount of work if your battery fails while you're in laptop mode.
128
129MINIMUM_BATTERY_MINUTES:
130
131Automatically disable laptop mode if the remaining number of minutes of
132battery power is less than this value. Default is 10 minutes.
133
134AC_HD/BATT_HD:
135
136The idle timeout that should be set on your hard drive when laptop mode
137is active (BATT_HD) and when it is not active (AC_HD). The defaults are
13820 seconds (value 4) for BATT_HD  and 2 hours (value 244) for AC_HD. The
139possible values are those listed in the manual page for "hdparm" for the
140"-S" option.
141
142HD:
143
144The devices for which the spindown timeout should be adjusted by laptop mode.
145Default is /dev/hda. If you specify multiple devices, separate them by a space.
146
147READAHEAD:
148
149Disk readahead, in 512-byte sectors, while laptop mode is active. A large
150readahead can prevent disk accesses for things like executable pages (which are
151loaded on demand while the application executes) and sequentially accessed data
152(MP3s).
153
154DO_REMOUNTS:
155
156The control script automatically remounts any mounted journaled filesystems
157with appropriate commit interval options. When this option is set to 0, this
158feature is disabled.
159
160DO_REMOUNT_NOATIME:
161
162When remounting, should the filesystems be remounted with the noatime option?
163Normally, this is set to "1" (enabled), but there may be programs that require
164access time recording.
165
166DIRTY_RATIO:
167
168The percentage of memory that is allowed to contain "dirty" or unsaved data
169before a writeback is forced, while laptop mode is active. Corresponds to
170the /proc/sys/vm/dirty_ratio sysctl.
171
172DIRTY_BACKGROUND_RATIO:
173
174The percentage of memory that is allowed to contain "dirty" or unsaved data
175after a forced writeback is done due to an exceeding of DIRTY_RATIO. Set
176this nice and low. This corresponds to the /proc/sys/vm/dirty_background_ratio
177sysctl.
178
179Note that the behaviour of dirty_background_ratio is quite different
180when laptop mode is active and when it isn't. When laptop mode is inactive,
181dirty_background_ratio is the threshold percentage at which background writeouts
182start taking place. When laptop mode is active, however, background writeouts
183are disabled, and the dirty_background_ratio only determines how much writeback
184is done when dirty_ratio is reached.
185
186DO_CPU:
187
188Enable CPU frequency scaling when in laptop mode. (Requires CPUFreq to be setup.
189See Documentation/admin-guide/pm/cpufreq.rst for more info. Disabled by default.)
190
191CPU_MAXFREQ:
192
193When on battery, what is the maximum CPU speed that the system should use? Legal
194values are "slowest" for the slowest speed that your CPU is able to operate at,
195or a value listed in /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies.
196
197
198Tips & Tricks
199-------------
200
201* Bartek Kania reports getting up to 50 minutes of extra battery life (on top
202  of his regular 3 to 3.5 hours) using a spindown time of 5 seconds (BATT_HD=1).
203
204* You can spin down the disk while playing MP3, by setting disk readahead
205  to 8MB (READAHEAD=16384). Effectively, the disk will read a complete MP3 at
206  once, and will then spin down while the MP3 is playing. (Thanks to Bartek
207  Kania.)
208
209* Drew Scott Daniels observed: "I don't know why, but when I decrease the number
210  of colours that my display uses it consumes less battery power. I've seen
211  this on powerbooks too. I hope that this is a piece of information that
212  might be useful to the Laptop Mode patch or its users."
213
214* In syslog.conf, you can prefix entries with a dash `-` to omit syncing the
215  file after every logging. When you're using laptop-mode and your disk doesn't
216  spin down, this is a likely culprit.
217
218* Richard Atterer observed that laptop mode does not work well with noflushd
219  (http://noflushd.sourceforge.net/), it seems that noflushd prevents laptop-mode
220  from doing its thing.
221
222* If you're worried about your data, you might want to consider using a USB
223  memory stick or something like that as a "working area". (Be aware though
224  that flash memory can only handle a limited number of writes, and overuse
225  may wear out your memory stick pretty quickly. Do _not_ use journalling
226  filesystems on flash memory sticks.)
227
228
229Configuration file for control and ACPI battery scripts
230-------------------------------------------------------
231
232This allows the tunables to be changed for the scripts via an external
233configuration file
234
235It should be installed as /etc/default/laptop-mode on Debian, and as
236/etc/sysconfig/laptop-mode on Red Hat, SUSE, Mandrake, and other work-alikes.
237
238Config file::
239
240  # Maximum time, in seconds, of hard drive spindown time that you are
241  # comfortable with. Worst case, it's possible that you could lose this
242  # amount of work if your battery fails you while in laptop mode.
243  #MAX_AGE=600
244
245  # Automatically disable laptop mode when the number of minutes of battery
246  # that you have left goes below this threshold.
247  MINIMUM_BATTERY_MINUTES=10
248
249  # Read-ahead, in 512-byte sectors. You can spin down the disk while playing MP3/OGG
250  # by setting the disk readahead to 8MB (READAHEAD=16384). Effectively, the disk
251  # will read a complete MP3 at once, and will then spin down while the MP3/OGG is
252  # playing.
253  #READAHEAD=4096
254
255  # Shall we remount journaled fs. with appropriate commit interval? (1=yes)
256  #DO_REMOUNTS=1
257
258  # And shall we add the "noatime" option to that as well? (1=yes)
259  #DO_REMOUNT_NOATIME=1
260
261  # Dirty synchronous ratio.  At this percentage of dirty pages the process
262  # which
263  # calls write() does its own writeback
264  #DIRTY_RATIO=40
265
266  #
267  # Allowed dirty background ratio, in percent.  Once DIRTY_RATIO has been
268  # exceeded, the kernel will wake flusher threads which will then reduce the
269  # amount of dirty memory to dirty_background_ratio.  Set this nice and low,
270  # so once some writeout has commenced, we do a lot of it.
271  #
272  #DIRTY_BACKGROUND_RATIO=5
273
274  # kernel default dirty buffer age
275  #DEF_AGE=30
276  #DEF_UPDATE=5
277  #DEF_DIRTY_BACKGROUND_RATIO=10
278  #DEF_DIRTY_RATIO=40
279  #DEF_XFS_AGE_BUFFER=15
280  #DEF_XFS_SYNC_INTERVAL=30
281  #DEF_XFS_BUFD_INTERVAL=1
282
283  # This must be adjusted manually to the value of HZ in the running kernel
284  # on 2.4, until the XFS people change their 2.4 external interfaces to work in
285  # centisecs. This can be automated, but it's a work in progress that still
286  # needs# some fixes. On 2.6 kernels, XFS uses USER_HZ instead of HZ for
287  # external interfaces, and that is currently always set to 100. So you don't
288  # need to change this on 2.6.
289  #XFS_HZ=100
290
291  # Should the maximum CPU frequency be adjusted down while on battery?
292  # Requires CPUFreq to be setup.
293  # See Documentation/admin-guide/pm/cpufreq.rst for more info
294  #DO_CPU=0
295
296  # When on battery what is the maximum CPU speed that the system should
297  # use? Legal values are "slowest" for the slowest speed that your
298  # CPU is able to operate at, or a value listed in:
299  # /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies
300  # Only applicable if DO_CPU=1.
301  #CPU_MAXFREQ=slowest
302
303  # Idle timeout for your hard drive (man hdparm for valid values, -S option)
304  # Default is 2 hours on AC (AC_HD=244) and 20 seconds for battery (BATT_HD=4).
305  #AC_HD=244
306  #BATT_HD=4
307
308  # The drives for which to adjust the idle timeout. Separate them by a space,
309  # e.g. HD="/dev/hda /dev/hdb".
310  #HD="/dev/hda"
311
312  # Set the spindown timeout on a hard drive?
313  #DO_HD=1
314
315
316Control script
317--------------
318
319Please note that this control script works for the Linux 2.4 and 2.6 series (thanks
320to Kiko Piris).
321
322Control script::
323
324  #!/bin/bash
325
326  # start or stop laptop_mode, best run by a power management daemon when
327  # ac gets connected/disconnected from a laptop
328  #
329  # install as /sbin/laptop_mode
330  #
331  # Contributors to this script:   Kiko Piris
332  #				 Bart Samwel
333  #				 Micha Feigin
334  #				 Andrew Morton
335  #				 Herve Eychenne
336  #				 Dax Kelson
337  #
338  # Original Linux 2.4 version by: Jens Axboe
339
340  #############################################################################
341
342  # Source config
343  if [ -f /etc/default/laptop-mode ] ; then
344	# Debian
345	. /etc/default/laptop-mode
346  elif [ -f /etc/sysconfig/laptop-mode ] ; then
347	# Others
348          . /etc/sysconfig/laptop-mode
349  fi
350
351  # Don't raise an error if the config file is incomplete
352  # set defaults instead:
353
354  # Maximum time, in seconds, of hard drive spindown time that you are
355  # comfortable with. Worst case, it's possible that you could lose this
356  # amount of work if your battery fails you while in laptop mode.
357  MAX_AGE=${MAX_AGE:-'600'}
358
359  # Read-ahead, in kilobytes
360  READAHEAD=${READAHEAD:-'4096'}
361
362  # Shall we remount journaled fs. with appropriate commit interval? (1=yes)
363  DO_REMOUNTS=${DO_REMOUNTS:-'1'}
364
365  # And shall we add the "noatime" option to that as well? (1=yes)
366  DO_REMOUNT_NOATIME=${DO_REMOUNT_NOATIME:-'1'}
367
368  # Shall we adjust the idle timeout on a hard drive?
369  DO_HD=${DO_HD:-'1'}
370
371  # Adjust idle timeout on which hard drive?
372  HD="${HD:-'/dev/hda'}"
373
374  # spindown time for HD (hdparm -S values)
375  AC_HD=${AC_HD:-'244'}
376  BATT_HD=${BATT_HD:-'4'}
377
378  # Dirty synchronous ratio.  At this percentage of dirty pages the process which
379  # calls write() does its own writeback
380  DIRTY_RATIO=${DIRTY_RATIO:-'40'}
381
382  # cpu frequency scaling
383  # See Documentation/admin-guide/pm/cpufreq.rst for more info
384  DO_CPU=${CPU_MANAGE:-'0'}
385  CPU_MAXFREQ=${CPU_MAXFREQ:-'slowest'}
386
387  #
388  # Allowed dirty background ratio, in percent.  Once DIRTY_RATIO has been
389  # exceeded, the kernel will wake flusher threads which will then reduce the
390  # amount of dirty memory to dirty_background_ratio.  Set this nice and low,
391  # so once some writeout has commenced, we do a lot of it.
392  #
393  DIRTY_BACKGROUND_RATIO=${DIRTY_BACKGROUND_RATIO:-'5'}
394
395  # kernel default dirty buffer age
396  DEF_AGE=${DEF_AGE:-'30'}
397  DEF_UPDATE=${DEF_UPDATE:-'5'}
398  DEF_DIRTY_BACKGROUND_RATIO=${DEF_DIRTY_BACKGROUND_RATIO:-'10'}
399  DEF_DIRTY_RATIO=${DEF_DIRTY_RATIO:-'40'}
400  DEF_XFS_AGE_BUFFER=${DEF_XFS_AGE_BUFFER:-'15'}
401  DEF_XFS_SYNC_INTERVAL=${DEF_XFS_SYNC_INTERVAL:-'30'}
402  DEF_XFS_BUFD_INTERVAL=${DEF_XFS_BUFD_INTERVAL:-'1'}
403
404  # This must be adjusted manually to the value of HZ in the running kernel
405  # on 2.4, until the XFS people change their 2.4 external interfaces to work in
406  # centisecs. This can be automated, but it's a work in progress that still needs
407  # some fixes. On 2.6 kernels, XFS uses USER_HZ instead of HZ for external
408  # interfaces, and that is currently always set to 100. So you don't need to
409  # change this on 2.6.
410  XFS_HZ=${XFS_HZ:-'100'}
411
412  #############################################################################
413
414  KLEVEL="$(uname -r |
415               {
416	       IFS='.' read a b c
417	       echo $a.$b
418	     }
419  )"
420  case "$KLEVEL" in
421	"2.4"|"2.6")
422		;;
423	*)
424		echo "Unhandled kernel version: $KLEVEL ('uname -r' = '$(uname -r)')" >&2
425		exit 1
426		;;
427  esac
428
429  if [ ! -e /proc/sys/vm/laptop_mode ] ; then
430	echo "Kernel is not patched with laptop_mode patch." >&2
431	exit 1
432  fi
433
434  if [ ! -w /proc/sys/vm/laptop_mode ] ; then
435	echo "You do not have enough privileges to enable laptop_mode." >&2
436	exit 1
437  fi
438
439  # Remove an option (the first parameter) of the form option=<number> from
440  # a mount options string (the rest of the parameters).
441  parse_mount_opts () {
442	OPT="$1"
443	shift
444	echo ",$*," | sed		\
445	 -e 's/,'"$OPT"'=[0-9]*,/,/g'	\
446	 -e 's/,,*/,/g'			\
447	 -e 's/^,//'			\
448	 -e 's/,$//'
449  }
450
451  # Remove an option (the first parameter) without any arguments from
452  # a mount option string (the rest of the parameters).
453  parse_nonumber_mount_opts () {
454	OPT="$1"
455	shift
456	echo ",$*," | sed		\
457	 -e 's/,'"$OPT"',/,/g'		\
458	 -e 's/,,*/,/g'			\
459	 -e 's/^,//'			\
460	 -e 's/,$//'
461  }
462
463  # Find out the state of a yes/no option (e.g. "atime"/"noatime") in
464  # fstab for a given filesystem, and use this state to replace the
465  # value of the option in another mount options string. The device
466  # is the first argument, the option name the second, and the default
467  # value the third. The remainder is the mount options string.
468  #
469  # Example:
470  # parse_yesno_opts_wfstab /dev/hda1 atime atime defaults,noatime
471  #
472  # If fstab contains, say, "rw" for this filesystem, then the result
473  # will be "defaults,atime".
474  parse_yesno_opts_wfstab () {
475	L_DEV="$1"
476	OPT="$2"
477	DEF_OPT="$3"
478	shift 3
479	L_OPTS="$*"
480	PARSEDOPTS1="$(parse_nonumber_mount_opts $OPT $L_OPTS)"
481	PARSEDOPTS1="$(parse_nonumber_mount_opts no$OPT $PARSEDOPTS1)"
482	# Watch for a default atime in fstab
483	FSTAB_OPTS="$(awk '$1 == "'$L_DEV'" { print $4 }' /etc/fstab)"
484	if echo "$FSTAB_OPTS" | grep "$OPT" > /dev/null ; then
485		# option specified in fstab: extract the value and use it
486		if echo "$FSTAB_OPTS" | grep "no$OPT" > /dev/null ; then
487			echo "$PARSEDOPTS1,no$OPT"
488		else
489			# no$OPT not found -- so we must have $OPT.
490			echo "$PARSEDOPTS1,$OPT"
491		fi
492	else
493		# option not specified in fstab -- choose the default.
494		echo "$PARSEDOPTS1,$DEF_OPT"
495	fi
496  }
497
498  # Find out the state of a numbered option (e.g. "commit=NNN") in
499  # fstab for a given filesystem, and use this state to replace the
500  # value of the option in another mount options string. The device
501  # is the first argument, and the option name the second. The
502  # remainder is the mount options string in which the replacement
503  # must be done.
504  #
505  # Example:
506  # parse_mount_opts_wfstab /dev/hda1 commit defaults,commit=7
507  #
508  # If fstab contains, say, "commit=3,rw" for this filesystem, then the
509  # result will be "rw,commit=3".
510  parse_mount_opts_wfstab () {
511	L_DEV="$1"
512	OPT="$2"
513	shift 2
514	L_OPTS="$*"
515	PARSEDOPTS1="$(parse_mount_opts $OPT $L_OPTS)"
516	# Watch for a default commit in fstab
517	FSTAB_OPTS="$(awk '$1 == "'$L_DEV'" { print $4 }' /etc/fstab)"
518	if echo "$FSTAB_OPTS" | grep "$OPT=" > /dev/null ; then
519		# option specified in fstab: extract the value, and use it
520		echo -n "$PARSEDOPTS1,$OPT="
521		echo ",$FSTAB_OPTS," | sed \
522		 -e 's/.*,'"$OPT"'=//'	\
523		 -e 's/,.*//'
524	else
525		# option not specified in fstab: set it to 0
526		echo "$PARSEDOPTS1,$OPT=0"
527	fi
528  }
529
530  deduce_fstype () {
531	MP="$1"
532	# My root filesystem unfortunately has
533	# type "unknown" in /etc/mtab. If we encounter
534	# "unknown", we try to get the type from fstab.
535	cat /etc/fstab |
536	grep -v '^#' |
537	while read FSTAB_DEV FSTAB_MP FSTAB_FST FSTAB_OPTS FSTAB_DUMP FSTAB_DUMP ; do
538		if [ "$FSTAB_MP" = "$MP" ]; then
539			echo $FSTAB_FST
540			exit 0
541		fi
542	done
543  }
544
545  if [ $DO_REMOUNT_NOATIME -eq 1 ] ; then
546	NOATIME_OPT=",noatime"
547  fi
548
549  case "$1" in
550	start)
551		AGE=$((100*$MAX_AGE))
552		XFS_AGE=$(($XFS_HZ*$MAX_AGE))
553		echo -n "Starting laptop_mode"
554
555		if [ -d /proc/sys/vm/pagebuf ] ; then
556			# (For 2.4 and early 2.6.)
557			# This only needs to be set, not reset -- it is only used when
558			# laptop mode is enabled.
559			echo $XFS_AGE > /proc/sys/vm/pagebuf/lm_flush_age
560			echo $XFS_AGE > /proc/sys/fs/xfs/lm_sync_interval
561		elif [ -f /proc/sys/fs/xfs/lm_age_buffer ] ; then
562			# (A couple of early 2.6 laptop mode patches had these.)
563			# The same goes for these.
564			echo $XFS_AGE > /proc/sys/fs/xfs/lm_age_buffer
565			echo $XFS_AGE > /proc/sys/fs/xfs/lm_sync_interval
566		elif [ -f /proc/sys/fs/xfs/age_buffer ] ; then
567			# (2.6.6)
568			# But not for these -- they are also used in normal
569			# operation.
570			echo $XFS_AGE > /proc/sys/fs/xfs/age_buffer
571			echo $XFS_AGE > /proc/sys/fs/xfs/sync_interval
572		elif [ -f /proc/sys/fs/xfs/age_buffer_centisecs ] ; then
573			# (2.6.7 upwards)
574			# And not for these either. These are in centisecs,
575			# not USER_HZ, so we have to use $AGE, not $XFS_AGE.
576			echo $AGE > /proc/sys/fs/xfs/age_buffer_centisecs
577			echo $AGE > /proc/sys/fs/xfs/xfssyncd_centisecs
578			echo 3000 > /proc/sys/fs/xfs/xfsbufd_centisecs
579		fi
580
581		case "$KLEVEL" in
582			"2.4")
583				echo 1					> /proc/sys/vm/laptop_mode
584				echo "30 500 0 0 $AGE $AGE 60 20 0"	> /proc/sys/vm/bdflush
585				;;
586			"2.6")
587				echo 5					> /proc/sys/vm/laptop_mode
588				echo "$AGE"				> /proc/sys/vm/dirty_writeback_centisecs
589				echo "$AGE"				> /proc/sys/vm/dirty_expire_centisecs
590				echo "$DIRTY_RATIO"			> /proc/sys/vm/dirty_ratio
591				echo "$DIRTY_BACKGROUND_RATIO"		> /proc/sys/vm/dirty_background_ratio
592				;;
593		esac
594		if [ $DO_REMOUNTS -eq 1 ]; then
595			cat /etc/mtab | while read DEV MP FST OPTS DUMP PASS ; do
596				PARSEDOPTS="$(parse_mount_opts "$OPTS")"
597				if [ "$FST" = 'unknown' ]; then
598					FST=$(deduce_fstype $MP)
599				fi
600				case "$FST" in
601					"ext3"|"reiserfs")
602						PARSEDOPTS="$(parse_mount_opts commit "$OPTS")"
603						mount $DEV -t $FST $MP -o remount,$PARSEDOPTS,commit=$MAX_AGE$NOATIME_OPT
604						;;
605					"xfs")
606						mount $DEV -t $FST $MP -o remount,$OPTS$NOATIME_OPT
607						;;
608				esac
609				if [ -b $DEV ] ; then
610					blockdev --setra $(($READAHEAD * 2)) $DEV
611				fi
612			done
613		fi
614		if [ $DO_HD -eq 1 ] ; then
615			for THISHD in $HD ; do
616				/sbin/hdparm -S $BATT_HD $THISHD > /dev/null 2>&1
617				/sbin/hdparm -B 1 $THISHD > /dev/null 2>&1
618			done
619		fi
620		if [ $DO_CPU -eq 1 -a -e /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq ]; then
621			if [ $CPU_MAXFREQ = 'slowest' ]; then
622				CPU_MAXFREQ=`cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq`
623			fi
624			echo $CPU_MAXFREQ > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
625		fi
626		echo "."
627		;;
628	stop)
629		U_AGE=$((100*$DEF_UPDATE))
630		B_AGE=$((100*$DEF_AGE))
631		echo -n "Stopping laptop_mode"
632		echo 0 > /proc/sys/vm/laptop_mode
633		if [ -f /proc/sys/fs/xfs/age_buffer -a ! -f /proc/sys/fs/xfs/lm_age_buffer ] ; then
634			# These need to be restored, if there are no lm_*.
635			echo $(($XFS_HZ*$DEF_XFS_AGE_BUFFER))	 	> /proc/sys/fs/xfs/age_buffer
636			echo $(($XFS_HZ*$DEF_XFS_SYNC_INTERVAL)) 	> /proc/sys/fs/xfs/sync_interval
637		elif [ -f /proc/sys/fs/xfs/age_buffer_centisecs ] ; then
638			# These need to be restored as well.
639			echo $((100*$DEF_XFS_AGE_BUFFER))	> /proc/sys/fs/xfs/age_buffer_centisecs
640			echo $((100*$DEF_XFS_SYNC_INTERVAL))	> /proc/sys/fs/xfs/xfssyncd_centisecs
641			echo $((100*$DEF_XFS_BUFD_INTERVAL))	> /proc/sys/fs/xfs/xfsbufd_centisecs
642		fi
643		case "$KLEVEL" in
644			"2.4")
645				echo "30 500 0 0 $U_AGE $B_AGE 60 20 0"	> /proc/sys/vm/bdflush
646				;;
647			"2.6")
648				echo "$U_AGE"				> /proc/sys/vm/dirty_writeback_centisecs
649				echo "$B_AGE"				> /proc/sys/vm/dirty_expire_centisecs
650				echo "$DEF_DIRTY_RATIO"			> /proc/sys/vm/dirty_ratio
651				echo "$DEF_DIRTY_BACKGROUND_RATIO"	> /proc/sys/vm/dirty_background_ratio
652				;;
653		esac
654		if [ $DO_REMOUNTS -eq 1 ] ; then
655			cat /etc/mtab | while read DEV MP FST OPTS DUMP PASS ; do
656				# Reset commit and atime options to defaults.
657				if [ "$FST" = 'unknown' ]; then
658					FST=$(deduce_fstype $MP)
659				fi
660				case "$FST" in
661					"ext3"|"reiserfs")
662						PARSEDOPTS="$(parse_mount_opts_wfstab $DEV commit $OPTS)"
663						PARSEDOPTS="$(parse_yesno_opts_wfstab $DEV atime atime $PARSEDOPTS)"
664						mount $DEV -t $FST $MP -o remount,$PARSEDOPTS
665						;;
666					"xfs")
667						PARSEDOPTS="$(parse_yesno_opts_wfstab $DEV atime atime $OPTS)"
668						mount $DEV -t $FST $MP -o remount,$PARSEDOPTS
669						;;
670				esac
671				if [ -b $DEV ] ; then
672					blockdev --setra 256 $DEV
673				fi
674			done
675		fi
676		if [ $DO_HD -eq 1 ] ; then
677			for THISHD in $HD ; do
678				/sbin/hdparm -S $AC_HD $THISHD > /dev/null 2>&1
679				/sbin/hdparm -B 255 $THISHD > /dev/null 2>&1
680			done
681		fi
682		if [ $DO_CPU -eq 1 -a -e /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq ]; then
683			echo `cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq` > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
684		fi
685		echo "."
686		;;
687	*)
688		echo "Usage: $0 {start|stop}" 2>&1
689		exit 1
690		;;
691
692  esac
693
694  exit 0
695
696
697ACPI integration
698----------------
699
700Dax Kelson submitted this so that the ACPI acpid daemon will
701kick off the laptop_mode script and run hdparm. The part that
702automatically disables laptop mode when the battery is low was
703written by Jan Topinski.
704
705/etc/acpi/events/ac_adapter::
706
707	event=ac_adapter
708	action=/etc/acpi/actions/ac.sh %e
709
710/etc/acpi/events/battery::
711
712	event=battery.*
713	action=/etc/acpi/actions/battery.sh %e
714
715/etc/acpi/actions/ac.sh::
716
717  #!/bin/bash
718
719  # ac on/offline event handler
720
721  status=`awk '/^state: / { print $2 }' /proc/acpi/ac_adapter/$2/state`
722
723  case $status in
724          "on-line")
725                  /sbin/laptop_mode stop
726                  exit 0
727          ;;
728          "off-line")
729                  /sbin/laptop_mode start
730                  exit 0
731          ;;
732  esac
733
734
735/etc/acpi/actions/battery.sh::
736
737  #! /bin/bash
738
739  # Automatically disable laptop mode when the battery almost runs out.
740
741  BATT_INFO=/proc/acpi/battery/$2/state
742
743  if [[ -f /proc/sys/vm/laptop_mode ]]
744  then
745     LM=`cat /proc/sys/vm/laptop_mode`
746     if [[ $LM -gt 0 ]]
747     then
748       if [[ -f $BATT_INFO ]]
749       then
750          # Source the config file only now that we know we need
751          if [ -f /etc/default/laptop-mode ] ; then
752                  # Debian
753                  . /etc/default/laptop-mode
754          elif [ -f /etc/sysconfig/laptop-mode ] ; then
755                  # Others
756                  . /etc/sysconfig/laptop-mode
757          fi
758          MINIMUM_BATTERY_MINUTES=${MINIMUM_BATTERY_MINUTES:-'10'}
759
760          ACTION="`cat $BATT_INFO | grep charging | cut -c 26-`"
761          if [[ ACTION -eq "discharging" ]]
762          then
763             PRESENT_RATE=`cat $BATT_INFO | grep "present rate:" | sed  "s/.* \([0-9][0-9]* \).*/\1/" `
764             REMAINING=`cat $BATT_INFO | grep "remaining capacity:" | sed  "s/.* \([0-9][0-9]* \).*/\1/" `
765          fi
766          if (($REMAINING * 60 / $PRESENT_RATE < $MINIMUM_BATTERY_MINUTES))
767          then
768             /sbin/laptop_mode stop
769          fi
770       else
771         logger -p daemon.warning "You are using laptop mode and your battery interface $BATT_INFO is missing. This may lead to loss of data when the battery runs out. Check kernel ACPI support and /proc/acpi/battery folder, and edit /etc/acpi/battery.sh to set BATT_INFO to the correct path."
772       fi
773     fi
774  fi
775
776
777Monitoring tool
778---------------
779
780Bartek Kania submitted this, it can be used to measure how much time your disk
781spends spun up/down.  See tools/laptop/dslm/dslm.c
782