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,910
  • 10
  • 34
  • 68

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!

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
  • 17,371
2

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.

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
  • 17,371
Alessandro
  • 368
  • 3
  • 9