Skip to content
Search

Loop device

lastmod 2025-01-09created 2020-07-047 edits

In Unix-like operating systems, a loop device, vnd (vnode disk), or lofi (loop file interface) is a pseudo-device that makes a file accessible as a block device.

Loopback device vs Loop device

Loopback device
In communications, a pseudo device that returns signals or data streams to their originating source without modification.
Loop device
A pseudo device that makes a file accessible as a block device.

About

In Linux, much system information and many devices are tied to files. For example, under /dev you can see the hardware actually connected to the computer exposed as files. Process (/proc) information is likewise represented as files. This is a logical representation of some real resource as a file, so could a file conversely be represented logically as some device?

A brief explanation of loop devices

Checking the man page with man loop shows the following text.

The loop device is a block device that maps its data blocks not to a physical device such as a hard disk or optical disk drive, but to the blocks of a regular file in a filesystem or to another block device. This can be useful for example to provide a block device for a filesystem image stored in a file, so that it can be mounted with the mount(8) command….

So, simply put, a loop device is a block device, but not a real device such as a disk; it is used to map a file in a file system to other block storage. That is about all you need to understand. Why is it called loop? Because the actual file concept in Linux comes back around once more. A device was turned into a file, and the file is turned back into a device; that's roughly the idea.

Another explanation

A device driver that makes an Image file mountable as if it were an ordinary Block device. (Using an image file as an ordinary Block device)

Yet another explanation

Linux lets users create a special block device that can map a regular file to a virtual block device.

Simply put, a loop device can act as a virtual file system that is very useful when working with isolated programs such as snaps.

So basically, a separate file system is mounted at a specific mount point. It is a way for developers/advanced users to pack multiple files into one place. It is then accessible to the operating system, and this behavior is called a loop mount.

Why use loop devices

Being a virtual file system, it has endless possibilities. The following are well-known use cases for loop devices.

  • It can be used to install an operating system via a file system without repartitioning the drive.
  • It is a convenient way to configure system images (after mounting).
  • It provides permanent separation of data.
  • It can be used for sandboxed applications that include all required dependencies.

And developers can do amazing things once they are given an isolated file system.

Loop devices can be managed easily with the losetup utility.

Loop devices in use on the system

The losetup command shows which loop devices are currently in use on the system. losetup:

NAME       SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE                             DIO LOG-SEC
/dev/loop1         0      0         1  1 /var/lib/snapd/snaps/core_6673.snap     0     512
/dev/loop2         0      0         1  1 /var/lib/snapd/snaps/core_6405.snap     0     512
/dev/loop0         0      0         1  1 /var/lib/snapd/snaps/core_6531.snap     0     512
/dev/loop3         0      0         1  1 /var/lib/snapd/snaps/fceux-gui_3.snap   0     512

The system is using 4 loop devices. Let's check the files pointed to by BACK-FILE, the device backing file.

ls -la /var/lib/snapd/snaps/:

합계 333344
drwxr-xr-x  3 root root     4096  3월 29 05:02 .
drwxr-xr-x 19 root root     4096  4월 26 14:52 ..
-rw-------  2 root root 95416320  3월  6 18:20 core_6405.snap
-rw-------  1 root root 95522816  3월 13 04:58 core_6531.snap
-rw-------  1 root root 93581312  3월 29 05:02 core_6673.snap
-rw-------  2 root root 56795136  3월  6 18:21 fceux-gui_3.snap
drwxr-xr-x  2 root root     4096 12월  1  2017 partial

The block devices created as a result of the loopback can also be checked with fdisk.

sudo fdisk -l:

Disk /dev/loop0: 91.1 MiB, 95522816 bytes, 186568 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/loop1: 89.3 MiB, 93581312 bytes, 182776 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/loop2: 91 MiB, 95416320 bytes, 186360 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/loop3: 54.2 MiB, 56795136 bytes, 110928 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
...

See also

Favorite site

author Zer0 sitemap