How to Use the fallocate Command in Linux to Instantly Allocate Large Swap Files and Storage Blocks

Creating large preallocated files in Linux is a routine task for database administrators configuring disk space, DevOps engineers testing filesystem write boundaries, and system administrators setting up swap space. Historically, administrators relied on the dd utility (e.g., dd if=/dev/zero of=file.img bs=1G count=10), which writes physical zeroes byte by byte across the entire target block. On large drives, creating a 50GB file via dd can take minutes and causes massive I/O saturation. The modern fallocate utility eliminates this delay by manipulating filesystem allocation pointers directly at the kernel inode level, creating multi-gigabyte or multi-terabyte files in milliseconds.

How fallocate Operates vs. Traditional File Creation

Unlike dd, which physically writes zeroes to every sector on the storage media, fallocate interacts directly with the Linux kernel’s posix_fallocate() system call. On modern filesystems (such as ext4, XFS, and Btrfs), fallocate allocates unwritten disk extents and updates filesystem metadata structures instantaneously without performing physical disk I/O. The allocated blocks are reserved exclusively for the file, preventing fragmentation and guaranteeing that subsequent writes will not encounter out-of-disk-space errors.

Basic Syntax and Core Parameters of fallocate

The standard syntax for creating an allocated file using fallocate is straightforward:

fallocate [options] -l <length> <filename>

The length parameter accepts human-readable size suffixes:

  • K: Kilobytes
  • M: Megabytes
  • G: Gigabytes
  • T: Terabytes

Step-by-Step: Creating a 4GB Swap File Instantly with fallocate

One of the most practical applications of fallocate is rapidly provisioning emergency swap space on Linux VPS instances experiencing memory pressure:

1. Allocate the Target File

To instantly create a 4-gigabyte swap file named /swapfile, execute:

sudo fallocate -l 4G /swapfile

Notice that the terminal prompt returns immediately within less than one-tenth of a second, even on standard cloud spinning disks.

2. Restrict File Permissions

For security, swap files must only be readable and writable by the root superuser:

sudo chmod 600 /swapfile

3. Format and Initialize Swap Structure

Mark the allocated storage block as Linux swap area:

sudo mkswap /swapfile

4. Activate the Swap File

Enable the newly formatted swap partition in the active kernel session:

sudo swapon /swapfile

5. Verify Active Swap Capacity

Confirm that the system kernel has registered the new 4GB allocation:

swapon --show
free -h

Punching Holes in Files with fallocate (Deallocation)

In addition to allocating disk space, fallocate can perform advanced storage reclamation known as hole punching. Using the -p (punch hole) flag alongside -n (keep size), you can deallocate a specific byte range within a file, converting it into a sparse file without altering the apparent file size:

fallocate -p -n -o 1G -l 2G large_database.bin

This command deallocates 2 gigabytes of physical storage starting at offset 1GB, freeing the physical underlying blocks back to the filesystem pool immediately.

Filesystem Compatibility and Crucial Caveats

Filesystem fallocate Support Notes & Limitations
ext4 Full Support Instant allocation & hole punching natively supported.
XFS Full Support Optimal performance on multi-terabyte preallocations.
Btrfs Partial Support Supported; swap files on Btrfs require disabling Copy-on-Write (chattr +C).

Important Note for Swapon: On certain older kernels or specific Btrfs filesystems, the kernel swapon driver may reject swap files created via fallocate due to holes in extent mapping. If swapon: /swapfile: swapon failed: Invalid argument appears, fall back to sudo dd if=/dev/zero of=/swapfile bs=1M count=4096 status=progress to ensure contiguous sector allocation.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.