Is laptop_mode enabled in Ubuntu 8.10

Posted by bikethetam Mon, 24 Nov 2008 03:47:00 GMT

laptop_mode is an extremely poorly documented tool that is distributed with Ubuntu and it's also a dangerous one as it is either the cause or the solution for the very widespread problem of the hard drive spindown issue. It's so poorly documented that nobody seems to know it it's in use or not in Ubuntu. It's also very difficult to figure out how to turn it on or off.

Once this issue is figured out, laptop_tools can be very convenient to configure how your laptop saves power. But unfortunately its interactions with other tools designed for a similar purpose are problematic to say the least.

Here's is what I managed to figure out by experimenting and going through various posts on the net.

What is laptop_mode anyway? laptop_mode is a tool with a set of configuration settings to configure your block devices, the frequency of the CPU, when to turn off the display, all in one place. For all disks on your system you can configure things such as
  • are the disks mounted witth noatime, relatime?
  • APM (Advanced Power Management)
  • using write cache...
All those things you can already set using hdparm and /etc/hdparm.conf but the difference is that laptop_mode makes it possible to have different settings for different modes (on AC or on battery mainly). For example you might want to sacrifice some of the performance of the disks to save some power but only when you're running on battery, not on AC.

laptop_mode files
  • /usr/sbin/laptop_mode: the script that applies the laptop_mode settings when invoked by a power management daemon
  • /etc/laptop_mode/laptop-mode.conf: the configuration file where the power saving parameters can be set for various power states
  • /proc/sys/vm/laptop_mode: current status of laptop_mode
  • /etc/init.d/laptop_mode: init script. If called with the "start" argument, it may create "/var/run/laptop-mode-tools/enabled" and call "/usr/sbin/laptop_mode start" (see below why it may not do that).


How to tell what state laptop_mode is in? There is no laptop_mode process running in the background. Instead laptop_mode is run when needed when the environment changes, it does what it needs to do and exits immediately. /proc/sys/vm/laptop_mode has the current status of laptop_mode:
  • 0: disabled
  • 2: enabled
I've never seen it at 1.
If the status is enabled, then the appropriate settings found in laptop_mode.conf will be applied as soon the power state of the machine changes.

How to turn it on? "/usr/sbin/laptop_mode start" will change the status to enabled. "/usr/sbin/laptop_mode stop" will change the status to disabled. That always works. The problem is to figure out if laptop_mode will automatically be invoked during startup.
There are several conditions that need to be true for that to happen.
/etc/init.d/laptop_mode must be executed by default during initialization. That is the case in 8.10:
me@myhost ~ $ ls /etc/rc?.d/*laptop*
/etc/rc0.d/K99laptop-mode  /etc/rc3.d/S99laptop-mode  /etc/rc6.d/K99laptop-mode
/etc/rc1.d/K99laptop-mode  /etc/rc4.d/S99laptop-mode
/etc/rc2.d/S99laptop-mode  /etc/rc5.d/S99laptop-mode
However that's not enough. Looking closely at /etc/init.d/laptop_mode, we see:
# FIXME: this shouldn't be configured there
if [ -f /etc/default/acpi-support ]; then
 . /etc/default/acpi-support;
fi

if [ x$ENABLE_LAPTOP_MODE = xfalse ]; then
 exit 0;
fi
and in /etc/default/acpi-support in Intrepid:
ENABLE_LAPTOP_MODE=false
What that means is that you can run "/etc/init.d/laptop_mode start" all you want, it won't do a thing, which means that in this configuration "/usr/sbin/laptop_mode start" is never called during startup or anytime after startup.

laptop_mode is not enabled by default in Ubuntu 8.10.
"laptop_mode status" says:
Laptop Mode Tools is NOT allowed to run: /var/run/laptop-mode-tools/enabled does not exist.

/proc/sys/vm/laptop_mode:
0
Naturally, editing /etc/default/acpi-support and setting that parameter to true changes everything. Then and only then will laptop_mode settings be applied on startup.
Experiments show that after setting it to true and rebooting the machine, "laptop_mode status" says:
Laptop Mode Tools is allowed to run: /var/run/laptop-mode-tools/enabled exists.

/proc/sys/vm/laptop_mode:
2
and plugging or unplugging the power cord will produce the expected results. You can easily check for that by checking the drives Readahead states. On my machine it goes from 128kB to 3072kB as configured in laptop_mode.conf.
My feeling is that the "Laptop Mode Tools is NOT allowed to run: /var/run/laptop-mode-tools/enabled does not exist." line is just there to confuse people. Laptop mode tools is seemingly another set of tools that is not directly related to laptop_mode and that has no influence on the interpretation of laptop_mode.conf. /var/run/laptop-mode-tools/enabled is created only by "/etc/init.d/laptop_mode start" but that doesn't seem to matter. Even if that file does not exist, enabling laptop_mode with "/usr/sbin/laptop_mode start" will totally enable laptop_mode.

Interactions with hdparm.conf hdparm.conf will be entirely ignored if laptop_mode is enabled. That's because hdparm settings are applied very early during the boot process by /lib/udev/hdparm and laptop_mode runs later overwriting whatever disk settings may have been set in hdparm.conf.

What about ACPI? laptop_mode, if in charge, also overrides ACPI settings related to hdparm APM (Advanced Power Management).
This is visible in /etc/acpi/start.d/90-hdparm.sh
if [ -e /usr/sbin/laptop_mode ] ; then
LMT_CONTROL_HD_POWERMGMT=$(. /etc/laptop-mode/laptop-mode.conf && echo "$CONTROL_HD_POWERMGMT")
if [ "$LMT_CONTROL_HD_POWERMGMT" != 0 ] ; then
   # Laptop mode controls hdparm -B settings, we don't.
   DO_HDPARM=n
fi
fi
Unfortunately, this is bogus because, as we've seen above, laptop_mode may very well NOT be enabled even if laptop_mode.conf exists and CONTROL_HD_POWERMGMT is non zero. If that happens then no one will take care of tuning the hdparm power management when switching between battery and AC.
See ubuntu-hdparm-apm-and-laptop-mode-tools for more details.

Note: this got fixed in Intrepid in an update mid-January: acpi hdparm fixed

But anyway, if laptop_mode LMT_CONTROL_HD_POWERMGMT is found to be 0, ACPI is in charge and controls the APM settings (-B) between 128 (battery) to 254 (AC).

What a mess! As for me, the poorly managed interactions with ACPI and udev/hdparm convinced me to turn off laptop_mode once and for all.

Make Blogger edit box bigger

Posted by bikethetam Mon, 17 Nov 2008 03:41:00 GMT

I've been using Blogger for a few days only but the one thing that annoyed me was the tiny size of the input box for writing a post. It's both too narrow and too short, forcing the writer to move the elevator constantly. blogger.com doesn't allow to change that size (as opposed to wordpress.com) Fortunately, users of Firefox can still trick their browser into resizing that box. This is done using the great Greasemonkey Firefox addon and an adequate Greasemonkey user script.

  1. First install the Greasemonkey addon to Firefox: download it here. You might also want Platypus but it's not necessary here.
  2. Restart Firefox.
  3. Now download the script for resizing the edit textarea: http://browservulsel.blogspot.com/2005/07/blogger-large-post-editor-user-script.html. Greasemonkey should automatically pick it up as you click on the Blogger Large Post Editor and from now on, any time you edit a post, the edit box will occupy the whole width of the page.

To my taste the edit is still too short, so I edited the script and increased the height of the box:

  1. Go to Tools/Greasemonkey/Manage User Scripts
  2. Select Blogger Large Post Editor and press the "Edit" button
  3. I chose to make the box height 500 pixels and to do that I just changed the ref.style.height and ta.style.height lines:
    var ref = document.getElementById('richeditorframe');
    ref.style.width = '100%';
    ref.style.height = '500px';
    
    var ta = document.getElementById('textarea');
    ta.style.height = '500px'; 

While I'm on the Greasemonkey subject, one other place where I find it very useful is for http://www.google.com/ig, the personalized home page of Google. The layout on that page changed in October 2008, mostly for the best, but it now includes a sidebar that is borderline useless and that leaves a good 15% of the width of the page on the left entirely black and sad. I decided to get rid of that sidebar, it's easy with Greasemonkey + Platypus. Platypus is another Firefox addon that can write Greasemonkey scripts based on the page element you select graphically and the action you decide to apply on that element using a contextual menu.

  1. Install the Platypus addon
  2. Go to http://www.google.com/ig
  3. If the platypus toolbar doesn't automatically show up, make it appear by selecting the Tools/Platypus! menu item
  4. Move your mouse over to the sidebar, it should entirely highlight itself (pink overlay), then right click, select remove, click the 'Save' button in the Platypus toolbar, and confirm.
  5. To make sure all variations of the http://www.google.com/ig URL get caught, append a * to it in the Greasemonkey User scripts manager.

The ig sidebar won't bother you anymore.


Enabling syntax highlighting in Blogger pages

Posted by bikethetam Sun, 16 Nov 2008 03:21:00 GMT

Blocks of source code don't always render very well in blogger if you just simply use the <blockquote> HTML tag, they just kind of melt in and are hardly distinguishable from the rest of the text. Thanks to Alex Gorbatchev, it's easy to make it better. Here are the steps:
  1. Go to http://code.google.com/p/syntaxhighlighter/ and download the latest version of the scripts. As of now the latest package is http://syntaxhighlighter.googlecode.com/files/SyntaxHighlighter_1.5.1.rar
  2. Now you need to find a web server where to host those files. Once you've found it, you need to copy the files to the DocumentRoot:
    cd /tmp
    unrar x SyntaxHighlighter_1.5.1.rar
    cd $docroot
    mkdir SyntaxHighlighter
    cd SyntaxHighlighter
    cp -r /tmp/dp.SyntaxHighlighter/S* .
    
  3. Go to the Layout tab on you Blogger console and choose "Edit HTML".
  4. Right after <Head> insert the following lines:
    <link href='http://yourwebsite.org/SyntaxHighlighter/Styles/SyntaxHighlighter.css' rel='stylesheet' type='text/css'/>
    <script language='javascript' src='http://yourwebsite.org/SyntaxHighlighter/Scripts/shCore.js'/>
    <script language='javascript' src='http://yourwebsite.org/SyntaxHighlighter/Scripts/shBrushCSharp.js'/>
    <script language='javascript' src='http://yourwebsite.org/SyntaxHighlighter/Scripts/shBrushXml.js'/>
    <script language='javascript' src='http://yourwebsite.org/SyntaxHighlighter/Scripts/shBrushCss.js'/>
    <script language='javascript' src='http://yourwebsite.org/SyntaxHighlighter/Scripts/shBrushJScript.js'/>
    <script language='javascript' src='http://yourwebsite.org/SyntaxHighlighter/Scripts/shBrushSql.js'/>
    <script language='javascript' src='http://yourwebsite.org/SyntaxHighlighter/Scripts/shBrushCpp.js'/>
    <script language='javascript' src='http://yourwebsite.org/SyntaxHighlighter/Scripts/shBrushJava.js'/>
    <script language='javascript' src='http://yourwebsite.org/SyntaxHighlighter/Scripts/shBrushRuby.js'/>
  5. Right before </body> insert the following lines:
    <script language='javascript'>
    dp.SyntaxHighlighter.ClipboardSwf = 'http://yourwebsite.org/SyntaxHighlighter/Scripts/clipboard.swf';
    dp.SyntaxHighlighter.BloggerMode();
    dp.SyntaxHighlighter.HighlightAll('code');
    </script>
    
You can now write blog entries with syntax highlighting: You just have to surround the source code with
<pre name="code" class="html:nogutter:nocontrols">
and
</pre>
Please go to Alex's documentation to find the list of languages that are supported and which class to apply.

Debian package manager cheat sheet

Posted by bikethetam Thu, 13 Nov 2008 02:58:00 GMT

Searching the local cache of repositories apt-cache search

$ apt-cache search activerecord
libactiveldap-ruby1.8 - an object-oriented interface to LDAP for Ruby
libactiverecord-ruby - Ruby library that ties database tables to classes
libactiverecord-ruby1.8 - Tie database tables to classes (Ruby 1.8)
...

Finding information about a non installed package

apt-cache showpkg

$ apt-cache showpkg libactiverecord-ruby1.8
Package: libactiverecord-ruby1.8
Versions:
2.1.0-3 (/var/lib/apt/lists/us.archive.ubuntu.com_ubuntu_dists_intrepid_universe_binary-i386_Packages)
Description Language:
        File: /var/lib/apt/lists/us.archive.ubuntu.com_ubuntu_dists_intrepid_universe_binary-i386_Packages
         MD5: 4eabc44d09608d0156c42018917a2881

Reverse Depends:
libactiverecord-ruby,libactiverecord-ruby1.8
Dependencies:
2.1.0-3 - libruby1.8 (0 (null)) libactivesupport-ruby1.8 (2 2.1.0-3) libsqlite3-ruby1.8 (16 (null)) libpgsql-ruby1.8 (16 (null)) libmysql-ruby1.8 (0 (null))
Provides:
2.1.0-3 -
Reverse Provides:

Listing all available packages matching a pattern

The packages matching the patterns may or may not be installed. dpkg -l

$ dpkg -l ruby*
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Cfg-files/Unpacked/Failed-cfg/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Hold/Reinst-required/X=both-problems (Status,Err: uppercase=bad)
||/ Name             Version          Description
+++-================-================-================================================
ii  ruby             4.2              An interpreter of object-oriented scripting lang
un  ruby-elisp                  (no description available)
ii ruby-full 4.2 Ruby full installation
ii ruby1.8 1.8.7.72-1 Interpreter of object-oriented scripting languag
ii ruby1.8-dev 1.8.7.72-1 Header files for compiling extension modules for
un ruby1.8-examples (no description available)
ii rubygems 1.3.0~RC1really1 package management framework for Ruby libraries/
un rubygems-doc (no description available)
ii rubygems1.8 1.3.0~RC1really1 package management framework for Ruby libraries/

Finding information about an installed package

dpkg -s

$ dpkg -s ruby1.8
Package: ruby1.8
Status: install ok installed
Priority: optional
Section: interpreters
Installed-Size: 136
Maintainer: Ubuntu Core Developers 

Listing all files belonging to an installed package

dpkg -L

$ dpkg -L ruby1.8
/.
/usr
/usr/bin
/usr/bin/ruby1.8
/usr/bin/erb1.8
/usr/bin/testrb1.8
/usr/share
/usr/share/man
/usr/share/man/man1
/usr/share/man/man1/erb1.8.1.gz
/usr/share/man/man1/testrb1.8.1.gz
/usr/share/man/man1/ruby1.8.1.gz
/usr/share/doc
/usr/share/doc/ruby1.8
/usr/share/doc/ruby1.8/README
/usr/share/doc/ruby1.8/README.ja.gz
/usr/share/doc/ruby1.8/ToDo.gz
/usr/share/doc/ruby1.8/README.Debian
/usr/share/doc/ruby1.8/copyright
/usr/share/doc/ruby1.8/LEGAL.gz
/usr/share/doc/ruby1.8/NEWS-1.8.0.gz
/usr/share/doc/ruby1.8/NEWS.gz
/usr/share/doc/ruby1.8/NEWS.Debian.gz
/usr/share/doc/ruby1.8/changelog.Debian.gz