Enable Hibernation on Ubuntu 22.04 with LVM and Full Disk Encryption

When settin up my new laptop I forgot to enable hibernation manually and the device deep decharged over night. It would only power on again after getting the original power supply from the office, since my USB charger had 63W instead of 65W according to Tuxedo support. Anyways, the following steps I have taken from Ask Ubuntu worked flawlessly.

Check if there is a swap partition in use

sudo swapon -s

Disable the current swap partition

sudo swapoff -a

and remove the record from /etc/fstab and add a new record for the new swap file we will create in the next step. My /etc/fstab file looks like the following, after the old record has been commented out (#) and the new record for the swapfile has been added.

# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
/dev/mapper/vgubuntu-root /               ext4    errors=remount-ro 0       1
# /boot was on /dev/nvme0n1p2 during installation
UUID=e62203cc-4593-4651-b239-c9d6c8710e2e /boot           ext4    defaults        0       2
# /boot/efi was on /dev/nvme0n1p1 during installation
UUID=6FC6-A930  /boot/efi       vfat    umask=0077      0       1
# Original swap partition
#/dev/mapper/vgubuntu-swap_1 none            swap    sw              0       0
/swapfile   none    swap     sw      0       0

Since the entire disk is encrypted, the swapfile is encrypted too.

Then create the new swapfile and ensure it is big enough to hold the entire memory plus some buffer. I simpy created 40 GB just to be sure.

sudo fallocate -l 32G /swapfile
sudo chown 0 /swapfile
sudo chmod 0600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

Reboot with sudo reboot and check if the new swap file is in use with sudo swapon -s

Filename				Type		Size		Used		Priority
/swapfile                               file		41943036	0		-2

Lookin good!

To enable hibernation, we have to tell the bootloader grub that there is a swapfile to resume from. To do this, we have to get the UUID from the root partition / using the following command.

findmnt / -o UUID

The output will be similar to this

UUID
4674b60a-68b3-427c-9b0e-124866c78828

Then we have to get the offset from the swapfile

sudo filefrag -v /swapfile |grep " 0:"| awk '{print $4}'

This will return the offset on position 0

sudo filefrag -v /swapfile |grep " 0:"
   0:        0..       0:    3717120..   3717120:      1:

In my case the offset is 3717120.

Now add the UUID of the root partition and the offset to the grub command as follows. Open the config file with vim /etc/default/grub and add the following parameters to your existing line.

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash resume=UUID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX resume_offset=XXXXX"

My final grub config looks like this, your mileage will be different

# If you change this file, run 'update-grub' afterwards to update
# /boot/grub/grub.cfg.
# For full documentation of the options in this file, see:
#   info -f grub -n 'Simple configuration'

GRUB_DEFAULT=0
GRUB_TIMEOUT_STYLE=hidden
GRUB_TIMEOUT=0
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
#GRUB_CMDLINE_LINUX_DEFAULT="quiet splash i915.enable_psr=0"
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash i915.enable_psr=0 i915.enable_dc=0 intel_idle.max_cstate=2 resume=UUID=4674b60a-68b3-427c-9b0e-124866c78828 resume_offset=3717120"
GRUB_CMDLINE_LINUX=""

# Uncomment to enable BadRAM filtering, modify to suit your needs
# This works with Linux (no patch required) and with any kernel that obtains
# the memory map information from GRUB (GNU Mach, kernel of FreeBSD ...)
#GRUB_BADRAM="0x01234567,0xfefefefe,0x89abcdef,0xefefefef"

# Uncomment to disable graphical terminal (grub-pc only)
#GRUB_TERMINAL=console

# The resolution used on graphical terminal
# note that you can use only modes which your graphic card supports via VBE
# you can see them in real GRUB with the command `vbeinfo'
#GRUB_GFXMODE=640x480

# Uncomment if you don't want GRUB to pass "root=UUID=xxx" parameter to Linux
#GRUB_DISABLE_LINUX_UUID=true

# Uncomment to disable generation of recovery mode menu entries
#GRUB_DISABLE_RECOVERY="true"

# Uncomment to get a beep at grub start
#GRUB_INIT_TUNE="480 440 1"

Finally, udate grub with sudo update-grub and restart with reboot. Then you can try to hibernate with sudo systemctl hibernate.