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.
1 Answer
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]:
2THE 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/loop3will set up the loop device
/dev/loop3to 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 loopis given), thenmountwill try to find some unused loop device and use that, for examplemount /tmp/disk.img /mnt -o loopThe
mountcommand automatically creates a loop device from a regular file if a filesystem type is not specified or the filesystem is known forlibblkid, for example:mount /tmp/disk.img /mnt mount -t ext4 /tmp/disk.img /mntThis type of mount knows about three options, namely
loop,offsetandsizelimit, that are really options tolosetup(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
mountwill be freed byumountindependently of/etc/mtab.You can also free a loop device by hand, using
losetup -dorumount -d.Since
util-linuxv2.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 sameoffsetandsizelimit. This is necessary to avoid a filesystem corruption.