If I want to add a swapfile on Fedora Workstation with btrfs, where should I put it?

Fedora Workstation now defaults to using zram to make an in-memory compressed swap device rather than using disk. I have a use-case where I’d really like on-disk swap. Since the partitioning is already set, I can’t easily just add a swap partition. What is the recommended approach?

It seems the best is to create a dedicated btrfs subvolume to hold the swap file only.

This arrangement provide a chance to make suspend to disk work.

https://wiki.archlinux.org/index.php/btrfs#Swap_file

2 Likes

My top recommendation if you need disk-based swap is to create a swap partition. In particular for the hibernation use case.

However, if you’re committed to swapfiles on Btrfs, see man 5 btrfs and make sure you can live with the limitations. And then continue:

Fedora doesn’t have an official location for swapfiles yet, so this is subject to change.

btrfs subvolume create /var/swap
chattr +C /var/swap
fallocate -l 4G /var/swap/swapfile1
mkswap /var/swap/swapfile1
swapon /var/swap/swapfile1

You can also add this swapfile to fstab for automatic enabling during startup.

If you snapshot / then the swap subvolume will not be snapshot. That’s good. But if you rollback to a snapshot, then this subvolume won’t be present, and thus swap activation files and thus boot fails. So you might want to add nofail to the fstab entry. The gotcha with that is it becomes a silent failure. You get no swap, but at least startup completes.

There are alternatives being explored, that will make this more elegant.

6 Likes

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.