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