losetup
Overview
losetup is used to set up and control loop devices in Linux, mapping regular files into block devices.
Syntax
losetup [options] [loopdev] [file]Common Options
| Option | Description |
|---|---|
-a, --all |
List status of all active loop devices |
-f, --find |
Find the first unused loop device |
-d, --detach LOOPDEV |
Detach specified loop device |
-r, --read-only |
Set up read-only loop device |
-P, --partscan |
Force kernel to scan partition table on newly created loop device |
Key Use Cases
- Mounting disk image files (
.img,.iso) as block devices. - Creating virtual storage devices for testing filesystems.
- Accessing individual partitions inside disk image files using
-P.
Examples with Explanations
Example 1: Map Image File to Loop Device
losetup -fP disk.imgFinds an available loop device, maps disk.img, and scans partition tables.
Example 2: Detach Loop Device
losetup -d /dev/loop0Unbinds and detaches /dev/loop0.