How to suspend and hibernate a laptop under Linux

3637

By Manolis Tzanidakis

Most modern laptops use Advanced Configuration & Power Interface (ACPI) for power management, so we’ll focus on that. Since ACPI support for Linux (ACPI4Linux) is in constant development, you’ll need a recent kernel (2.6.15 or later) in order to utilize all the latest advancements.

Suspend

ACPI state S3 — also know as Suspend-to-RAM — is the state where everything in the system enters a low-power state except for RAM, which consumes a small amount of power in order to retain its contents, so that upon resuming, everything is loaded back from the memory and all running applications are restored immediately.

To check whether Suspend-to-RAM is supported by your laptop and your kernel, you should run cat /sys/power/state. If the last command returns mem, you’re good to go; if not, you should check that ACPI_SLEEP support is built into your kernel by issuing grep ACPI_SLEEP KERNEL_CONFIG . Replace KERNEL_CONFIG with the actual kernel configuration file — by default /usr/src/linux-`uname -r`/.config or /boot/config-`uname -r`.

If your kernel supports ACPI sleep states but the cat command does not return mem, then either your laptop is not supported and you should file a bug report to the ACPI4Linux project, or your laptop does not use ACPI.

If the latter is the case you don’t need to worry. Your laptop probably supports Advanced Power Management (APM), so install the Linux APM Daemon (apmd) instead. Packages are available for virtually all distributions. After installing apmd, you can suspend your laptop by running apm -z. But please read on — the hibernate method is valid for APM laptops too.

To suspend the laptop you can run echo -n mem > /sys/power/state as root, but when you resume, your screen will probably be blank. It’s better to create a shell script that takes care of that problem and does the actual suspending procedure. Copy the following lines to a file named /usr/local/sbin/suspend.sh. Detailed explanations about each command are included in the comments (lines starting with #).

 

#!/bin/sh
# discover video card's ID
ID=`lspci | grep VGA | awk '{ print $1 }' | sed -e 's@0000:@@' -e 's@:@/@'`
# securely create a temporary file
TMP_FILE=`mktemp /var/tmp/video_state.XXXXXX`trap 'rm -f $TMP_FILE' 0 1 15
# switch to virtual terminal 1 to avoid graphics
# corruption in X
chvt 1
# write all unwritten data (just in case)
sync
# dump current data from the video card to the
# temporary filecat 
/proc/bus/pci/$ID > $TMP_FILE
# suspend
echo -n mem > /sys/power/state
# restore video card data from the temporary file
# on resume
cat $TMP_FILE > /proc/bus/pci/$ID
# switch back to virtual terminal 7 (running X)chvt 7
# remove temporary filerm -f $TMP_FILE

 

I tested this script on a Gentoo system. It might not work as written on all distros or laptops, but it’s a good starting point.

You should also add Option “VBERestore” “true” to your X server’s configuration file (/etc/X11/xorg.conf or /etc/X11/XF86Config-4) in the video card device section, so it looks like this:

 

Section
"Device"        Identifier      
"intel_855gm"        Driver          
"i810"        BusID           
"PCI:0:2:0"        Option         
"VBERestore"    "true"
EndSection

 

Now just make the suspend.sh script executable (chmod +x /usr/local/sbin/suspend.sh) and run it. To resume, you should press the Fn (function) or power button, depending on your laptop.

You can also launch commands automatically before suspending or after resuming — for instance, to bring your network interface down and up again or restart a daemon — by adding them before or after the echo -n mem line on that script.

Hibernate

Hibernate, also known as ACPI State S4 or Suspend-to-disk, operates like to Suspend-to-RAM but stores all current data to the hard disk. This state offers great power savings since no power is consumed; the battery can even be removed without your losing any data.

There are three methods for hibernating on Linux: swsusp, which is part of the kernel; uswsusp, which runs in user space but is not ready for production yet; and Software Suspend (suspend2), which has been around for some time and works as advertised. You can check this page for a comparison between these implementations.

The only problem with Software Suspend is that it’s not included in the Linux kernel yet, so it requires manual patching and kernel compilation. Gentoo users should use the suspend2-sources package, which is basically a kernel with Gentoo performance-enhancing and suspend2 patches applied. Users of other distros should start by downloading the latest stable kernel and a matching patch for suspend2. Unpack the kernel and patch tarballs, enter the kernel directory, and apply the patch:

 

cd /usr/srcwget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.15.tar.bz2 
\  http://www.suspend2.net/downloads/all/suspend2-2.2-rc16-for-2.6.15.tar.bz2
tar jxvpf linux-2.6.15.tar.bz2; tar jxvpf suspend2-2.2-rc16-for-2.6.15.tar.bz2
cd linux-2.6.15../suspend2-2.2-rc16-for-2.6.15/apply

 

You may need to change the actual filenames to match the most recent versions.

Configure the kernel as usual, making sure that these options are built in (not modules): Power management options (ACPI, APM) —> Suspend2 —> Swap Writer (also add your swap partition to the Default resume device name tab) and Cryptographic options —> LZF compression algorithm.

If you select “Swap Writer,” suspend2 will write all data to the swap space, so make sure your swap is at least twice the amount of your RAM in size. You can also select “File Writer” and save the suspend data on a file on the hard disk instead, but I prefer the swap method since it’s easier to set up. Compile, install your kernel, and reboot to it.

Next, you need to install the hibernate-script (packaged as hibernate in Debian). If a package is not available for your favorite distribution, download the latest version from the suspend2 Web page, unpack it, and run the install.sh script. Open the configuration file /etc/hibernate/hibernate.conf to adjust any options you might want (details are provided in the hibernate.conf man page) and run hibernate. To resume, press the power button.

The installation and configuration of suspend2 are documented in detail in this HOWTO.

Automating hibernation

Running scripts to suspend or hibernate your laptop is not very convenient, so we’d better automate things a little bit. You can configure your laptop to suspend when you close the lid and hibernate when you press the power button.

For this purpose you need to install the ACPI daemon, acpid. Packages are available for most distributions, and compiling from source is just a simple make && make install away. After finishing the installation, stop the daemon if it was started automatically, back up the default configuration directory, run mv /etc/acpi /etc/acpi.orig and create a new acpi directory with two subdirectories, events and actions: mkdir -p /etc/acpi/{events,actions}. Now create files to handle lid and power button actions and events:

/etc/acpi/events/lid

event=button[ /]lid.*action=/etc/acpi/actions/lid.sh

 

/etc/acpi/events/pwrbtn

event=button[ /]poweraction=/etc/acpi/actions/pwrbtn.sh

 

/etc/acpi/actions/lid.sh

#!/bin/sh/usr/local/sbin/suspend.sh

 

/etc/acpi/actions/pwrbtn.sh

#!/bin/shhibernate

 

The first two files instruct the daemon to call the other two files when the lid is closed or the power button is pressed, respectively. Make the last two files executable, with chmod +x /etc/acpi/actions/*, and start the acpid daemon. Now, close the lid and say goodnight to your laptop. Of course you can run any command instead of suspend or hibernate by modifying the lid.sh and pwrbtn.sh files.

Conclusion

Since most manufacturers tend to add proprietary extensions to their implementations of ACPI, there’s a slight chance that your laptop might need some additional steps in order to suspend or hibernate. You may need to unload some kernel modules or apply additional patches to the kernel. Since there are so many different laptops, it’s hard to offer concrete advice.

A helpful resource is the TuxMobil Linux installation survey, where you can find user installation reports about Linux on almost any laptop, or even write a report about your laptop.