Manual installation, setting up swapfile with btrfs

I did the following and it mounts the swap but I don’t know if the snapshots will work with this

mkdir /swapfile
cd /swapfile
touch swap
truncate -s 0 swap
chattr +C swap
fallocate -l 48G swap
chmod 0600 swap
mkswap swap
swapon swap

fstab

UUID=2af739aa-b6e0-40fd-aa99-022ae38aba2d none            swap    sw              0       0

free

               total        used        free      shared  buff/cache   available
Mem:           31966        8894         563         649       23612       23071
Swap:          49151           0       49151```

This would balloon out your snapshots with garbage. (Unless you left out that you made a @swap volume (or similar), and had it mounted in /swap.

You can put on the no-CoW attribute per file/directory which would have similar effect, but having it on another volume is cleaner.

Edit: I just noticed I got the directory name backwards. I really think the Arch wiki is weird calling the directory swapfile, and the file just swap. Either way, either name…

Thanks! I didn’t make a subvolume and have done auto snapshot because of installations. so before touch I make a btrfs subvolume and mount it in fstab with subvol?

Yes, that would be the easier way. (As opposed to doing it first, then moving the volume.)

If you called it @swap and mounted it in /swap (or whatever). The rest of the steps seem fine.

It would just have an in fstab entry like:

UUID=########-####-####-####-############  /swap  btrfs  subvol=/@swap,defaults 0 0

Then /swap/swapfile or anything in /swap do not touch your snapshots, and things like checksum validation is skipped. Everything you want for this.

(Not quite as optimal as a separate partition, but this is the best way to do it for a file in a btrfs file system.)

1 Like

Using a swap file in BTRFS is not optimal. Don’t be stingy and buy a swap-partition if you’re already partitioning.

Yes, btrfs supports swap files. In other file systems this may also be a valid option. The developers have added this out of necessity :wink: . But a swap file has to be handled differently in many respects than all other files. This limits btrfs’ freedom to clean up data (because the swap file always has to stay in the same place for reasons). I would never do that. (Even if it’s possible) :footprints:

2 Likes

If you have enough ram, say above 16G, and do not want to make an extra partition, a valid option would be swap in ram, so that programs requiring it would be happy.

https://wiki.archlinux.org/title/Zswap

2 Likes

I think this is most likely right. I can resize for the swap partition.

1 Like