How to mount an image file without root?

Solution 1:

You can use udiskctl:

$ udisksctl loop-setup --file your_file.iso
Mapped file your_file.iso as /dev/loop6.

Now, your file is mapped to a block device, and you can mount it as:

$ udisksctl mount -b /dev/loop6
Mounted /dev/loop6 at /media/user/your_file.

when you're done, unmount is using:

$ udisksctl unmount -b /dev/loop6
Unmounted /dev/loop6.

finally, delete it by:

$ udisksctl loop-delete -b /dev/loop6

Have fun!

Solution 2:

You can have it in /etc/fstab. My home directory is encrypted, yet:

$ dd if=/dev/zero of=ext4_file bs=1024 count=1024
1024+0 records in
1024+0 records out
1048576 bytes (1,0 MB) copied, 0,0341311 s, 30,7 MB/s
$ /sbin/mkfs.ext4 -F ext4_file
mke2fs 1.42.12 (29-Aug-2014)

Filesystem too small for a journal
Discarding device blocks: done                            
Creating filesystem with 1024 1k blocks and 128 inodes

Allocating group tables: done                            
Writing inode tables: done                            
Writing superblocks and filesystem accounting information: done

$ grep ext4_directory /etc/fstab
/home/alessandro/ext4_file /home/alessandro/ext4_directory ext4 noauto,user 0 0
$ mount ext4_directory
$ mount | grep ext4_directory
/home/alessandro/ext4_file on /home/alessandro/ext4_directory type ext4 (rw,nosuid,nodev,noexec,relatime,user=alessandro)

Solution 3:

GNOME Disk Image Mounter

Beside udisksctl and guestmount (libguestfs-tools) you can just:

gnome-disk-image-mounter ~/ISOs/file.iso

From manual:

Accept both regular files and GVfs URIs (such as smb://filer/media/file.iso)")

By default the disk images are attached read-only, use the option --writable to change this.

  • gnome-disk-image-mounter is part of gnome-disk-utility package, which also brings gnome-disks (run it with -h for more info).
  • Despite its name you can use it on KDE without any gnome dependency.
  • Also check gio mount and udisksctl.
  • See also How to access a usb flash drive from the terminal?

Solution 4:

guestmount libguestfs trickery

sudo apt-get install libguestfs-tools

# Workarounds for Ubuntu 18.04 bugs.
# https://serverfault.com/questions/246835/convert-directory-to-qemu-kvm-virtual-disk-image/916697#916697
sudo rm -rf /var/cache/.guestfs-*
echo dash | sudo tee /usr/lib/x86_64-linux-gnu/guestfs/supermin.d/zz-dash-packages
sudo chmod +r /boot/vmlinuz-*

# Create a test image.
mkdir sysroot
dd if=/dev/urandom of=sysroot/myfile bs=1024 count=1024
virt-make-fs --format=raw --type=ext2 sysroot sysroot.ext2

# Mount it, have fun, unmount!
mkdir -p mnt
# /dev/sda becuase we have a raw filesystem.
guestmount -a sysroot.ext2.qcow2 -m /dev/sda mnt
cmp sysroot/myfile mnt/myfile
guestunmount mnt

Relies on:

  • userland implementation of the filesystems
  • FUSE

Docs: http://libguestfs.org/guestmount.1.html

Tested on Ubuntu 18.04, libguestfs-tools 1:1.36.13-1ubuntu3.