The error is not because you already have another ISO device mounted; the mount -o loop
will always get the first unused loop device or create a new one.
And it's not because the same ISO image / loop device is already mounted on another directory -- the same device can be mounted on different mount points at the same time.
What you're probably trying to do is mount the same ISO on the same mount point twice. The mount(8)
utility will try to guard you against that by checking if the backing file is already attached to a loop device and reusing that device, instead of attaching the same file to multiple loop devices.
But if you really want to do that, you can bypass the check by attaching the loop device by hand with losetup
:
# mount -o ro a.iso dir/
# mount -o ro a.iso dir/
mount: /tmp/a.iso is already mounted
# losetup --show -f a.iso
/dev/loop1
# mount -o ro /dev/loop1 dir/
#
The two combined:
# dev=$(losetup --show -f a.iso) && { mount -o ro "$dev" dir/ || losetup -d "$dev"; }
You should then use the -d
option of umount(8)
explicitly, or detach the the loop by hand afterwards, with losetup -d
:
# umount -d dir/
That should be pretty safe with read-only filesystems, like ISOs. Don't ever use it with filesystems mounted in read-write mode.
/tmp/foo
a directory? What about ownership and permissions? Are you mounting with root privileges (logged in asroot
or withsudo
)?/tmp/foo
is a directory and I run the script assudo
.losetup