Is a /dev/loop preferred for mounting file-as-filesystem?

0

I have been hacking around with files as filesystems, for things like encrypted volumes and the like. This tutorial uses losetup and a /dev/loopX device to facilitate the mount process. However, another tutorial does not, they just mount file.fs /mnt/tmp.

Is the /dev/loop approach best practice? I could not get the first example of the first tutorial (using losetup) to work as-is, though mount -o /dev/loopX worked for me. The second tutorial worked fine just as well, though it looks like based on df it automagically created a /dev/loopX...soo is this approach even any different?

This question is similar, but not quite, it asks about journalling on top of loopback. https://superuser.com/a/266177/617695

DeusXMachina

Posted 2019-12-17T23:49:55.227

Reputation: 113

Answers

0

is this approach even any different?

No. Modern mount is smart enough to automate the process, so you don't need to invoke losetup by hand. The following fragment of the manual is relevant [emphasis mine]:

THE LOOP DEVICE

One further possible type is a mount via the loop device. For example, the command

mount /tmp/disk.img /mnt -t vfat -o loop=/dev/loop3

will set up the loop device /dev/loop3 to correspond to the file /tmp/disk.img, and then mount this device on /mnt.

If no explicit loop device is mentioned (but just an option -o loop is given), then mount will try to find some unused loop device and use that, for example

mount /tmp/disk.img /mnt -o loop

The mount command automatically creates a loop device from a regular file if a filesystem type is not specified or the filesystem is known for libblkid, for example:

mount /tmp/disk.img /mnt

mount -t ext4 /tmp/disk.img /mnt

This type of mount knows about three options, namely loop, offset and sizelimit, that are really options to losetup(8). (These options can be used in addition to those specific to the filesystem type.)

Since Linux 2.6.25 auto-destruction of loop devices is supported, meaning that any loop device allocated by mount will be freed by umount independently of /etc/mtab.

You can also free a loop device by hand, using losetup -d or umount -d.

Since util-linux v2.29 mount command re-uses the loop device rather than initialize a new device if the same backing file is already used for some loop device with the same offset and sizelimit. This is necessary to avoid a filesystem corruption.

Kamil Maciorowski

Posted 2019-12-17T23:49:55.227

Reputation: 38 429

By "modern mount" are we talking anything since at least, say, Debian 8 / ubuntu 16.04? – DeusXMachina – 2019-12-18T16:02:50.550

@DeusXMachina More like since Debian 1.1 (1996), if this comment is right. Ubuntu started few years later.

– Kamil Maciorowski – 2019-12-18T16:21:17.473