losetup
Associate file as a block device
TLDR
List loop devices with detailed info
Attach a file to a given loop device
Attach a file to a new free loop device and scan the device for partitions
Attach a file to a read-only loop device
Detach all loop devices
Detach a given loop device
SYNOPSIS
losetup [options] [loopdevice [file]]
losetup -a | --all
losetup -d | --detach <loopdevice>
losetup -D | --detach-all
losetup -f | --find [file]
losetup -L | --list [loopdevice]
PARAMETERS
-a, --all
Show status of all loop devices. Used for listing.
-c, --crypt-setup
Create a crypt device instance, using the specified name. Requires the 'cryptsetup' utility and 'dm_crypt' kernel module.
-d, --detach
Detach the specified loop device. If a file is mounted on it, it must be unmounted first.
-D, --detach-all
Detach all loop devices. This operation is potentially dangerous if filesystems are still mounted.
-E, --encryption
Specify the encryption type. This option is deprecated; it is recommended to use 'cryptsetup' directly for encrypted loop devices.
-f, --find
Find the first unused loop device. If a file is specified, it will set up this file on the found loop device.
-F, --nooverlap
When setting up, check for overlaps. This means that if an offset or sizelimit is specified, losetup will fail if the resultant device extends beyond the real end of the underlying file.
-h, --help
Display help information and exit.
-L, --list
List detailed information about specified loop device(s). This is the default when no action option is given.
-n, --no-fork
When using --crypt-setup, do not fork a new process after setting up the loop device.
-o, --offset
The data start is moved offset bytes into the specified file or device.
-P, --partscan
Force the kernel to scan the partitions on the newly set up loop device. Useful for disk images containing multiple partitions.
-r, --read-only
Set up the loop device in read-only mode. The underlying file will not be written to.
-s, --sizelimit
The data end is limited to size bytes from the start of the data. Useful for loop devices that are smaller than the actual file.
-v, --verbose
Enable verbose output.
-V, --version
Display version information and exit.
DESCRIPTION
The losetup command is a utility used to manage loop devices in Linux. A loop device is a pseudo-device that makes a regular file accessible as a block device. This allows users to treat a file (like an ISO image, a disk image, or an encrypted container) as if it were a physical disk, enabling operations such as mounting a filesystem from it, creating partitions, or applying encryption.
losetup can perform several key functions:
- Associating a loop device with a specified file or block device.
- Querying the status and properties of existing loop devices.
- Detaching loop devices from their associated files.
- Finding the first available (unused) loop device.
- Setting up parameters like offset, size limits, and read-only mode for the loop device.
- Integrating with encryption frameworks like dm-crypt.
It is commonly used for mounting CD/DVD ISO images without burning them, accessing virtual machine disk images, or creating encrypted filesystems within a single file. Most operations require root privileges.
CAVEATS
Root Privileges: Most operations (setting up, detaching) require root privileges. Querying (-a, -L) can often be done by non-root users.
Kernel Module: Loop devices depend on the 'loop' kernel module. It is usually loaded automatically, but if not, it can be loaded manually using 'modprobe loop'.
Unmounting First: When detaching a loop device (-d), ensure that any filesystems mounted on it are unmounted first, otherwise the operation will fail or lead to data corruption.
Data Integrity: Incorrect usage of offset or sizelimit can lead to data corruption or inaccessible data if not carefully managed.
Security: Be cautious when attaching untrusted disk images or files. A maliciously crafted image could potentially exploit kernel vulnerabilities.
AUTOMATIC DEVICE ASSIGNMENT
When setting up a loop device without specifying a loopdevice (e.g., losetup -f <file>), losetup automatically finds the first available loop device (e.g., /dev/loop0, /dev/loop1, etc.) and assigns the file to it. This simplifies the process by not requiring the user to manually track free loop devices.
LOOP DEVICE NAMING CONVENTION
Loop devices are typically named /dev/loopX, where X is an integer starting from 0. The number of available loop devices is usually pre-configured (e.g., 8 or 256) and can sometimes be increased if needed by kernel parameters or module options.
HISTORY
Loop devices and the losetup command have been a fundamental part of the Linux kernel and userland utilities since the early days of Linux. It is part of the util-linux project, which provides a large set of essential system utilities. Over time, new features like partition scanning (-P) and dm-crypt integration (-c) have been added to enhance its capabilities, reflecting the evolving needs for disk image management and encryption. Its core functionality has remained consistent, serving as a reliable tool for virtual disk management.
SEE ALSO
mount(8), umount(8), dd(1), cryptsetup(8), mkfs(8)