August 1, 2006

Put your laptop to sleep

Author: Jonathan Oxer, Kyle Rankin, and Bill Childers

Part of proper power management is the ability to put your laptop to sleep. ACPI sleep is defined as a state where the system is still technically powered on, but the screen and hard disk are powered down and the computer is using just enough power to keep the contents of RAM alive. The Ubuntu development team has devoted an immense amount of effort toward getting ACPI power management working properly. As it stands, Ubuntu is power-management-friendly right out of the box, thanks to the recent addition of the gnome-power-manager package. It turns out there's not much required to get most modern laptops to sleep and wake up correctly.

This article is excerpted from the newly published book Ubuntu Hacks, copyright 2006 Kyle Rankin, Jonathan Oxer, and Bill Childers. All rights reserved. Used with permission.

The Dapper Drake release of Ubuntu Linux includes the new gnome-power-manager package, which enables ACPI sleep much like the system-tray power applet in Windows. Finally, sleep "just works" in Linux. The gnome-power-manager applet is configured to start automatically, and it lives in GNOME's panel notification area. If you right-click on the little battery icon, you'll see a menu pop up with choices to Suspend or Hibernate, and a Preferences choice.

This deceptively simple little application gives you a lot of control over your laptop's sleep behavior. If you click on Preferences, you'll see the Power Management Preferences dialog box. In this dialog's Sleep tab, you can configure different behavior depending on whether you're plugged into AC power or running on battery. One of the most interesting features is your ability to control the backlight brightness of your laptop's screen depending on the machine's power state. A large consumer of power in a laptop is the screen's backlight, so being able to automatically turn down that lamp while on battery will help squeeze more runtime out of the system while it's unplugged.

The Options tab is where you can set the default type of sleep you wish for the system to use, as well as what actions will engage the sleep mechanism. The default sleep type is set to Suspend, which refers to ACPI sleep. (Hibernate is a totally different type of sleep mechanism that we'll cover in a moment.) If you wish, under the Actions section of the dialog box, you can set the system to automatically sleep when the laptop lid is closed. This is a handy feature if you're on the go: simply shut the lid and run off to your next appointment; then open the lid later, and the machine will wake up without any intervention.

The Advanced tab of the dialog controls the notification icon's behavior. If you want the icon to appear only when you're charging or discharging, or you want to turn off the icon altogether, here's where you change those settings.

If you're not seeing the notification icon, perhaps it's because you're plugged into AC power. If you'd like to see the applet all the time, unplug your laptop for a moment and the icon should appear. You can then use the Advanced tab to change the notification icon settings.

When you have all your settings configured to your liking, simply click the Close button, and the dialog box will close, saving your configuration changes.

Your system is now ready for you to test sleep mode. Ensure that your system is running on battery; then simply shut the lid of the laptop and see what happens. You should hear the hard disk power down, and one of the power LEDs should indicate a power-state change by blinking or some other method.

Hopefully, your machine went to sleep properly. Now you need to see if it wakes up correctly. Simply open the lid, and the computer should start waking up. When it's ready for use, you'll be prompted for your system password by gnome-screensaver. Enter your password, and your system should be in the exact same state as it was when you powered it off.

Hibernate your laptop

Because ACPI sleep does require a trickle of power to keep the CPU and RAM alive, it may not be desirable in all situations. Hibernate has been designed as the complement to ACPI sleep. It takes the contents of RAM and saves them to the system's swap partition. As a result, it requires absolutely zero power to maintain that hibernated state.

There is a downside to hibernating a machine, however. Due to the fact that the system saves the contents of RAM before powering off, and then loads the contents of RAM after the kernel loads on reboot, it does take a fair amount of time to enter and exit the hibernated state. However, hibernating is still faster than powering off and restarting your machine, and there's the added benefit of saving application state.

Since hibernation saves the exact contents of RAM to your swap partition, the size of your swap partition must be larger than the amount of RAM you have in the machine. Ubuntu configures this automatically if you have done a default installation, but if you override the default partitioner during installation, you need to keep this point in mind.

Configuring and using hibernation

Since hibernation and sleep are similar and use the underlying Linux ACPI subsystem, they both use gnome-power-manager to configure and control their settings. You can adjust the default type of sleep to be hibernation from within the preferences of gnome-power-manager, but keeping the default set to Suspend makes sense due to the time involved in entering and exiting a hibernated state. If you do decide to adjust this setting, you can right-click on the gnome-power-manager icon, select Preferences, and click on the Options tab.

Now that you have your preferences set how you like them, it's time to test hibernation. Assuming you're using the stock Ubuntu preferences for gnome-power-manager, you'll need to engage hibernation by right-clicking on the gnome-power-manager applet and selecting Hibernate. Once you do, the system will immediately dim the screen, and you should hear a good deal of hard-disk activity. Once the disk stops churning, the system will power off. At this point, the system is in hibernate mode and can be left in this state indefinitely without using any battery power.

To exit the hibernated state, simply power on the system normally. The bootloader will come up, and the kernel will boot normally, until it detects a RAM image on the swap partition. At that point, the system will load up the RAM image and should return to where you left it. Typical times to enter hibernation run between 30 seconds and 1 minute, and times to exit hibernation (including the BIOS test) run about the same.

Warning -- if your system is configured to boot multiple operating systems, you need to be careful here. In theory, you should be able to hibernate your Ubuntu system and then boot into a different operating system. But this is fundamentally risky: if you change anything on the Ubuntu partition, you'll be in heaps of trouble. And if you're sharing a swap partition between Ubuntu and another Linux distribution, you'll be in a world of trouble if that other Linux distribution boots up, since it will erase your hibernated state (or may itself try to resume from that hibernated state). Play it safe: if you are hibernated, don't boot into anything except the system you hibernated from.

Between sleep and hibernation, you have all the great power management capabilities at your disposal.

Click Here!