Monday Oct 11, 2004

Automounting ISO images....

One thing that I find irritating on Solaris is not being able to look into ISO images that I have on disk. Obviously I can do this but it involves, being root, knowing about lofiadm and then typing a mount command. It really should not be like that. Well at least for me now it is not. I just put the ISO file in ~/isos (or a symbolic link) and then cd /iso/${LOGNAME}/image where image is the name of the ISO, eg:


$ ls ~/isos
Trusted_S8_0401_CD1_of_2.img
$ ls /isos/cjg/Trusted_S8_0401_CD1_of_2.img
Copyright          Trusted_Solaris_8
$ df -k  /isos/cjg/Trusted_S8_0401_CD1_of_2.img
Filesystem            kbytes    used   avail capacity  Mounted on
/dev/lofi/1           539590  539590       0   100%    /isos/cjg/Trusted_S8_0401_CD1_of_2.img
$

All this is achieved with three short shell scripts. First one that will mount an iso image directly:


#!/bin/ksh -p

function getsubopts
{
	typeset x
	typeset y
	typeset generic_opts

	y=$1
	x=${y##,\*}

	while [[ "${x}" != "" ]]
	do
		case $x in
			lofi_backfs=\*) BACK_FS=${i#lofi_backfs=} ;;
			\*) generic_opts="${generic_opts}${generic_opts:+,}$x" ;;
		esac
		y=${y%${x}}
		y=${y%,}
		x=${y##,\*}
	done
}

while getopts Ormqo: c
do
	case $c in
		o) generic_opts=$(getsubopts $OPTARG) ;;
		O|m|q|r) opt="${opt}${opt:+ }-$c" ;;
	esac
done

shift $(($OPTIND - 1))

image=$1
dir=$2

lofidev=$(lofiadm ${image} 2> /dev/null)
if [[ "${lofidev}" = "" ]]
then
	lofidev=$(lofiadm -a ${image})
	cleanup="lofiadm -d ${image}"
else
	cleanup=:
fi
fstype=${BACK_FS:-$(fstyp ${lofidev})}

if ! mount -F ${fstype} ${generic_opts:+-o }${generic_opts} ${opt}  $lofidev $dir 
then
	${cleanup}
	exit 1
fi
exit 0

That goes in /usr/lib/fs/isofs/mount.

Now this command:

mount -F isofs /iso_store/Trusted_Solaris/Trusted_S8_0401_CD2_of_2.img /mnt



will mount the iso the image /iso_store/Trusted_Solaris/Trusted_S8_0401_CD2_of_2.img automatically without any need to mess with creating lofi device by hand. Then I just need a couple of executable automount maps so that I can let the automounter do it's stuff.

First /etc/auto_iso:

#!/bin/ksh -p
if ! getent passwd $1 > /dev/null
then
        exit 0
fi
root_dir=/var/run/auto_iso
user_dir=${root_dir}/users
master_script=${root_dir}/master_script
if mkdir -p ${user_dir}
then
        cp /etc/auto_iso_users $master_script
fi
if ! [[ -f  ${user_dir}/$1 ]]
then
        ln $master_script ${user_dir}/$1
fi
echo "/ -fstype=autofs ${user_dir}/$1"
exit 0

This just allows me to have a separate executable map per user, so that that executable map can have some context, (the users login name), passed in as the name of the script. for who it is run. The script /etc/auto_iso_users then does the work for the users:

#!/bin/ksh -p

user=${0##\*/}

if [[ -f ~${user}/isos/$1 ]]
then
        echo / -fstype=isofs ~${user}/isos/$1
fi
exit 0

Nice simple one to finish with, then I just needed the /etc/auto_master entry:

/isos           auto_iso -nosuid

This or this can be added to another automount point, that we already have, like /share by adding this entry:

isos      / -fstype=autofs /usr/local/share/sh/auto_iso

The only problem with this is that there is no way for the system to automatically destroy the lofi devices when they are unmounted..

Update: The lofi devices can be removed using dtrace.

About

This is the old blog of Chris Gerhard. It has mostly moved to http://chrisgerhard.wordpress.com

Search

Archives
« April 2014
MonTueWedThuFriSatSun
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Today