How to configure suspend-then-hibernate properly?

Hi,

I actually have this working on my laptop and have included my notes below on how I set it up for a SWAP File.

Linux | Setup Suspend\Hibernation to Swap File

To save the most amount of power it is possible to configure your system to:

(1) Suspend to Memory.

(2) After delay of 15 mins write RAM to SWAP File\Partition and shutdown.

Steps:

(1) Create a Swap File

(B) Create Swap File

B1. Creating and initializing a new swap file (example assumes 16 GB swap file)

dd if=/dev/zero of=/swapfile bs=1M count=16384 status=progress

Then run the following command:

sudo mkswap /swapfile

B2. Setting permissions for your new swap file

sudo chmod 0600 /swapfile

B3. Enabling your new swap file

sudo swapon /swapfile

B4. Make your changes permanent (update the “/etc/fstab” file)

Make sure Linux Operating System knows to use your swap file every single time it boots up. Do this by running the following command which posts an entry in the /etc/fstab file:

sudo bash -c “echo /swapfile none swap defaults 0 0 >> /etc/fstab”

B5. Reboot the system.

(2) Configure system to use SWAP for Suspend then Hibernate

Using a swap file requires also setting the resume=swap_device and additionally a resume_offset=swap_file_offset kernel parameters. See the kernel documentation.

swap_device is the the volume where the swap file resides and it follows the same format as for the root parameter. The value of swap_file_offset can be obtained by running filefrag -v swap_file, the output is in a table format and the required value is located in the first row of the physical_offset column.

(a) Get swap_device (findmnt -no UUID -T /swapfile).

findmnt -no UUID -T /swapfile
c9244e6e-3127-4c6e-99f1-f61c0efa81eb

(b) Get swap_file_offset

sudo filefrag -v /swapfile | awk ‘{ if($1==“0:”){print $4} }’

6600704…

OR

physical_offset =6600704…

sudo filefrag -v /swapfile

Filesystem type is: ef53
File size of /swapfile is 6442450944 (1572864 blocks of 4096 bytes)
ext: logical_offset: physical_offset: length: expected: flags:
0: 0… 0: 6600704… 6600704: 1:
1: 1… 18431: 6600705… 6619135: 18431: unwritten
2: 18432… 49151: 6623232… 6653951: 30720: 6619136: unwritten
3: 49152… 79871: 6653952… 6684671: 30720: unwritten
4: 79872… 110591: 6684672… 6715391: 30720: unwritten
5: 110592… 141311: 6715392… 6746111: 30720: unwritten
6: 141312… 172031: 6746112… 6776831: 30720: unwritten
7: 172032… 202751: 6776832… 6807551: 30720: unwritten
8: 202752… 210943: 6807552… 6815743: 8192: unwritten
9: 210944… 241663: 6848512… 6879231: 30720: 6815744: unwritten
10: 241664… 272383: 6879232… 6909951: 30720: unwritten
11: 272384… 303103: 6909952… 6940671: 30720: unwritten
12: 303104… 333823: 6940672… 6971391: 30720: unwritten
13: 333824… 364543: 6971392… 7002111: 30720: unwritten
14: 364544… 395263: 7002112… 7032831: 30720: unwritten
15: 395264… 425983: 7032832… 7063551: 30720: unwritten
16: 425984… 456703: 7063552… 7094271: 30720: unwritten
17: 456704… 487423: 7094272… 7124991: 30720: unwritten
18: 487424… 518143: 7124992… 7155711: 30720: unwritten
19: 518144… 548863: 7155712… 7186431: 30720: unwritten
20: 548864… 579583: 7186432… 7217151: 30720: unwritten
21: 579584… 610303: 7217152… 7247871: 30720: unwritten
22: 610304… 641023: 7247872… 7278591: 30720: unwritten
23: 641024… 671743: 7278592… 7309311: 30720: unwritten
24: 671744… 702463: 7309312… 7340031: 30720: unwritten
25: 702464… 733183: 7372800… 7403519: 30720: 7340032: unwritten
26: 733184… 763903: 7403520… 7434239: 30720: unwritten
27: 763904… 794623: 7434240… 7464959: 30720: unwritten
28: 794624… 825343: 7464960… 7495679: 30720: unwritten
29: 825344… 856063: 7495680… 7526399: 30720: unwritten
30: 856064… 886783: 7526400… 7557119: 30720: unwritten
31: 886784… 917503: 7557120… 7587839: 30720: unwritten
32: 917504… 948223: 7587840… 7618559: 30720: unwritten
33: 948224… 978943: 7618560… 7649279: 30720: unwritten
34: 978944… 1009663: 7649280… 7679999: 30720: unwritten
35: 1009664… 1040383: 7680000… 7710719: 30720: unwritten
36: 1040384… 1071103: 7710720… 7741439: 30720: unwritten
37: 1071104… 1101823: 7741440… 7772159: 30720: unwritten
38: 1101824… 1132543: 7772160… 7802879: 30720: unwritten
39: 1132544… 1163263: 7802880… 7833599: 30720: unwritten
40: 1163264… 1193983: 7833600… 7864319: 30720: unwritten
41: 1193984… 1224703: 7897088… 7927807: 30720: 7864320: unwritten
42: 1224704… 1255423: 7927808… 7958527: 30720: unwritten
43: 1255424… 1259519: 7958528… 7962623: 4096: unwritten
44: 1259520… 1290239: 7964672… 7995391: 30720: 7962624: unwritten
45: 1290240… 1320959: 7995392… 8026111: 30720: unwritten
46: 1320960… 1351679: 8026112… 8056831: 30720: unwritten
47: 1351680… 1382399: 8056832… 8087551: 30720: unwritten
48: 1382400… 1413119: 8087552… 8118271: 30720: unwritten
49: 1413120… 1443839: 8118272… 8148991: 30720: unwritten
50: 1443840… 1474559: 8148992… 8179711: 30720: unwritten
51: 1474560… 1505279: 8179712… 8210431: 30720: unwritten
52: 1505280… 1535999: 8210432… 8241151: 30720: unwritten
53: 1536000… 1566719: 8241152… 8271871: 30720: unwritten
54: 1566720… 1572863: 8271872… 8278015: 6144: last,unwritten,eof
/swapfile: 6 extents found

