TrumanWong

losetup

Setting up and controlling loop devices

Supplementary instructions

losetup command is used to set up loop equipment. The loop device can virtualize files into block devices, thereby simulating the entire file system, allowing users to treat it as a hard drive, optical drive, floppy drive, etc., and mount it as a directory.

grammar

losetup [ -e encryption ] [ -o offset ] loop_device file
losetup [ -d ] loop_device

Options

-a displays the status of all loop devices.
-d Dismount the device.
-e <encryption option> Enable encryption encoding.
-f Finds the first unused loop device.
-o <offset> sets the data offset in bytes.

Parameters

  • loop_device: The loop device can be /dev/loop0, /dev/loop1 ... /dev/loop7.
  • file: The file name to be associated with the loop device. This is often a disk image file, such as *.img

Introduction to loop equipment

In UNIX-like systems, the loop device is a pseudo-device, or it can also be said to be an emulated device. It allows us to access a file like a block device. Before use, a loop device must be connected to a file. This combination provides users with an alternative to block special files. Therefore, if the file contains a complete file system, the file can be mounted like a disk device.

The file formats mentioned above, what we often see are ISO disc image files of CD or DVD or *.img image files of floppy disks (hard disks). Through this loop mount method, these image files can be mounted to a directory in the current file system.

At this point, we can understand the meaning of loop again: for the first-level file system, it is directly installed on the physical device of our computer; and for this mounted image file (which also contains the file system), it It is built on the first-layer file system. From this point of view, it is like a file system that goes around the first-layer file system, so it is called a loop.

Example

Create an empty disk image file, here create a 1.44M floppy disk:

dd if=/dev/zero of=floppy.img bs=512 count=2880

Use losetup to virtualize the disk image file into a fast device:

losetup /dev/loop1 floppy.img

Mount block device:

mount /dev/loop0 /tmp

After the above three steps, we can access the disk image file floppy.img through the /tmp directory just like accessing a real fast device.

Uninstall the loop device:

umount /tmp
losetup -d /dev/loop1