Custom Initramfs/Hibernation

From Gentoo Wiki
Jump to:navigation Jump to:search
This article has been flagged for not conforming to the wiki guidelines. Please help Gentoo out by starting fixing things.
This article is a stub. Please help out by expanding it - how to get started.

This article describes how to enable hibernation with a custom initramfs.

Hibernation (suspend to disk) is a special case. If you intend to use it in your Custom Initramfs, you need to be aware of the pitfalls, or you will suffer from utter and complete data loss. If you intend to use this, you should have a really good understanding of how Linux works, particularly how its various storage layers play together. Alternatives are available. You could Suspend to RAM, which does not involve initramfs at all. Or you could just shutdown properly...

Warning
To quote relevant Linux Documentation: (swsusp.txt, swsusp-dmcrypt.txt and swsusp-and-swap-files.txt)
If you touch anything on disk between suspend and resume...
...kiss your data goodbye.
If you do resume from initrd after your filesystems are mounted...
...bye bye root partition.

This article assumes you already have written your custom and fully operational /init and now wish to add Hibernation support to it. If you are not using a Custom Initramfs, or for a less specific introduction to the topic, see Suspend and hibernate instead.

Theory

In theory, the process is simple. To suspend to disk, you could do something like

root #echo disk > /sys/power/state

and the kernel will dump the entire memory state to your swap partition and power off the computer. When you turn it back on, your /init might do something like

root #echo /dev/SWAP > /sys/power/resume

and the kernel will load the dump and continue where it left off.

Pitfalls

Of course, things are not that simple. When you suspend to disk and the computer powers off - the computer isn't powered off at all! It's still up and running! Or at least, that's what the software believes. The box may be off physically, but from a kernel/userspace point of view,

  • the system is still up and running,
  • filesystems are still mounted read-write,
  • although you forgot to save your thesis it's still open in the editor,
  • your RAID is still in mid-reshape,
  • your LVM is working hard to get those extents relocated,
  • your database is still in the middle of a transaction,
  • the movie you shot at your last vacation is still in the middle of re-encoding,

and so on and so forth. As you can see the machine is doing quite a lot while consuming zero power. It powered off without shutting down.

Note
After suspend to disk, your system is in Hibernation and your disks and filesystems are in an unclean state.
Warning
While in Hibernation, until you resume, you must avoid changes to those disks at any and all costs.

Do you see the problem yet? You turn your machine back on. Your initramfs does what it always does: it starts RAID (which automatically continues the reshape), LVM (might resume block relocations), maybe it mounts something because it needs a file or key or program, and then you resume.

The resume sets the state of memory back in time. It does not undo changes you made to disk, so ... suddenly the RAID reshapes already reshaped data (bye RAID), LVM relocates already relocated extents (bye LVM), the filesystem state is completely not what it's supposed to be (bye all your files), the machine crashes and your doctorate thesis is gone forever (use a typewriter next time, will you?).

Before Hibernation, the goal of the initramfs was to reach the root partition.

After Hibernation, the goal of the initramfs is to reach the swap partition while staying in hard read-only land.

Kernel configuration

KERNEL CONFIG_SWAP=y CONFIG_HIBERNATION=y
General setup  --->
    [*] Support for paging of anonymous memory (swap)

Power management and ACPI options  --->
    [*] Hibernation (aka 'suspend to disk')

Resume from

regular partition

LVM

LUKS

Swapfile