Swap Partition to Swapfile with Hibernation in 20.04:
Increase swapfile size to match RAM size up to 8GB.
Check the swap that is in use:
sudo swapon -s
If swap partition(s) found:
sudo swapoff -a
sudo nano -Bw /etc/fstab
In /etc/fstab
add "# " before the UUID of the swap partition(s):
# UUID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX none swap sw 0 0
Add a line for the swapfile, if one does not exist:
swapfile none swap sw 0 0
Create the swap file:
sudo fallocate -l XG /swapfile
*
Where X is the swapfile size in GB
sudo mkswap /swapfile
sudo chown 0600 /swapfile
sudo swapon /swapfile
Setup Hibernation (Optional)
Add resume location and offset to grub.cfg:
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash resume=UUID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX resume_offset=XXXXX"
Use UUID from root.
Use offset from sudo filefrag -v /swapfile |grep " 0:"| awk '{print $4}'
Update GRUB
sudo update-grub
Test hibernation
sudo systemctl hibernate
A hibernate button can be added using gnome extensions.
*There is a slight possibility of getting holes in a swapfile when creating it with fallocate. /var/log/syslog
can be searched for the phrase swapon: swapfile has holes
to ensure there will be no data loss.
A swapfile can alternatively be created using dd
: sudo dd if=/dev/zero of=/swapfile bs=1G count=8
Caution: An error when using dd may overwrite your HDD