[GUIDE] How to hibernate to a swap file in Linux Mint 19.x

Write tutorials here
There are more tutorials here http://community.linuxmint.com/tutorial/welcome
Forum rules
Please don't add support questions to tutorials,start your own thread in the appropriate sub-forum instead. Before you post please read this
Post Reply
gm10
Level 19
Level 19
Posts: 9169
Joined: Thu Jun 21, 2018 5:11 pm

[GUIDE] How to hibernate to a swap file in Linux Mint 19.x

Post by gm10 » Sun Dec 23, 2018 7:38 am

Linux Mint 19 does not support hibernation out of the box - this guide shows you how to enable it when using the default swap file. If you are looking to hibernate into a swap partition instead, refer to the following guide: viewtopic.php?f=42&t=273202. Note that some file systems like btrfs do not support swap files.

Contents: 1. Introduction and preparations

Hibernation, also called "Suspend to Disk", is a variant to the regular suspend feature where the computer ends up completely powered off (unlike regular suspend which only keeps the system in a state of low power consumption), but starts up as if you had resumed it from a regular suspend. To achieve this, memory contents are written to disk and restored when you turn the system back on (which takes a bit longer than regular suspend).

Not all systems are able to successfully resume from hibernation, usually due to driver or BIOS issues. Therefore, before you continue, use the pre-installed Timeshift tool to create a system snapshot so the system can easily be restored to its previous state. Also ideally have a live USB/DVD at hand for easy restoring of said snapshot in case the system fails to boot.

2. Required swap file size

By default, the kernel writes a compressed hibernation image of a size up to 2/5 the size of your RAM. If your RAM is filled more than that, the surplus gets swapped before hibernation can occur. If there is not enough room in the swap file for the contents of your RAM plus whatever else you may have swapped out already, hibernation will fail.

There is no "safe" size, it's always a trade-off and depends on much you usually use your RAM and swap space. As a rule of thumb, simply set up your swap file to at least the size of your RAM, or even double your RAM on systems with very low total RAM (since you are more likely to swap).

Check both your total RAM size as well as your current swap usage by running this command in a terminal window:

Code: Select all

free -h
Important: All commands below assume that your swapfile is located at /swapfile, which is the installation default in LM19. They will not work if you set up a custom swap file location.

First check if your existing swap file meets the requirements laid out above by opening a terminal window and running this command:

Code: Select all

swapon
If it is big enough and at the right location, you can skip straight to section 4 below.

3. Setting up the swap file

Otherwise, if the existing swapfile is too small or you do not have a swap file yet, we need to create a large enough one.

First we disable all swap space:

Code: Select all

sudo swapoff -a
Then we set up the desired size of the swap file in full Gigibytes (GiB). For example, to set up a 4 GB swap file, you run this:

Code: Select all

SIZE=4
Adjust the number according to your needs.

Now check that you've got enough free space available by running this command, the value under Avail should be considerably bigger than the size you configured above:

Code: Select all

df / -h
If you do not have enough free space available, you cannot continue.

Otherwise run this (copy & paste as a whole into the same terminal window):

Code: Select all

sudo dd if=/dev/zero of=/swapfile bs=1M count=$(($SIZE * 1024))
sudo chmod 0600 /swapfile
sudo mkswap /swapfile
sudo sed -i '/swap/{s/^/#/}' /etc/fstab
sudo tee -a /etc/fstab<<<"/swapfile  none  swap  sw 0  0"
This creates the swap file and configures the system to use it. It also disables any existing swap space you may have had set up already because that can lead to conflicts (it's possible to use multiple swap spaces but that's outside the scope of this guide).

4. Setting up the kernel parameters

Run this (copy & paste as a whole into a terminal window):

Code: Select all

RESUME_PARAMS="resume=UUID=$(findmnt / -o UUID -n) resume_offset=$(sudo filefrag -v /swapfile|awk 'NR==4{gsub(/\./,"");print $4;}') "
followed by

