Topic: Script to quickly mount/unmount an iso file

I got tired of typing the same stuff in a terminal over and over, so put this script together to add to Thunar's custom actions.
If you are looking at .iso files a lot it might be handy. It will make a folder in the current directory, mount the iso file to it (with gksu) and open thunar in that folder. If the iso is mounted already it will unmount it. The folder is deleted once you've unmounted it. You won't be able to edit the iso file, just view it, or copy stuff out of it. edit - unmounted folder deleted, and added some checks

#!/bin/bash
# open-iso.sh
# mount .iso file in new folder and open with thunar, unmount if mounted already

[[ -f $1 ]] && [[ $1 = *.iso ]] ||  { notify-send "$1 is not an iso file"; exit 1 ;}
foldername=${1%.iso}
if grep "^/dev/loop.*$(readlink -f $foldername)" /etc/mtab >/dev/null
then
    gksu --description "open-iso (unmount)" "umount $foldername" || { notify-send "failed to unmount $1"; exit 1;}
    notify-send "$1 has been unmounted"
    [[ -d $foldername ]] && [[ -z $( ls -A $foldername ) ]] && rm -r $foldername  # only remove folder if it is empty
else
    [[ -d $foldername ]] && [[ $( ls -A $foldername ) ]] && { notify-send "$foldername is not empty" ; exit 1 ;}
    mkdir -p $foldername || { notify-send "Could not create folder $foldername"; exit 1;}
    gksu --description "open-iso (mount)" "mount -o loop $1 $foldername" || { notify-send "failed to mount $1"; exit 1;}
    thunar $foldername
fi    

exit

As usual, put it in ~/scripts or somewhere handy and add a custom action like '~/scripts/open-iso.sh %f'. Make the appearance conditions "Other Files" and File pattern '*.iso'.

Last edited by johnraff (2010-03-05 18:02:24)

John
------------------------
( a boring Japan blog , and idle twitterings )
“There is more Unix-nature in one line of shell script than there is in ten thousand lines of C.” - Master Foo

Re: Script to quickly mount/unmount an iso file

Nice!

I just have an ISO shortcut (to a previously created dir) on the side with a custom action (same appearance conditions) to mount the iso file:

sudo mount -o loop -t iso9660 %f /mnt/ISO

and another one to umount it. Not as fancy. wink

Cumprimentos. Regards.
--
Asus EeeBoxPC 1501P and EeePC 1000H with #! Xfce Linux

Re: Script to quickly mount/unmount an iso file

Edited it a bit: decided to delete the folder once the iso is unmounted, and added a few checks and error messages to 'notify-send' (it's installed by default in #!).

This kind of stuff is fun - once you've got it set up, the computer does the work! smile

Last edited by johnraff (2010-03-05 18:06:19)

John
------------------------
( a boring Japan blog , and idle twitterings )
“There is more Unix-nature in one line of shell script than there is in ten thousand lines of C.” - Master Foo

Re: Script to quickly mount/unmount an iso file

Excuse the necro-bump but thanks for the tip, using it now myself smile

Re: Script to quickly mount/unmount an iso file

are we talking about statler/debian?
I am and if I insert a cd etc. or a usb stick, the system mounts it and opens thunar for me already!
am I doing something wrong ?!

p.s. I always close thunar and start mc, but it's usefule to see the contents straight away.

regards

Linux - It's not rocket surgery, after all!
web - nigelhoward.site90.com
skype - nogg321

Re: Script to quickly mount/unmount an iso file

nogg321 wrote:

am I doing something wrong ?!

you think/talk about CDs (actual physical discs made of plastic) they are talking about .iso image files (just some file format like any other)

Last edited by luc (2010-10-20 20:41:16)

Re: Script to quickly mount/unmount an iso file

When I run this script on ubuntu 11.10 it works great but it opens another instance of thunar?

Re: Script to quickly mount/unmount an iso file

Yes, it opens a new thunar window with the mounted iso file system. Unfortunately, you need to close that window manually after the iso is unmounted.

To be honest, I think that script has been outdated by the fuseiso package, which lets you mount iso files without having to use root privileges. I might write a new version, which would be much simpler. Meanwhile, check the Arch Wiki.

John
------------------------
( a boring Japan blog , and idle twitterings )
“There is more Unix-nature in one line of shell script than there is in ten thousand lines of C.” - Master Foo