Mounting disk images read/write in KDE

Submitted by Falken on

All the advice on articles like https://unix.stackexchange.com/questions/47080/mounting-iso-in-linux-kde don't work in modern KDE, or are about mounting .iso files, which is now built in, or a disk image as read only.

I needed to mount disk images read/write, which appears to be more complicated because it needs to run as root, so prompt for the password, fingerprint or what have you.

A screen shot of the Dolphin file manager with a right click menu showing. It has two options.

You need a helper script ~/bin/img_manager-mount-image.sh (create this folder if required, and make the new file executable)

#!/bin/bash
echo "$USER ($UID) $2 $1" > /tmp/a
f=`basename "$1"`
p="/media/$USER/$f"
echo "  @$p is file $f" >> /tmp/a
if [ "$2" == "umount" ]; then
  pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY sudo sh -c "umount $p" >>/tmp/a 2>&1
else
  pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY sudo sh -c "mkdir -p $p; chmod u+rwx $p; chown -R $USER $p ;mount -o loop,uid=$UID,rw \"$1\" $p" >>/tmp/a 2>&1
fi
l=`cat /tmp/a`
kdialog --msgbox "$l"

Then a file ~/.local/share/kservices5/ServiceMenus/mount-img.desktop (create this path if it doesn't exist)

[Desktop Entry]
Type=Service
ServiceTypes=KonqPopupMenu/Plugin
MimeType=application/vnd.efi.img
Actions=mountIMG;
X-KDE-Priority=TopLevel
X-KDE-StartupNotify=false
Icon=application-x-cd-image

[Desktop Action mountIMG]
Name=Mount as rw
Icon=media-import-audio-cd
Exec=~/bin/img_manager-mount-image.sh %F mount

 


Yes, the logging is insecure. Yes, it's output could be better formatted. But it works and didn't exist before.

Once mounted, it's listed as a standard mount in Dolphin's side bar and can be removed from there.

YMMV etc :)

Sections