(c) Set the Kernel Parameter:

resume=<swap_device> resume_offset=<swap_file_offset>

resume=UUID=c9244e6e-3127-4c6e-99f1-f61c0efa81eb resume_offset=6600704…

The below configuration assumes that the system will automatically Hibernate after being suspended for 30 minutes.

(A) Find the swap partition, and add the UUID to the GRUB_CMDLINE_LINUX_DEFAULT.

(B) Add the resume=UUID= to “/etc/default/grub”

Resume UUID set to enable hybernation, will write RAM to swap

GRUB_CMDLINE_LINUX_DEFAULT=“resume=UUID= loglevel=3 quiet”

Example:

(d) Final Grub Configuration line.
GRUB_CMDLINE_LINUX_DEFAULT=“resume=UUID=c9244e6e-3127-4c6e-99f1-f61c0efa81eb resume_offset=6600704… i915.enable_psr=1 apparmor=1 lsm=lockdown,yama,apparmor”

(b2) From there, you can start hibernating by adding the MAJ:MIN values for the swap partition to /sys/power/resume with the following command[2]:

sudo echo 259:2 > /sys/power/resume

(c3) Configure the initramfs

When an initramfs with the base hook is used, which is the default, the resume hook is required in /etc/mkinitcpio.conf. Whether by label or by UUID, the swap partition is referred to with a udev device node, so the resume hook must go after the udev hook. This example was made starting from the default hook configuration:

Example:

HOOKS=(base udev autodetect keyboard modconf block filesystems resume fsck)

sudo gedit /etc/mkinitcpio.conf

HOOKS=(base udev autodetect modconf block keyboard keymap filesystems resume fsck)

(c4) Regenerate the initramfs.

sudo mkinitcpio -P

(c5) Update the GRUB configuration.

sudo grub-mkconfig -o /boot/grub/grub.cfg

(c6) Reboot the Operating System to apply the changes.

(D) Configure Suspend-Then-Hibernate

Once it’s possible to enter & wake from the hibernation state, we can then transition to the suspend-then-hibernate step. For this, we have to configure systemd to handle entering hibernation from the suspend state after a given period.

(D1) Set a delay in the sleep.conf, which will inform systemd how long to stay in the suspend state before hibernating.

(a) Add the following to the “/etc/systemd/sleep.conf” file.

[Sleep]
AllowSuspendThenHibernate=yes
HibernateState=disk
#Suspend then Hibernate after 15 min
HibernateDelaySec=15min

(D2) Inform systemd the behavior of the power switch, lid closing, and suspend state.

(a) Add the following to “/etc/systemd/logind.conf”

[Login]
HandlePowerKey=suspend-then-hibernate
HandleLidSwitch=suspend-then-hibernate

(E) Quick Recovery from Hibernation (optional)

(E1) To speed up recovering from hibernation is to reduce the time delay the grub boot menu from 4 seconds to 1.

(a) sudo gedit /etc/default/grub

(b) Update the setting “GRUB_TIMEOUT=1”.

(c) Update the GRUB configuration.

sudo grub-mkconfig -o /boot/grub/grub.cfg

(d) Reboot the OS

Source:

Create a Swap Partition\File

https://mattjones.tech/2020/01/08/how-to-add-swap-in-manjaro/

Configuring Hibernation

https://wiki.archlinux.org/title/Swap#Swap_file
https://wiki.archlinux.org/index.php/Power_management/Suspend_and_hibernate
https://jlk.fjfi.cvut.cz/arch/manpages/man/sleep.conf.d.5

2 Likes