LinuxCommandLibrary

fallocate

Preallocate file space on a filesystem

TLDR

Reserve a file taking up 700 MiB of disk space

$ fallocate [[-l|--length]] [700M] [path/to/file]
copy

Shrink an already allocated file by 200 MiB
$ fallocate [[-c|--collapse-range]] [[-l|--length]] [200M] [path/to/file]
copy

Shrink 20 MB of space after 100 MiB in a file
$ fallocate [[-c|--collapse-range]] [[-o|--offset]] [100M] [[-l|--length]] [20M] [path/to/file]
copy

SYNOPSIS

fallocate [options] filename

PARAMETERS

-c, --collapse-range
    Removes a byte range from a file, without leaving holes. The range from offset to offset+length is deleted. After the operation, offset+length becomes the end of the file.

-d, --dig-holes
    Detects and removes holes within the given range. A 'hole' is a sequence of zero bytes that doesn't have actual disk space allocated to it.

-l, --length length
    Specifies the length of the range to be preallocated or deallocated. Defaults to the end of the file.

-n, --keep-size
    Do not modify file size after preallocation. This option implies that space is reserved without changing the file's perceived size.

-o, --offset offset
    Specifies the starting offset, in bytes, from the beginning of the file where the operation should begin.

-p, --punch-hole
    Deallocates space within the specified range, creating a hole. Data within the hole is logically removed, freeing up disk space.

-z, --zero-range
    Zeroes out the specified range of bytes in the file, ensuring that the allocated space contains only zeros.

-v, --verbose
    Enable verbose output.

-x, --allocate-mode
    Select the allocation mode. (required for DAX files)

--version
    Display version information and exit.

--help
    Display a help message and exit.

DESCRIPTION

The fallocate command is used to preallocate disk space to a file. Unlike simply writing to a file, fallocate can reserve the space without actually writing any data, which can be significantly faster. This is useful for several scenarios: improving write performance later on by ensuring contiguous space is available, preventing out-of-space errors during critical operations, and creating sparse files.

Preallocation can be performed in various modes, such as allocating actual disk blocks, creating holes (spaces that appear as zero-filled but consume no actual disk space), or deallocating existing space. The command operates directly on the filesystem level, bypassing the need to physically write zeros, and is therefore efficient. Be aware that not all filesystems support all fallocate modes. Check your filesystem documentation before use.
Proper error handling is essential when using fallocate, as failure can lead to unexpected behavior.

CAVEATS

Not all filesystems support all fallocate modes. Some filesystems may also exhibit different behavior when dealing with holes. fallocate may fail if the filesystem is full or if the user does not have sufficient permissions to modify the file.

FILESYSTEM SUPPORT

The availability and behavior of fallocate features can depend heavily on the filesystem being used. Ext4, XFS, and Btrfs generally offer good support, but others may have limitations. Before utilizing advanced features such as hole punching, consult the documentation for your specific filesystem to ensure compatibility. Failure to do so may lead to unexpected behavior and data corruption.

DAX FILES

The -x/--allocate-mode option is required to allocate DAX files on filesystems that support it.

HISTORY

fallocate was originally introduced to Linux as a command-line utility to provide a way to efficiently preallocate space for files, avoiding the need to write actual data to disk initially. The command has been refined and extended over time to support various features such as hole punching and zeroing of ranges, enhancing its utility for different use cases, particularly where disk space management and I/O performance are critical. The development focused on making it a fast and reliable tool for managing file storage.

SEE ALSO

truncate(1), dd(1), du(1), stat(1)

Copied to clipboard