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

NOTE: THE OFFSET IS DIFFERENT FROM SYSTEM TO SYSTEM! YOU NEED TO USE THE VALUE RETURNED ON YOUR SYSTEM!

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

40 Kommentare zu „How to hibernate and resume from swap file in Ubuntu 20.04 using full disk encryption“

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

  2. 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?

  3. ***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 ?

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

  5. 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“?

  6. 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?

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

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

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

  10. 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…

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

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

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

  14. Questions to understand it right:
    We need two swapfiles, one for swapping during execution and one dedicated swapfile for hibernating only?
    Or can we use one swapfile for both ?
    How we handle an entry in /etc/fstab?

  15. Hi, you use one swapfile, and you remove a dedicated Swap partition if you have one.

    The fstab entry is identical to the entry you already have for a dedicated partition, just replace your swap blockdevice in the first column (e.g. /dev/sdaX) with the filename of the swapfile (e.g. /swapfile.16G or whatever).

  16. Hi , I am running Lubuntu 20.04 on my old eeepc. I tried all these step the Author has described and the computer doesn’t hibernate. I tried the GUI method of clicking on ‚Hibernate‘ radio button and also the CLI method using the ’sudo systemctl hibernate‘ line. The display dims and eventually becomes completely black. However the computer doesn’t turnoff. I have waited for about 5 min and no disk activity is shown by the hdd led so i assume nothing is going to happen.

  17. typo at:
    RESUME=UUID=20562a02-cfa6-42e0-bb9e-5e936ea763d0 resume_offset=34816
    should be 34818 accordingly to swap-offset command.

  18. Awesome! Thanx.
    I‘ m using hibernation mode since Ubuntu 14.04. Each version, installation and tutorial had its stumbling block and pit falls.
    That’s the first Ubuntu Release and the first short tutorial that works just as it is!

    Good Job!

  19. Great Job!

    Seems for me this was the main part required to make it work (I dont use encrypted disks):

    sudo dpkg-reconfigure -pmedium uswsusp
    and then create the systemd-hibernate.service

    I did not need to use the swapp offset.

  20. Vielen dank, Florian. This works great.
    However, as crimson_king has helpfully pointed out, when running the command, „sudo update-initramfs -u -k all“, if you get a message saying, ‚but no matching swap device is available‘, then the entry in /etc/initramfs-tools/conf.d/resume MUST start with a lowercase resume= (as opposed to RESUME=)

  21. Great post ! It worked.

    Just a couple of details:
    1) `etc/default/grub` is missing a `/` at the beginning
    2) As offset in some places you have 34818 and in other 34816. I know I had to replace with my value but 2 different numbers might be confusing for the reads

  22. I get this error (after fixing the resume RESUME error):

    emil@test ~ $ sudo systemctl hibernate
    Failed to hibernate system via logind: Not enough swap space for hibernation

    emil@test ~ $ cat /proc/meminfo |grep Swap
    SwapCached: 0 kB
    SwapTotal: 100999416 kB
    SwapFree: 100999392 kB

    Clearly, there is enough swap file, some other bug.

  23. Thank you all for the good instructions.

    Below are the steps I followed on a no-systemd system (Devuan Chimaera 4.0) to achieve the same.
    I add them here hoping to prove useful to someone.

    Assumptions:
    a1. The entire disk is encrypted containing ALL filesystems (e.g. /boot, /, /home, ..).
    a2. No swap partition is used; only a swap file
    a3. Swap file is located at /swapfile
    a4. A swap file entry exists in /etc/fstab e.g.:
    /swapfile none swap sw 0 0

    1. Deactivate any existing swap file and delete it.
    # sudo swapoff /swapfile
    # sudo rm /swapfile

    2. Create the file. This depends on the swaping needs of the system. It is a different discussion, but I suggest a minimum value of count=1024 (that is swap file the same size as your available memory as calculated below). Below values worked for me.

    # sudo dd if=/dev/zero of=/swapfile bs=$(cat /proc/meminfo | grep MemTotal | grep -oh ‚[0-9]*‘) count=2048 conv=notrunc

    3. Change permissions of the new file
    # sudo chmod 0600 /swapfile

    4. Create the swap file and activate it. You may check the results with any tool (e.g. htop).
    # sudo mkswap /swapfile
    # sudo swapon /swapfile

    5. Find the UUID of the file system where the swap file resides, as explained by @crimson_king
    # sudo findmnt -no SOURCE,UUID -T /swapfile

    6. Find the value for the resume_offset, as explained by @crimson_king
    # sudo filefrag -v /swapfile

    7. Change /etc/defaults/grub file. Values for UUID and resume_offset as found above

    GRUB_CMDLINE_LINUX_DEFAULT=“resume=UUID=536d0de3-cb24-4d54-9ea4-fb4672110910 resume_offset=1933312 quiet splash“

    8. Create file /etc/initramfs-tools/conf.d/resume with below two lines

    resume=UUID=536d0de3-cb24-4d54-9ea4-fb4672110910
    resume_offset=1933312

    9. Execute updates to grub and initramfs
    # sudo update-grub
    # sudo update-initramfs -u -k all

    10. Hibernate and then resume your system

    On system resume, I was asked for the disk encryption password once by GRUB and once when kernel tried to mount root filesystem.

  24. Hi,
    thanks for the detailed article.
    I’ve got an error during the update-initramfs:

    update-initramfs: Generating /boot/initrd.img-5.15.13-051513-generic
    cryptsetup: ERROR: Couldn’t resolve device 6f9e1e11-294c-4ff3-8008-5dea89738e1a

    The UUID of the swapfile was copied and pasted where requested, but it seems that update-initramfs doesn’t recognize the /swapfile UUID as a valid swap space.
    My distro is KDE Neon 20.04, based on Ubuntu 20.04.
    Any suggestion?

    Thanks

Kommentar verfassen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

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

Nach oben scrollen