loopback: a device file with no hard-wired device
A tradition and truism in the unix world is that "everything is a file." In particular devices are treated as files, reflecting the inherent similarity that both function as data sources and destinations. So a single common mechanism for I/O can be implemented, and serve both. First of all then, devices in unix have their "device files." You can see them sitting right there in the filesystem, in the /dev/directory.
/dev/hdb - an IDE disk drive
/dev/sda - a scsi disk drive
/dev/tty0 - a user terminal (screen and keyboard combo)
/dev/fd0 - a floppy diskette drive
And second, the commands and operations that accept files for processing (cp, cat, sort, redirection > and piping | ) will also accept devices. Because everything is a file. Here's a file, depicted as a data repository:
And here's a device, depicted as just the same thing:
Specific examples include a hard drive:
a terminal:
USB storage devices like flash drives (which masquerade as scsi devices):
and floppy disks:
Most of the device files are hard-wired to a physical device. /dev/fd0 for example is tied to the floppy drive. But you will see some other device names in the /dev directory that, unlike hda, sda, fd0, tty0, don't match up with any device or device type. These orphans have no hardware to go with them. They are
/dev/loop0 - nothing in particular
/dev/loop1 - unattached
/dev/loop2 - longing for a drive
/dev/loop3 - orphaned at birth
/dev/loop4 - nada zip void
etc
They're available (and intended) to be used as indirect references to other devices. Synonyms for those. So if for some reason you'd like to refer to your floppy disk drive as /dev/loop0 instead of its regular name /dev/fd0, you can set up a temporary relationship between the device file and the actual device and go ahead. At that point the device file /dev/loop0 is "soft-wired" to the floppy drive. That tie must be set up and torn down, and is transient.
The idea is that a device file just needs as its backend something that does I/O. Something that gives and takes data. Pictorially, something with the left- and right-arrows. So a device file that lacks any actual device can marry a device file that has one. Because the latter device file's got those arrows.
The name "/dev/loop0" can front for any data repository. And /dev/fd0 is one. Now, above, /dev/loop0 is a functional synonym for /dev/fd0. This can work for any device file? and everything is a file? So it can also work for a partition, or just a regular file.
We started by discussing devices and files as an equivalent alternatives to each other. You can use a device just as you would a file, instead. Here though you see them merged, and you use the file as a device.
You might wonder what's the point? The floppy already has a perfectly good name. What good is having another one? The file is already a file. What good is treating it as a device? In the first case the advantage is that the loopback driver offers the option of dynamic encryption of data passing through it, while the regular hardware drivers don't.
If you're interested in encryption, this is one way to get it done dynamically, transparently, for the device's I/O dataflow as a whole.
In the second case there are a few things you can do to a device but not a file. Like mounting it after making a filesystem upon it. Making a filesystem (mkfs command in linux) is also known as "formatting." It's a preliminary step in setting the device up so you can read and write files to it. You can format a diskette. You can format a hard disk. But you can also format a disk file! The files that you read and write to the resulting filesystem go inside that file. It sits behind the loopback device, such that you can then mount it and begin reading and writing other files into it.
You probably see why this is called the "loopback" device. Any reference to the first device, loop0, gets looped around back to the second device, fd0, and through it to the physical diskette.
The commands involved in using a loopback device are
losetup <loopback file> <device file or ordinary file> | # connect the loopback device file to a real data repository |
mkfs <loopback file> | # make a filesystem on it (unless one is there already!) |
mount <loopback file> <mountpoint> | # integrate it into your file hierarchy, for access |
<use the device by using the "mountpoint" subtree> | # access |
umount <loopback file> | # take it out of your file hierarchy |
losetup -d <loopback file> | # disconnect the loopback device file from its backend |
Here is are screenshots showing the set up, usage, and tear down of a loopback device using a disk file as backend.