Howto Enable hibernation mode on Ubuntu 20.04 to hibernate on lid close.
Please notice that Midnight Commander (mcedit) is used for editing files. You may use gedit, pico, vi, vim or what ever editor you are familiar with too.
To save file changes using mcedit, press [F2].
To exit mcedit press [Esc]
[code]
you@home:$ sudo swapoff /swapfile
you@home:$ sudo dd if=/dev/zero of=/swapfile bs=$(cat /proc/meminfo | grep MemTotal | grep -oh '[0-9]*') count=1024 conv=notrunc
you@home:$ sudo mkswap /swapfile
you@home:$ sudo swapon /swapfile
you@home:$ sudo findmnt -no SOURCE,UUID -T /swapfile
/dev/sda5 10101a01-cfa1-01e0-bb1f-0e101ea010d0
[/code]
!Note UUID!
[code]
you@home:$ sudo apt -y install uswsusp
you@home:$ sudo dpkg-reconfigure -pmedium uswsusp
# Answer "Yes" to continue without swap space
# Select "/dev/disk/by-uuid/10101a01-cfa1-01e0-bb1f-0e101ea010d0" replace the UUID with the result from the previous findmnt command
# Encrypt: "No"
[/code]
[code]
you@home:$ sudo systemctl edit systemd-hibernate.service
[/code]
[code]
[Service]
ExecStart=
ExecStartPre=-/bin/run-parts -v -a pre /lib/systemd/system-sleep
ExecStart=/usr/sbin/s2disk
ExecStartPost=-/bin/run-parts -v --reverse -a post /lib/systemd/system-sleep
[/code]
[code]
you@home:$ sudo swap-offset /swapfile
resume offset = 34818
[/code]
!Note offset!
[code]
you@home:$ sudo mcedit /etc/default/grub
[/code]
[code]
GRUB_CMDLINE_LINUX_DEFAULT="resume=UUID=10101a01-cfa1-01e0-bb1f-0e101ea010d0 resume_offset=34818 quiet splash"
[/code]
[code]
you@home:$ sudo update-grub
[/code]
[code]
you@home:$ sudo mcedit /etc/initramfs-tools/conf.d/resume
[/code]
[code]
RESUME=UUID=10101a01-cfa1-01e0-bb1f-0e101ea010d0 resume_offset=34816
[/code]
[code]
you@home:$ sudo update-initramfs -u -k all
[/code]
[code]
you@home:$ sudo systemctl hibernate
[/code]
[code]
you@home:$ sudo mcedit /etc/systemd/logind.conf
[/code]
[code]
## Find line
# HandleLidSwitch=suspend
## Uncomment and change to
HandleLidSwitch=hibernate
[/code]
[code]
you@home:$ sudo systemctl restart systemd-logind.service
[/code]
Special thanx @rephlex https://rephlex.de/blog/2019/12/27/how-to-hibernate-and-resume-from-swap-file-in-ubuntu-20-04-using-full-disk-encryption/