Skip to content

File Based Swap on Linux: ext4, XFS and Btrfs

Updated 5 min read

Updated: 22 October 2026 · Applies to: Linux 5.0 and later on ext4, XFS and Btrfs; tested on Linux 6.12 with util-linux 2.41, e2fsprogs 1.47, xfsprogs 6.13 and btrfs-progs 6.14 (Debian 13)

File based swap lets you add swap on a Linux server whose disk is already fully partitioned: you create an ordinary file and use it as swap, with no repartitioning. On ext4 and XFS, a file made with fallocate or dd works as it is. On Btrfs, the file must be marked no copy-on-write, so create it with btrfs filesystem mkswapfile. This article explains when a swap file is the right choice and what each file system needs.

For the plain step-by-step on a server with no swap, see How to Create a Swap File in Linux (Ubuntu: How to Add Swap Space on Ubuntu 24.04 and 26.04). To add to swap you already have, see How to Increase Swap Space in Linux.

Swap file or swap partition?

Both hold the same kind of swap area, made by mkswap. When you run swapon on a file, the kernel looks up where the file's blocks are on the disk and from then on reads and writes those blocks directly. That is why a swap file must have no holes and must not move, and why the file system matters.

  • Swap file: no free partition needed, easy to resize, move or remove, and you can add several. It has to follow the file system rules below.
  • Swap partition or LVM volume: independent of the file system, but changing its size means repartitioning or free space in the volume group. See CentOS / RHEL : How to add new swap partition.

For hibernation, a swap partition is simpler: resuming from a swap file needs its physical offset on the disk passed to the kernel as well.

Check which file system you have

findmnt -no FSTYPE /

This prints ext4, xfs or btrfs for the root file system. Use the directory where the swap file will live if it is not on /.

ext4

fallocate creates a usable swap file at once:

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

XFS (default on AlmaLinux, Rocky Linux and RHEL)

The same commands work. According to the swapon manual, preallocated swap files are supported on XFS since Linux 4.18; in our test on Linux 6.12 a fallocate file on XFS turned on without any warning. On an older kernel, create the file with dd instead:

sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress

Btrfs

A file made with plain fallocate fails on Btrfs:

sudo swapon /swap/plain

Output (lab example):

swapon: /swap/plain: swapon failed: Invalid argument

The kernel log (sudo dmesg | tail) says why: BTRFS warning (device ubdd): swapfile must not be copy-on-write. According to the Btrfs documentation, a swap file on Btrfs needs Linux 5.0 or later, must have no holes, must be NODATACOW (which also means no compression), and the file system must be on a single device with a single data profile. A subvolume that holds an active swap file cannot be snapshotted, so put the swap file in a subvolume of its own:

  1. Create the subvolume:
    sudo btrfs subvolume create /swap
  2. Create the swap file. btrfs filesystem mkswapfile (btrfs-progs 6.1 or later) sets the no copy-on-write flag, the size, mode 0600 and the swap signature in one step:
    sudo btrfs filesystem mkswapfile --size 2g /swap/swapfile
  3. Turn it on and check:
    sudo swapon /swap/swapfile
    swapon --show

    Output (lab example with 1 GB):

    NAME           TYPE  SIZE USED PRIO
    /swap/swapfile file 1024M   0B   -2
  4. Add it to /etc/fstab:
    /swap/swapfile none swap defaults 0 0

With util-linux 2.41 or later, sudo mkswap --file --size 2G /swap/swapfile also sets the flag; lsattr /swap/swapfile shows a C when it is set. On older tools, the Btrfs documentation's manual method is: truncate -s 0 the new file, chattr +C it while it is still empty, then fallocate, chmod 600 and mkswap.

In our test, a snapshot of the subvolume with the active swap file failed with ERROR: Could not create subvolume: Text file busy, while a snapshot of the parent subvolume worked, because snapshots do not include nested subvolumes. The Btrfs documentation also warns that balance and scrub skip every block group that holds part of an active swap file, and discourages this especially on the root file system. If that matters for your server, a swap partition avoids it.

Files that never work as swap

  • Sparse files, made by truncate, cp or dd with seek=: mkswap warns contains holes or other unsupported extents and swapon answers skipping - it appears to have holes.
  • Files on NFS: according to the swapon manual, swap over NFS may not work.
  • Any swap inside a container: swapon answers Operation not permitted, because containers share the host kernel. A KVM VPS has its own kernel, so swap files work there.

For other Invalid argument and read swap header failed errors, see Swapon Fails With read swap header failed or Invalid argument: Causes and Fix. For sizing, see How Much Swap Should You Use in Linux?

Official documentation: Btrfs swapfile and swapon(8).

Related services

KVM VPS hosting

NVMe SSD and full root access, with Free Basic Managed Support.

Unmanaged dedicated servers

full root access and free IPMI KVM, with Free Basic Managed Support.

Linux server management

hardening, patching, monitoring and automation for Ubuntu, Debian, RHEL and AlmaLinux.

Was this answer helpful?

Still stuck? Open a ticket