mountiso - Mount any ISO file, intended to make a CD or DVD, to any Folder

Our next script file today allows you to mount any ISO image on your PC to any folder, read only, so you may browse or copy the contents of that ISO file. Now consider that this script is being placed here as an inspiration to creating script files of your own. The fact that it might do something useful in addition, is just a plus. The meat of this script is the command: mount -o loop -t iso9660 name_of_iso_file folder_to_mount_to. So, you can bypass the script and use this command if you wish.

However, this script will check to make sure your ISO file exists, that the target folder exists, it will request a root user password and provide an error code should something go wrong. In order to create this script, copy and past the text in the following code section and save it as the text file mountiso in your home area bin folder (~/bin).

#!/bin/bash

#: Title       : mountiso - Mount any ISO file, intended to make a CD or DVD, to any Folder
#: Date Created: Fri Oct 8 20:20:31 CDT 2010
#: Last Edit   : Sat Oct 9 18:51:03 CDT 2010
#: Author      : J. McDaniel
#: Version     : 1.00
#: Description : Mount an ISO File to a Folder
#: Options     : $1=ISOFILE $2=Where to be mounted

# Created for openSUSE forums of Saturday October 9th, 2010 

# Copy and paste this text into a file in your home as ~/bin/mountiso
# Run the following Terminal Command: chmod +x ~/bin/mount
# To make the file executable.  To use, simply open a Terminal session
# and type the command: mountiso file mountpoint

#
# Check to see if we got any command line options
#

if  $# -eq 0 ] ; then
  tput setf 7
  tput setb 4
  tput bold
  echo
  echo "Terminal Command Syntax: mountiso file mountpoint"
  tput sgr0
  echo
  exit 1
fi

#
# Check to see if we are root
#

tput clear
if  $UID -ne 0 ]]; then
  id -Gn | grep -q wheel || echo "Root User Permissions are required, Please Enter the ..."
  echo
  sudo $0 $1 $2
  exit 0
fi

#
# Display Program Header in Color
#

tput setf 7
tput setb 2
tput bold
echo
echo "mountiso - Mount any ISO file, intended to make a CD or DVD, to any Folder"
tput sgr0
echo

#
# make sure the file exists
#

if  ! -e $1 ] ; then
  tput setf 7
  tput setb 4
  tput bold
  echo "The File $1 does not Exist!"
  tput sgr0
  echo
  exit 1
fi

#
# make sure the folder exists
#

if  ! -d $2 ]]; then
  tput setf 7
  tput setb 4
  tput bold
  echo "The Folder $2 does not Exist!"
  tput sgr0
  echo
  exit 1  
fi

mount -o loop -t iso9660 $1 $2
mount_exit_value=$?

#
# Determine if the mount process was successful
#

if  "${mount_exit_value}" -ne "0" ] ; then
  echo
  tput setf 7
  tput setb 4
  tput bold
  echo "Error Code Reported During Mount Process: "$mount_exit_value
  tput sgr0
  echo
  exit 1
fi

# Mount RETURN error CODES
#       mount has the following return codes (the bits can be ORed):
#
#       0      success
#       1      incorrect invocation or permissions
#       2      system error (out of memory, cannot fork, no more loop devices)
#       4      internal mount bug
#       8      user interrupt
#       16     problems writing or locking /etc/mtab
#       32     mount failure
#       64     some mount succeeded


#
# Display Folder Directory and success display
#

echo
dir -x $2
echo

tput setf 7
tput setb 1
tput bold
echo "ISO Disk $1 was mounted in folder $2. "
echo "To remove the ISO mount, use the following terminal command as root: sudo umount $2   "
tput sgr0
echo

exit 0
# End Of Script


To make the script file executable, run the following terminal command:

chmod +x ~/bin/mount

As an example, suppose we downloaded the 64 bit openSUSE DVD and we wanted to look at its contents without making a new DVD. Normally, downloaded files go to your home Downloads folder. Further, if you look at your openSUSE folder system, you will see a normally unused folder called /mnt (it is normally unused, but look first with a File Manager to make sure). Let us suppose we enter the following command using mountiso:

 mountiso ~/Downloads/openSUSE-11.3-DVD-x86_64.iso /mnt

Here is the resulting output from that iso file mount using mountiso.

mountiso - Mount any ISO file, intended to make a CD or DVD, as a disk drive

mount: warning: /mnt seems to be mounted read-only.

ARCHIVES.gz                       autorun.inf                       boot                              ChangeLog
content                           content.asc                       content.key                       control.xml
directory.yast                    docu                              dosutils                          gpg-pubkey-0dfb3188-41ed929b.asc
gpg-pubkey-307e3d54-4be01a65.asc  gpg-pubkey-3d25d3d9-36e12d04.asc  gpg-pubkey-3dbdc284-4be1884d.asc  gpg-pubkey-56b4177a-4be18cab.asc
gpg-pubkey-7e2e3b05-4be037ca.asc  gpg-pubkey-9c800aca-4be01999.asc  gpg-pubkey-a1912208-446a0899.asc  GPLv2.txt
GPLv3.txt                         images                            INDEX.gz                          license.tar.gz
ls-lR.gz                          media.1                           openSUSE11_3_LOCAL.exe            openSUSE11_3_NET.exe
pubring.gpg                       README                            suse                              SuSEgo.ico

