Loopback filesystems are very interesting components of Linux-like systems. We usually create filesystems on devices (for example, disk drive partitions). These storage devices are available as device files such as /dev/device_name. In order to use the storage device filesystem, we mount it at a directory called a mount point . On the other hand, loopback filesystems are those that we create in files rather than a physical device. We can then mount those files as filesystems at a mount point. This essentially lets you create logical “disks” inside a file on your physical disk!
losetup is a system administration command. It is used to set up and control loop devices. It can attach a loop device to a regular file or block device, detach a loop device, or query a loop device. A loop device can be used to mount an image file as if it were a normal device.
Command line Options
|-d||Detach specified loopdevice.|
|-e encryption, -Enumber||Use specified kernel encryption module when performing writes and reads. (Usually NONE, DES, and XOR.)|
|-o offset||Start reading data at offset bytes from the beginning of file.|
|-p fd||Read the passphrase from file descriptor fd.|
An ISO file is an archive of an optical media. We can mount ISO files in the same way that we mount physical disks by using loopback mounting. We can even use a nonempty directory as the mount path. Then, the mount path will contain data from the devices rather than the original contents until the device is unmounted. For example:
# mkdir /mnt/iso # mount -o loop linux.iso /mnt/iso
Now perform operations using files from /mnt/iso. ISO is a read-only filesystem.
losetup Command Examples
1. List loop devices with detailed info:
# losetup -a
2. Attach a file to a given loop device:
# sudo losetup /dev/loop /path/to/file
3. Attach a file to a new free loop device and scan the device for partitions:
# sudo losetup --show --partscan -f /path/to/file
4. Attach a file to a read-only loop device:
# sudo losetup --read-only /dev/loop /path/to/file
5. Detach all loop devices:
# sudo losetup -D
6. Detach a given loop device:
# sudo losetup -d /dev/loop