Best Nautilus script to mount ISO images
I know, there’s a lot of stuff on the internet about mounting ISO images as a loopback device on linux . Basically you can do this from a terminal:
mkdir /media/example
mount -o loop example.iso /media/example
from root account or using the sudo facility. There are some GUIs to automate this (gmountiso for example).
Yesterday i was searching for a fast way to mount ISO images directly from the Nautilus file manager (gnome’s one),
- allowing multiple images (mass) mount/unmount
- possibly without root privileges.
<update>: In recent gnome/ubuntu versions (i’ve found it in 10.04) it is possible to use “Archive Mounter” (right click on file.iso -> open with Archive Mounter) which uses GVFS and works fine. If i had seen it before i would not have reinvented the wheel…it should be advertised more, because many forums and guides don’t refer to this application. Anyway the following info will be useful for people using older versions of gnome, for example the one included in Red Hat Enterprise Linux 5 (or Centos 5).</update>
I found these two scripts on the Ubuntu forum, but they missed both my requirements, as they need root privileges and can mount/unmount one image at a time. So i rewrited the scripts to meet my requirements. Let’s see how:
Prerequisites
You must have FuseIso installed, and a directory inside /media writable by all users, which i called “ISO”. To avoid security issues, i will allow only users from the admin group; you are free to use another group of your choice, or to give write privileges to everyone (chmod 777).
Instructions for debian/ubuntu based systems (type in a terminal window):
sudo -s # on ubuntu and if you use sudo in the same manner, otherwise "su -"
apt-get install fuseiso
mkdir /media/ISO
chgrp admin /media/ISO
chmod 770 /media/ISO
To do the same with Centos/RHEL 5, just type the following commands as root:
wget http://packages.sw.be/fuse-iso/fuse-iso-0.0.20070708-2.el5.rf.i386.rpm
yum localinstall --nogpgcheck fuse-iso-0.0.20070708-2.el5.rf.i386.rpm
usermod -a -G fuse YOUR_USER_NAME
mkdir /media/ISO
chgrp fuse /media/ISO
chmod 770 /media/ISO
We’ve manually downloaded fuse-iso package, which isn’t included in the default Centos repositories, for the i386 architecture. Obviously you should download the one that matches your architecture.
Another difference is that we used the fuse group, because the /bin/fuse executable belongs to that group in RHEL.
We’re done with prerequisites
Scripts
We’re going to create two scripts, mount-iso.sh and unmount-iso.sh, and place them in the nautilus scripts directory ~/.gnome2/nautilus-scripts
Use your favourite text editor to do that. For example
gedit ~/.gnome2/nautilus-scripts/mount-iso.sh ~/.gnome2/nautilus-scripts/unmount-iso.sh
will open both files in two tabs.
mount-iso.sh:
#!/bin/bash
#
# nautilus-mount-iso
RETVAL=0
for I in "$@"
do
NAME=$(echo "$I" | sed s/\.[iI][sS][oO]$//) #strip .iso extension if present
mkdir /media/ISO/"$NAME"
if fuseiso "$I" /media/ISO/"$NAME"
then
MOUNTED_DIRS="$MOUNTED_DIRS$NAME \n"
else
rmdir /media/ISO/"$NAME"
zenity --error --title "ISO Mounter" --text "Cannot mount $I!"
RETVAL=$(expr $RETVAL + 1)
fi
done
if [ $RETVAL -ne $# ] #if one mount at least was successful
then
if zenity --question --title "ISO Mounter" --text "Successfully Mounted:\n$MOUNTED_DIRS\nOpen Volumes?"
then
nautilus /media/ISO --no-desktop
fi
fi
exit $RETVAL
unmount-iso.sh
#!/bin/bash
#
# nautilus-unmount-iso
for I in "$@"
do
NAME=$(echo "$I" | sed s/\.[iI][sS][oO]$//)
fusermount -u "/media/ISO/$NAME" && UNMOUNTED_DIRS="$UNMOUNTED_DIRS$NAME \n" && rmdir "/media/ISO/$NAME/"
done
zenity --info --text "Successfully unmounted:\n$UNMOUNTED_DIRS"
exit 0
We need to make the scripts executable to make them recognized by nautilus. In a terminal:
chmod +x ~/.gnome2/nautilus-scripts/*mount-iso.sh
This way when you right click on a file, a “Script” menu should appear, containing the two options (mount and unmount). With these scripts i’ve met my two requirements, with a bonus: i can unmount the image right cliking on both the original .iso file and the mount point! Let’s see how it works:
Now you can spend your day mounting and unmounting iso images
Have fun!
Bye