3

I am trying understand what the difference is between two ways of creating a loop mount point. Now I can create this with:

  1. sudo mount -o loop /file_to_use /mntpoint, and

  2. sudo losetup /dev/loop1 /file_to_use and then mount sudo mount /dev/loop1 /mntpoint

Question:

Are these the two processes the same as I have tested them and there both result in the same mount points as seen here, but I could be wrong?

NAME   FSTYPE LABEL UUID                                 MOUNTPOINT
sda                                                      
├─sda1 ext4         7af461fe-8d2d-4499-a12a-72ff586ff5d6 /boot
├─sda2 swap         44744c25-8797-4cd2-b83e-f129e97efc85 [SWAP]
├─sda3 ext4         3d39068d-96da-4f90-be42-b0921fb7278e /
└─sda4 ext4         643c8738-9823-43cd-b2b2-efe0b81b7611 /home
sr0                                                      
loop0  ext4         ac7deda8-7f3b-4835-8308-e4020ffe5302 /mnt/vfs
loop1  ext4         68a0676b-ad59-4dff-9c50-eca5d972c1c7 /mnt/tempdir
3
  • I'm not aware of internals but both results same thing, mount tries to find first unused loop device just like losetup, unless you specify a specific path. you can control a loop device created by mount using losetup however something in mount man page is interesting:The loop option with the offset or sizelimit options used may fail when using older kernels if the mount command can't confirm that the size of the block device has been configured as requested This situation can be worked around by using the losetup command manually before calling mount with the configured loop device
    – Ravexina
    Sep 8, 2018 at 20:57
  • The workaround part is interesting as it appears to me that this is a pointer to my hunch that both are different paths to the same end! Sep 8, 2018 at 21:10
  • 1
    Yeah, the loop option of mount command is trying to do all the necessary stuff while we are running it so we don't have to manage the loop devices our self. Also it has to be able to do this (Managing loop devices). So it will be able to use this ability when it's necessary to mount a device but user doesn't know that.
    – Ravexina
    Sep 8, 2018 at 21:15

1 Answer 1

1

Yes, both are essentially doing the same thing. Refer to man page of mount: https://linux.die.net/man/8/mount

The Loop Device

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

mount /tmp/fdimage /mnt -t vfat -o loop=/dev/loop3

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

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

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.

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

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

Bonus: sudo mount /file_to_use /mntpoint will work too. mount will automatically setup a loop device for you!

1
  • excellent answer and link! Sep 10, 2018 at 12:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .