Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Friday, April 27, 2007

Hibernate Your Laptop

Hibernate has the ability to take a snapshot of the state of a computer's memory and save it to disk, and restore that image later. This allows you to fully power down a computer at an arbitrary point in time and not need to reload programs and reopen documents on power-up. In modern laptops, suspension is not done by the BIOS. They use ACPI and ask the operating system to do most of the job. If you can't satisfy yourself with standby mode (suspend to RAM) because you leave your computer in suspend mode for too long (or because you're not using a 2.5 or later kernel and therefore lack suspend-to-RAM support), then you have to suspend to disk. Now, we can hibernate into not only a swap partition, but also a file. And to complete this task, you should patch your linux kernel firstly. I practised the suspend2 in my notebook with Gentoo Linux. Also you can follow the HOWTO docuements at suspend2 official website, which is a nice tutorial. If you also use Gentoo, you need not patch the linux kernel manually. You just only run the command below:
# emerge suspend2-sources
And portage system will patch your linux kernel automaticly, then:
# cd /usr/src/linux
# make menuconfig
Select suspend2 options, here is the path:
  Power management options (ACPI, APM)  --->
  [*] Suspend2  ---> 
      --- Suspend2 
      ---   Image Storage (you need at least one allocator)
      [*]   File Allocator   
      [*]   Swap Allocator 
      ---   General Options 
      (swap:/dev/hda8) Default resume device name
      [*]   Allow Keep Image Mode
      [*]   Replace swsusp by default
And make sure your .config contains such settings, and "swap:/dev/hda8" is the default swap partition to be suspended:
CONFIG_SUSPEND2=y
CONFIG_SUSPEND2_FILE=y
CONFIG_SUSPEND2_SWAP=y
CONFIG_CRYPTO_LZF=y
CONFIG_SUSPEND2_DEFAULT_RESUME2="swap:/dev/hda8"
Then compile and install your new kernel, then reboot. *ATTATION* DO NOT forget to backup your original working kernel. If you find such content when "dmesg| less", (eg, "Software Suspend 2.1.8.9: Suspending enabled."), it means you can hibernate your computer now. Now, It's time to manage the hibernate actions. Firstly, fetch the hibernate-script by emerge or from suspend2 website:
# emerge hibernate-script
If you use Thinkpad, you can use the hotkey "Fn+F12" to hibernate with acpid. The event files lie in the directory /etc/acpi/events, and Here is the content of /etc/acpi/events/hibernate:
event=(button/power|ibm/hotkey HKEY 00000080 0000100c)
action=/usr/sbin/hibernate -F /etc/hibernate/suspend2.conf
The HKEY number 00000080 0000100c is the action to press the hotkey "Fn+F12". Here is the detail. If you mount vfat filesystems, you'd better to umount them, when hibernating. Put such settings into the configuration file /etc/hibernate/suspend2.conf
UnmountFSTypes vfat fat msdos
And remount them when re-booting your system with this setting in /etc/hibernate/ususpend-disk.conf:
Mount /path/to/vfat/

Sunday, October 01, 2006

howto sleep and hibernate on thinkpad

In Windows, we can use Fn-F4 to suspend system to memory, and Fn-F12 to suspend system to disk. It is really very expedite and useful. When using Linux as a desktop, we can also have such way to sleep and hibernate our system and even faciler.

Sleep is also called suspend (to memory), while hibernate is called suspend2 ( to disk). Here is the specific steps:

  1. It is necessary to compile your kernel with ibm-acpi support, if you do not want to download it by yourself, and make sure your kernel source version is new enough, because the latest kernel has contained it. Modify your kernel configure file, add this sentence, which means that ibm-acpi will be compiled as a module.

    CONFIG_ACPI_IBM=m

    If you wanna actuate this module when system startup, usually it is. You should add "ibm-acpi" into, for example, /etc/modules.autoload.d/kernel-2.6.

  2. Install the ACPI daemon "acpid", and let it run when your system boots.
  3. Download the ibm-acpi package from here: http://ibm-acpi.sf.net/. We only need its scripts. Decompress the package, and copy the directory "config" into the root directory.
  4. Download the tpb package from here: http://www.nongnu.org/tpb/. Complie and install it.
  5. To make hotkeys work, you should add this into the startup script, /etc/conf.d/local.start:
    echo enable,0xffff > /proc/acpi/ibm/hotkey

    In order to use tpb successfully, you should have the privilege to read the device nram. Thus, add this script into local.start also:

    chmod a+r /dev/nvram
    echo 1024 > /proc/sys/dev/rtc/max-user-freq
  6. Install hibernate-script, which can be found here: http://www.suspend2.net. If you just hope to sleep, you can write a script yourself. such as:
    echo "mem" > /sys/power/state
    

    It can also work successfully. If you want to use the whole functions of this hibernate-script, you have to patch your kernel source and recomplie it again. The patch can also be found at that website.

  7. Now, When you press ACPI key such as Fn-F4, then cat /var/log/acpid, you will find all ACPI events:
    [Sun Oct  1 17:54:44 2006] received event "ibm/hotkey HKEY 00000080 00001004"
    [Sun Oct  1 17:54:44 2006] executing action "/etc/acpi/default.sh ibm/hotkey HKEY 00000080 00001004"
    [Sun Oct  1 17:54:44 2006] BEGIN HANDLER MESSAGES
    [Sun Oct  1 17:54:44 2006] END HANDLER MESSAGES
    [Sun Oct  1 17:54:44 2006] action exited with status 0
    [Sun Oct  1 17:54:44 2006] executing action "/usr/sbin/hibernate -F /etc/hibernate/ram.conf"

    And this means you can use your function key to suspend your system. If everything is ok, your system will sleep into memory just now.

  8. There is some other tips to use xmodmap, touch ~/.Xmodmap, write this into it:
    keycode 227 = F35
    clear lock
    add mod4 = Caps_Lock

    The first line let you can use Fn key, and the next two lines let you use Caps_Lock key as Windows Key. Run xmodmap ~/.Xmodmap, try CAPS_LOCK, and check if its action has changed. So cool :)

The further detail can be found here: http://www.thinkwiki.org/wiki/How_to_get_special_keys_to_work#tpb_configuration