fallocate
Preallocate file space on a filesystem
TLDR
Reserve a file taking up 700 MiB of disk space
Shrink an already allocated file by 200 MiB
Shrink 20 MB of space after 100 MiB in a file
SYNOPSIS
fallocate [options] file
fallocate {-c|-d|-p|-z} [-o offset] -l length file
fallocate -x [-o offset] -l length file
PARAMETERS
-l, --length LENGTH
Specifies the length of the allocation or deallocation range in bytes.
-o, --offset OFFSET
Defines the starting offset (position) for the operation within the file.
-c, --collapse-range
Removes a specified range from the file and shifts subsequent data to the left, shrinking the file size.
-d, --deallocate, --punch-hole
Deallocates (punches a hole in) a specified range of bytes, turning them into unallocated sparse regions.
-p, --punch-hole
An alias for --deallocate, also deallocates space within a range.
-z, --zero-range
Writes zeros into a specified range of the file, guaranteeing that the blocks are allocated and contain zeros.
-x, --posix
Uses the posix_fallocate(3) system call, which guarantees disk space allocation and may write zeros if necessary.
-n, --keep-size
Prevents the file size from being extended by the allocation operation.
-v, --verbose
Enables verbose output, showing more details about the operation.
-h, --help
Displays a help message and exits.
-V, --version
Outputs version information and exits.
DESCRIPTION
The fallocate command is a powerful utility for managing disk space allocation for files. Unlike simply extending a file's size, which might create a sparse file with unallocated 'holes,' fallocate explicitly preallocates disk blocks. This is crucial for applications like virtual machine disk images, databases, or large video files that require contiguous or guaranteed disk space to prevent fragmentation and ensure consistent performance.
It can efficiently reserve space without immediately writing data (unless using the -z or -x options), making it significantly faster than using tools like dd for large file preallocation. Beyond preallocation, fallocate can also deallocate space (create holes) within a file using the -d or -p options, or even remove a range of a file and shift subsequent data (-c), effectively shrinking the file from within. This operation directly manipulates the file system's block allocation information, providing a low-overhead method for disk space management.
CAVEATS
Filesystem Support: Not all file systems support the underlying fallocate(2) system call. Modern Linux file systems like ext4, XFS, Btrfs, and ZFS typically do, but older or network file systems (e.g., NFS, FAT) may not.
Atomic Operations: While fallocate operations are generally atomic on supported file systems, some operations like --collapse-range might not be fully atomic against system crashes, potentially leading to data corruption if a crash occurs mid-operation.
Sparse Files: Using options like -d or -z can create 'holes' in files, leading to discrepancies between the apparent file size (as seen by ls -l) and the actual disk space consumed (as seen by du -h).
SPARSE FILES AND HOLES
fallocate can create 'sparse files' by reserving blocks or punching holes. A sparse file appears to be a certain size (e.g., 1GB) but only consumes disk space for the blocks that actually contain data. The unallocated regions are 'holes.' This is beneficial for saving disk space and speeding up file creation where not all data is immediately available.
UNDERLYING SYSTEM CALLS
The fallocate command primarily interfaces with the fallocate(2) system call, which is Linux-specific and offers features like `punch-hole` and `collapse-range`. The -x option specifically uses posix_fallocate(3), which is a POSIX-standardized way to allocate space but has slightly different semantics (e.g., it might write zeros to the allocated region if needed to guarantee space).
HISTORY
The fallocate(2) system call, which fallocate utilizes, was introduced into the Linux kernel in version 2.6.23 (released October 2007). The fallocate utility, part of the util-linux project, provides a user-space interface to this system call. Its development was driven by the need for more efficient and robust disk space management, particularly for applications requiring guaranteed contiguous (or at least pre-reserved) disk blocks, such as virtual machine disk images and database files, where traditional dd methods were too slow and inefficient.