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 /mnt/4GiB.swap
> /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: