Mounting ISOs without Root

Posted by Brad on Sat 16 March 2019

Mounting ISOs is a fairly common task and it's always kind of annoyed me that it requires sudo or root. Of course most file managers have built in support to handle this without explicit privilege escalation, but for whatever reason KDE's dolphin doesn't. There appears to be some 3rd party widget available but feedback on it was fairly bad. In any case it got me thinking that if file managers can handle it I should be able to as well. And a quick google search later I found udisks and the udisksctl command which is mostly likely installed by default on desktop versions of the common distros.

Unfortunately, udisks does add some complexity and requires 2 commands to replace sudo mount -o loop your.iso isodir. To make it even more fun the output of the loop-setup command is human readable, like Mapped file your.iso as /dev/loop2. so we need to pull out the /dev/loop2 part out to use later in the mount command. The awk command in the function below does exactly that, taking the 5th chunk and then dropping the last character to get rid of the period.

function mountiso {
        LOOP=$(udisksctl loop-setup -r -f $1 | awk '{ print substr($5, 1, length($5)-1) }')
        udisksctl mount -b $LOOP

On the flip side the udisksctl unmount also only accepts the block device, so it's not terribly useful here. Instead we'll use df to determine the block device, then unmount it, and finally clean up the loop device.

function umountiso {
        LOOP=$(df | grep /run/media/youruser/youriso | awk '{print $1 }')
        umount $1
        udisksctl loop-delete -b $LOOP

Now we have a nice couple bash functions to avoid lazily using sudo.

tags: tips, bash