Linux.com

Feature

How to suspend and hibernate a laptop under Linux

By Manolis Tzanidakis on June 06, 2006 (8:00:00 AM)

Share    Print    Comments   

Many people prefer working with laptops instead of desktops for the flexibility they offer. Some of them would also like to switch to a free and open source operating system like GNU/Linux and have their laptop do all the things that proprietary OSes offer, such as suspending their laptops. Several distributions try to make this work out of the box, but knowing what's under the hood always comes in handy, particularly when something goes wrong and needs fixing. Let's take a look at how to suspend and hibernate your laptop under Linux.

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 file
cat /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 file
rm -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/src
wget 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[ /]power
action=/etc/acpi/actions/pwrbtn.sh

/etc/acpi/actions/lid.sh

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

/etc/acpi/actions/pwrbtn.sh

#!/bin/sh
hibernate

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.

Share    Print    Comments   

Comments

on How to suspend and hibernate a laptop under Linux

Note: Comments are owned by the poster. We are not responsible for their content.

Fn Esc?

Posted by: Anonymous Coward on June 07, 2006 04:17 AM
Buy an Acer laptop and life is good...

#

Great article!

Posted by: Keith Winston on June 07, 2006 08:38 PM
Nice coverage of the Linux laptop suspend options. Well done.

#

It would be nice

Posted by: Anonymous Coward on June 07, 2006 02:11 AM
if we could do this with regular desktops like windows user can.

#

Re:It would be nice

Posted by: Anonymous Coward on June 07, 2006 06:56 AM
> if we could do this with regular desktops like windows user can.


      I have been doing so with my desktop for over one year now. Have a look in www.suspend2.net.

#

Re:It would be nice

Posted by: Anonymous Coward on June 10, 2006 12:57 AM
>> if we could do this with regular desktops like windows user can.
>
> I have been doing so with my desktop for over one year now. Have a look in www.suspend2.net.

Or use Fedora. Works for me.

#

Re:It would be nice

Posted by: Anonymous Coward on June 22, 2006 03:20 AM
Have you tried it? I've been doing this for years on a desktop PC. (I've had to sometimes switch between S3 and S4 with new kernels, because one or the other was broken on my BIOS, but one of them has always worked.)

#

Suspend2 gives positive results

Posted by: Anonymous Coward on June 07, 2006 05:35 AM
It's been some days since I've been using Suspend2 in my laptop. The poweroff method it features has the advantage that it doesn't need any kind of ACPI support, so it works in almost any situation. You may have a laptop with a broken DSDT and it doesn't really matter. You don't need to patch it. Furthermore, the poweroff method saves power completely. You can even unplug the computer.

With it, I can boot to a fully functional desktop in about 30 seconds (timer in hand). It's important to note that after those 30 seconds all applications I need are already running and disk caches are restored, etc. So, in fact, it leaves you in a more advanced and responsive state that simply booting and launching those applications. Also, of those 30 seconds, 10 are spent in the BIOS sequence, so the real time it takes to boot (Grub+System) is about 20 seconds. Very useful, fast and convenient.

On a side note, another good site for laptop reviews is <a href="http://www.linux-on-laptops.com/" title="linux-on-laptops.com">http://www.linux-on-laptops.com/</a linux-on-laptops.com>.

#

Encryption

Posted by: Anonymous Coward on June 07, 2006 06:42 AM
Laptops are often lost or stolen, so it makes sense to use disk encryption. However that would seem to be defeated if an image of RAM were stored on the drive. Is there any way to encrypt the hibernation file or is it best to just not use this feature on secured systems?

#

Re:Encryption

Posted by: Anonymous Coward on June 07, 2006 07:01 AM
Suspend2 seems to allow disk encryption. Check <a href="http://wiki.suspend2.net/EncryptedSwapAndRoot" title="suspend2.net">http://wiki.suspend2.net/EncryptedSwapAndRoot</a suspend2.net>.

#

Already works with Ubuntu.

Posted by: Anonymous Coward on June 07, 2006 06:49 AM
Luckily this all works out-of-the-box with Ubuntu 6.06 on most laptops.

#

Re:Already works with Ubuntu.

Posted by: Anonymous Coward on June 07, 2006 02:00 PM
Agreed, it's the main reason I'm keepiong Kubuntu on my laptop in spite of a few pet peeves (the other reason being wireless network switching Just Works). It works just like Windows, no complaints here, and no script hacking either...

John

#

Re:Already works with Ubuntu.

Posted by: Anonymous Coward on June 07, 2006 03:02 PM
Worked for me from ubuntu 5.4 (or was it 5.10?)

#

Re:Already works with Ubuntu.

Posted by: Anonymous Coward on June 08, 2006 07:06 AM
Suse's suspend to disk has been working for me out of the box since version 9.2. I use Asus hardware.
It works on Dapper too now!

#

Re:Already works with Ubuntu.

Posted by: Anonymous Coward on June 10, 2006 12:28 AM
Unless you have an ATI card.

#

Cool!

Posted by: Anonymous Coward on June 07, 2006 12:58 PM
With a few changes, I can get the script to work on Debian Sarge with kernel.org 2.6.16.20. I needed to do two things: disable SMP to get the ACPI sleep kernel option, and add the kernel boot option "acpi_sleep=s3_bios" to get video (radeon) to work after resume. I had a clock issue I fixed by adding to the script:
"/etc/init.d/hwclock.sh stop" before suspend and
"/etc/init.d/hwclock.sh start" after resume

