27

When a Linux system hibernates and resumes from hibernation, I think that the kernel needs to know that it's resuming, not booting. Otherwise, the kernel will start the usual boot process, and will not load the swapped data. How does the kernel know it's resuming?

At first, I thought that GRUB (or another boot loader) tells the kernel by kernel parameters. But as long as I check /proc/cmdline, the kernel parameters are the same as usual. Is there any mechanism for the kernel to know it's resuming?

0

1 Answer 1

41

Hibernation works by using a swap partition¹ to swap out all processes' memories, the kernel state, finally save some state of CPU and possibly other devices, then power off. On the way, it notes in the swap partition that this is a hibernation image.

Resuming from hibernation works by telling the kernel to try resuming from a swap partition, using the resume= kernel argument, resume=/dev/sda4 (if /dev/sda4 is your swap partition), resume=UUID=deadbeef-cafe-b00b-1337-123456123456 or similar.

The kernel during boot then looks into that partition, finds the note in the swap partition that says "hey, this is a hibernation image", and restores device, kernel and processes from that. If the note is not there, it just boots as normal.


You can check the source code, specifically the description of the software_resume() function:

* software_resume - Resume from a saved hibernation image.
*
* This routine is called as a late initcall, when all devices have been
* discovered and initialized already.
*
* The image reading code is called to see if there is a hibernation image
* available for reading.  If that is the case, devices are quiesced and the
* contents of memory is restored from the saved image.

So this involves two instances of the kernel, the "boot kernel" and "the image kernel" and the process is described in the official kernel documentation which also explains why this isn't done via the bootloader:

Although in principle the image might be loaded into memory and the pre-hibernation memory contents restored by the boot loader, in practice this can’t be done because boot loaders aren’t smart enough and there is no established protocol for passing the necessary information. So instead, the boot loader loads a fresh instance of the kernel, called “the restore kernel”, into memory and passes control to it in the usual way. Then the restore kernel reads the system image, restores the pre-hibernation memory contents, and passes control to the image kernel. Thus two different kernel instances are involved in resuming from hibernation.


¹ These days, can also be a swap file on many (most?) file systems, or an LVM volume….

8
  • 3
    Wow, @don_crissti, thanks for the extensive improvement! Dec 22, 2022 at 12:53
  • 3
    Don't mention it. Always a pleasure joining forces with a fellow pirate ;) Dec 22, 2022 at 20:41
  • 5
    Arrrr, drink yer next grog on me, mate! Dec 22, 2022 at 20:43
  • @Marcus Müller Thank you for your answer. Your answer really made me understand what happens during hibernation and resumption. I also understande the relationship between the boot kernel and image kernel.
    – user356126
    Dec 27, 2022 at 1:39
  • So, I understand that you need to add the resume parameter which will make the kernel check if a hibernation image exists on the specified swap volume. Does the use of 2 kernel images mean the resume will work even if you do not pick the same kernel version in the 1st stage? And more importantly, how does this work when you have multiple swap partitions? Can you use the resume argument multiple times, or is there a way to control which partition is used for the hibernation image? Thx!
    – RJVB
    Feb 18 at 18:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .