How to configure suspend-then-hibernate properly?

I have been trying to configure suspend-then-hibernate but I am afraid I don’t find any way to do it. The following is an excerpt from man systemctl:

suspend-then-hibernate
Suspend the system and hibernate it after the delay specified in systemd-sleep.conf. This will trigger activation of
the special target unit suspend-then-hibernate.target. This command is asynchronous, and will return after the hybrid
sleep operation is successfully enqueued. It will not wait for the sleep/wake-up or hibernate/thaw cycle to complete.

Having searched for systemd-sleep.conf the only hit seems to be:

/usr/share/man/man5/systemd-sleep.conf.5.gz

man systemd-sleep

SYSTEMD-SUSPEND.SERVICE(8) systemd-suspend.service SYSTEMD-SUSPEND.SERVICE(8)

NAME
systemd-suspend.service, systemd-hibernate.service, systemd-hybrid-sleep.service, systemd-suspend-then-hibernate.service,
systemd-sleep - System sleep state logic

SYNOPSIS
systemd-suspend.service

   systemd-hibernate.service

   systemd-hybrid-sleep.service

   systemd-suspend-then-hibernate.service

   /usr/lib/systemd/system-sleep

DESCRIPTION
systemd-suspend.service is a system service that is pulled in by suspend.target and is responsible for the actual system
suspend. Similarly, systemd-hibernate.service is pulled in by hibernate.target to execute the actual hibernation. Finally,
systemd-hybrid-sleep.service is pulled in by hybrid-sleep.target to execute hybrid hibernation with system suspend and
pulled in by suspend-then-hibernate.target to execute system suspend with a timeout that will activate hibernate later.

   Immediately before entering system suspend and/or hibernation systemd-suspend.service (and the other mentioned units,
   respectively) will run all executables in /usr/lib/systemd/system-sleep/ and pass two arguments to them. The first argument
   will be "pre", the second either "suspend", "hibernate", "hybrid-sleep", or "suspend-then-hibernate" depending on the
   chosen action. Immediately after leaving system suspend and/or hibernation the same executables are run, but the first
   argument is now "post". All executables in this directory are executed in parallel, and execution of the action is not
   continued until all executables have finished.

   Note that scripts or binaries dropped in /usr/lib/systemd/system-sleep/ are intended for local use only and should be
   considered hacks. If applications want to react to system suspend/hibernation and resume, they should rather use the
   Inhibitor interface[1].

   Note that systemd-suspend.service, systemd-hibernate.service, systemd-hybrid-sleep.service, and
   systemd-suspend-then-hibernate.service should never be executed directly. Instead, trigger system sleep with a command such
   as systemctl suspend or systemctl hibernate.

   Internally, this service will echo a string like "mem" into /sys/power/state, to trigger the actual system suspend. What
   exactly is written where can be configured in the [Sleep] section of /etc/systemd/sleep.conf or a sleep.conf.d file. See
   systemd-sleep.conf(5).

OPTIONS
systemd-sleep understands the following commands:

   -h, --help
       Print a short help text and exit.

   --version
       Print a short version string and exit.

   suspend, hibernate, hybrid-sleep, suspend-then-hibernate
       Suspend, hibernate, suspend then hibernate, or put the system to hybrid sleep.

SEE ALSO
systemd-sleep.conf(5), systemd(1), systemctl(1), systemd.special(7), systemd-halt.service(8)

Excerpt from man systemd-sleep:

Note that systemd-suspend.service, systemd-hibernate.service, systemd-hybrid-sleep.service, and
systemd-suspend-then-hibernate.service should never be executed directly. Instead, trigger system sleep with a command such
as systemctl suspend or systemctl hibernate.

Running systemctl suspend-then-hibernate will immediately turn off the screen but the power button of the laptop is still on. So I am not sure how to take this any further. Otherwise the system hibernate and wakes up successfully to and from a swapfile.

I didn’t find anything relative this alternative mode of suspension-hibernation in ArchWiki. Maybe I overlooked something.

I’ll appreciate greatly any pointers to a solution.

I found the file sleep.conf in/etc/systemd with this content.

This file is part of systemd.

systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

Entries in this file show the compile time defaults.
You can change settings by editing this file.
Defaults can be restored by simply deleting this file.

See systemd-sleep.conf(5) for details

[Sleep]
#AllowSuspend=yes
#AllowHibernation=yes
#AllowSuspendThenHibernate=yes
#AllowHybridSleep=yes
#SuspendMode=
#SuspendState=mem standby freeze
#HibernateMode=platform shutdown
#HibernateState=disk
#HybridSleepMode=suspend platform shutdown
#HybridSleepState=disk
#HibernateDelaySec=180min

Is this the right file to be modified? And if it is, is it enough to un-comment the third and the last line and set the delay time as needed?

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

Hi @kagetora13,
Great write up! Thanks for sharing!

It’s been a while since I’ve done this, but my first thoughts:

I’m almost positive this command will give you 12GB swap file, not 16

I thought fallocate was deprecated out a few kernels ago, but I could be wrong. I swear I remember reading that though.

2 Likes

100% correct on your part sir. I had tailored the notes for myself and reduced the swap size. I corrected it (thank you for the catch and keeping me honest).

Additionally I notice for allocating the swap file the Arch wiki uses the ‘dd’ command. This example allocated 16 GB

Use dd to create a swap file the size of your choosing. For example, creating a 16 GB swap file:

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

https://wiki.archlinux.org/title/Swap#Swap_file

1 Like

I’m not sure Manjaro is relevant here either. :wink:

Lol. Only for my wifes system. I haven’t rebuilt her computer yet :innocent:.

You need to edit sleep.conf and logind.conf. Here is what works for me:

sleep.conf
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it under the
#  terms of the GNU Lesser General Public License as published by the Free
#  Software Foundation; either version 2.1 of the License, or (at your option)
#  any later version.
#
# Entries in this file show the compile time defaults. Local configuration
# should be created by either modifying this file, or by creating "drop-ins" in
# the sleep.conf.d/ subdirectory. The latter is generally recommended.
# Defaults can be restored by simply deleting this file and all drop-ins.
#
# See systemd-sleep.conf(5) for details.

[Sleep]
#AllowSuspend=yes
#AllowHibernation=yes
AllowSuspendThenHibernate=yes
#AllowHybridSleep=yes
#SuspendMode=
#SuspendState=mem standby freeze
#HibernateMode=platform shutdown
#HibernateState=disk
#HybridSleepMode=suspend platform shutdown
#HybridSleepState=disk
HibernateDelaySec=30min
logind.conf
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it under the
#  terms of the GNU Lesser General Public License as published by the Free
#  Software Foundation; either version 2.1 of the License, or (at your option)
#  any later version.
#
# Entries in this file show the compile time defaults. Local configuration
# should be created by either modifying this file, or by creating "drop-ins" in
# the logind.conf.d/ subdirectory. The latter is generally recommended.
# Defaults can be restored by simply deleting this file and all drop-ins.
#
# Use 'systemd-analyze cat-config systemd/logind.conf' to display the full config.
#
# See logind.conf(5) for details.

[Login]
#NAutoVTs=6
#ReserveVT=6
#KillUserProcesses=no
#KillOnlyUsers=
#KillExcludeUsers=root
#InhibitDelayMaxSec=5
#UserStopDelaySec=10
#HandlePowerKey=poweroff
HandleSuspendKey=ignore
HandleHibernateKey=ignore
HandleLidSwitch=suspend-then-hibernate
#HandleLidSwitchExternalPower=suspend
#HandleLidSwitchDocked=ignore
#HandleRebootKey=reboot
#PowerKeyIgnoreInhibited=no
#SuspendKeyIgnoreInhibited=no
#HibernateKeyIgnoreInhibited=no
#LidSwitchIgnoreInhibited=yes
#RebootKeyIgnoreInhibited=no
#HoldoffTimeoutSec=30s
#IdleAction=ignore
#IdleActionSec=30min
#RuntimeDirectorySize=10%
#RuntimeDirectoryInodes=400k
#RemoveIPC=yes
#InhibitorsMax=8192
#SessionsMax=8192

Thanks @mbod for the suggestion!
I had just given up to configure suspention and hibernation on this particular

machine
$ inxi -M
Machine:   Type: Laptop System: LENOVO product: 82A2 v: Yoga Slim 7 14ARE05 serial: <superuser required> 
           Mobo: LENOVO model: LNVNB161216 v: SDK0J40709 WIN serial: <superuser required> UEFI: LENOVO v: DMCN38WW 
           date: 01/18/2020

after failing to set it up properly. I’ll look into what you have posted and report back here later on.

One key item to testing this is how you suspend your laptop. I notice my system would hibernate if I closed the lid of the laptop. But not hibernate if I manually suspended the laptop. After allot of playing around I just accept closing the lid and it suspending then hibernating was good enough for me. :stuck_out_tongue: