Zum Inhalt

How to hibernate and resume from swap file in Ubuntu 20.04 using full disk encryption

On my laptop, I am running full disk encryption (LUKS with a single encrypted ext4 partition), and a single, large swap file as large as my system RAM (16GB).

Here's how to make it work:

Make your swapfile have at least the size of your systems RAM:

sudo swapoff /swapfile
sudo dd if=/dev/zero of=/swapfile bs=$(cat /proc/meminfo | grep MemTotal | grep -oh '[0-9]*') count=1024 conv=notrunc
sudo mkswap /swapfile
sudo swapon /swapfile

Note the UUID of the partiton containing your swapfile:

$ sudo findmnt -no SOURCE,UUID -T /swapfile
/dev/nvme0n1p5 20562a02-cfa6-42e0-bb9f-5e936ea763d0

Reconfigure the package uswsusp correctly:

sudo apt -y install uswsusp
sudo dpkg-reconfigure -pmedium uswsusp
# Answer "Yes" to continue without swap space
# Select "/dev/disk/by-uuid/20562a02-cfa6-42e0-bb9f-5e936ea763d0" replace the UUID with the result from the previous findmnt command
# Encrypt: "No"

Edit the SystemD hibernate service using sudo systemctl edit systemd-hibernate.service and fill it with the following content:

[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

Note the offset of your swapfile relative to the partition start:

$ sudo swap-offset /swapfile
resume offset = 34818

Tell grub to resume by editiing your etc/default/grub

GRUB_CMDLINE_LINUX_DEFAULT="resume=UUID=20562a02-cfa6-42e0-bb9f-5e936ea763d0 resume_offset=34818 quiet splash"

Update grub:

sudo update-grub

Create /etc/initramfs-tools/conf.d/resume

RESUME=UUID=20562a02-cfa6-42e0-bb9e-5e936ea763d0 resume_offset=34816
# Resume from /swapfile

Update initramfs:

sudo update-initramfs -u -k all

Now you can just hibernate your system with

sudo systemctl hibernate
Published inAllgemein

16 Comments

  1. Anonymous Anonymous

    Not sure if you have encrypted your swap, if not, your encryption password might leak….

  2. Anonymous Anonymous

    Well, after that you can't log in and out anymore

  3. Anonymous Anonymous

    sudo swapoff /swapfile
    sudo dd if=/dev/zero of=/swapfile bs=$(cat /proc/meminfo | grep MemTotal | grep -oh '[0-9]*') count=1024 conv=notrun
    sudo mkswap /swapfile
    sudo swapon /swapfile

    typo at line 2
    conv=notunc

  4. It's tested against a stock Ubuntu 20.04 installation, so I think you most likely have some other problem there 🤔

  5. Mauro Henrique Mulati Mauro Henrique Mulati

    Thanks for this post!

    It worked for me with the file /etc/initramfs-tools/conf.d/resume with:
    resume=UUID=20562a02-cfa6-42e0-bb9e-5e936ea763d0 resume_offset=34816

    And, also, perhaps there is a typo in the resume_offset value here?

  6. Hi, If you mean the number, that might differ from installation to installation.

  7. ***CAUTION***
    it did not work for me, the laptop hibernates but does not wake up and starts anew instead

    ***worse: now the shift keys do not work, could use the screen keyboard to log into my session, but have no clue about how to restore the shift keys, any help ?

  8. olivier olivier

    Shift keys are back, i do not know how…

    would be willing try it again if someone is interested in figuring out what's going on, debug and so on.

  9. … it was only temporary, shift keys are down again…

  10. Claudius Claudius

    This worked for me, thanks for the writeup!
    What's missing though is: can the system put itself into hibernation when the battery is low?
    Can I get a "hibernate"-button in the menu that also has "shut down"?

  11. christian christian

    I get this:
    chrbro@chrbro-ThinkPad-X201:~$ sudo swapoff /swapfile
    swapoff: /swapfile: swapoff fehlgeschlagen: Das Argument ist ungültig
    chrbro@chrbro-ThinkPad-X201:~$ sudo dd if=/dev/zero of=/swapfile bs=$(cat /proc/meminfo | grep MemTotal | grep -oh '[0-9]*') count=1024 conv=notrunc
    1024+0 Datensätze ein
    1024+0 Datensätze aus
    8134737920 Bytes (8,1 GB, 7,6 GiB) kopiert, 10,2299 s, 795 MB/s
    chrbro@chrbro-ThinkPad-X201:~$ sudo mkswap /swapfile
    mkswap: /swapfile: unsichere Zugriffsrechte 0644, 0600 wird empfohlen
    Auslagerungsbereich Version 1 wird angelegt, Größe = 7,6 GiB (8134733824 Bytes)
    keine Bezeichnung, UUID=456ccda6-37ba-45de-b4e4-5244ec631e2b
    chrbro@chrbro-ThinkPad-X201:~$ sudo swapon /swapfile
    swapon: /swapfile: unsichere Zugriffsrechte 0644, 0600 wird empfohlen
    swapon: /swapfile: wird übersprungen – scheint Löcher zu enthalten.
    chrbro@chrbro-ThinkPad-X201:~$

    Any idea how to fix this?

  12. Anonymous Anonymous

    WTF, These instructions are enabling the hybridization by saving the RAM into a unprotected, unencrypted partition??? and you have the ubuntu on full disk encryption??? its like putting your entire house into a gigantic safe, then hang the key to the safe outside of the safe available to the public, enjoy your false sense of security. -Prince

  13. Wrong, RAM gets suspended to a swapfile resisiding on the encrypted partition.

  14. Yes, read the error message and set 0600 permittions in the swapfile before mkswap 🙂

  15. fisk fisk

    I get:

    initramfs-tools configuration sets RESUME=UUID=e2b4846f-867a-4f84-a3d7-0f5d10d24a33

    On boot it also says "giving up waiting for suspend/resume device"

    I can get rid of this warning by setting RESUME=/dev/nvme0n1p7 instead of using the UUID, but then it also doesn't resume correctly.

    Stock 20.04 on XPS 13 9300.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht.

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.