Monday, February 18, 2008

Mounting ISO images

How I can open my ISO image in Ubuntu? That's very necessary question for newbees. OK, it's very easy in actual fact. All that you need is a standard command mount:

Create any folder, for example /media/isoimage:
sudo mkdir /media/isoimage

Mount .iso file (for example something.iso) in that folder:
sudo mount something.iso /media/isoimage/ -t iso9660 -o loop

For unmount use:
sudo umount /media/isoimage

Yeah, using commands everytime is a difficult for newbees. But Nautilus file-manager has one very good feature - it can use scripts. Now we will write a simple script for mount ISO image with the help of context menu.

1. Create the file mount.sh (click by right mouse button in Nautilus and select Create document -> Empty file, enter the name, double click on it for editing and write text)


#!/bin/bash
# mount
gksudo -k /bin/echo "got r00t?"
BASENAME=`basename $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS .iso`
sudo mkdir "/media/$BASENAME"
zenity --info --title "ISO Mounter" --text "$BASENAME e $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS"
if sudo mount -o loop -t iso9660 $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS "/media/$BASENAME"
then
if zenity --question --title "ISO Mounter" --text "$BASENAME Image already mounted. Open?"
then
nautilus /media/"$BASENAME" --no-desktop
fi
exit 0
else
sudo rmdir "/media/$BASENAME"
zenity --error --title "ISO Mounter" --text "Cannot mount $BASENAME!"
exit 1
fi


2. Open the Properties window for this file and check Allow for execute (unlike Windows Linux recognizes programs not with the help of extention (like .exe for example) but with the help of special attribute eXecute). The other method is command: sudo chmod +x mount.sh

3. Create the file unmount.sh


#!/bin/bash
# unmount
gksudo -k /bin/echo "got r00t?"
BASENAME=`basename $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS .iso`
sudo umount "/media/$BASENAME"
sudo rmdir "/media/$BASENAME"
zenity --info --text "Unmounted succesfully /media/$BASENAME"
exit 0


4. As in 2nd item make unmount.sh executable.

5. Open another Nautilus windows, go to home folder, press Ctrl+H for show hidden files and go to .gnome2/nautilus-scripts

6. Copy out two files in that folder (simply drag and drop)

That's all. Now you can simply click right mouse button on the ISO file and select mount.sh in the Scripts submenu. For unmounting - select unmount.sh.

If you want you can use more interesting names for our scripts. For example you can use name "Mpunt ISO" for "mount.sh" script, and "Unmount ISO" for "unmount.sh". Because the first string of both these files is #!/bin/bash, file extention is not some sense, Ubuntu know what it is.

No comments: