Arch Linux KDE Hibernate Configuration
2024-11-19  / Arch Linux Series
Power Management/Suspend and Hibernate

Usually, on Linux, there are various methods available for suspending the system. The system state is saved in memory or on the disk, entering a low-power mode. When the computer is resumed, it allows for a quick recovery to the previous state.

  • Suspend to RAM (Suspend to memory, commonly known as suspend/sleep): Saves the current system state in RAM by powering down most components in the machine that are not related to RAM. In this case, RAM is necessary to restore the machine’s state and must continue to be powered. Since the system state is stored in RAM, the system recovery speed is very fast.
  • Suspend to disk (Suspend to hard disk, commonly known as hibernate): Saves the current system state on the disk (usually in a swap partition or swap file) and then turns off the power. When resuming, the system recovers data from the disk. Compared to the RAM method, hibernation can maintain the system state for a longer period, but the recovery time is longer. This article mainly introduces how to enable and configure hibernation mode.
  • Suspend to both (Suspend to both/hybrid suspend): Combines the advantages of suspending to RAM and disk methods. It saves the current system state on the disk (swap space) and then enters sleep mode while keeping RAM powered. When resuming, the system will first recover quickly from RAM, and if RAM is lost, it will recover from the disk. For example, if the battery is depleted, the system will recover from the disk.

Adding Swap

Swap - ArchWiki

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# If there is no swap space currently, a new swap needs to be created
# Use dd to create a 15 GiB swap file
$ sudo dd if=/dev/zero of=/swapfile bs=1M count=15k status=progress

# Set the right permissions (a world-readable swap file is a huge local vulnerability):
$ sudo chmod 0600 /swapfile

# After creating the correctly sized file, format it to swap:
$ sudo mkswap -U clear /swapfile
mkswap: /swapfile: warning: wiping old swap signature.
Setting up swapspace version 1, size = 15 GiB (16106123264 bytes)
no label, UUID=00000000-0000-0000-0000-000000000000

# Activate the swap file:
$ sudo swapon /swapfile

# Finally, edit the fstab configuration to add an entry for the swap file:
$ sudo vim /etc/fstab
/swapfile none swap defaults 0 0

# Verify if the swap is active:
$ sudo swapon --show
NAME TYPE SIZE USED PRIO
/swapfile file 15G 256K -2

Hibernate

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Confirm the UUID of the partition where the swap file is located
$ findmnt -no UUID -T /swapfile
0fdd557a-e3bb-4db4-a93a-0d1ebcf75769

# Check the swapfile offset for use with resume_offset
$ sudo filefrag -v /swapfile | awk '{ if($1=="0:"){print substr($4, 1, length($4)-2)} }'

# The resume parameter is the partition where the swap file is located, which can be specified as /dev/nvme0n1p5 or UUID=0fdd557a-e3bb-4db4-a93a-0d1ebcf75769
# resume_offset is the physical offset of the file's beginning in this partition, which can be viewed with `filefrag -v /swapfile`.
resume=UUID=0fdd557a-e3bb-4db4-a93a-0d1ebcf75769 resume_offset=17278976

# Regarding the setting of kernel parameters, if using GRUB, append to GRUB_CMDLINE_LINUX_DEFAULT
$ sudo vim /etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT="loglevel=3 quiet resume=UUID=0fdd557a-e3bb-4db4-a93a-0d1ebcf75769 resume_offset=17278976"

# Regenerate grub.cfg
$ sudo grub-mkconfig -o /boot/grub/grub.cfg

###################

# If your initramfs does not use the systemd hook (uses base), then you also need to add a resume hook to attempt to recover from hibernation:
# Edit the /etc/mkinitcpio.conf file, add resume to any position after udev in HOOKS=(...).
$ sudo vim /etc/mkinitcpio.conf
# Finally, regenerate the initramfs:
$ sudo mkinitcpio -p linux
# The configuration work is all done, hibernate through `systemctl hibernate`, and then press the power button to start up and check if the hibernation function is working properly.

References