Over the past few years, Linux has migrated out of the machine room and onto the desktop, even the laptop. Like Linux on servers, Linux on personal computers offers speed, reliability, and a host of varied, powerful, and sophisticated applications. But where the desktop is quite commonplace, laptops are still a niche, and most Linux distributions aren’t tailored to users on-the-go with a finite power supply and wireless connections.
This article provides ten useful “power tools” for using Linux in a mobile environment. It includes mobile how-to’s and tricks and configuration tips for creating a mobile Linux you can rely on. So, break out your cell phone, wireless card, suitcase, and briefcase. It’s time to get unplugged.
If you run Linux on your desktop, you know that it’s fast, reliable, and gets jobs done quickly. However, that performance comes with a price: electrical power.
On the desktop, electrical power is hardly ever an issue: your desktop or disk-side computer — with its fast processor, feature-rich motherboard, and peripherals — is plugged into the wall, and your flat screen display and other devices are plugged in separately. There’s no shortage of power.
However, on your laptop, power is a precious, limited resource that must be shared between all components. Indeed, if you’ve ever tried to do anything complex on your laptop while running on battery, you’ve no doubt run into real longevity limitations.
Out of the box, most Linux distributions are configured to operate on machines with an unlimited supply of power. So, to use Linux in a mobile environment, you’ve got to make a few tweaks and take advantage of the many tools available to manage and conserve power.
Saving Power: apmd Services Management
Linux’s Advanced Power Management daemon, apmd, manages basic power savings functions from within the operating system. For example, apmd can detect changes in power status, turn services on or off as available power dictates, and call other applications to react appropriately. To configure apmd, you configure both the apmd daemon itself and the apm configuration script.
The apmd configuration, (typically) located in /etc/sysconfig/apmd, contains basic configuration information for the apm daemon. This configuration is reasonable out-of-the-box, so you typically only need to tweak a few pieces of it to start. The most important directives are POWER_SERVICES, LOWPOWER _SERVICES, and CPUFREQ.
* POWER_SERVICES lists all of the services you’d like to temporarily shut down when switching to battery power. (These services are restarted when the laptop is reconnected to AC.) The list is initially somewhat short, so you should add a few things to the list, including crond, autofs, cups, pcmcia, and syslog. You can read why in the sidebar “Keeping a Low-Power Profile.”
Keeping a Low-Power Profile
A number of services are probably not needed when your laptop is running off battery power. Using apmd, you can shut those services off when using a battery and restart them when you reconnect to AC.
* crond. Unless you plan on running cron jobs while you’re on battery power, it’s a good idea to shut the cron daemon off. This prevents any configured cron jobs from firing, which may wake up your hard drive and increase processor use. You’ll especially want to turn off this service at battery time if you have cron jobs that run every minute, as the hard disk won’t ever go to sleep.
* autofs. This service is usually shut down when running on battery by default, as it prevents the frequent polling of your CD-ROM and other removable peripherals. Leaving autofs on may result in higher bus utilization and consume additional power.
* cups. Unless you plan on printing while on battery power, shutting down cups removes another scheduler from running in resident memory, and may prevent occasional polling.
* pcmcia. If you don’t use any PCMCIA peripherals while on battery power, shutting down the pcmcia service helps your bus rest and conserve power.
* syslog. It’s essential to shutdown syslog while on battery power, because as long as syslog is running, system messages will be written to disk, causing the disk to wake up to write them and eat battery power. Shutting down syslog at “battery time” is one of the best ways to quiet your system enough to spin down the hard disks.
|
* LOWPOWER_SERVICES lists services to shut down whenever the battery reaches a specific threshold.
* CPUFREQ enables processor frequency scaling. Frequency scaling is described in more detail later, and once you’ve got frequency scaling configured, you’ll want to set this option to yes.
After you tweaked these values accordingly, use the command service apmd restart to load the new settings.
The next batch of tweaks gets applied to the apm script, located at /etc/sysconfig/apm-scripts/apmscript. It’s a good idea to call apmscript directly from your /etc/rc.local file to adjust the system to match the battery state it’s booted up with. The script has several sections, and each section is tailored to specific power management messages. Of particular interest are the resume and change functions.
But don’t edit apmscript just yet — let’s look at several power-related utilities first.
Saving Power Redux: CPU Frequency Scaling
The CPU is the single most power hungry component of your laptop. To increase battery life, most modern processors support CPU frequency scaling or clock modulation, which allows the processor to slow itself down when running on battery power. How much the processor slows down depends on the processor.
CPUFreq is a Linux kernel patch (available at http://www.brodo.de/cpufreq) that adds clock modulation to Linux. Some Linux distributions include CPU frequency scaling out-of-the-box, while others require the patch.
To check whether CPUFreq is already loaded in your kernel, just type cat /proc/cpufreq. You should see output similar to this:
minimum CPU frequency - maximum CPU frequency - policy
CPU 0 1998345 kHz (100 %) - 1998345 kHz (100 %) - performance
If you see both lines, CPUFreq is loaded in your kernel. If you see the first line, but don’t see a CPU, then CPUFreq is available, but a driver isn’t loaded.
Depending on the type of processor you have, you should be able to modprobe the appropriate driver from the list in Table One. The easiest way to load the appropriate driver is to add a modprobe to /etc/rc.local. If your kernel doesn’t support CPUFreq at all, you’ll need to download a copy and patch it into your kernel.
Table One: Modules for clock modulation in Linux
| MODULE | PROCESSOR |
| speedstep | Generic speedstep, present in older versions of CPUFreq |
| speedstep-ich | Most pre-Centrino Pentiumprocessors |
| speedstep-centrino | Pentium processors with Centrino technology |
| p4-clockmod | Pentium 4 clock modulation; common in Pentium 4M processors |
| longrun | Transmeta Crusoe with LongRun powernow-k6,k7,k8 AMD processors with PowerNow technology |
| longhaul | Processors with LongHaul support |
|
Once you have CPUFreq up and running, you can read from and write to /proc/cpufreq. To set a value, you can execute a command like one of these:
# echo -n “0%100%100%performance” > /proc/cpufreq
# echo -n “0%60%60%powersave” > /proc/cpufreq
# echo -n “0%12%80%userspace” > /proc/cpufreq
The first value is the processor id — this will almost always be zero on a single-processor laptop. The second and third values represent the minimum and maximum percentage of processor power the operating system may scale to. The final parameter represents the name of the governor. The three choices are performance, powersave, and userspace. performance attempts to provide maximum performance while on battery power; powersave attempts to keep the processor utilization at a minimum; and userspace is a good middle-ground, which adjusts the frequency based on demand.
Chances are, you want to be running at 100% capacity when on AC power, and at a lower value when on battery power. Once you’ve decided on these values, you can add them to /etc/sysconfig/apm-scripts/apmscript. By the way, CPUFreq will automatically tweak your settings to the closest acceptable values, if necessary. So, depending on the type of processor you have, the values that you request may not be the actual values set.
Listing One shows an example of CPUFreq in apmscript, tweaked from the Fedora Core 1 distribution.
Listing One: Setting the CPU frequency in apmscript
if test “x$CPUFREQ“ = “xyes“; then
if apm | LC_ALL=C grep -q on-line &>/dev/null; then
echo -n “0%100%100%performance“ > /proc/cpufreq
else
echo -n “0%12%60%userspace“ > /proc/cpufreq
fi
fi
|
Don’t forget to enable CPUFreq in the apmd configuration, if it hasn’t been turned on already.
Saving More Power: Hard Disk Spin-down
After the CPU, your hard disk is your laptop’s most power-hungry component. As long as it spins, it consumes power.
Linux’s out-of-the-box configuration makes it nearly impossible to spin down the hard disk, as the operating system performs flushes every few moments. So, to successfully spin down the hard disk, you’ll need two different tools: a script to set laptop_mode in the kernel and the hdparm utility.
The laptop_mode switch used to be all you needed to spin down the hard disk, but over time other settings have cropped up that must be changed as well. There are numerous laptop mode scripts available on the Internet, but you can download a handy one from http://www.linux-mag.com/downloads/2004-07/laptop/laptop_mode.sh. This particular script is handy because it works with both 2.4 and 2.6 kernels. Run the script using the options start or stop, and run it either at startup, say from /etc/rc.local, or only when going into battery mode.
Once you’ve enabled laptop_mode, set the hard disk spin-down threshold using the hdparm command. hdparm lets you set the spin-down timeout with -S, and allows you to check the status of the drive using -C. See the hdparm man page for specific information about the timeout values.
The hdparm command can be added to apmscript to automatically adjust the timeout when on AC and on battery. When plugged in, the command…
… sets standby to four minutes, which is quite suitable for most laptops. When running on battery, the command…
… sets standby to one minute. Depending on how fast your hard drive spins up and how critical your battery life is, you may wish to increase or decrease these values.
Lasting a Long Time: Swap Management
The final tip to conserve battery life is to disable your swap when running on battery power. Disabling swap causes all of your programs and data to be served out of RAM, thereby generating less hard disk traffic. Disabling swap should also allow your hard disk go to sleep faster. Make sure you should have ample memory to handle all of your programs at the time you disable swap.
To turn swap on and off, use the swapon and swapoff commands with -a (specifying “all”). These commands can also be added to apmscript.
The final copy of a laptop-friendly apm script might look something like Listing Two.
Listing Two: Saving power in apmscript
/etc/rc.local
modprobe p4-clockmod
sh /etc/sysconfig/apm-scripts/laptop_mode.sh start
sh /etc/sysconfig/apm-scripts/apmscript change
/etc/sysconfig/apm-scripts/apm-script
if test “x$CPUFREQ” = “xyes”; then
if apm | LC_ALL=C grep -q on-line &>/dev/null; then
echo -n “0%100%100%performance” > /proc/cpufreq
hdparm -S 48 /dev/hda
swapon -a
else
echo -n “0%12%60%userspace” > /proc/cpufreq
hdparm -S 12 /dev/hda
swapoff -a
fi
fi |
Going Mobile: MiniPCI Prism2 Cards
One of the real benefits of having a laptop is all of the great untethered hacking you can do. As with power management, some smart tailoring can make using your laptop even better.
Many newer laptops, such as IBM laptops, come standard with internal wireless cards based on a technology called MiniPCI. Many of these cards are based on Prism2, and as a result, most users are installing the linux-wlan-ng drivers to get their cards working. Unfortunately, the linux-wlan-ng drivers lack a lot of features, including compatibility with wireless extensions, leaving many with a proprietary interface to configure their cards.
One little known fact about the wireless drivers included with Linux is that the orinoco_pci driver is actually a Prism2-based driver for PCI cards that works with Prism2-based internal wireless cards. Better yet, the orinoco_pci driver can use wireless extensions and tools such as iwconfig and iwpriv.
To use the orinoco_pci driver, edit modules.conf and replace your existing wireless NIC alias with this:
Next, you can either reboot or unload your old module, then run modprobe orinoco_pci. Your internal wireless card should now be serviced by the Orinoco PCI kernel module, giving you access to the standard set of wireless tools.
Fancy-Free: Using Windows Drivers in Linux
If you’re unfortunate enough to have one of those wireless cards that isn’t formally supported in Linux yet, you’ll be happy to know that for a mere $20, you can use the manufacturer’s Windows-based card drivers on your Linux machine. Just head over to Linuxant (http://www.linuxant.com) and grab Driver Loader. If you don’t have your manufacturer’s CD, Driver Loader will even help you download the correct drivers from the manufacturer’s web site.
Staying Mobile: War Driving Essentials
If you’re into war-driving or just war-walking, you’ll soon find that you can’t live without Kismet and Airsnort. Combined, these two tools provide the ultimate wireless hacking and diagnostic tools available for Linux.
According to the Kismet web site (http://www.kismetwireless.net), Kismet is a complete wireless network detector, sniffer, and intrusion detection system. The free utility allows you to scan for wireless networks, performs real-time WEP decryption, and can even tell you the IP ranges being used on the network. Kismet shows you what (if any) encryption is being used on a network, how many clients are connected to each network, and shows wireless access points left in their factory configuration. If you have WEP keys to some of the networks you’re authorized on, you can even perform real-time WEP decryption and sniffing. Combine that with a tool like Driftnet (http://www.ex-parrot.com/~chris/driftnet) or Ethereal (http://www.ethereal.com), and you’ll be able to satisfactorily analyze any type of traffic on your network.
Airsnort is an excellent WEP recovery tool. If you travel a lot, and new WEP keys don’t always make it into your inbox, or if you are a security professional paranoid about the security of your wireless network (or if you and your friends just like to hack each other for fun), Airsnort can help recover the WEP key for a wireless network within about an hour, depending on the amount of traffic on the network. (WEP has a fundamental deficiency making it possible to deduce the key by analyzing what are called initialization vectors.)
Make Data Calls with a Mobile Phone
There are many different types of mobile phones on the market today, but most data-capable phones share a common method of communicating with the software modem either in the phone or at the cell provider’s location. The Linux Abstract Control Model (ACM) is designed to communicate with abstract devices, such as data modems on cellular phones. The Linux ACM maps the device to a TTY in /dev, which you can then talk to like a normal modem.
(Most Linux distributions do not have a functional ACM module out of the box, so it’s sometimes necessary to recompile the kernel to get ACM working correctly. Moreover, some versions of Red Hat and Fedora Linux require the module first be loaded using modprobe. The Linux ACM module works well in both 2.4 and 2.6 kernels after a custom compile. If you find yourself stuck and unable to get the ACM module to work properly, consider performing a custom build of your kernel.)
To use ACM, the first thing you’ll need is a cable to connect your mobile phone to your laptop. In many cases, a generic USB cable compatible with the phone will work, but it’s sometimes necessary to purchase a mobile kit from the provider. Forking over the extra $20 for a kit may be better than getting burned with a cheap cable on eBay.
Once you have the cable, run the command tail -f /var/log/messages. (If you’re running on battery power and have disabled syslog, either plug your laptop in or restart the syslog service to see these messages). When you plug the cable into the phone and the laptop, you should see something like this scroll by:
kernel: hub.c: new USB device 00:1d.1-2, assigned address 2
kernel: usb.c: registered new driver acm
kernel: ttyACM0: USB ACM device
kernel: acm.c: v0.21:USB Abstract Control Model driver for USB
Here, the kernel’s ACM module detected the device and assigned /dev/input/ttyACM0 as a logical device to communicate with the mobile phone. Now you can talk to /dev/ input/ttyACM0 as if it were a normal modem and can configure a dialup connection. For example, the sidebar “Phoning Home” shows the use of the popular wvdial dialer to establish a call.
To use the the wvdial dialer to establish a data connection via your cell phone, follow these steps.
STEP 1: ADD THE MODEM TO /ETC/WVDIAL.CONF
First, add your modem to wvdial.conf. Once added to the configuration, other tools such as Red Hat Network Manager can be used to set up the dialup connection, or you can hard-code them as in this example.
The modem configuration in wvdial.conf should look something like this:
[Mobile]
Modem = /dev/input/ttyACM0
Baud = 57600
SetVolume = 2
Dial Command = ATDT
Init1 = ATZ
FlowControl = Hardware (CRTSCTS)
STEP 2: CONFIGURE THE DIALER INTERFACE
Now that you’ve added the modem into wvdial.conf, you need to create a dialer interface for the dialup connection. You do this in /etc/sysconfig/network-scripts. Create a file named ifcfg-ISP, where ISP is the name of your Internet provider. The configuration should look something like this:
ONBOOT=no
USERCTL=yes
PEERDNS=yes
TYPE=Modem
DEVICE=ppp2
BOOTPROTO=dialup
CCP=off
PC=off
AC=off
BSDCOMP=off
VJ=off
VJCCOMP=off
LINESPEED=57600
MODEMPORT=/dev/input/ttyACM0
IDLETIMEOUT=600
PROVIDER=ISP
DEFROUTE=yes
PERSIST=no
PAPNAME=username
WVDIALSECT=ISP
MODEMNAME=Mobile
DEMAND=no
You’ll want to change username to your account name with your ISP. Next, add authentication information.
STEP 3: AUTHENTICATE YOURSELF
Once you’ve configured a dialer interface, you’ll need to provide the username, password, and phone number you want to use. This information should be added to both /etc/wvdial.conf and /etc/ppp/chap-secrets.
Going back to /etc/wvdial.conf, add the following block of information to the bottom:
[Dialer ISP]
Username = username
Password = mypassword
Phone = [Phone Number to ISP]
Stupid Mode = 1
You’ll also want to add the following to /etc/ppp/chap-secrets:
“username” “ISP” “mypassword”
Finally, you’re ready to make a connection. Type…
# ifup ISP
to activate your interface. Your mobile phone should identify that it’s making a data call, and you should see status messages scroll by in the logs. Once authenticated, most conventional ISPs will assign an IP address, and from there, you’ll be able to communicate on the network. When you’re finished, type ifdown ISP to shut down the connection.
|
Connecting to Verizon’s MobileOffice Service
Verizon Wireless provides two types of wireless services. The first, called MobileOffice, provides speeds up to 14.4k and generally charges only for minutes. The second type of service is called ExpressNetwork and provides speeds up to 144k, but is rather pricey at around $80 a month for unlimited use.
The MobileOffice service is configured in the same way a traditional dialup connection is, with only a few twists.
Start with the instructions in the “Phoning Home” sidebar, but convert the connection to support Verizon Wireless’ MobileOffice.
Specifically, you’ll want to dial the number #777 with your mobile phone. This is changed in the Dialer configuration from step 3 of “Phoning Home.” Next, add an initialization string. A special initialization string sets a register that tells Verizon’s modem banks you want to connect to their Mobile Office service. Add the following to your modem block in /etc/ wvdial.conf:
If you subscribe to Verizon’s high-speed ExpressNetwork instead, load the QCMDR register with the number 3.
To authenticate on the MobileOffice network, use the username and password qnc. You’ll want to change this in both /etc/wvdial.conf and /etc/ppp/chap-secrets.
To authenticate on ExpressNetwork, use the username xxxxxxxxxx@vzw3g.com, where the x’s represent your 10-digit mobile telephone number. Use the password vzw. Of course, you’ll need to be an ExpressNetwork subscriber to authenticate on it.
And that’s it! Once you’ve changed the settings above, you should be able to bring your dialup interface up on either of the Verizon networks. Please check with Verizon to find any charges and airtime fees that may apply.
Dynamic 3D X Server Creation
Many laptops have somewhat limited VRAM, which creates a conflict: either run your display without 3D support, or run it with limited color depth.
Dynamic X server creation allows you to have the best of both worlds by running your laptop’s display in 2D mode for normal work, and spawning a new X server when you want to play a game.
You can accomplish this with a little script and the creation of a separate XF86Config file.
First, create a separate XF86Config file. The primary XF86-Config configuration will be used for whatever 2D configuration you need for day-to-day work, but the new configuration will be specifically for 3D.
To begin, just copy the existing file to a new file called XF86Config.3D (these files should be located in /etc/X11). Edit the new 3D file to set the resolution and color depth of your 3D environment.
Next, create a 3D startup script for the game. Spawning a 3D server is done with the help of a small script and use of X’s desktop-switching hotkeys. The script should looks a little something like the one below.
/usr/X11R6/bin/X :1 -xf86config XF86Config.3D
-depth 16 -auth /var/gdm/:1.Xauth vt8 &
DISPLAY=:1.0
export DISPLAY
chromium
DISPLAY=:0.0
export DISPLAY
In this example, the 3D X server is spawned to play chromium. Running this script spawns a new window and loads chromium. From there, you can use X’s hotkeys to terminate the X session using ALT-CTRL-BACKSPACE and return to your 2D desktop, or you can switch between the two desktops using ALT-CTRL-F7 and ALT-CTRL-F8. It’s generally a good idea to terminate the session when you’re finished with it.
On the Road Again
This article explored just a small set of hacks for optimizing Linux in the mobile environment. Linux is an extremely flexible operating system, and with a little bit of creativity, it can provide a mobile computer with just as many features as commercial operating systems.
Jonathan Zdziarski is the creator of many popular open source tools including DSPAM
, the Apache mod_dosevasive
module, and WebConference/LiveHelp
. With 20 years of experience in computing, Jonathan has played a key role in the development of many large corporate infrastructures and well-known commercial products such as Micromuse Netcool
. Jonathan is an active Linux and UNIX enthusiast, and hacks away at his Linux-powered IBM Thinkpad
in his spare time. You can reach Jonathan at jonathan@nuclearelephant.com.