In Unix-like operating systems, a loop device , loopback device , vnd (vnode disk), or lofi (loopback file interface) is a pseudo-device that makes a file accessible as a pseudo-device. A loop device may allow some kind of data elaboration during this redirection; for example, the device may be the unencrypted version of an encrypted file.
Since loop devices allow seeing a regular file as a device, they allow for mounting those regular files that contain an entire file system. Files of this kind that are often used are CD ISO images and floppy disc images. Mounting a file containing a filesystem via such a loop mount makes the files within that filesystem accessible as regular files, located on the mount point directory.
The file that is associated to a loop device may be a regular file, but also another pseudo-device. This is mostly useful when this device contains an encrypted file system. If supported, the loop device is in the case the decrypted version of the original device, and can therefore be mounted directly.
Examples
Mounting a file on a directory requires two steps:
- the file is associated with a loop device node, which is a special file
- the loop device is mounted on the directory
These two operations can be performed either using two separate commands, or by letting the mount command perform the first step before actually doing the mount. The first operation can be executed by a specific command such as losetup in Linux. As an example, if example.img
is a regular file containing a filesystem and /home/you/dir
is a directory on a Linux box, the root user can mount the file on the directory by executing the following two commands:
losetup /dev/loop0 example.img
mount /dev/loop0 /home/you/dir
The first command associates the loop device node /dev/loop0
with the regular file example.img
. This association can be later destroyed by executing losetup -d /dev/loop0
. The second command mounts the device on the directory /home/you/dir
. The global effect of executing these two commands is that the content of the file is used as the whole mounted directory. The system call used by losetup to associate and disassociate files with loop devices is an ioctl on the loop device.
An alternative way of doing the same is to let mount handle the setting up of the loop device:
mount -o loop example.img /home/you/dir
In this case, the mount command performs both the association of the file with the loop device and the mount itself.
At the level of system calls, the association and disassociation of a file with a loop device are done via ioctl 's on the loop device. Both losetup and mount therefore use such ioctl's to operate on loop devices. For example, losetup /dev/loop0 example.img
opens device /dev/loop0
and performs an ioctl on the resulting file descriptor, passing LOOP_SET_FD
as the request number and the string example.img
as the third argument.
Uses of loop mounting
After mounting a file containing a filesystem, the files within the filesystem can be accessed through the usual filesystem interface of the operating system, without any need for special functionality, such as reading and writing to ISO images, in applications.
Uses include managing and editing filesystem images meant for later normal use (especially CD or DVD images or installation systems) or permanent segregation of data in actual use (for example simulating removable media on a faster and more convenient hard disk or encapsulating encrypted filesystems).
Availability
Various Unix-like operating system provide the loop device functionality under different names. In Linux, the device is called loop device and generally named /dev/loop0 , /dev/loop1 , etc. and created by the makedev script. In BSD, is called virtual node device or vnd, and generally located at /dev/vnd0 , /dev/rvnd0 or /dev/svnd0 , etc. In SunOS, is called loopback file interface or lofi, and located at /dev/lofi/1 , etc.
from: http://gorebill.blog.163.com/blog/static/240616372008575646832/