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

Write tutorials for Linux Mint here
More tutorials on https://github.com/orgs/linuxmint/discu ... /tutorials and (archive) on https://community.linuxmint.com/tutorial
Forum rules
Don't add support questions to tutorials; start your own topic in the appropriate sub-forum instead. Before you post read forum rules
gm10

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

Post by gm10 »

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 how 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 Nov 05, 2019 9:53 am, edited 2 times in total.
michael louwe

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

Post by michael louwe »

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: 23
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 »

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 3
Level 3
Posts: 158
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 »

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.
A very average user on two PC's on Linux Mint 21.x Cinnamon. Thank you.
gm10

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

Post by gm10 »

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.
User avatar
Pjotr
Level 24
Level 24
Posts: 20062
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 »

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 21.3 Virginia
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 3
Level 3
Posts: 167
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 »

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

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

Post by gm10 »

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.
Bronze Globe
Level 3
Level 3
Posts: 167
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 »

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

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

Post by nimitz21 »

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 »

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 18
Level 18
Posts: 8498
Joined: Fri Sep 05, 2014 12:18 am
Location: Irish Brit in Portugal
Contact:

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

Post by smurphos »

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

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

Post by gm10 »

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

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

Post by tnimxunil22 »

gm10 wrote: Sun Dec 23, 2018 7:38 amNote that some file systems like btrfs do not support swap files
Does it means that if I decide to install Mint on BTRFS partition, I won't have swap system at all and will be limited only to my ram memory?
gm10

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

Post by gm10 »

tnimxunil22 wrote: Wed Oct 16, 2019 3:48 am
gm10 wrote: Sun Dec 23, 2018 7:38 amNote that some file systems like btrfs do not support swap files
Does it means that if I decide to install Mint on BTRFS partition, I won't have swap system at all and will be limited only to my ram memory?
It is recommended to use a swap partition with btrfs. Mint ships with kernel series 4.15, and while kernels from version 5.0 upwards have limited swap file support certain limitations still apply.

That said, I cannot actually tell you how Mint's installer handles installation to a btrfs partition, I never tried nor did I look at that part of the code. It might just install a swap file regardless. That would be dangerous and could lead to system corruption. I suggest you try this in a virtual machine first, or on a system you are prepared to wipe clean again. If you try it I'd appreciate if you let me know how it goes so I could make a mention in the guide.
tnimxunil22

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

Post by tnimxunil22 »

gm10 wrote: Wed Oct 16, 2019 4:15 amIf you try it I'd appreciate if you let me know how it goes so I could make a mention in the guide.
Thanks for clarification, although I don't plan to test it in near future, sorry.
brian_stl

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

Post by brian_stl »

Thank you so much! I was dying without hibernate and this worked perfectly.
jonf

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

Post by jonf »

Great tutorial! Worked for me :)
But yes would be better if it was enabled by default!
trulden

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

Post by trulden »

Thanks, worked on my RedmiBook!

Had to disable secure boot in BIOS.

UPD

After enabling hibernation I had a problem — grub showed up every time I powered my laptop on.

Fixed that with help of this bug report.
nikapos

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

Post by nikapos »

although this worked like a charm for 19.1, it did not work for 19.3 (same computer, fresh install). the system reported hibernation was available, however after i pressed the hibernation button it did not save the state (no hdd activity), it did not shut down, it just turned off the screen.i was able to fix it by going back to 19.1. further tries with 19.3, proved the problem was with kernel 5.3.28. no problem with kernel 5.0.0. another suspect party maybe the nvidia drivers.
Locked

Return to “Tutorials”