Configuring Lubuntu 18.04 to enable hibernation using a swap file

In an earlier post about Lubuntu 18.04 I stated that hibernation is precluded because the Lubuntu Installer installs the OS with a swap file instead of a swap partition. In fact, even with a swap file it is possible to configure Lubuntu so that hibernation is possible. This is how I did it.

1.  This PC has 4 GiB RAM but the Lubuntu Installer had created a 2 GiB swap file named /swapfile, so I increased the size of the swap file to 4 GiB to ensure it was large enough to store the memory image:

user $ sudo swapoff -a
user $ sudo dd if=/dev/zero of=/swapfile bs=1024 count=4M
user $ sudo chmod 600 /swapfile
user $ sudo mkswap /swapfile
user $ sudo swapoff -a
user $ sudo swapon /swapfile
user $ cat /proc/meminfo | grep -i memtotal
MemTotal:        3924108 kB
user $ ls -la /swapfile
-rw------- 1 root root 4294967296 Jul 10 18:25 /swapfile

Note that you can check the status of the swap file before and after the above steps by using either of the following commands:

user $ swapon -s
user $ free -m

2.  The Lubuntu Installer had previously configured /etc/fstab for the swap file, so I left that as it is:

user $ grep swapfile /etc/fstab
/swapfile                                 none            swap    sw              0       0

3.  I checked on which device the root partition with the file /swapfile is located (sda2 in my case) and found out its UUID (ignore the PARTUUID):

user $ sudo blkid
[sudo] password for fitzcarraldo:
/dev/sda1: UUID="3602-BD57" TYPE="vfat" PARTLABEL="EFI System Partition" PARTUUID="72b3693e-b81f-7299-84fb-bf3781bef43d"
/dev/sda2: UUID="afe17116-26fa-4169-b2d9-fb6ac8afc63c" TYPE="ext4" PARTUUID="738fed17-293d-832f-c7a4-e83471fe8ca6"

4.  I found the resume_offset for the file /swapfile, which is 16746496 in my case (look for the first value in the two columns under ‘physical_offset‘):