Code: Select all

if grep resume /etc/default/grub>/dev/null; then echo -e "\nERROR: Hibernation already configured. Remove the existing configuration from /etc/default/grub and add these parameters instead:\n$RESUME_PARAMS";else sudo sed -i "s/GRUB_CMDLINE_LINUX_DEFAULT=\"/GRUB_CMDLINE_LINUX_DEFAULT=\"$RESUME_PARAMS/" /etc/default/grub;fi
If there was an error message, do what it tells you, otherwise run:

Code: Select all

sudo update-grub
5. Adding Hibernate to the shutdown dialog

Run this (copy & paste as a whole into a terminal window):

Code: Select all

sudo tee /etc/polkit-1/localauthority/50-local.d/com.ubuntu.enable-hibernate.pkla <<'EOB'
[Enable hibernate]
Identity=unix-user:*
Action=org.freedesktop.login1.hibernate;org.freedesktop.login1.handle-hibernate-key;org.freedesktop.login1;org.freedesktop.login1.hibernate-multiple-sessions
ResultActive=yes
EOB
6. Trying it out

Reboot the system, run this command in a terminal window to verify whether hibernation is generally possible:

Code: Select all

busctl call org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager CanHibernate
The output should be "yes", otherwise you'll need to fix that before you can continue (see the Troubleshooting section below).

Now leaving the terminal window open, try to Hibernate from the shutdown menu, wait for the system to shut down and then turn it back on. If all worked correctly, your system will resume to the point that you hibernated from - the terminal window you opened earlier along with the command you ran should still be there.

You can also hibernate directly from a command line using this command:

Code: Select all

systemctl hibernate
7. Troubleshooting

If your system says it does not support hibernation, it's likely due to your BIOS/UEFI. Boot to your BIOS/UEFI and check the settings, it needs to support ACPI sleep state S4. An UEFI's secure boot and fast boot features can also prevent hibernation in some cases, so disable them.

If you are using a non-default kernel it's also possible that it was compiled without hibernation support. To check:

Code: Select all

grep CONFIG_HIBERNATION /boot/config-$(uname -r)
If your system does allow for hibernation, it can still fail either at the hibernation or more commonly at the resume stage. This can again be due to issues with the BIOS/UEFI, some of them have a faulty ACPI implementation, or due to issues with the hardware, including kernel drivers and firmware.

So update your BIOS, remove/disable as many devices as possible and try again, maybe that's already enough. Try out different kernels as well.

For NVIDIA users, this may be relevant: viewtopic.php?t=258577

