51

I have configured a new Ubuntu installation in a Notebook to use a swap file, instead of using a swap partition.

By default is not possible to get Ubuntu to hibernate using a swap file, so I tried this tutorial, but it is specific to grub1, and Ubuntu now uses grub2.

Does anybody know how to do this?

muru
  • 207,228
tfmoraes
  • 534

6 Answers6

67

Here is what I did to make it work with Ubuntu 18.04.

  • Make your /swapfile have at least the size of your RAM
sudo swapoff /swapfile
sudo dd if=/dev/zero of=/swapfile bs=$(cat /proc/meminfo | awk '/MemTotal/ {print $2}') count=1024 conv=notrunc
sudo mkswap /swapfile
sudo swapon /swapfile
  • Note the UUID of the partition containing your /swapfile:
$ sudo findmnt -no UUID -T /swapfile
20562a02-cfa6-42e0-bb9f-5e936ea763d0
  • Reconfigure the package uswsusp in order to correctly use the swapfile:
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 resume offset of your /swapfile:
$ sudo swap-offset /swapfile
resume offset = 34818
  • Configure Grub to resume from the swapfile by editing /etc/default/grub and modify the following line:
GRUB_CMDLINE_LINUX_DEFAULT="resume=UUID=20562a02-cfa6-42e0-bb9f-5e936ea763d0 resume_offset=34818 quiet splash"
  • Update Grub:
sudo update-grub
  • Create the following /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 hibernate with sudo systemctl hibernate.

One can also create those scripts:

sudo tee /usr/local/bin/gotosleep <<EOF
dbus-send --type=method_call --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.Lock
sleep 2
sudo /usr/sbin/s2both
EOF
sudo chmod +x /usr/local/bin/gotosleep
sudo tee /usr/local/bin/gotohibernation <<EOF
dbus-send --type=method_call --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.Lock
sleep 2
sudo systemctl hibernate
EOF
sudo chmod +x /usr/local/bin/gotohibernation

So you can sleep with gotosleep or hibernate with gotohibernation.

You must be able to execute sudo s2both, sudo s2ram and sudo systemctl hibernatewithout having to enter your password for the previous scripts to work.

You could do that for example by creating a powerdev group, add your current user to it, and configure the following sudoers config (edit it with sudo visudo -f /etc/sudoers.d/powerdev):

%powerdev ALL=NOPASSWD: /usr/sbin/s2both, /usr/sbin/s2ram, /bin/systemctl hibernate

Documentation used:

Anthony O.
  • 2,559
31

Hibernate with Swap file using uswusp

Although it is possible to hibernate to swap file and it supposedly works with systemd hibernate by setting kernel parameters. However, I couldn't get it to resume so instead switched to using uswsusp (userspace software suspend). Here are the steps I used on Ubuntu 17.04/17.10.

Create the Swap File

The commands to create a formatted 4GiB swap file, mounted and added to /etc/fstab:

sudo fallocate -l 4g /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile swap swap defaults 0 0' | sudo tee -a /etc/fstab

Verify Swap File Partition

sudo findmnt -no SOURCE,UUID -T /swapfile
> /dev/sda1 11cc33ee-1234-abcd-1234-ddeeff112233

Install Userspace Software Suspend (uswsusp)

sudo apt install uswsusp

Configure uswsusp

To create /etc/uswsusp.conf and recreate initramfs:

sudo dpkg-reconfigure -pmedium uswsusp
  • Yes to 'Continue without a valid swap space?' (Wizard not set swap file yet.)
  • Select the partition that the swap-file resides on, cross-reference with details from findmnt above. (Note:not the swap-file itself)

Note: Any changes manually made to /etc/uswsusp.conf will require recreating initramfs afterwards with this command:

sudo update-initramfs -u

Test uswusp hibernate

sudo s2disk

There should be snapshot messages on the screen on hibernate and resume.

Use s2disk with systemd hibernate

By default systemd will use it's own hibernate commands so replace them with the uswusp commands by overriding the systemd-hibernate.service:

sudo systemctl edit systemd-hibernate.service

In the text editor that opens put the following text (the blank ExecStart is required). Then save and exit:

[Service]
ExecStart=
ExecStart=/usr/sbin/s2disk 
ExecStartPost=/bin/run-parts -a post /lib/systemd/system-sleep

This will create /etc/systemd/system/systemd-hibernate.service.d/override.conf file with override details.

Test systemd hibernate :

systemctl hibernate 

