SWAP on Btrfs

SWAP on BTRFS is supported since Kernel 5.0.7. SWAP cannot be located on snapshoted volume. The best is to create seperate volume for swap file.

Get top level ID of BTRFS:

btrfs sub list /

ID 256 gen 394 top level 5 path @
ID 258 gen 401 top level 5 path @home
...

Create non-compressed, no COW, no snapshoted swap file:

mkdir -p /mnt/btrfs
mount -o subvolid=5 /dev/... /mnt/btrfs
cd /mnt/btrfs
btrfs sub create @swap
cd @swap
 
truncate -s 0 ./swapfile
chattr +C ./swapfile
btrfs property set ./swapfile compression none
 
fallocate --length 8.2GB swapfile
chown root swapfile 
chmod 600 swapfile 
mkswap swapfile 
swapon swapfile
swapoff swapfile
 
umount /mnt/btrfs

Create mount point:

mkdir /.swap

Adapt fstab:

UUID=1cf7e34b-8b09-45bd-80ae-f4658b3e0509  /.swap     btrfs    compress=none,nodatacow,noatime,subvol=@swap
/.swap/swapfile                            none	      swap     defaults

add resume in swap file

RESUME=/.swap/swapfile
update-initramfs -k all -u