Is it safe to add a swapfile on an encrypted Btrfs installation?
Asked by
+leave a margin
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?
Answers
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.
Reply to this answer
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.
Reply to this answer
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.
Reply to this answer
Add your answer
Answers are reviewed before publication.
trivia.nightcap: 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.