user $ sudo filefrag -v /swapfile
Filesystem type is: ef53
File size of /swapfile is 4294967296 (1048576 blocks of 4096 bytes)
 ext:     logical_offset:        physical_offset: length:   expected: flags:
   0:        0..   30719:   16746496..  16777215:  30720:            
   1:    30720..   63487:   16809984..  16842751:  32768:   16777216:
   2:    63488..   96255:   16842752..  16875519:  32768:            
   3:    96256..  126975:   16875520..  16906239:  30720:            
   4:   126976..  129023:   16908288..  16910335:   2048:   16906240:
   5:   129024..  161791:   16912384..  16945151:  32768:   16910336:
   6:   161792..  194559:   16945152..  16977919:  32768:            
   7:   194560..  227327:   16977920..  17010687:  32768:            
   8:   227328..  249855:   17010688..  17033215:  22528:            
   9:   249856..  282623:   17035264..  17068031:  32768:   17033216:
  10:   282624..  315391:   17068032..  17100799:  32768:            
  11:   315392..  319487:   17100800..  17104895:   4096:            
  12:   319488..  321535:   17121280..  17123327:   2048:   17104896:
  13:   321536..  325631:   17129472..  17133567:   4096:   17123328:
  14:   325632..  327679:   17137664..  17139711:   2048:   17133568:
  15:   327680..  329727:   17143808..  17145855:   2048:   17139712:
  16:   329728..  331775:   17154048..  17156095:   2048:   17145856:
  17:   331776..  339967:   17162240..  17170431:   8192:   17156096:
  18:   339968..  344063:   24485888..  24489983:   4096:   17170432:
  19:   344064..  346111:   32665600..  32667647:   2048:   24489984:
  20:   346112..  348159:   32677888..  32679935:   2048:   32667648:
  21:   348160..  350207:   33261568..  33263615:   2048:   32679936:
  22:   350208..  352255:   33363968..  33366015:   2048:   33263616:
  23:   352256..  354303:   33853440..  33855487:   2048:   33366016:
  24:   354304..  356351:   34000896..  34002943:   2048:   33855488:
  25:   356352..  389119:   34027520..  34060287:  32768:   34002944:
  26:   389120..  391167:   34060288..  34062335:   2048:            
  27:   391168..  393215:   34134016..  34136063:   2048:   34062336:
  28:   393216..  395263:   34158592..  34160639:   2048:   34136064:
  29:   395264..  428031:   34189312..  34222079:  32768:   34160640:
  30:   428032..  452607:   34222080..  34246655:  24576:            
  31:   452608..  485375:   34248704..  34281471:  32768:   34246656:
  32:   485376..  518143:   34281472..  34314239:  32768:            
  33:   518144..  550911:   34314240..  34347007:  32768:            
  34:   550912..  583679:   34347008..  34379775:  32768:            
  35:   583680..  616447:   34379776..  34412543:  32768:            
  36:   616448..  643071:   34412544..  34439167:  26624:            
  37:   643072..  645119:   34445312..  34447359:   2048:   34439168:
  38:   645120..  649215:   34457600..  34461695:   4096:   34447360:
  39:   649216..  659455:   34463744..  34473983:  10240:   34461696:
  40:   659456..  688127:   34476032..  34504703:  28672:   34473984:
  41:   688128..  690175:   34506752..  34508799:   2048:   34504704:
  42:   690176..  692223:   34510848..  34512895:   2048:   34508800:
  43:   692224..  724991:   34514944..  34547711:  32768:   34512896:
  44:   724992..  757759:   34549760..  34582527:  32768:   34547712:
  45:   757760..  778239:   34582528..  34603007:  20480:            
  46:   778240..  786431:   34637824..  34646015:   8192:   34603008:
  47:   786432..  819199:   34648064..  34680831:  32768:   34646016:
  48:   819200..  843775:   34680832..  34705407:  24576:            
  49:   843776..  845823:   34707456..  34709503:   2048:   34705408:
  50:   845824..  849919:   34713600..  34717695:   4096:   34709504:
  51:   849920..  854015:   34729984..  34734079:   4096:   34717696:
  52:   854016..  886783:   34744320..  34777087:  32768:   34734080:
  53:   886784..  919551:   34777088..  34809855:  32768:            
  54:   919552..  950271:   34809856..  34840575:  30720:            
  55:   950272..  983039:   34842624..  34875391:  32768:   34840576:
  56:   983040.. 1015807:   34875392..  34908159:  32768:            
  57:  1015808.. 1048575:   34908160..  34940927:  32768:             last,eof
/swapfile: 38 extents found

Note that you can also find the resume_offset by installing the package uswsusp and using the command swap-offset on the swap file:

user $ sudo apt install uswsusp
user $ sudo swap-offset /swapfile
resume offset = 16746496

5.  I updated the file /boot/grub/grub.cfg using the information found in Steps 3 & 4 as follows:

5.1  I added ‘resume=UUID=afe17116-26fa-4169-b2d9-fb6ac8afc63c resume_offset=16746496 resumedelay=15‘ to the parameters in the variable GRUB_CMDLINE_LINUX_DEFAULT in the file /etc/default/grub (Your existing parameters could be different to mine; that is not a problem):

user $ sudo nano /etc/default/grub
#
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash acpi_backlight=vendor acpi_osi='!Windows 2013' acpi_osi='!Windows 2012' resume=UUID=afe17116-26fa-4169-b2d9-fb6ac8afc63c resume_offset=16746496 resumedelay=15"
#

Note that the optional ‘resumedelay=15‘ specifies the delay (in seconds) to pause before attempting to read the resume files. I added this to try to allow enough time for the filesystem containing the swap file to become Read-Write.

5.2  I regenerated /boot/grub/grub.cfg by using the following command:

user $ sudo update-grub

6.  I edited the file /etc/initramfs-tools/conf.d/resume using the information found in Steps 3 & 4, and regenerated the initramfs files for the kernel images in the /boot directory:

user $ sudo nano /etc/initramfs-tools/conf.d/resume
RESUME=UUID=afe17116-26fa-4169-b2d9-fb6ac8afc63c resume_offset=16746496
# Resume from /swapfile
user $ sudo update-initramfs -u -k all

