fix(btrfs): allocate the swapfile with fallocate instead of dd

On a btrfs root the swapfile was written with dd from /dev/zero, so a
swapfile the size of the RAM cost tens of GiB of writes and stalled
every other writer on the filesystem. A caller that bounds the run, such
as an async Ansible task, killed dd halfway, and the next run found the
wrong size and started over. btrfs accepts a preallocated NOCOW
swapfile and the swap directory already carries +C, so every filesystem
now allocates with fallocate.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
kevinveenbirkenbachandClaude Opus 5.5 committed 2026-10-23 10:36:04 +02:00
1 parent 43cdce5fba
commit 91e1eff8a6
4 files changed
+19 -34

No files matched your search

+6 -7
View File
@@ -27,10 +27,10 @@ flowchart TD
I -- yes --> J["swapoff, delete it, drop its fstab line"]
I -- no --> K
J --> K{"root filesystem is btrfs?"}
K -- yes --> L["chattr +C, turn compression off, allocate with dd"]
K -- yes --> L["chattr +C, turn compression off"]
K -- no --> M["allocate with fallocate"]
L --> N["chmod 600"]
M --> N
L --> M
M --> N["chmod 600"]
N --> O["mkswap"]
O --> P["swapon"]
P --> Q["append the entry to /etc/fstab"]
@@ -43,7 +43,7 @@ The fstab entry is written **after** activation succeeds, so a failed run never
## 🛠 Features
- **Automated Swapfile Creation:** Easily create a swapfile with a specified size.
- **Btrfs Aware:** Disables copy-on-write and compression on the swap directory and allocates with `dd`; other filesystems use `fallocate`.
- **Btrfs Aware:** Disables copy-on-write and compression on the swap directory before allocating; every filesystem allocates with `fallocate`.
- **FSTAB Integration:** Automatically updates `/etc/fstab` to ensure the swapfile is mounted at boot.
- **Safety Checks:** Skips creation when a swapfile of the correct size already exists, and recreates it when the size differs.
- **Simple CLI Interface:** Run the command with a single argument.
@@ -66,9 +66,8 @@ The package installs **two** identical commands: `swap-forge` (primary) and `swa
- **Python 3.10+** 🐍
- **root privileges** — the tool writes `/etc/fstab` and activates swap
- Always required on `PATH`: `findmnt`, `swapoff`, `chmod`, `mkswap`, `swapon`
- On a **btrfs** root additionally: `chattr`, `btrfs`, `dd`
- On any **other** root filesystem additionally: `fallocate`
- Always required on `PATH`: `findmnt`, `swapoff`, `fallocate`, `chmod`, `mkswap`, `swapon`
- On a **btrfs** root additionally: `chattr`, `btrfs`
If a required command is missing, the tool exits with code `127` and a one‑line error instead of a traceback.