Sunday, April 12, 2009

Mounting ISO on Linux

Some people don't know how easy it is to mount and use an ISO image of a disc. Once it is mounted, you can access it fairly easily with most any Linux multimedia program. I wrote a script to handle mounting ISO images for me (so I wouldn't have to remember everything every time I wanted to mount an ISO image).

First, let's get some things set up. Make a directory in your home folder, and let's call it bin (you can call it something else, you can even place it somewhere else, but since this is your personal script, it should be in your home directory). To create a directory from the command line, do this: mkdir ~/bin.

The next prepatory thing you need to do, is make sure that the bin directory is included in the path. I did this by modifying my .bashrc file. Since you're in the terminal, the easiest path would be to modify it from here. So type this: nano ~/.bashrc. From within nano (the text editor), you can just page down until you reach the end of the file. Modify it so the end of the file includes the new bin directory.

PATH=${PATH}:~/bin
export PATH

This has the advantage that it doesn't mess with any of the existing PATH items, but it does add the new one to the list.

Here is my script, I named it mntiso for my convenience. You can create it in your favorite text editor and save it to your bin directory, or you can type this at the command line: nano ~/bin/mntiso.

#! /bin/bash
sudo modprobe loop
sudo mount $1 /media/iso -t iso9660 -o loop

Note: If you are using 9.04, then you need to skip the sudo modprobe loop line. It is no longer necessary.

If you make a script of it (something I strongly recommend), don't forget to make it executable. You can make it executable by typing this at the command line: chmod +x ~/bin/mntiso. My script also assumes that the directory /media/iso exists. If you don't have the directory, then I suggest you go ahead and create it (sudo mkdir /media/iso). To use the script, you would do the following: mntiso movie.iso. Once it is mounted, you can view your home movie ISO from kaffeine by opening the directory /media/iso. Once it is open, then your home movie will play and you don't have to worry about burning a disc only to find out that you don't like some feature that you included in it.

What to do once your done viewing your home movie? You'll need to unmount it before you can replace it with something new. So, I created another simple script to do this as well. You can by typing this at the command line: nano ~/bin/umntiso.

#! /bin/sh
sudo umount -d /media/iso

I named this one umntiso. If you changed the directory that you mount the ISO image, you'll need to make the change here as well. When you use this script, you won't have to pass it anything. The script just arbitrarily unmounts the ISO image from the preestablished directory.

Note: I do know that the unmounting generates an error on pre-9.04 systems, so I'll assume that you'll see the same error when you run the script.
ioctl: LOOP_CLR_FD: No such device or address
From what I can tell, this error has no impact upon things functioning correctly. It appears to be nothing more than an annoyance.