2

I setup a loopback device following this guide.

The device is OK, but only writable for root. I searched solutions and found some answer such as using fusermount, fstab etc., e.g. this one.

I would like to know specifically:

  1. Requiring root to create the device is not a problem, but it must be writable for normal users after mounted.
  2. I hope avoid using /etc/fstab, because all I need is to do it spontaneously, or in a shellscript, where I have neither a fixed loopback image nor a fixed mount point.
AdminBee
  • 22,803
xrfang
  • 215
  • 2
  • 10
  • Do you want to write the loop device or the files in a file system mounted on it ? Loop devices permission can be changed by chown and chmod. – Frédéric Loyer Nov 15 '21 at 09:49
  • 2
    Writeable loop devices: chown/chmod /dev/loop*

    Writable FS after mounting? chown/chmod /mnt/mountpoint or using uid/fmask/dmask in case your FS is not "unix".

    – Artem S. Tashkinov Nov 15 '21 at 09:58
  • the loop device is formated by mkfs.ext4, and mounted. I want to write to the file system contained in the device – xrfang Nov 15 '21 at 09:58

1 Answers1

0

I setup a loopback device

Unless you use a really ancient kernel, this is longer necessary - the normal mount process implicitly generates a loop device.

The device is OK, but only writable for root.

Permissions on the loop device do not influence permissions on the mounted filesystem. Nor does it influence who can actually mount a file.

Requiring root to create the device is not a problem, but it must be writable for normal users after mounted.

The filesystem permissions are derived from the actual data in what you mount. Some filesystems (e.g. FAT) have options where you can attach unix permissions. Other filesystem (e.g. ext) don't.

I want to write to the file system contained in the device

Then you are not going to solve this by using a loop device. (Also, please read up on XY questions. Your X is "All users should be able to read from the filesystem", your Y is "I think I need a loop device", unsurprisingly that does not work, and you only ask about Y, instead of starting out asking about X).

As written in the comments, you can chown/chmod (as root) after mounting to let all users read it, changing the contents of the filesystem. That's the simplest solution.

There is no easy solution for "I want to mount an ext4 filesystem, but transparently overwrite permissions on everything on mount". At least none I know of.


From man mount:

THE LOOP DEVICE

One further possible type is a mount via the loop device. [...] 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 [...]

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.)

dirkt
  • 32,309
  • 1
    I don't think it is a XY question. The reason to use a loopback device is to use a file of fixed size as a disk, that's all. Could you please explain your first comment. i.e. if I do not do losetup etc. how to specify a fixed size file as the container for loopback device, using only 'mount'? thanks – xrfang Nov 16 '21 at 01:05