Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save JettIsOnTheNet/b0b38f1ea71e3f5595ba49cc4f82a3b7 to your computer and use it in GitHub Desktop.

Select an option

Save JettIsOnTheNet/b0b38f1ea71e3f5595ba49cc4f82a3b7 to your computer and use it in GitHub Desktop.
How to Enable Hibernation on Linux with LVM Disk Encryption

How-To

Enabling Hibernation beyond basic soft suspend is not always straight forward on Linux.

  • Note: Please read these instructions start to finish beforehand.

Depending on your configuration this can require:

  • BIOS update / BIOS settings to be changed
  • Being sure your hardware states are reported and supported in kernel
  • Choosing a proper way to mange sleep functions
  • Choosing how to set up your partitioning or swap file

Step 1: Choose your partitioning scheme when installing

There are 2 ways to set up your partitions.

  1. If you are using a swap partition, you will not be able to use LVM or disk encryption on your root boot disk.
  2. If you are using a swap file, you can use LVM or disk encryption on your root boot disk.

1 is the easier way to be sure hibernation modes work properly. However, it is not hard to make things work with a swap file, LVM and encryption. It simply requires a few additional steps.

Step 2: Boot your install, check available states

This will command will show the states currently available.

sudo cat /sys/power/state
freeze mem disk

If mem is shown, your mem state will likely be s2idle (hardware).

sudo cat /sys/power/mem_sleep
[s2idle]

If you have disk state available, check your disk states.

sudo cat /sys/power/disk
[platform] shutdown reboot suspend test_resume

Platform will freeze state to disk and allow hardware (BIOS) to take over state. Shutdown will freeze state to disk and shutdown. Reboot will freeze state to disk and reboot. Suspend will freeze state to disk and idle. Test_resume will test disk state is functioning and resume.

Step 2.1: I do not see x state.

Most likely, you will not have disk or freeze state available. If you have chosen to use LVM, Disk Encryption, or have BIOS settings which conflict, the kernel can not set or adjust states of the hardware.

Update your BIOS

Be sure you are using the most current version of your BIOS, and update it if neccessary.

Disable Secure Boot

Check BIOS UEFI and disable Secure Boot and any BIOS lock features. Reboot and check if you see disk states available.

Be sure power management features are enabled

Check BIOS settings to be sure power management features are enabled. Reboot and check if your see disk states available.

Step 3: Setting up for LVM / Disk Encription

Step 3.1: Set up the swap file

Check your fstab:

sudo cat /etc/fstab

If it has a swap entry, check if the swap is enabled. If not, skip ahead to: Configure a blank swap file.

sudo swapon -s

If swap is enabled, disable it.

sudo swapoff -a

Configure a blank swap file. This will reside on your LVM or encrypted root partition. If you have 8GB or 16GB RAM, the standard practice is to double your RAM to get your swap size.

sudo fallocate -l 16G /swapfile

Adjust the permissions of the swapfile.

sudo chown 0 /swapfile
sudo chmod 0600 /swapfile

Allocate the swap file.

sudo mkswap /swapfile

Enable the swap file

sudo swapon /swapfile

Check your new swap is available. It should report /swapfile as the current swap enabled and Type file.

sudo swapon -s

Now that swap is running, add it to your /etc/fstab. If you already have a swap partition or swapfile in your fstab, comment it out and add the new mount point for swap to the end of the file.

/swapfile   none    swap     sw      0       0

Step 3.2: Add the swapfile into grub bootloader defaults.

To resume, grub bootloader needs to be made aware of the swapfile so it can check as soon as the volume is mounted.

Get the UID of the root partition. Copy this value down.

sudo findmnt / -o UUID
UUID
12345789-1234-1234-1234-123456789098

Obtain the block 0 offset for the location where the swapfile starts on disk.

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

Edit /etc/default/grub to add the swapfile to grub bootloader.

GRUB_CMDLINE_LINUX_DEFAULT="quiet resume=UUID=12345789-1234-1234-1234-123456789098 resume_offset=1234567"

Update grub.

sudo update-grub

Wait for grub to rebuild, then reboot your machine.

Step 4: Log in and check states work

Check mem suspend state works. Your machine should go into suspend.

sudo echo "mem" > /sys/power/state

Check disk suspend state works. Your machine should go into full hibernate disk suspend. (This will take a few seconds.)

sudo echo "disk" > /sys/power/state

Step 6: Configure your daemon / userland software

Configure power management software to your liking. Lid closing. Powerbutton interactions. Sleep on idle etc. There are many options for this.

  • acpid
  • systemd
  • pm-utils
  • libsmbios (Dell)
  • TLP
  • powertop
  • xfce4-power-manager
  • gnome-power-manager
  • powerkit

Be sure that you only have 1 power management daemon running. A common problem is conflicting power management daemons or userspace utilities. Pick one and stick with it. Be sure that the others are removed or completely disabled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment