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"
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
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 hibernate
without 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:
Best Answer
The primary advantage of a swapfile is that it is easy to resize, so there isn't much point in transitioning unless you are unhappy with your swap partition size. You could move the swap to an encrypted partition for security, but there are other ways of encrypting your swap.
Traditionally swapfile were discouraged for a number of reasons. The swapfile used to be slower before Linux v2.4, and might still be slower if you create the swapfile on a fragmented filesystem. You may be a bit more likely to hit memory bugs with swapfiles, for example the catch-22s where you can't allocate any more memory until you swap some out, but the filesystem needs to allocate before you can swap out. A few years back hibernating to swapfiles was a controversial new feature for similar reasons. Years ago when filesystems were still a bit buggy (and not journaled) it was unwise to do huge numbers of write important filesystems, when you could just use a swapfile instead. As the tradition default, swap partitions have been tested more than swapfiles. Presumably Canonical think that these problems aren't worth worrying about anymore.
The biggest reason now not to switch to a swapfile, is "why fix something that isn't broken". If you don't backup your main partition, and accidentally delete your
/home
instead of your swapfile, it would be a hassle trying to get it back.Another reason not to switch to swapfiles, is if you are using
btrfs
which doesn't support swapfiles before kernel 5.0 (except via slow loopback files)Even if you do decide to go for a swap file, there is no real need to delete your swap partition unless you are short on space. You can use both at the same time if you want. If you do decide to delete the swap partition, first of all boot with a Ubuntu LiveCD, and go
try without installing
. Then in a terminal rungparted
, delete the swap partition, and resize the remaining partitions (doing a backup first may be a good idea).Now reboot into your regular Ubuntu install and create the swapfile. Since the advantage of a swapfile is that it is easy to resize, you may want to use SwapSpace to manage the size of your swapfile:
You could also manually create a swapfile by following the instructions at: https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-16-04
To cut a long story short, you can make a 1G swapfile by pasting the following into a terminal
If that works, you can make it permanent by doing: