Ubuntu 20.04.01, full disk encryption, btrfs, & hibernation?

Swap File and Hibernation

A computer can have multiple swap partitions and a fresh install of Ubuntu will use them all, even if they are not listed in fstab, In addition the computer can use a single swap file if it is listed in fstab.

Even Live and Persistent bootable USB devices will use all the swap partition(s) they find.

Ubuntu 20.04 will create a swap file during installation as long as there are no existing swap partitions on the computer.

Once the swap file has been added to fstab the computer will not use any swap partition that are not listed in fstab.

Security:

A swap file is part of the root partition and automatically gets encrypted along with it.

To replace a swap partition with a swap file:

  • Check the swap that is in use:

    sudo swapon -s

  • If swap partition(s) found:

    sudo swapoff -a

    sudo nano -Bw /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:

    swapfile none swap sw 0 0

  • Create the swap file:

    sudo fallocate -l XG /swapfile

Where X is the swapfile size in GB

    sudo chown 0 /swapfile

    sudo chmod 0600 /swapfile

    sudo mkswap /swapfile

    sudo swapon /swapfile
  • Reboot:

    sudo reboot

To Enable Hibernation:

  • Increase swap file size to match RAM size up to 8GB per above as required.

  • Edit /etc/default/grub to 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. You can find it with: findmnt / -o UUID

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

NOTES:

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