disk_clean.sh revision 07a25aa6781390766777765ec1d9fb1c2c68c1c7
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# CDDL HEADER START
d39757aa11b7b514615ceb1ec0388a6d0521a202mlf# The contents of this file are subject to the terms of the
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# Common Development and Distribution License (the "License").
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# You may not use this file except in compliance with the License.
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# See the License for the specific language governing permissions
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# and limitations under the License.
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# When distributing Covered Code, include this CDDL HEADER in each
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# If applicable, add the following below this CDDL HEADER, with the
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# fields enclosed by brackets "[]" replaced with your own identifying
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# information: Portions Copyright [yyyy] [name of copyright owner]
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# CDDL HEADER END
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# Copyright 2010 Sun Microsystems, Inc. All rights reserved.
0f2c99a46e005b1add7df43157ee8516e585157ayt# Use is subject to license terms.
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# This is a clean script for removable disks
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# Following is the syntax for calling the script:
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# scriptname [-s|-f|-i|-I] devicename [-A|-D] username zonename zonepath
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# -s for standard cleanup by a user
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# -f for forced cleanup by an administrator
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# -i for boot-time initialization (when the system is booted with -r)
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# -I to suppress error/warning messages; the script is run in the '-i'
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# $1: devicename - device to be allocated/deallocated, e.g., sr0
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# $2: -A if cleanup is for allocation, or -D if cleanup is for deallocation.
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# $3: username - run the script as this user, rather than as the caller.
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# $4: zonename - zone in which device to be allocated/deallocated
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# $5: zonepath - root path of zonename
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# A clean script for a removable media device should prompt the user to
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# insert correctly labeled media at allocation time, and ensure that the
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# media is ejected at deallocation time.
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# Unless the clean script is being called for boot-time
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# initialization, it may communicate with the user via stdin and
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# stdout. To communicate with the user via CDE dialogs, create a
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# script or link with the same name, but with ".windowing" appended.
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# For example, if the clean script specified in device_allocate is
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# /etc/security/xyz_clean, that script must use stdin/stdout. If a
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# script named /etc/security/xyz_clean.windowing exists, it must use
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# dialogs. To present dialogs to the user, the dtksh script
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# This particular script, disk_clean, will work using stdin/stdout, or
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# using dialogs. A symbolic link disk_clean.windowing points to
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# disk_clean.
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# ####################################################
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# ################ Local Functions #################
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# ####################################################
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# Set up for windowing and non-windowing messages
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# Display a message for the user. For windowing, user must press OK button
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# to continue. For non-windowing, no response is required.
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf form=`gettext "Media in %s is ready. Please store safely."`
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# Ask the user an OK/Cancel question. Return 0 for OK, 1 for Cancel.
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# Ask the user an Yes/No question. Return 0 for Yes, 1 for No
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# Display an error message, put the device in the error state, and exit.
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf "\n\nDevice has been placed in allocation error state." \
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# get_reply prompt choice ...
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf while true
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# Find the first disk slice containing a file system
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf # The list of files in device_maps(4) is in an unspecified order.
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf # To speed up the fstyp(1M) scanning below in most cases, perform
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf # 1) Select only block device files of the form "/dev/dsk/*".
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf # 2) Sort the list of files in an order more likely to yield
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf # matches: first the fdisk(1M) partitions ("/dev/dsk/cNtNdNpN")
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf # then the format(1M) slices ("/dev/dsk/cNtNdNsN"), in ascending
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf /usr/bin/sed '/^\/dev\/dsk\//!d; s/\([sp]\)\([0-9]*\)$/ \1 \2/;' | \
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf if [ $? = 0 ]; then
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# Find all mountpoints in use for a set of device special files.
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# Usage: findmounts devpath ...
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf nawk -f - -v vold_root="$VOLD_ROOT" -v devs="$*" /etc/mnttab <<\
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# Allocate a device.
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# Ask the user to make sure the disk is properly labeled.
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# Ask if the disk should be mounted.
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf if [ $VOLUME_MEDIATYPE = floppy ]; then
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf if [ $? != 0 ]; then
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf if [ $? != 0 ]; then
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf if [ $? != 0 ]; then
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf if [ $VOLUME_MEDIATYPE = cdrom -o $VOLUME_MEDIATYPE = rmdisk ]; then
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf # Give ourself write permission on device file so file system gets
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf # Do the actual mount. VOLUME_* environment variables are inputs to
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf if [ $rmmount_status -eq 0 ]; then
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf elif [ $rmmount_status -gt 0 -a $VOLUME_MEDIATYPE != cdrom ]; then
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf # Try again in readonly mode. cdrom is always mounted ro, so
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf echo "Read-write mount of $DEVICE failed. Mounting read-only."
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf if [ $? -eq 0 ]; then
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf # Set permissions on directory used by vold, sdtvolcheck, etc.
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf if [ -d /tmp/.removable ]; then
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf if [ $VOLUME_MEDIATYPE = cdrom -o $VOLUME_MEDIATYPE = rmdisk ]; then
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf if [ -h /$VOLUME_MEDIATYPE/$DEVICE ]; then
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf if [ -h $name ]; then
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf error_exit $DEVICE "Error unmounting $DEVICE" "$rmmount_msg";;
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# Reclaim a device
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# ####################################################
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# ################ Begin main program ################
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# ####################################################
507c32411f3f101e90ca2120f042b5ee698ba1d5mlfUSAGE="Usage: disk_clean [-s|-f|-i|-I] devicename -[A|D] [username] [zonename] [zonepath]"
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# Parse the command line arguments
507c32411f3f101e90ca2120f042b5ee698ba1d5mlfwhile getopts ifsI c
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf#get the device_maps information
507c32411f3f101e90ca2120f042b5ee698ba1d5mlfFILES=`echo $MAP | cut -f4 -d:` # e.g., /dev/dsk/c0t6d0s0 /dev/dsk/c0t6d0s1 ...
507c32411f3f101e90ca2120f042b5ee698ba1d5mlfDEVFILE=`echo $FILES | cut -f1 -d" "` # e.g., "/dev/dsk/c0t6d0s0"
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf# Set VOLUME_ variables that are inputs to rmmount
507c32411f3f101e90ca2120f042b5ee698ba1d5mlfVOLUME_DEVICE=`echo $FILES | cut -f2 -d" "` # e.g., "/dev/dsk/c0t6d0s1"
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf # e.g., "cdrom" or "floppy"
507c32411f3f101e90ca2120f042b5ee698ba1d5mlfif [ "$MACH" = "i386" ] && [ "$MEDIATYPE" = "rmdisk" ]; then
507c32411f3f101e90ca2120f042b5ee698ba1d5mlfSYMDEV=`echo $DEVICE | sed -e 's/_//'` # e.g., "cdrom" or "floppy"
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf # If there's a directory for the device under /<mediatype>, get the
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf # user name from there, to use in cleaning up that directory. Otherwise,
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf # the user name isn't actually used in deallocation.
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf if [ -d ${VOLUME_ZONE_PATH}/${VOLUME_MEDIATYPE}/*-${DEVICE} ]; then
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf VOLUME_USER=`ls -ld ${VOLUME_ZONE_PATH}/${VOLUME_MEDIATYPE}/*-${DEVICE} | awk '/^d/{print $3}'`
507c32411f3f101e90ca2120f042b5ee698ba1d5mlf if [ -n "$3" ]; then
507c32411f3f101e90ca2120f042b5ee698ba1d5mlfexport VOLUME_ACTION VOLUME_DEVICE VOLUME_MEDIATYPE VOLUME_NAME VOLUME_PCFS_ID