ISO Disk /home/james/Downloads/openSUSE-11.3-DVD-x86_64.iso was mounted in folder /mnt. 
To remove the ISO mount, use the following terminal command as root: sudo umount /mnt   

If you have any comments or suggestions for mountiso, please let me hear what they are.

Thank You,

Hi James
IMO you should define a target folder by default (in a variable at the top), so that the script could also be run with just the iso file as only argument and use the default mount point if the second argument is omitted… of course check if somebody is already mounted there, etc.:wink:

please_try_again Hi James
IMO you should define a target folder by default (in a variable at the top), so that the script could also be run with just the iso file as only argument and use the default mount point if the second argument is omitted… of course check if somebody is already mounted there, etc.:wink:
That is a very good idea please_try_again. Lets see if there are any bugs found or any other suggestions, then I will post a new script. Thank you for your suggestions!

Thank You,

Oh OK, I just could not wait to make the changes suggested by please_try_again. So, here is a second version that will use the default mount location /mnt which can be modified if you so desire and thus be an optional entry.

Just as before, copy and past the text in the following code section into a text file called mountiso in your home area bin folder (~/bin/mountiso):

#!/bin/bash

#: Title       : mountiso - Mount any ISO file, intended to make a CD or DVD, to any Folder
#: Date Created: Fri Oct 8 20:20:31 CDT 2010
#: Last Edit   : Sat Oct 9 19:42:09 CDT 2010
#: Author      : J. McDaniel
#: Version     : 1.01
#: Description : Mount an ISO File as a Partition
#: Options     : $1=ISOFILE & Optional $2=Where to be mounted

# Created for openSUSE forums of Saturday October 9th, 2010 

# Copy and paste this text into a file in your home as ~/bin/mountiso
# Run the following Terminal Command: chmod +x ~/bin/mount
# To make the file executable.  To use, simply open a Terminal session
# and type the command: mountiso file [mountpoint]

#
# Default Folder for mounting if none is given
#

DFOLDER=/mnt

#
# Check to see if we got any command line options
#

if  $# -eq 0 ] ; then
  tput setf 7
  tput setb 4
  tput bold
  echo
  echo "Terminal Command Syntax: mountiso file [mountpoint]"
  tput sgr0
  echo
  exit 1
fi

#
# Check to see if we are root
#

tput clear
if  $UID -ne 0 ]]; then
  id -Gn | grep -q wheel || echo "Root User Permissions are required, Please Enter the ..."
  echo
  sudo $0 $1 $2
  exit 0
fi

#
# Check for Folder Mount Location Given, if any.  Set for Default if not present.
#

if  "$2" == "" ] ; then
  FOLD=$DFOLDER
else
  FOLD=$2
fi

#
# Display Program Header in Color
#

tput setf 7
tput setb 2
tput bold
echo
echo "mountiso - Mount any ISO file, intended to make a CD or DVD, to any Folder"
tput sgr0
echo

#
# make sure the file exists
#

if  ! -e $1 ] ; then
  tput setf 7
  tput setb 4
  tput bold
  echo "The File $1 does not Exist!"
  tput sgr0
  echo
  exit 1
fi

#
# make sure the folder exists
#

if  ! -d $FOLD ]]; then
  tput setf 7
  tput setb 4
  tput bold
  echo "The Folder $FOLD does not Exist!"
  tput sgr0
  echo
  exit 1  
fi

mount -o loop -t iso9660 $1 $FOLD
mount_exit_value=$?

#
# Determine if the mount process was successful
#

if  "${mount_exit_value}" -ne "0" ] ; then
  echo
  tput setf 7
  tput setb 4
  tput bold
  echo "Error Code Reported During Mount Process: "$mount_exit_value
  tput sgr0
  echo
  exit 1
fi

# Mount RETURN error CODES
#       mount has the following return codes (the bits can be ORed):
#
#       0      success
#       1      incorrect invocation or permissions
#       2      system error (out of memory, cannot fork, no more loop devices)
#       4      internal mount bug
#       8      user interrupt
#       16     problems writing or locking /etc/mtab
#       32     mount failure
#       64     some mount succeeded


#
# Display Folder Directory and success display
#

echo
dir -x $2
echo

tput setf 7
tput setb 1
tput bold
echo "ISO Disk $1 was mounted in folder $FOLD. "
echo "To remove the ISO mount, use the following terminal command as root: sudo umount $FOLD   "
tput sgr0
echo

exit 0
# End Of Script

To make the script file executable issue the terminal command:


chmod +x ~/bin/mount

The new syntax allows for the folder to be mounted to as being optional, using the script file folder default if no folder location is provided.

Thank You,

The correct command for chmod would be:

chmod +x ~/bin/mountiso

Boy it has been a long day today.

Thank You,