Note: To check that the systemd override.conf has been created, loaded and no errors, run:

systemctl status systemd-hibernate.service

References:

Cas
  • 8,707
14

Ubuntu 22.04

uswusp is deprecated. You can still use it for Ubuntu <= 20.04. To find swap offset on Ubuntu 22.04:

  • Run sudo filefrag -v /swapfile
  • In the "physical_offset" column, copy the first row number (without dots!). Value example: 1234567.

Source: How To Enable Hibernation On Ubuntu (When Using A Swap File)

All other steps remain the same for Ubuntu 22.04, so you can follow other answers except installing and using uswusp.

Nairum
  • 251
9

I've given a quick read to the tutorial and, if I have understood correctly, you just need to specify the resume options to the Linux command line. With Grub2 is really simple, and your changes will be always preserved. You need to edit the /etc/default/grub file, specifically this line:

GRUB_CMDLINE_LINUX="resume=... resume_offset=..."

After that, run sudo update-grub for the changes to take effect.

Changing GRUB_CMDLINE_LINUX won't affect other Linux installations you have (because /etc/grub.d/30_os-prober does not use this variable).

About the problem you're having: is the partition of the swapfile encrypted? If so, hibernation won't work. If not, then the output of filefrag -v /swapfile may be helpful.

2

Since no from the previous answers seems to me to cover all aspects fully, here all the steps which worked for me to get everything running smoothly on Debian Bullseye, so should be applicable for Ubuntu 22 and hopefully higher too with grub2 used without the usage of the no longer existing package uswsusp and the whole s2* facility. I expect this should be also applicable to lower versions:

  1. Lets suppose, we have prepared the swap file named /tmpdisk/system/swap.file. How to prepare it, consult for example this answer.
  2. Get the information about the swap offset by the determination of the first fragment of the swap file on the partition, where it is placed, for example:

# filefrag -v /tmpdisk/system/swap.file

obtaining the result where the offset is the first physical offset fragment number

Filesystem type is: ef53
File size of /tmpdisk/system/swap.file is 68719476736 (16777216 blocks of 4096 bytes)  
ext:     logical_offset:        physical_offset: length:   expected: flags:
  0:        0..   63487:      34816..     98303:  63488:             
  1:    63488..  126975:     100352..    163839:  63488:      98304:
  2:   126976..  190463:     165888..    229375:  63488:     163840:
 …  

Same information can be retrieved by the simple issuing of
# filefrag -v /tmpdisk/system/swap.file | awk '$1=="0:" {print substr($4, 1, length($4)-2)}'
obtaining the offset value directly:
34816

  1. Find the UUID of the partition with the swap file (in my case of file /tmpdisk/system/swap.file placed on partition, mounted on mount point /tmpdisk, so):

# findmnt -no UUID,SOURCE -T /tmpdisk
resulting in something like this
6b127402-e917-4ab0-9490-00faa74e88e5 /dev/sdb1

  1. Use the obtained UUID of the partition with the swap file and of the offset in /etc/default/grub:

GRUB_CMDLINE_LINUX_DEFAULT="resume=UUID=6b127402-e917-4ab0-9490-00faa74e88e5 resume_offset=34816"

  1. Use the obtained UUID of the partition with the swap file and of the offset in /etc/initramfs-tools/conf.d/resume:

in the following way

resume=UUID=6b127402-e917-4ab0-9490-00faa74e88e5
resume_offset=34816
  1. Update the ramdisks for all kernels used:

# update-initramfs -u -k all

  1. Update grub:

# update-grub

and that's it...


Remark: The values UUID=6b127402-e917-4ab0-9490-00faa74e88e5 and resume_offset=34816 will be individual on every system

0

I'd spent hours to get my KDE (neon) with hibernation option on shutdown menu, and since I'd solved, I've decided to share it here. If you're using KDE, you should create a file "/etc/polkit-1/localauthority/10-vendor.d/hibernate.pkla" with the content:

[Re-enable hibernate by default in upower]
Identity=unix-user:*
Action=org.freedesktop.upower.hibernate
ResultActive=yes

[Re-enable hibernate by default in logind] Identity=unix-user:* Action=org.freedesktop.login1.hibernate;org.freedesktop.login1.handle-hibernate-key;org.freedesktop.login1;org.freedesktop.login1.hibernate-multiple-sessions;org.freedesktop.login1.hibernate-ignore-inhibit ResultActive=yes

I've got it here: https://forum.kde.org/viewtopic.php?f=309&t=135294