13

I have a file with an ext4 file system in it and want to mount it without using sudo every time (the script should run with user rights). The file I want to mount and the folder which I want to mount it too both are in my home directory which is encrypted, so I can't put the file location into /etc/fstab.

I tried fusermount but I always get error messages like "fusermount: extra arguments after the mountpoint".

UTF-8
  • 5,710
  • 10
  • 31
  • 67

4 Answers4

3

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!

  • I tried mounting some Linux image I had laying around (kali-linux-2019.1a-amd64.iso) but I always get: Object /org/freedesktop/UDisks2/block_devices/loop5 is not a mountable filesystem. (With the device number varying, of course.) This happens even when I try to mount it read-only. Mounting it read-only via sudo mount works just fine. – UTF-8 Jul 09 '19 at 11:18
  • use udisksctl loop-setup --file your_file.iso again, so it gives you a new loop device, and mount that one (e.g. /dev/loop8). In my OS, it doesn't work using loop6 and don't know why. @UTF-8 – Mohammad Kholghi Jul 09 '19 at 16:47
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.

Pablo Bianchi
  • 15,657
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)
Pablo Bianchi
  • 15,657
Alessandro
  • 368
  • 3
  • 9
  • If he says that because it's encrypted, he can't put it in /etc/fstab, he's right. That doesn't fully mount it. – David Sep 05 '15 at 21:57
  • Well, nothing is stopping me from writing it into /etc/fstab, so I tried it before I posted this questing. I knew that it probably wouldn't mount it but assumed that maybe it would fail without consequences and that the fact that it's in /etc/fstab enabled me to mount it without root. Unfortunately this caused my system to fail to boot and I had to remove the line in order to be able to use my machine again. – UTF-8 Sep 05 '15 at 22:03
  • David, all I wrote I performed on a PC with /home mounted on an encrypted partition. And it did work. And I cannot see why it couldn't. – Alessandro Sep 06 '15 at 14:03
  • UTF-8, for it to work /home must be mounted before your file. Or you set your file not to be mounted automatically, like I set it to be in the directions I gave you, they are a real-case scenario. – Alessandro Sep 06 '15 at 14:05
  • Who put the ext4_file file in fstab?? Didn't you skip anything? – Mohammad Kholghi Jul 09 '19 at 09:20
  • Of course I put the ext4_file file in fstab, who else? I didn't skip anything, I fully reported all the steps I performed. If you think something's amiss please repeat those steps and verify for yourself. Would you still believe something was left out, please state clearly what and why. – Alessandro Jul 28 '19 at 22:43
1

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.