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

22 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.

  16. Thanks a lot! Worked great for me on a Thinkpad T14. A small note: the commands use an offset of 34816 once but 34818 a few other times. For me, `swap-offset` returned 34816 which I then used every time.

  17. Jared Jared

    Thanks for this, worked great on the first try.

    There is one odd thing though; I was not asked for my password when restarting.

    It picked up right where I left off, without requiring a password.

    Is that to be expected? I wouldn't think so…

  18. That's a sure sign your system didn't hibernate to disk but rather suspend to RAM. Maybe try "systemctl hibernate"?

  19. Jared Jared

    Definitely not suspended, as it was rebooted to Windows 10.

    And I did use systemctl hibernate.

    Just restarted again, same result.

    Just occurred to me: perhaps for some reason my login was changed to login automatically.

    I will test that out later.

    Currently converting Windows to run in a VM on Ubuntu.

    Ubuntu has reached the point with 20.04 that everything works well enough that I can use it on my Lenovo P50. When I first tried it on this machine 3 years ago, Ubuntu could not properly handle dual displays or wireless.

  20. crimson_king crimson_king

    I didn't need to install uswsusp or edit the systemd service on Xubuntu 20.04.

    1) Ubuntu disables hibernation support for systemd-logind through PolicyKit. To enable it, follow this: https://askubuntu.com/questions/94754/how-to-enable-hibernation

    This will also enable the "Hibernate" option in the logoff dialogs and the power manager settings.

    2) Expand the swap file to the appropriate size

    3) Get the UUID of the partition where the swapfile resides and its resume offset. By the way, you can get the latter using:
    # filefrag -v /swapfile

    The number you need is on the first row, where it says "physical_offset". The number that ends with two dots e.g. 38912..

    I learned that from the Arch Wiki: https://wiki.archlinux.org/index.php/Power_management/Suspend_and_hibernate#Hibernation_into_swap_file

    4) Configure GRUB as originally instructed.

    5) In the Initramfs settings, don't use RESUME in uppercase. Use it in lowercase: resume=UUID=

    Otherwise when updating the initramfs, it will warn you that it couldn't find a swap device by that UUID.

    6) ??? profit

  21. By the way,

    If you kind of person who like to manually turn-off turn-on swap, you can add your swap file on /etc/fstab with content, such as:

    /swapfile none swap sw 0 0

    Therefor, your swap file will not disappear after you turn it on again.

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.