GNU coreutils 9.5+ introduces atomic file swapping via mv --exchange
The new --exchange flag in GNU coreutils allows for atomic swapping of two files or directories using the kernel's renameat2() system call.
Summary
GNU coreutils version 9.5 and newer includes a new --exchange option for the mv command, designed to replace the traditional three-step manual swap process. Previously, users had to move a file to a temporary name, move the second file into place, and then move the temporary file to the final destination, creating a window where one file was missing.
This new functionality utilizes the Linux kernel's renameat2() system call with the RENAME_EXCHANGE flag to ensure the operation is atomic. This means both files or directories exist at all times during the swap, preventing service failures caused by missing configuration files during deployment or updates.
Technical implementation and usage constraints
- Atomic Operation: The feature leverages the RENAME_EXCHANGE flag within the renameat2() system call, ensuring the swap occurs as a single, indivisible kernel operation.
- Filesystem Requirements: The operation is restricted to paths residing on the same filesystem; it cannot swap files across different mounts (e.g., between a disk and tmpfs).
- Directory Handling: When swapping directories, the -T (or --no-target-directory) flag must be used to prevent mv from attempting to move the source into the destination directory.
- Compatibility: The feature requires GNU coreutils version 9.5 or higher and relies on filesystem support found in ext4, Btrfs, XFS, and tmpfs.
Why it matters
This update provides a safer mechanism for system administrators and DevOps engineers to perform atomic configuration rollouts and software deployments. By eliminating the 'missing file' window, it reduces the risk of service interruptions during critical system updates.
Original reference: feed.itsfoss.com →