Skip to main content

SwapForge (swap-forge) 🔄

GitHub Sponsors Patreon Buy Me a Coffee PayPal

License: MIT GitHub stars

SwapForge is a small Python CLI for creating and managing Linux swapfiles. Whether you need to boost system performance or add swap space to your setup, SwapForge automates the process quickly and reliably — and it knows the difference between btrfs and everything else.


🧭 How it works

flowchart TD
    A["swap-forge SIZE"] --> B["parse SIZE into MiB"]
    B --> B2{"parsable?"}
    B2 -- no --> X["Invalid size format - exit 1"]
    B2 -- yes --> C["findmnt -no FSTYPE /"]
    C --> D{"root filesystem is btrfs?"}
    D -- yes --> E["target: /var/swap/swapfile"]
    D -- no --> F["target: /swapfile"]
    E --> G["read the size of the existing swapfile"]
    F --> G
    G --> H{"same size already?"}
    H -- yes --> Y["Skipping - exit 0"]
    H -- no --> I{"a swapfile is already there?"}
    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"]
    K -- no --> M["allocate with fallocate"]
    L --> M
    M --> N["chmod 600"]
    N --> O["mkswap"]
    O --> P["swapon"]
    P --> Q["append the entry to /etc/fstab"]

The fstab entry is written after activation succeeds, so a failed run never leaves a half-written fstab behind.


🛠 Features

  • Automated Swapfile Creation: Easily create a swapfile with a specified size.
  • 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.

📥 Installation

pip install swap-forge

pip is the single supported installation path.

The package installs two identical commands: swap-forge (primary) and swafo (kept for older documentation and scripts).


🔧 Requirements

  • Python 3.10+ 🐍
  • root privileges — the tool writes /etc/fstab and activates swap
  • 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.


🚀 Usage

Run SwapForge by specifying the desired swapfile size. For example, to create a 2G swapfile:

sudo swap-forge 2G

Accepted sizes are whole numbers with an optional unit: 2048, 2048M, 2048MB, 64G, 64GB. Without a unit the value is read as MiB.

The swapfile location follows the root filesystem:

Root filesystem Swapfile
btrfs /var/swap/swapfile
anything else /swapfile

SwapForge compares the existing swapfile against the requested size, skips when they match, and otherwise removes the old one — including its /etc/fstab line — before creating the new one.

Exit codes

Code Meaning
0 Swapfile is in place, either created or already correct.
1 Invalid size argument, or one of the swap commands failed.
2 Invalid command line arguments.
127 A required command is not installed.

🧪 Development

make lint              # ruff check + ruff format --check
make format            # apply ruff format
make test              # unit + integration tests
make test-unit
make test-integration
make test-e2e          # install the package in a container and exercise the CLI

Tests run against the working tree — the Makefile puts src/ on PYTHONPATH, so no install is needed. Every command execution is mocked and fstab handling is exercised against a temporary file, so the suite never touches real swap.


🧑‍💻 Author

Developed by Kevin Veen-Birkenbach


📜 License

This project is licensed under the MIT License.


🤝 Contributions

Contributions are welcome! Feel free to fork the repository, submit pull requests, or open issues if you have suggestions or encounter any problems. Let's make Linux swap management easier together! 😊

Metadata

Release files for swap-forge 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for swap-forge 1.0.1
File Size Uploaded Provenance Actions
swap_forge-1.0.1.tar.gz 10.8 kB Without provenance Details for swap_forge-1.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for swap-forge 1.0.1
File Interpreter ABI Platform Actions
swap_forge-1.0.1-py3-none-any.whl Python 3 none any Details for swap_forge-1.0.1-py3-none-any.whl

Total release size: 17.5 kB

Release files / swap_forge-1.0.1.tar.gz

Download URL swap_forge-1.0.1.tar.gz
Size 10.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c8215fb477325740553c92b6e42dcdff3c9bf43c0cf59fefb922d111b2e9ad3a
BLAKE2b-256 checksum
How to use checksums
830d54dbd5275153b7a66420fc5e7c25a8459b6d4015f561d5b03684c4f7d5ba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / swap_forge-1.0.1-py3-none-any.whl

Download URL swap_forge-1.0.1-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
329b2f92cafe582a359ae3d7e93a1a8becc4a683f33d6141bf10afe8d05c5062
BLAKE2b-256 checksum
How to use checksums
0e372372a537389c8e6fb077add9e625a00b0065c782e12d8c565a4f6278655c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Security for swap-forge 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page