#!/bin/ksh########################################################################################## Program : mntiso## Purpose : Mount ISO images so we don't need to burn a CD or DVD to view the content.## Version : 0.1## Author : Dale Sears## Original : http://blogs.sun.com/blahblah/entry/mntiso## Notes : I added a bunch of features mentioned in the initial version (0?):## * Added capability to mount multiple ISO images under a single root.# * Added checks for tabs and spaces in filenames, convert each to hyphen.# * Added getopts usage and features:## -h Usage or Help# -m Set the mount root to be different from the default /iso# -s Share the files under MOUNTROOT (read only of course...)# -S Unshare the files under MOUNTROOT# -u Unmount ALL hsfs filesystems under MOUNTROOT# -v Verbose output of mount points and other trivia.## Todo : It's rather root-centric in the defaults... There *are* other users, and# maybe they don't have permission to mkdir /iso or share. Maybe you could# tell me if it would be better to have a default /mountroot like: ~/iso## A little more precision with -u (single ISO umount) instead of ALL.############################################################################################################################################################### The default /mountroot under which the ISO's would be mounted# like so:## /iso/image1.iso# /iso/image2.iso#######################################################################MOUNTROOT=/isoBASENAME=${0##*/}CMDLINE=$*usage() { echo echo "Usage: " echo " $BASENAME [-h] [-m </mountroot>] [-s|-S] [-u] [-v] /fully/qualified/path.iso [file.iso]..." echo echo " -h # Help, usage." echo echo " -m </mountroot> # Set a new /mountroot to be different from the default $MOUNTROOT" echo echo " -s # Share the files under </mountroot>" echo echo " -S # Unshare the files under </mountroot>" echo echo " -u # Unmount ALL hsfs filesystems under </mountroot>" echo echo " -v # Verbose output of mount points and other trivia." echo}####################################################################### Parse command-line:######################################################################while getopts 'hm:sSuv' argdo case $arg in h) usage; exit 0 ;; m) MOUNTROOT="${OPTARG%%*/}" ;; s) SHARE=1 ###################################################################### # We can't share and unshare at the same time: ###################################################################### if [ $UNSHARE ] then usage echo "ERROR: -s and -S are mutually exclusive." exit 1 fi ;; S) UNSHARE=1 ###################################################################### # We can't share and unshare at the same time: ###################################################################### if [ $SHARE ] then usage echo "ERROR: -s and -S are mutually exclusive." exit 1 fi ###################################################################### # -S implies -u even if we don't get one from the user. ###################################################################### UMOUNT=1 ;; u) UMOUNT=1 ###################################################################### # -u implies -S even if we don't get one from the user. ###################################################################### UNSHARE=1 ;; v) VERBOSE=1 ;; esacdone####################################################################### Rid ourselves of args and opts that we've already processed.## "$#" will now hold the number filenames we have.## "$@" will now contain only the filenames (if any).######################################################################shift $(($OPTIND - 1))####################################################################### If we get -u# umount each mounted ISO under $MOUNTROOT## Also, we're not quitting here because we can think of -u as a# form of forcing the umounts before we try to mount things:######################################################################if [ $UMOUNT ] then ###################################################################### # We will need to unshare if we're going to unmount these: ###################################################################### if [ $VERBOSE ] then echo "Unsharing $MOUNTROOT ..." ###################################################################### # I'm chopping off the 'not shared' error output from 'unshare' # because it doesn't add value to the output whether it was shared # or not shared when we want to unshare it here. We just want it to # be unshared. Any other error messages will remain visible: ###################################################################### /usr/sbin/unshare $MOUNTROOT | \ /usr/bin/grep -v 'not shared' 2>&1 else ###################################################################### # Same as above note regarding error message from 'unshare': ###################################################################### /usr/sbin/unshare -F nfs $MOUNTROOT | \ /usr/bin/grep -v 'not shared' 2>&1 fi ###################################################################### # Unmount all of the ISO's we find under MOUNTROOT: ###################################################################### /usr/sbin/df -F hsfs -n | \ /usr/bin/grep ^$MOUNTROOT | \ /usr/bin/awk -F: '{print $1}' | \ while read fs do if [ $VERBOSE ] then echo "Unmounting: $fs ..." fi /sbin/umount $fs doneelse ###################################################################### # At this point, the user did not want to unmount ISO's so we need # some files to mount. But if we didn't get any filenames then usage # and exit. # # But what if the user only wanted to run 'mntiso -s' ? # # We're getting complicated now because of the relationships of all # of the options... # # So don't complain if the user is sharing or unsharing. Whew... ###################################################################### if [ ! $SHARE -a ! $UNSHARE ] then if [ $# -lt 1 ] then usage echo "ERROR: No filenames given." exit 1 fi fifi####################################################################### For each ISO image filename given, complete a few tests, then try# to mount the ISO image. The remaining args in $@ should be our# list of ISO image filenames:######################################################################for filename in "$@"do ###################################################################### # Check for leading / on the ISO arg (needed by lofiadm): ###################################################################### case "$filename" in /*) ISO="$filename" ;; *) usage echo "ERROR: $filename is not a fully qualified filename starting with /" exit 1 ;; esac ###################################################################### # Get the basename of $ISO and then convert spaces or tabs into # a hyphen with 'tr' to create a mountpoint for the ISO image: ###################################################################### ISOMOUNTPOINT=`echo "${ISO##*/}" | /usr/bin/tr ' ' '--'` MOUNTPOINT="$MOUNTROOT/$ISOMOUNTPOINT" ###################################################################### # Remove possibly existing lofi device for the given ISO as opposed # to using the given ISO and attempting to resolve the error from that: ###################################################################### /usr/sbin/lofiadm -d "$ISO" 2> /dev/null ###################################################################### # Add lofi device for the ISO, ignoring all except one possible error: ###################################################################### LOFIDEVICE=`/usr/sbin/lofiadm -a "$ISO" 2> /dev/null` if [ -z "$LOFIDEVICE" ] then echo echo 'Uh, oh... : No lofi Device.' echo echo "Perhaps $ISO is already mounted?" echo echo "To unmount all ISO's under $MOUNTROOT, add -u like so:" echo echo " $BASENAME -u $CMDLINE" echo exit 1 fi ###################################################################### # Check for $MOUNTROOT and create as needed: ###################################################################### if [ ! -d "$MOUNTPOINT" -a ! -f "$MOUNTPOINT" ] then /bin/mkdir -p -m 555 $MOUNTPOINT fi ###################################################################### # We've passed enough tests, let's try to mount the ISO and 'mount' # will complain if $MOUNTROOT is a file or is busy: ###################################################################### /sbin/mount -F hsfs -o ro $LOFIDEVICE $MOUNTPOINTdone####################################################################### For the curious, Print a list of currently mounted hsfs# files under $MOUNTROOT:######################################################################if [ $VERBOSE ]then /usr/sbin/df -F hsfs -n | \ /usr/bin/grep ^$MOUNTROOT | \ /usr/bin/awk -F: '{print $1}' | \ while read fs; do echo " Mounted: $fs"; donefi####################################################################### If we get -s then share via NFS everything under $MOUNTROOT:######################################################################if [ $SHARE ]then /usr/sbin/share -F nfs -o ro,anon=0 $MOUNTROOT if [ $VERBOSE ] then ###################################################################### # Show the current shares: ###################################################################### /usr/sbin/share fifi