Troubleshooting hibernation issues is not always easy (there's a reason the feature is disabled by default!), best search the forums and the Internet for a solution regarding your hardware and/or create a new thread asking for help. A solution cannot be found in all cases, unfortunately.

8. References

If you are interested, here's the kernel documentation on hibernation:
https://www.kernel.org/doc/Documentatio ... swsusp.txt
and using a swap file for it:
https://www.kernel.org/doc/Documentatio ... -files.txt
Last edited by gm10 on Tue Jul 16, 2019 3:04 pm, edited 1 time in total.

User avatar
michael louwe
Level 10
Level 10
Posts: 3300
Joined: Sun Sep 11, 2016 11:18 pm

Re: [GUIDE] How to hibernate to a swap file in Linux Mint 19.x

Post by michael louwe » Sun May 26, 2019 12:40 pm

gm10 wrote:
Sun Dec 23, 2018 7:38 am
Linux Mint 19 does not support hibernation out of the box - this guide shows you how to enable it when using the default swap file.
.
Thanks. The Tutorial is mostly beyond newbies and average users. LM 18/Ubuntu 16.04 and earlier used to support hibernation out of the box by using a swap partition instead of a swap file when using the auto-install method, eg "Erase disk and install LM". Removing the useful hibernation feature out of the box in LM 19/Ubuntu 18.04 is not a good idea at all, especially for newbies.

Windows systems support hibernation out of the box even though they also use swap file or page file(plus hiber file) and not partitions. In this respect,
Windows > Ubuntu 18.04/LM 19.

If I were to install LM 19/Ubuntu 18.04, I would need to manually create the swap partition, in order to have hibernation out of the box. In this case, the auto-created swap file would become quite redundant. Others may stick to the defaults and make do without hibernation out of the box or use the above Tutorial to get hibernation into the box.

Seems, the size of the auto-created swap file is 1/4 the size of RAM because file compression is used.

cjclm7
Level 1
Level 1
Posts: 9
Joined: Mon Oct 09, 2017 8:32 am

Re: [GUIDE] How to hibernate to a swap file in Linux Mint 19.x

Post by cjclm7 » Mon Jun 10, 2019 1:13 pm

gm10, followed your guide and it is working 100%

When resuming from the swap file, it is faster than I was expecting.

This is very useful! Thanks

FamCompKid
Level 1
Level 1
Posts: 17
Joined: Thu Apr 25, 2019 1:18 am

Re: [GUIDE] How to hibernate to a swap file in Linux Mint 19.x

Post by FamCompKid » Sat Jun 15, 2019 8:21 am

Thanks for the guide...but I'll be following this when I'm brave enough to break my PC :D
michael louwe wrote:
Sun May 26, 2019 12:40 pm
Removing the useful hibernation feature out of the box in LM 19/Ubuntu 18.04 is not a good idea at all
What's the philosophy behind this?

I didn't always use Hibernate (when I was still on Windows until two weeks ago), but when I did, it really helped. If I'm taking a 30- to 60-minute break and a bunch of apps/whatever are running, I don't really like closing them all, and then coming back to type my long master password and opening everything again. It gets annoying (especially when I'm in the mood for breaks!). Plus I don't want my UPS left on just so my desktop can Suspend.

I hope Hibernate becomes a default option again. I don't know of any downsides to using it. It really is a nice feature.
Two PC's on Linux Mint 19.2 Cinnamon. I don't like using the terminal and it doesn't even matter.

gm10
Level 19
Level 19
Posts: 9169
Joined: Thu Jun 21, 2018 5:11 pm

Re: [GUIDE] How to hibernate to a swap file in Linux Mint 19.x

Post by gm10 » Sat Jun 15, 2019 11:50 am

FamCompKid wrote:
Sat Jun 15, 2019 8:21 am
What's the philosophy behind this?
Mostly Ubuntu not wanting to support it on all possible hardware combinations and deciding that suspend to RAM is good enough.
Tune up your LM 19.x: ppa:gm10/linuxmint-tools

User avatar
Pjotr
Level 21
Level 21
Posts: 13493
Joined: Mon Mar 07, 2011 10:18 am
Location: The Netherlands (Holland)
Contact:

Re: [GUIDE] How to hibernate to a swap file in Linux Mint 19.x

Post by Pjotr » Mon Jun 17, 2019 5:59 pm

gm10 wrote:
Sat Jun 15, 2019 11:50 am
FamCompKid wrote:
Sat Jun 15, 2019 8:21 am
What's the philosophy behind this?
Mostly Ubuntu not wanting to support it on all possible hardware combinations and deciding that suspend to RAM is good enough.
Which it is. :mrgreen:
Tip: 10 things to do after installing Linux Mint 19.2 Tina
Keep your Linux Mint healthy: Avoid these 10 fatal mistakes
Twitter: twitter.com/easylinuxtips
All in all, horse sense simply makes sense.

Bronze Globe
Level 2
Level 2
Posts: 67
Joined: Wed Dec 14, 2016 1:27 am

Re: [GUIDE] How to hibernate to a swap file in Linux Mint 19.x

Post by Bronze Globe » Mon Jul 08, 2019 11:37 am

Where, and how, do I put the "hibernate=nocompress" boot parameter mentioned in the kernel.org documentation? Not familiar, and I figured just guessing at it might be a Bad Idea.

