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?

Peter Mortensen
  • 1,029
  • 1
  • 8
  • 10
user356126
  • 599
  • 4
  • 11

1 Answers1

40

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….

don_crissti
  • 79,330
  • 30
  • 216
  • 245
Marcus Müller
  • 21,602
  • 2
  • 39
  • 54
  • 3
    Wow, @don_crissti, thanks for the extensive improvement! – Marcus Müller Dec 22 '22 at 12:53
  • 3
    Don't mention it. Always a pleasure joining forces with a fellow pirate ;) – don_crissti Dec 22 '22 at 20:41
  • 5
    Arrrr, drink yer next grog on me, mate! – Marcus Müller Dec 22 '22 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 '22 at 01: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 '23 at 18:16
  • @RJVB don't know! Please ask that in a new question post, not a comment :) – Marcus Müller Feb 18 '23 at 18:18
  • Someone else may know and it'd be a useful complement to the answer! The kernel docs do say "resume= [SWSUSP] Specify the partition device for software suspend" which seems to imply that this also controls behaviour during the hibernating dump, i.e. that it goes to the specified device. – RJVB Feb 18 '23 at 18:22
  • @RJVB please don't ask new questions in comments. That's a sensible rule – comments aren't searched. Also, aside from me, it's unlikely many other people will even read your comment. – Marcus Müller Feb 18 '23 at 18:24