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
Not sure if you have encrypted your swap, if not, your encryption password might leak….
Well, after that you can’t log in and out anymore
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
Fixed, thank you!
It’s tested against a stock Ubuntu 20.04 installation, so I think you most likely have some other problem there 🤔
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?
Hi, If you mean the number, that might differ from installation to installation.
***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 ?
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.
… it was only temporary, shift keys are down again…
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“?
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?
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
Wrong, RAM gets suspended to a swapfile resisiding on the encrypted partition.
Yes, read the error message and set 0600 permittions in the swapfile before mkswap 🙂
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.
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.
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…
That’s a sure sign your system didn’t hibernate to disk but rather suspend to RAM. Maybe try „systemctl hibernate“?
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.
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
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.
worked for me, many thanks for sharing!
it worked, thanks for the article
Thank you so much! It works like a charm! (Xubuntu 20.04)
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?
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).
This article explicitly has the swap file on the encrypted root partition.
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.
typo at:
RESUME=UUID=20562a02-cfa6-42e0-bb9e-5e936ea763d0 resume_offset=34816
should be 34818 accordingly to swap-offset command.
not exactly… it differs from time to time
not exactly – it’s different for installations. Most of the time it’s around 34000, but some users hat totally different numbers.
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!
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.
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=)
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
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.
Thanks a lot, just tested it and it works!
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.
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