These messages look a bit scary:
Jun 6 21:41:08 hostname kernel: pnp: Failed to activate device 00:09.
Jun 6 21:41:08 hostname kernel: pnp: Failed to activate device 00:0a.
Jun 6 21:41:08 hostname kernel: ACPI Error (evevent-0312): No installed handler for fixed event [00000002] [20060127]

...but nothing is conspicuously wrong after resume.

I didn't need to alter XF86Config and I removed the temp file things from the script.

#

Re:Cool!

Posted by: Anonymous Coward on June 08, 2006 07:33 AM
I resumed the box after a night suspended and everything seems to work fine. HDD/CDROM DMA, sound, network, USB, clock, etc. It sure is nice having it come up in 5 seconds!

#

Re:Cool!

Posted by: Anonymous Coward on June 09, 2006 02:46 PM
Cool, I got it working too wiht Radeon too now using your kernel argument, thanks.

#

its automagic in Fedora Core 5

Posted by: Anonymous Coward on June 10, 2006 12:55 AM
Hibernate already works out-of-the-box in FC5, with both my 5-yr old HP laptop and my desktop.

Being able to suspend the desktop rocks, since I powerdown every night.

RFE: One improvement to gnome-power-manager I would like is to have the screen password-locked on resume, even if I'm not using password-lock in the screensaver.

#

Re:its automagic in Fedora Core 5

Posted by: Anonymous Coward on June 10, 2006 02:51 AM
It doesn't work out of the box with my laptop on FC5.

#

Re:its automagic in Fedora Core 5

Posted by: Anonymous Coward on June 17, 2006 06:08 AM
Works in Suse 10.1 out of the box as well.

#

Pipemania

Posted by: Anonymous Coward on June 22, 2006 03:17 AM
I've long had a similar script, but the graphics regularly choked it, even with chvt. I hope this is the long hoped for relief. But what a pipe to get the ID! How about:


        lspci | awk '/VGA/ { print $1 }' | sed -e 's@0000:@@' -e 's@:@/@'

Probably AWK can also do the formatting, but I dug out my old sed know how:


        lspci | sed -e '/VGA/!d' -e 's/<nobr> <wbr></nobr>.*//' -e 's@0000:@@' -e 's@:@/@' -eq

My preference is not to use any external tool, just the Shell:


        lspci | while IFS='<nobr> <wbr></nobr>:' read dir id type x; do [ $type = VGA ] && print $dir/$id && break; done

This doesn't do the 0000:, which is not needed on SuSE.

Now that would probably best be done in Perl! Then you could also read in the data into a variable rather than call 5 external proggies to handle the temp file (of which one rm is redundant).

#

Re:Pipemania

Posted by: Anonymous Coward on August 23, 2006 05:59 AM
Is there a way to automatically resume from S3?
i.e. when I use the command,

        echo -n mem ><nobr> <wbr></nobr>/sys/power/state,
the system goes to S3 suspend state.

How do I resume/wake up from S3?
Can I specify a time where I can say sleep for 60 seconds and wake up after 60 seconds automatically?

#

resume from suspend = shutdown

Posted by: Anonymous Coward on August 23, 2006 11:33 AM
Great tutorial, I've been struggling to get suspend working in Ubuntu and Xandros (dual booting). They both go into suspend properly as per the script, but when I press the power button, it resumes, and then starts the shutdown sequence and powers off completely.

Any ideas would be welcomed! It's not a laptop actually, it's a AMD Duron 800 with Asus motherboard. If I could get this beast of a desktop to sleep properly, it'd make my day! Thanks.

#

Re: resume from suspend = shutdown

Posted by: Anonymous [ip: 121.45.252.161] on August 12, 2007 02:05 PM
Sounds a bit like two scripts are responding to a power button press. Does the same thing happen if you just echo > disk directly?

#

How to run the script?

Posted by: Anonymous Coward on May 02, 2007 06:27 AM
Silly question, but it says:
"Now just make the suspend.sh script executable (chmod +x<nobr> <wbr></nobr>/usr/local/sbin/suspend.sh) and run it"

How do I run this script?

Thanks!

#

Hibernation and the RTC....

Posted by: Administrator on June 08, 2006 02:31 PM
Hi,

I noticed that your hibernate script did not include saving system time to RTC and more importantly restoring System time from RTC.

It would be advisable to include the latter since when you come out of hibernation/Suspend-to-ram the system clock will continue from the point when you hibernated thus will be late ---System time != RTC..... And also when you power down the the computer, guess what most distros saves the system time to RTC..... at least Slackware does by default. hehehe<nobr> <wbr></nobr>:)

Have fun!

#

How to suspend and hibernate a laptop under Linux

Posted by: Anonymous [ip: 151.56.153.208] on December 10, 2007 05:07 PM
ID=$(lspci |grep VGA |cut -d'.' -f1 |cut -d':' -f1) is easiest (and correct), btw

#

How to suspend and hibernate a laptop under Linux

Posted by: Anonymous [ip: 151.56.153.208] on December 10, 2007 05:12 PM
No, i misunderstood the command. Sorry.

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya