I installed Kubuntu with the system fully encrypted using Btrfs, but I did not create a swap partition or swapfile. With many browser tabs open, YouTube playing, a virtual machine running, and an IDE in use, the system sometimes freezes. I read the Btrfs swapfile documentation, but I'm unsure whether adding one could cause problems, especially with disk encryption. Do I need to run a balance, scrub, defragmentation, or trim operation before creating the swapfile?
3 Answers
Encryption itself isn’t a problem. In a typical setup, Btrfs is stored on top of LUKS, so the swapfile is still protected by the underlying disk encryption. Btrfs does have specific requirements and limitations for swapfiles, so those should be followed, but encryption doesn’t add a special risk. The main caveat is that a swapfile on Btrfs generally shouldn’t be used for hibernation unless the system is configured very carefully for resume support.
You don’t need to run a balance, scrub, defrag, or trim before creating the swapfile. Create it according to the Btrfs documentation: ensure copy-on-write is disabled for the file, allocate it with the appropriate method, run `mkswap`, and enable it with `swapon`. Also make sure the file meets Btrfs’s restrictions, such as being on a supported single-device filesystem and not using compression or other incompatible features.
A swapfile can help prevent memory exhaustion when your workload exceeds available RAM, although heavy swapping will still be much slower than adding more memory. Since your symptoms include system freezes, also consider enabling or tuning zram and checking whether the system’s out-of-memory handling is working properly. If you only need ordinary swap and not hibernation, a correctly configured Btrfs swapfile should be fine.

The important distinction is that the swapfile itself must satisfy Btrfs’s requirements. Disk encryption underneath doesn’t change those rules, and it doesn’t make the file equivalent to a remote or multi-device swap setup.