7.  I edited the Polkit rules files to permit hibernation (Create the files if they do not already exist):

7.1  For Polkit version 0.106 and higher

user $ sudo nano /etc/polkit-1/rules.d/85-suspend.rules
polkit.addRule(function(action, subject) {
    if (action.id == "org.freedesktop.login1.suspend" ||
        action.id == "org.freedesktop.login1.suspend-multiple-sessions" ||
        action.id == "org.freedesktop.login1.hibernate" ||
        action.id == "org.freedesktop.login1.hibernate-multiple-sessions")
    {
        return polkit.Result.YES;
    }
});

7.2  For Polkit versions below 0.106

user $ sudo nano /var/lib/polkit-1/localauthority/50-local.d/50-enable-suspend-on-lockscreen.pkla
[Allow hibernation and suspending with lock screen]
Identity=unix-user:*
Action=org.freedesktop.login1.suspend;org.freedesktop.login1.suspend-multiple-sessions;org.freedesktop.login1.hibernate;org.freedesktop.login1.hibernate-multiple-sessions
ResultAny=yes
ResultInactive=yes
ResultActive=yes

It does not do any harm to create both the above-mentioned rules files, whatever the version of Polkit that happens to be installed.

8.  I rebooted, logged in, launched a few GUI applications and then clicked on the Lubuntu menu icon on the Panel and selected ‘Logout’ > ‘Hibernate’, which did put the machine into hibernation. I then pressed the PC’s power push-button to resume from disk, entered my password on the lock screen and the Desktop appeared exactly as it was prior to hibernation. All good.

About Fitzcarraldo
A Linux user with an interest in all things technical.

