xref: /titanic_53/usr/src/cmd/allocate/disk_clean.sh (revision f875b4ebb1dd9fdbeb043557cab38ab3bf7f6e01)
1*f875b4ebSrica#! /usr/bin/sh
2*f875b4ebSrica#
3*f875b4ebSrica# CDDL HEADER START
4*f875b4ebSrica#
5*f875b4ebSrica# The contents of this file are subject to the terms of the
6*f875b4ebSrica# Common Development and Distribution License (the "License").
7*f875b4ebSrica# You may not use this file except in compliance with the License.
8*f875b4ebSrica#
9*f875b4ebSrica# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*f875b4ebSrica# or http://www.opensolaris.org/os/licensing.
11*f875b4ebSrica# See the License for the specific language governing permissions
12*f875b4ebSrica# and limitations under the License.
13*f875b4ebSrica#
14*f875b4ebSrica# When distributing Covered Code, include this CDDL HEADER in each
15*f875b4ebSrica# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*f875b4ebSrica# If applicable, add the following below this CDDL HEADER, with the
17*f875b4ebSrica# fields enclosed by brackets "[]" replaced with your own identifying
18*f875b4ebSrica# information: Portions Copyright [yyyy] [name of copyright owner]
19*f875b4ebSrica#
20*f875b4ebSrica# CDDL HEADER END
21*f875b4ebSrica#
22*f875b4ebSrica# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23*f875b4ebSrica# Use is subject to license terms.
24*f875b4ebSrica#
25*f875b4ebSrica#
26*f875b4ebSrica# ident	"%Z%%M%	%I%	%E% SMI"
27*f875b4ebSrica#
28*f875b4ebSrica#
29*f875b4ebSrica# This is a clean script for removable disks
30*f875b4ebSrica#
31*f875b4ebSrica# Following is the syntax for calling the script:
32*f875b4ebSrica#	scriptname [-s|-f|-i|-I] devicename [-A|-D] username zonename zonepath
33*f875b4ebSrica#
34*f875b4ebSrica#    	-s for standard cleanup by a user
35*f875b4ebSrica# 	-f for forced cleanup by an administrator
36*f875b4ebSrica# 	-i for boot-time initialization (when the system is booted with -r)
37*f875b4ebSrica# 	-I to suppress error/warning messages; the script is run in the '-i'
38*f875b4ebSrica#	   mode
39*f875b4ebSrica#
40*f875b4ebSrica# $1:	devicename - device to be allocated/deallocated, e.g., sr0
41*f875b4ebSrica#
42*f875b4ebSrica# $2:	-A if cleanup is for allocation, or -D if cleanup is for deallocation.
43*f875b4ebSrica#
44*f875b4ebSrica# $3:	username - run the script as this user, rather than as the caller.
45*f875b4ebSrica#
46*f875b4ebSrica# $4:	zonename - zone in which device to be allocated/deallocated
47*f875b4ebSrica#
48*f875b4ebSrica# $5:	zonepath - root path of zonename
49*f875b4ebSrica#
50*f875b4ebSrica# A clean script for a removable media device should prompt the user to
51*f875b4ebSrica# insert correctly labeled media at allocation time, and ensure that the
52*f875b4ebSrica# media is ejected at deallocation time.
53*f875b4ebSrica#
54*f875b4ebSrica# Unless the clean script is being called for boot-time
55*f875b4ebSrica# initialization, it may communicate with the user via stdin and
56*f875b4ebSrica# stdout.  To communicate with the user via CDE dialogs, create a
57*f875b4ebSrica# script or link with the same name, but with ".windowing" appended.
58*f875b4ebSrica# For example, if the clean script specified in device_allocate is
59*f875b4ebSrica# /etc/security/xyz_clean, that script must use stdin/stdout.  If a
60*f875b4ebSrica# script named /etc/security/xyz_clean.windowing exists, it must use
61*f875b4ebSrica# dialogs.  To present dialogs to the user, the dtksh script
62*f875b4ebSrica# /etc/security/lib/wdwmsg may be used.
63*f875b4ebSrica#
64*f875b4ebSrica# This particular script, disk_clean, will work using stdin/stdout, or
65*f875b4ebSrica# using dialogs.  A symbolic link disk_clean.windowing points to
66*f875b4ebSrica# disk_clean.
67*f875b4ebSrica#
68*f875b4ebSrica
69*f875b4ebSrica# ####################################################
70*f875b4ebSrica# ################  Local Functions  #################
71*f875b4ebSrica# ####################################################
72*f875b4ebSrica
73*f875b4ebSrica#
74*f875b4ebSrica# Set up for windowing and non-windowing messages
75*f875b4ebSrica#
76*f875b4ebSricamsg_init()
77*f875b4ebSrica{
78*f875b4ebSrica    if [ `basename $0` != `basename $0 .windowing` ]; then
79*f875b4ebSrica	WINDOWING="yes"
80*f875b4ebSrica	case $VOLUME_MEDIATYPE in
81*f875b4ebSrica	  cdrom)   TITLE="CD-ROM";;
82*f875b4ebSrica	  rmdisk)  TITLE="Removable Disk";;
83*f875b4ebSrica	  floppy)  TITLE="Floppy";;
84*f875b4ebSrica	  *)       TITLE="Disk";;
85*f875b4ebSrica	esac
86*f875b4ebSrica
87*f875b4ebSrica	if [ "$MODE" = "allocate" ]; then
88*f875b4ebSrica	    TITLE="$TITLE Allocation"
89*f875b4ebSrica	else
90*f875b4ebSrica	    TITLE="$TITLE Deallocation"
91*f875b4ebSrica	fi
92*f875b4ebSrica    else
93*f875b4ebSrica	WINDOWING="no"
94*f875b4ebSrica    fi
95*f875b4ebSrica}
96*f875b4ebSrica
97*f875b4ebSrica#
98*f875b4ebSrica# Display a message for the user.  For windowing, user must press OK button
99*f875b4ebSrica# to continue. For non-windowing, no response is required.
100*f875b4ebSrica#
101*f875b4ebSricamsg() {
102*f875b4ebSrica    if [ "$WINDOWING" = "yes" ]; then
103*f875b4ebSrica	$WDWMSG "$*" "$TITLE" OK
104*f875b4ebSrica    elif [ "$silent" != "y" ]; then
105*f875b4ebSrica	echo "$*" > /dev/${MSGDEV}
106*f875b4ebSrica    fi
107*f875b4ebSrica}
108*f875b4ebSrica
109*f875b4ebSricaok_msg() {
110*f875b4ebSrica	if [ "$WINDOWING" = "yes" ]; then
111*f875b4ebSrica		$WDWMSG "$*" "$TITLE" READY
112*f875b4ebSrica	else
113*f875b4ebSrica		form=`gettext "Media in %s is ready. Please store safely."`
114*f875b4ebSrica		printf "${form}\n" $PROG $DEVICE > /dev/{MSGDEV}
115*f875b4ebSrica	fi
116*f875b4ebSrica}
117*f875b4ebSrica
118*f875b4ebSricaerror_msg() {
119*f875b4ebSrica	if [ "$WINDOWING" = "yes" ]; then
120*f875b4ebSrica		$WDWMSG "$*" "$TITLE" ERROR
121*f875b4ebSrica	else
122*f875b4ebSrica		form=`gettext "%s: Error cleaning up device %s."`
123*f875b4ebSrica		printf "${form}\n" $PROG $DEVICE > /dev/${MSGDEV}
124*f875b4ebSrica	fi
125*f875b4ebSrica}
126*f875b4ebSrica
127*f875b4ebSrica#
128*f875b4ebSrica# Ask the user an OK/Cancel question.  Return 0 for OK, 1 for Cancel.
129*f875b4ebSrica#
130*f875b4ebSricaokcancel() {
131*f875b4ebSrica    if [ "$WINDOWING" = "yes" ]; then
132*f875b4ebSrica	$WDWMSG "$*" "$TITLE" OK Cancel
133*f875b4ebSrica    elif [ "$silent" != "y" ]; then
134*f875b4ebSrica	get_reply "$* (y to continue, n to cancel) \c" y n
135*f875b4ebSrica    fi
136*f875b4ebSrica}
137*f875b4ebSrica
138*f875b4ebSrica#
139*f875b4ebSrica# Ask the user an Yes/No question.  Return 0 for Yes, 1 for No
140*f875b4ebSrica#
141*f875b4ebSricayesno() {
142*f875b4ebSrica    if [ "$WINDOWING" = "yes" ]; then
143*f875b4ebSrica	$WDWMSG "$*" "$TITLE" Yes No
144*f875b4ebSrica    elif [ "$silent" != "y" ]; then
145*f875b4ebSrica	get_reply "$* (y/n) \c" y n
146*f875b4ebSrica    fi
147*f875b4ebSrica}
148*f875b4ebSrica
149*f875b4ebSrica#
150*f875b4ebSrica# Display an error message, put the device in the error state, and exit.
151*f875b4ebSrica#
152*f875b4ebSricaerror_exit() {
153*f875b4ebSrica	if [ "$silent" != "y" ]; then
154*f875b4ebSrica		msg "$2" "$3" \
155*f875b4ebSrica		    "\n\nDevice has been placed in allocation error state." \
156*f875b4ebSrica		    "\nPlease inform system administrator."
157*f875b4ebSrica	fi
158*f875b4ebSrica	exit 1
159*f875b4ebSrica}
160*f875b4ebSrica
161*f875b4ebSrica#
162*f875b4ebSrica# get_reply prompt choice ...
163*f875b4ebSrica#
164*f875b4ebSricaget_reply() {
165*f875b4ebSrica	prompt=$1; shift
166*f875b4ebSrica	while true
167*f875b4ebSrica	do
168*f875b4ebSrica		echo $prompt > /dev/tty
169*f875b4ebSrica		read reply
170*f875b4ebSrica		i=0
171*f875b4ebSrica		for choice in $*
172*f875b4ebSrica		do
173*f875b4ebSrica			if [ "$choice" = "$reply" ]
174*f875b4ebSrica			then
175*f875b4ebSrica				return $i
176*f875b4ebSrica			else
177*f875b4ebSrica				i=`expr $i + 1`
178*f875b4ebSrica			fi
179*f875b4ebSrica		done
180*f875b4ebSrica	done
181*f875b4ebSrica}
182*f875b4ebSrica
183*f875b4ebSrica#
184*f875b4ebSrica# Find the first disk slice containing a HSFS file system
185*f875b4ebSrica#
186*f875b4ebSricafind_fs()
187*f875b4ebSrica{
188*f875b4ebSrica	for DEVn in $FILES ; do
189*f875b4ebSrica		x="`labelit -F hsfs $DEVn 2>&1| grep 'Volume id'`"
190*f875b4ebSrica		if [ $? = 0 ]; then
191*f875b4ebSrica			FSPATH=$DEVn
192*f875b4ebSrica			if [ "$x" != "" ]; then
193*f875b4ebSrica				y="`echo $x|cut -f3- -d' '|\
194*f875b4ebSrica				    /usr/xpg4/bin/tr '[:upper:] ' '[:lower:]_'`"
195*f875b4ebSrica				FSNAME=$y
196*f875b4ebSrica			fi
197*f875b4ebSrica			return
198*f875b4ebSrica		fi
199*f875b4ebSrica	done
200*f875b4ebSrica}
201*f875b4ebSrica
202*f875b4ebSrica#
203*f875b4ebSrica# Find all mountpoints in use for a set of device special files.
204*f875b4ebSrica# Usage: findmounts devpath ...
205*f875b4ebSrica#
206*f875b4ebSrica
207*f875b4ebSricafindmounts() {
208*f875b4ebSrica	nawk -f - -v vold_root="$VOLD_ROOT" -v devs="$*" /etc/mnttab <<\
209*f875b4ebSrica	    "ENDOFAWKPGM"
210*f875b4ebSrica	BEGIN {
211*f875b4ebSrica		split(devs, devlist, " ");
212*f875b4ebSrica		for (devN in devlist) {
213*f875b4ebSrica			dev = devlist[devN];
214*f875b4ebSrica			realdevlist[dev] = 1;
215*f875b4ebSrica			sub(/.*\//, "", dev);
216*f875b4ebSrica			sub(/s[0-9]$/, "", dev);
217*f875b4ebSrica			if (vold_root != "") {
218*f875b4ebSrica				vold_dir[vold_root "/dev/dsk/" dev] = 1;
219*f875b4ebSrica				vold_dir[vold_root "/dev/rdsk/" dev] = 1;
220*f875b4ebSrica			}
221*f875b4ebSrica		}
222*f875b4ebSrica	}
223*f875b4ebSrica
224*f875b4ebSrica	{
225*f875b4ebSrica		for (dev in realdevlist) {
226*f875b4ebSrica			if ($1 == dev) {
227*f875b4ebSrica				mountpoint = $2;
228*f875b4ebSrica				print mountpoint;
229*f875b4ebSrica			}
230*f875b4ebSrica		}
231*f875b4ebSrica		for (dev in vold_dir) {
232*f875b4ebSrica			if (substr($1, 1, length(dev)) == dev) {
233*f875b4ebSrica				mountpoint = $2;
234*f875b4ebSrica				print mountpoint;
235*f875b4ebSrica			}
236*f875b4ebSrica		}
237*f875b4ebSrica	}
238*f875b4ebSricaENDOFAWKPGM
239*f875b4ebSrica}
240*f875b4ebSrica
241*f875b4ebSrica#
242*f875b4ebSrica# Allocate a device.
243*f875b4ebSrica# Ask the user to make sure the disk is properly labeled.
244*f875b4ebSrica# Ask if the disk should be mounted.
245*f875b4ebSrica#
246*f875b4ebSricado_allocate()
247*f875b4ebSrica{
248*f875b4ebSrica	if [ $VOLUME_MEDIATYPE = floppy ]; then
249*f875b4ebSrica		# Determine if media is in drive
250*f875b4ebSrica		eject_msg="`eject -q $DEVFILE 2>&1`"
251*f875b4ebSrica		eject_status="$?"
252*f875b4ebSrica		case $eject_status in
253*f875b4ebSrica		1) # Media is not in drive
254*f875b4ebSrica			okcancel "Insert disk in $DEVICE."
255*f875b4ebSrica			if [ $? != 0 ]; then
256*f875b4ebSrica				exit 0
257*f875b4ebSrica			fi;;
258*f875b4ebSrica		3) # Error
259*f875b4ebSrica			error_exit $DEVICE \
260*f875b4ebSrica			    "Error checking for media in drive.";;
261*f875b4ebSrica		esac
262*f875b4ebSrica	else
263*f875b4ebSrica		okcancel "Insert disk in $DEVICE."
264*f875b4ebSrica		if [ $? != 0 ]; then
265*f875b4ebSrica			exit 0
266*f875b4ebSrica		fi
267*f875b4ebSrica	fi
268*f875b4ebSrica
269*f875b4ebSrica	yesno "Do you want $DEVICE mounted?"
270*f875b4ebSrica	if [ $? != 0 ]; then
271*f875b4ebSrica		exit 0
272*f875b4ebSrica	fi
273*f875b4ebSrica
274*f875b4ebSrica	if [ $VOLUME_MEDIATYPE = cdrom -o $VOLUME_MEDIATYPE = rmdisk ]; then
275*f875b4ebSrica		# Get the device path and volume name of a partition
276*f875b4ebSrica		find_fs
277*f875b4ebSrica		if [ "$FSPATH" != "" ]; then
278*f875b4ebSrica			VOLUME_PATH=$FSPATH
279*f875b4ebSrica		fi
280*f875b4ebSrica		if [ "$FSNAME" != "" ]; then
281*f875b4ebSrica			VOLUME_NAME=$FSNAME
282*f875b4ebSrica		fi
283*f875b4ebSrica	fi
284*f875b4ebSrica	VOLUME_ACTION=insert
285*f875b4ebSrica
286*f875b4ebSrica	# Give ourself write permission on device file so file system gets
287*f875b4ebSrica	# mounted read/write if possible.
288*f875b4ebSrica	# rmmount only cares about permissions not user...
289*f875b4ebSrica	chown $VOLUME_USER $VOLUME_PATH
290*f875b4ebSrica	chmod 700 $VOLUME_PATH
291*f875b4ebSrica
292*f875b4ebSrica	# Do the actual mount.  VOLUME_* environment variables are inputs to
293*f875b4ebSrica	# rmmount.
294*f875b4ebSrica	rmmount_msg="`/usr/sbin/rmmount 2>&1`"
295*f875b4ebSrica	rmmount_status="$?"
296*f875b4ebSrica	if [ $rmmount_status -eq 0 ]; then
297*f875b4ebSrica		EXIT_STATUS=$CLEAN_MOUNT
298*f875b4ebSrica	elif [ $rmmount_status -gt 0 -a $VOLUME_MEDIATYPE != cdrom ]; then
299*f875b4ebSrica		# Try again in readonly mode. cdrom is always mounted ro, so
300*f875b4ebSrica		# no need to try again.
301*f875b4ebSrica		echo "Read-write mount of $DEVICE failed. Mounting read-only."
302*f875b4ebSrica		VOLUME_ACTION=remount; export VOLUME_ACTION
303*f875b4ebSrica		VOLUME_MOUNT_MODE=ro; export VOLUME_MOUNT_MODE
304*f875b4ebSrica		`/usr/sbin/rmmount`
305*f875b4ebSrica		if [ $? -eq 0 ]; then
306*f875b4ebSrica			EXIT_STATUS=$CLEAN_MOUNT
307*f875b4ebSrica		fi
308*f875b4ebSrica	fi
309*f875b4ebSrica
310*f875b4ebSrica	# Set permissions on directory used by vold, sdtvolcheck, etc.
311*f875b4ebSrica	if [ -d /tmp/.removable ]; then
312*f875b4ebSrica		chown root /tmp/.removable
313*f875b4ebSrica		chmod 777 /tmp/.removable
314*f875b4ebSrica	fi
315*f875b4ebSrica}
316*f875b4ebSrica
317*f875b4ebSrica
318*f875b4ebSricado_deallocate()
319*f875b4ebSrica{
320*f875b4ebSrica	if [ $VOLUME_MEDIATYPE = cdrom -o $VOLUME_MEDIATYPE = rmdisk ]; then
321*f875b4ebSrica		# Get the device path and volume name of a partition
322*f875b4ebSrica		find_fs
323*f875b4ebSrica		if [ "$FSPATH" != "" ]; then
324*f875b4ebSrica			VOLUME_PATH=$FSPATH
325*f875b4ebSrica		fi
326*f875b4ebSrica		if [ "$FSNAME" != "" ]; then
327*f875b4ebSrica			VOLUME_NAME=$FSNAME
328*f875b4ebSrica		fi
329*f875b4ebSrica	fi
330*f875b4ebSrica	VOLUME_ACTION=eject
331*f875b4ebSrica
332*f875b4ebSrica	# Do the actual unmount.  VOLUME_* environment variables are inputs to
333*f875b4ebSrica	# rmmount.
334*f875b4ebSrica	rmmount_msg="`/usr/sbin/rmmount 2>&1`"
335*f875b4ebSrica	rmmount_status="$?"
336*f875b4ebSrica
337*f875b4ebSrica	# Remove symbolic links to mount point
338*f875b4ebSrica	for name in $VOLUME_ZONE_PATH/$VOLUME_MEDIATYPE/*; do
339*f875b4ebSrica		if [ -h $name ]; then
340*f875b4ebSrica			target=`ls -l $name | awk '{ print $NF; }'`
341*f875b4ebSrica			target_dir=`dirname $target`
342*f875b4ebSrica			target_device=`echo $target_dir | \
343*f875b4ebSrica			    sed -e 's/^.*-\(.*\)$/\1/'`
344*f875b4ebSrica			if [ "$target_device" = "$DEVICE" ]; then
345*f875b4ebSrica				rm -f $name
346*f875b4ebSrica			fi
347*f875b4ebSrica		fi
348*f875b4ebSrica	done
349*f875b4ebSrica
350*f875b4ebSrica	case $rmmount_status in
351*f875b4ebSrica	1) # still mounted
352*f875b4ebSrica		error_exit $DEVICE "Error unmounting $DEVICE" "$rmmount_msg";;
353*f875b4ebSrica	0) # not mounted
354*f875b4ebSrica		# Eject the media
355*f875b4ebSrica		if [ "$FLAG" = "f" ] ; then
356*f875b4ebSrica			eject_msg="`eject -f $DEVFILE 2>&1`"
357*f875b4ebSrica		else
358*f875b4ebSrica			eject_msg="`eject $DEVFILE 2>&1`"
359*f875b4ebSrica		fi
360*f875b4ebSrica		eject_status="$?"
361*f875b4ebSrica		case $eject_status in
362*f875b4ebSrica		0|4) # Media has been ejected
363*f875b4ebSrica			case $VOLUME_MEDIATYPE in
364*f875b4ebSrica			floppy|cdrom|rmdisk)
365*f875b4ebSrica				msg "Please remove the disk from $DEVICE.";;
366*f875b4ebSrica			esac;;
367*f875b4ebSrica		1|3) # Media didn't eject
368*f875b4ebSrica			EXIT_STATUS=2
369*f875b4ebSrica			msg $DEVICE "Error ejecting disk from $DEVICE" \
370*f875b4ebSrica			"$eject_msg";;
371*f875b4ebSrica		esac
372*f875b4ebSrica	esac
373*f875b4ebSrica}
374*f875b4ebSrica
375*f875b4ebSrica#
376*f875b4ebSrica# Reclaim a device
377*f875b4ebSrica#
378*f875b4ebSricado_init()
379*f875b4ebSrica{
380*f875b4ebSrica	eject_msg="`eject -f $DEVFILE 2>&1`"
381*f875b4ebSrica	eject_status="$?"
382*f875b4ebSrica
383*f875b4ebSrica	case $eject_status in
384*f875b4ebSrica	0) # Media has been ejected
385*f875b4ebSrica		if [ "$silent" != "y" ]; then
386*f875b4ebSrica			ok_msg
387*f875b4ebSrica		fi
388*f875b4ebSrica		exit 0;;
389*f875b4ebSrica	1) # Media not ejected
390*f875b4ebSrica		if [ "$silent" != "y" ]; then
391*f875b4ebSrica			error_msg
392*f875b4ebSrica		fi
393*f875b4ebSrica		exit 0;;
394*f875b4ebSrica	3) # Error
395*f875b4ebSrica		if [ "$silent" != "y" ]; then
396*f875b4ebSrica			error_msg
397*f875b4ebSrica		fi
398*f875b4ebSrica		msg $DEVICE "Error ejecting disk from $DEVICE" \
399*f875b4ebSrica		    "$eject_msg"
400*f875b4ebSrica		exit 2;;
401*f875b4ebSrica	esac
402*f875b4ebSrica}
403*f875b4ebSrica
404*f875b4ebSrica
405*f875b4ebSrica# ####################################################
406*f875b4ebSrica# ################ Begin main program ################
407*f875b4ebSrica# ####################################################
408*f875b4ebSrica
409*f875b4ebSricatrap "" INT TERM QUIT TSTP ABRT
410*f875b4ebSrica
411*f875b4ebSricaPATH="/usr/bin:/usr/sbin"
412*f875b4ebSricaMODE="allocate"
413*f875b4ebSricaSILENT=n
414*f875b4ebSricaWDWMSG="/etc/security/lib/wdwmsg"
415*f875b4ebSricaVOLUME_ZONE_PATH="/"
416*f875b4ebSricaUSAGE="Usage: disk_clean [-s|-f|-i|-I] devicename -[A|D] [username] [zonename] [zonepath]"
417*f875b4ebSricaEXIT_STATUS=0
418*f875b4ebSricaCLEAN_MOUNT=4
419*f875b4ebSricaMACH=`uname -p`
420*f875b4ebSricaFLAG=i
421*f875b4ebSrica#
422*f875b4ebSrica# Parse the command line arguments
423*f875b4ebSrica#
424*f875b4ebSricawhile getopts ifsI c
425*f875b4ebSricado
426*f875b4ebSrica	case $c in
427*f875b4ebSrica	i)
428*f875b4ebSrica		FLAG=$c;;
429*f875b4ebSrica	f)
430*f875b4ebSrica		FLAG=$c;;
431*f875b4ebSrica	s)
432*f875b4ebSrica		FLAG=$c;;
433*f875b4ebSrica	I)
434*f875b4ebSrica		FLAG=i
435*f875b4ebSrica		silent=y;;
436*f875b4ebSrica	\?)
437*f875b4ebSrica		echo $USAGE
438*f875b4ebSrica		exit 1;;
439*f875b4ebSrica      esac
440*f875b4ebSricadone
441*f875b4ebSrica
442*f875b4ebSricashift `expr $OPTIND - 1`
443*f875b4ebSrica
444*f875b4ebSricaDEVICE=$1
445*f875b4ebSricaMODE="deallocate"
446*f875b4ebSricaif [ "$2" = "-A" ]; then
447*f875b4ebSrica	MODE="allocate"
448*f875b4ebSricaelif [ "$2" = "-D" ]; then
449*f875b4ebSrica	MODE="deallocate"
450*f875b4ebSricafi
451*f875b4ebSrica
452*f875b4ebSrica#get the device_maps information
453*f875b4ebSricaMAP=`/usr/sbin/list_devices -s -l $DEVICE`
454*f875b4ebSricaFILES=`echo $MAP | cut -f4 -d:`	# e.g., /dev/dsk/c0t6d0s0 /dev/dsk/c0t6d0s1 ...
455*f875b4ebSricaDEVFILE=`echo $FILES | cut -f1 -d" "` 		# e.g., "/dev/dsk/c0t6d0s0"
456*f875b4ebSrica
457*f875b4ebSrica# Set VOLUME_ variables that are inputs to rmmount
458*f875b4ebSrica
459*f875b4ebSricaVOLUME_DEVICE=`echo $FILES | cut -f2 -d" "` 	# e.g., "/dev/dsk/c0t6d0s1"
460*f875b4ebSricaMEDIATYPE=`echo $MAP | cut -f3 -d: | cut -f2 -d" "`
461*f875b4ebSrica					 	# e.g., "cdrom" or "floppy"
462*f875b4ebSricaif [ "$MEDIATYPE" = "sr" ]; then
463*f875b4ebSrica	VOLUME_MEDIATYPE="cdrom"
464*f875b4ebSricaelif [ "$MEDIATYPE" = "fd" ]; then
465*f875b4ebSrica	VOLUME_MEDIATYPE="floppy"
466*f875b4ebSricaelif [ "$MEDIATYPE" = "rmdisk" ]; then
467*f875b4ebSrica	VOLUME_MEDIATYPE="rmdisk"
468*f875b4ebSricafi
469*f875b4ebSrica
470*f875b4ebSricaVOLUME_PATH=$DEVFILE				# e.g., "/dev/dsk/c0t6d0s0"
471*f875b4ebSricaif [ "$MACH" = "i386" ] && [ "$MEDIATYPE" = "rmdisk" ]; then
472*f875b4ebSrica	VOLUME_PATH=`echo $DEVFILE | sed -e 's/s0/p0/'`
473*f875b4ebSricafi
474*f875b4ebSrica
475*f875b4ebSricaSYMDEV=`echo $DEVICE | sed -e 's/_//'`		# e.g., "cdrom" or "floppy"
476*f875b4ebSricaSYMNUM=`echo $SYMDEV | sed -e 's/[a-z]*//g'`
477*f875b4ebSricaSYMDEV=`echo $SYMDEV | sed -e 's/[0-9]*//g'`
478*f875b4ebSricaif [ "$SYMDEV" = "sr" ]; then
479*f875b4ebSrica	VOLUME_SYMDEV="cdrom"$SYMNUM
480*f875b4ebSricaelif [ "$SYMDEV" = "fd" ]; then
481*f875b4ebSrica	VOLUME_SYMDEV="floppy"$SYMNUM
482*f875b4ebSricaelif [ "$SYMDEV" = "rmdisk" ]; then
483*f875b4ebSrica	VOLUME_SYMDEV="rmdisk"$SYMNUM
484*f875b4ebSricaelse
485*f875b4ebSrica	VOLUME_SYMDEV=$SYMDEV$SYMNUM
486*f875b4ebSricafi
487*f875b4ebSrica
488*f875b4ebSricaVOLUME_ZONE_NAME=$4
489*f875b4ebSrica
490*f875b4ebSricaVOLUME_ZONE_PATH=$5
491*f875b4ebSrica
492*f875b4ebSricaif [ "$MODE" = "allocate" ]; then
493*f875b4ebSrica	if [ -n "$3" ]; then			# e.g., "joeuser"
494*f875b4ebSrica		VOLUME_USER=$3
495*f875b4ebSrica	else
496*f875b4ebSrica		VOLUME_USER=`/usr/xpg4/bin/id -u -nr`
497*f875b4ebSrica	fi
498*f875b4ebSricaelse
499*f875b4ebSrica	# If there's a directory for the device under /<mediatype>, get the
500*f875b4ebSrica	# user name from there, to use in cleaning up that directory. Otherwise,
501*f875b4ebSrica	# the user name isn't actually used in deallocation.
502*f875b4ebSrica	if [ -d ${VOLUME_ZONE_PATH}/${VOLUME_MEDIATYPE}/*-${DEVICE} ]; then
503*f875b4ebSrica		VOLUME_USER=`ls -ld ${VOLUME_ZONE_PATH}/${VOLUME_MEDIATYPE}/*-${DEVICE} | awk '/^d/{print $3}'`
504*f875b4ebSrica	else
505*f875b4ebSrica		if [ -n "$3" ]; then
506*f875b4ebSrica			VOLUME_USER=$3
507*f875b4ebSrica		else
508*f875b4ebSrica			VOLUME_USER=`/usr/xpg4/bin/id -u -nr`
509*f875b4ebSrica		fi
510*f875b4ebSrica	fi
511*f875b4ebSricafi
512*f875b4ebSrica
513*f875b4ebSricaVOLUME_NAME=unnamed_${VOLUME_MEDIATYPE}
514*f875b4ebSrica					# e.g., "joeuser-cdrom0/unnamed_cdrom"
515*f875b4ebSrica
516*f875b4ebSricaif [ "$VOLUME_MEDIATYPE" = "rmdisk" ]; then
517*f875b4ebSrica	VOLUME_PCFS_ID=c
518*f875b4ebSricaelse
519*f875b4ebSrica	VOLUME_PCFS_ID=
520*f875b4ebSricafi
521*f875b4ebSrica
522*f875b4ebSricaexport VOLUME_ACTION VOLUME_DEVICE VOLUME_MEDIATYPE VOLUME_NAME VOLUME_PCFS_ID
523*f875b4ebSricaexport VOLUME_PATH VOLUME_SYMDEV VOLUME_USER VOLUME_ZONE_NAME VOLUME_ZONE_PATH
524*f875b4ebSrica
525*f875b4ebSricaUSERDIR=${VOLUME_USER}-${DEVICE}	# e.g., "joeusr-cdrom0"
526*f875b4ebSrica
527*f875b4ebSricamsg_init
528*f875b4ebSrica
529*f875b4ebSricaif [ "$MODE" = "allocate" ]; then
530*f875b4ebSrica	MSGDEV=tty
531*f875b4ebSrica  	do_allocate
532*f875b4ebSricaelse
533*f875b4ebSrica    if [ "$FLAG" = "i" ] ; then
534*f875b4ebSrica	MSGDEV=console
535*f875b4ebSrica	do_init
536*f875b4ebSrica    else
537*f875b4ebSrica	MSGDEV=tty
538*f875b4ebSrica	do_deallocate
539*f875b4ebSrica    fi
540*f875b4ebSricafi
541*f875b4ebSrica
542*f875b4ebSricaexit $EXIT_STATUS
543