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