gm10
Level 19
Level 19
Posts: 9169
Joined: Thu Jun 21, 2018 5:11 pm

Re: [GUIDE] How to hibernate to a swap file in Linux Mint 19.x

Post by gm10 » Mon Jul 08, 2019 12:05 pm

Bronze Globe wrote:
Mon Jul 08, 2019 11:37 am
Where, and how, do I put the "hibernate=nocompress" boot parameter mentioned in the kernel.org documentation? Not familiar, and I figured just guessing at it might be a Bad Idea.
Add it to /etc/default/grub to the GRUB_CMDLINE_LINUX_DEFAULT= line within the "". Then sudo update-grub afterwards. I'll assume you understand that this parameter will require a bigger swap file.
Tune up your LM 19.x: ppa:gm10/linuxmint-tools

Bronze Globe
Level 2
Level 2
Posts: 67
Joined: Wed Dec 14, 2016 1:27 am

Re: [GUIDE] How to hibernate to a swap file in Linux Mint 19.x

Post by Bronze Globe » Mon Jul 08, 2019 4:26 pm

gm10 wrote:
Mon Jul 08, 2019 12:05 pm
Bronze Globe wrote:
Mon Jul 08, 2019 11:37 am
Where, and how, do I put the "hibernate=nocompress" boot parameter mentioned in the kernel.org documentation? Not familiar, and I figured just guessing at it might be a Bad Idea.
Add it to /etc/default/grub to the GRUB_CMDLINE_LINUX_DEFAULT= line within the "". Then sudo update-grub afterwards. I'll assume you understand that this parameter will require a bigger swap file.
Thanks! Of course, and I have the swap file sized accordingly. The process takes a couple minutes longer than I'd prefer, so I thought that skipping the compression/decompression steps may move things along a bit faster.

nimitz21
Level 1
Level 1
Posts: 1
Joined: Tue Jul 23, 2019 2:41 am

Re: [GUIDE] How to hibernate to a swap file in Linux Mint 19.x

Post by nimitz21 » Tue Jul 23, 2019 2:44 am

It worked for me, I'm so relieved, thanks!

tigerwillow1
Level 1
Level 1
Posts: 7
Joined: Mon Aug 26, 2019 9:13 pm

Re: [GUIDE] How to hibernate to a swap file in Linux Mint 19.x

Post by tigerwillow1 » Fri Aug 30, 2019 12:36 am

Worked for me, too, following the original post. For somebody who doesn't understand what the commands are doing, it looks like a daunting and scary task. I just took it a step at a time and got to the finish line. Hibernate is important to me. Thanks for all the work setting up the directions so well!

User avatar
smurphos
Level 12
Level 12
Posts: 4320
Joined: Fri Sep 05, 2014 12:18 am
Location: Britisher
Contact:

Re: [GUIDE] How to hibernate to a swap file in Linux Mint 19.x

Post by smurphos » Thu Sep 26, 2019 1:26 am

If having followed the guide an end user realises they didn't make their swap file big enough am I correct in thinking they need to redo 3. Setting up the swap file & 4. Setting up the kernel parameters in full?
For custom Nemo actions, useful scripts for the Cinnamon desktop, and Cinnamox themes visit my Github pages.

gm10
Level 19
Level 19
Posts: 9169
Joined: Thu Jun 21, 2018 5:11 pm

Re: [GUIDE] How to hibernate to a swap file in Linux Mint 19.x

Post by gm10 » Thu Sep 26, 2019 3:33 am

smurphos wrote:
Thu Sep 26, 2019 1:26 am
If having followed the guide an end user realises they didn't make their swap file big enough am I correct in thinking they need to redo 3. Setting up the swap file & 4. Setting up the kernel parameters in full?
Yep.
Tune up your LM 19.x: ppa:gm10/linuxmint-tools

Post Reply

Return to “Tutorials”