This question already has an answer here:

man losetup says

losetup is used to associate loop devices with regular files or block devices, to detach loop devices, and to query the status of a loop device.

https://en.wikipedia.org/wiki/Loop_device says

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.

Since a loop file makes a file accessible as a block device, why does losetup need to associate loop devices with block devices? Isn't that unnecessary? Thanks.

marked as duplicate by Stephen Harris, Jeff Schaller, Christopher, X Tian, Haxiel Feb 26 at 18:34

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    I actually thinked about that, I think it's just linux can implement this functionality easily, although it's unlikely to be used. After all, for linux, there's not much difference between block device and regular files… – 炸鱼薯条德里克 Feb 26 at 1:48

The ability of configuring a loop device on top of another block device can be useful in cases where the loop device is being used to access only parts of the block device, to change its properties or to apply a transfer function on its contents.

For example, losetup can take a --offset offset and a --sizelimit limit arguments, allowing it to only map part of the underlying block device. (Similar to how partitions work, but not necessarily where you have a partition table.)

It also can take a -r or --read-only option to make the loop device block writes, which can be useful to prevent unwanted writes from applications that take a block device and are not expected to write to them.

Finally, losetup can take a -e, -E or --encryption encryption_type argument to use a transfer function that implements encryption on top of the underlying block device.

These three use cases are potentially useful on top of other block devices (as well as files), since their result is not just an identical translation of the underlying device.

Arguably, the devicemapper is a more modern API to accomplish this kind of transformations (and modern features for managing them, such as LVM and cryptsetup, are based on devicemapper.) The original losetup features are still present though, since users may still depend on them.

See the man page of losetup(8) for more details on the options discussed above.

  • 1
    Yeah...more like an old simple device mapper functionality, but does device mapper support regular file as block device? It seems not, so for disk image, loop device may still be needed – 炸鱼薯条德里克 Feb 26 at 3:14
  • Thanks. Why use losetup on a file which is not an image file? Is it because of the same purposes of using losetup on a block device? – Tim Feb 26 at 13:06
  • @Tim One example would be a file with a disk image, with partitions, where you want to map a specific region to the file, corresponding to a single filesystem, into a block device, so you can mount it. (The traditional way to do it is a loop device for the whole disk image, then use kpartx to split the partitions. That uses devicemapper, which I mentioned in my response. But if you wanted to use loop devices only, you could.) – filbranden Feb 26 at 13:47
  • Thanks. How about using losetup on a file which is not an image file? Is it because of the same purposes of using losetup on a block device? – Tim Feb 26 at 13:50
  • @Tim A disk image is not a filesystem image (you can't mount it directly.) Eventually the main point of a block device is to mount it, so somewhere in there you'll have something resembling a filesystem image... You don't have to though, if you have other uses for the resulting loop device. Linux won't force you to do anything specific with it. In the end, a file is just a sequence of bytes (hard to say which files qualify as an "image" file and which don't.) What did you have in mind when you say a file that's not an image file? – filbranden Feb 26 at 13:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.