27 Responses to Configuring Lubuntu 18.04 to enable hibernation using a swap file

  1. TheNewHEROBRINE says:

    This guide didn’t work for me. When I turn on the computer after the hibernation it remains stuck in a black screen

    • mat says:

      had the same problem. adding “`pci=nomsi“` to GRUB_CMDLINE_LINUX_DEFAULT solved it for me – don’t know whay

  2. Very nice. I tried your instructions on an ASUS netbook with 2G of ram, a new SSD, and Ubuntu LTS 18.04. Worked like a charm.

  3. devHammed says:

    Thanks, it works I am using Dell Latitude E5430 Ubuntu 18.04.

  4. andrzej says:

    thank you so much !
    It works great !
    I have surface pro 2017
    Running Ubuntu 18.10 with kernel https://github.com/jakeday/linux-surface.
    After resume wifi and touch is working 🙂

    Only issue is I get some error during boot :
    tpm error 2314 self test
    But after some sek system boot successfully .

  5. Jeremy says:

    Thanks so much for this! It works for lubuntu 18.10 as well

  6. /df says:

    If you’re providing resume= and resume_offset= as boot parameters (via grub) you don’t need to put anything in /etc/initramfs-tools/conf.d/resume. Just make sure that no random file has been installed there and delete it if it has been (and update-initramfs). On booting the initramfs relevant parameters from the boot command line are set as shell variables in the init script that invokes the resume process (if applicable) using the resume parameter values from the boot line.

    It seems likely that you could use /etc/initramfs-tools/conf.d/resume *instead of* /etc/default/grub, but I haven’t tried that.

  7. Abderrahim Elassaly says:

    Not working for 19.04 !!

  8. sys says:

    Using Kubuntu 18.04:

    Back from hibernation! It works! God bless you! Thank you a lot!

    P.S. From the command line this can be used in order to hibernate:
    sudo systemctl hibernate

  9. Subbaraju says:

    Thank you. It worked for me on Ubuntu 19.04 with “pci=nomsi” as mentioned in 1st comment

  10. Emil Stricker says:

    Thank you so much for this tutorial. I have a Linux Mint 19.2 machine, and I wanted to enable hibernation there, but I also only had a 2GB swapfile while having 8GB of RAM. It was some tinkering with the system, and it took a while, but on my machine it works with exactly the steps you describe. Great job, really useful and very well done, so even a moderately advanced user like me can understand and follow your steps.

  11. volodymyr says:

    Thanks
    It work fine for me together with this tutorial http://ubuntuhandbook.org/index.php/2018/05/add-hibernate-option-ubuntu-18-04/

  12. julio says:

    Thanks for your guide. I tried many many thing and now I am not sure what as the one that in fact worked, but I guess the main thing for me, in addition to the instructions above, was to disable in BIOS the secure boot (as I read in other forums) and the fast boot.

  13. Evan says:

    Hey thanks for the instructions. Worked for me fine. Surface Pro 4. Linux Mint 19.3.

    I didn’t bother with step 6 as the file resume was not there and df above said you don’t really need it so I tried without and it was no problem.

    One thing though, sys above says you don’t need to do any of this if you run sudo systemctl hibernate. Is this correct? Could have it been that simple? Should you update these instructions if this is the case?

    Thanks again much appreciated.

  14. Emil Stricker says:

    I have already left a reply here, but there is a little update: I am still on the same machine, but on Linux mint 19.3 in the meantime and a new kernel obviously not natively supporting hibernation either – 5.3.0-40. I went through all your steps again, but found that all my changes in the various configs were still there. I did sudo update-initramfs -u -k all and reminded my machine of the still existing polkit rules, and now it goes to hibernation mode again and wakes up allright. So up to this kernel your procedures still work. Thank you again.

  15. Emil Stricker says:

    And a reply to Evan and sys: I tried “sudo systemctl hibernate” against “sudo pm-hibernate” on my machine, and there are differences:
    1. Sys’s suggestions hibernates the computer, but on waking up you see the Linux Mint logo and the message “Resuming from MYUUID” plus “You may abort file system check by pressing CTL+C”. Afterwards you need to log in again, and all you open programs before the hibernation command are closed and have to be launched again. So this rather feels like a reboot.

    2. Sudo pm-hibernate sends my machine to hibernation. When I wake it up, I only see “Resuming from MYUUID”, and after a few more seconds I see my session as I left it without having to log in again. From a safety point of view this certainly is dangerous, but it is very comfortable and saves energy. And to me it seems more like the idea behind the power saving states of Hibernate and Suspend.

    I must say, though, that I did not try “sudo systemctl hibernate” on a machine which has been left untouched with what Fitzcarraldo describes here. I will do so, however, later on my HP laptop with the same OS and write what happened there.

  16. Emil Stricker says:

    This is my update to finish off my annotations from the previous post:

    Now I can compare my laptop’s behaviour with my desktop machine’s:
    1. What can be said about the desktop computer is already in the previous post. The only thing I need to mention in addition is: when you do “sudo pm-hibernate”, you see feedback on the screen when it goes to hibernation: “Snapshotting system” with the possibility to abort. When you do “sudo systemctl hibernate”, there is no such thing. After waking up (when you leave some unsaved document open), this one is still there with pm, but not with systemctl, although both ways show you a screen message: Resuming from /dev/MYUUID.
    2. The laptop with Linux Mint Tricia (19.3): I did not set up a swap file, but there is already a sufficiently large Swap partition on its HD. So my setup there is somewhat simpler now. I edited /etc/default/grub adding the UUID of the swap partition for resume there and updated grub. Then I updated initramfs (sudo update-initramfs -u). Otherwise there is no difference in behaviour between the two commands: you need to log in after wakeup, and all unsaved work is lost. Watching the post-command behaviour of my laptop, I must say that I am not really sure if there is anything written on the swap partition at all. You see some send-error message on the screen and some hard disk activity, before the computer goes to sleep, but otherwise everything feels like a restart after wakeup. Maybe I should try a swapfile instead of a swap partition instead when I have time…

    To sum it up: on the laptop there is no difference, and on the desktop machine I don’t care so much about what happens after sudo pm-hibernate, because this is a private computer only used by me. I just enjoy getting back to where I came from in the session before.

  17. Emil Stricker says:

    Hoping to be not too verbose here, I have to add one thing, though, just for the record: some people say that hibernating your machine is not such a good idea if you have an SSD in it, because of the sytem writing the contents of your RAM to disk when it goes into hibernation mode. An SSD is said to survive only a limited number of write cycles, so hibernation may contribute to shortening the life of an SSD.
    Personally I would not be too paranoid about this (I have an SSD in my machine for the root system myself), because it’s not meant to be used a lot anyway. If I am only away for a short time, I always suspend it instead, which involves hardly any writing on disk. Tests in computer magazines I have seen have shown that SSDs seem to last longer than the manufacturers assume. This is good news and makes me quite confident that hibernating my computer from time to time will not do much harm to the hardware…

  18. Pete B123 says:

    Hello,
    I have gone through this great stepthrough and some comment linked documents * carefully and have got it from bailing out of hibernate-reporting no swapfile header found to the point where it will now go down but fails to resume when it comes back up, as in it just restarts.

    I would really like this working as I use a lot of workspaces and it seems firefox 64 on 64b isn’t managing to restore its windows to the correct workspaces. So I have two issues I suppose but either being fixed would be a big help but both would be nice.

    I have 16gb of ram on a dell xps 15 9530 with mint 18.3, did have a swap partition but got rid of that and now have a 18gb~ swapfile (which doesn’t seem to be getting used at the moment) but previous boots and configurations attempted in this effort have shown it being used.

    Just now realised the Suspend is not working either now it is shutting down as well, which is a real problematic issue.

    Any help or pointing to a laptop issue system admin professional would be great. I have a syslog chunk from a failed from a failed hibernation and also a failed suspend

    Regards,
    Pete

    • Fitzcarraldo says:

      Look through your system log file for error messages that could relate to your problem. Don’t know what it’s called in Mint but in Lubuntu it’s /var/log/syslog. Also see if there are any related messages in the output from the dmesg command.

  19. Piotr says:

    Great HowTo, thanks and Congratulations!

    Kind Regards and let the Resurrected God, Jesus Christ Bless you!

    P.S. If allowed, I would maybe only add, how to check Polkit version, between 7 and 7.1:

    7.0 Check, what version of Polkit you have
    $ systemctl status polkit.service
    … polkitd[764]: started daemon version 0.105 …

    I additionally did for myself the following modification, between 7.2 and 8:
    7.3 Modify the file “10-vendor.d/com.ubuntu.desktop.pkla”. In two Sections change from “ResultActive=no” to “ResultActive=no”.
    $ sudo mousepad /var/lib/polkit-1/localauthority/10-vendor.d/com.ubuntu.desktop.pkla &

    WAS:

    [Disable hibernate by default in upower]
    Identity=unix-user:*
    Action=org.freedesktop.upower.hibernate
    ResultActive=no

    [Disable 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=no

    CHANGED TO:

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

    [Disable 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

  20. Piotr says:

    Sorry, I noticed in my above entry that I made a mistake. It should be:
    7.3 Modify the file “10-vendor.d/com.ubuntu.desktop.pkla”. In two Sections change from “ResultActive=no” to “ResultActive=yes”.

  21. Hello, I’m facing a strange behavior. Basically the whole process went without any problem. The problem is that now the PC is always hibernating, also if I restart or shutdown. Every time it boots, boots form hibernation.gg

    • To clarify better, the system is saving the hibernation status only if I trigger the hibernation (correctly), but If I shut down or reboot it recovers always from the same file (like th eHibernate Once Resume Many of Windows Embedded)

  22. thosecars82 says:

    Hello

    I carried out the guideliness explained on this page on an old laptop with Ubuntu 18.04 in which there was not a swap partition. It only had a swap file. But after carrying out all the steps exactly as explained on this page, I do not see any Hibernate option by clicking on exit. Anyway I tried the command “sudo systemctl hibernate” and after restarting the computer for some reason not only the laptop does not recover its previous state but its keyboard and touchpad do not work. I had to connect a USB keyboard and USB mouse to be able to be able to use the computer. Then I repeated the experiment for a second time and the same happened again. The laptop’s keyboard and mouse work well now only if I do not run the command sudo systemctl hibernate before starting the computer.

    Does anyone know what I could do to overcome this issue?

    Thanks in advance.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.