1 #!/bin/ksh -p
   2 #
   3 # CDDL HEADER START
   4 #
   5 # The contents of this file are subject to the terms of the
   6 # Common Development and Distribution License (the "License").
   7 # You may not use this file except in compliance with the License.
   8 #
   9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10 # or http://www.opensolaris.org/os/licensing.
  11 # See the License for the specific language governing permissions
  12 # and limitations under the License.
  13 #
  14 # When distributing Covered Code, include this CDDL HEADER in each
  15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16 # If applicable, add the following below this CDDL HEADER, with the
  17 # fields enclosed by brackets "[]" replaced with your own identifying
  18 # information: Portions Copyright [yyyy] [name of copyright owner]
  19 #
  20 # CDDL HEADER END
  21 #
  22 #
  23 # Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  24 # Use is subject to license terms.
  25 #
  26 
  27 #
  28 # get script name (bname) and path (dname)
  29 #
  30 bname=`basename $0`
  31 
  32 #
  33 # common shell script functions
  34 #
  35 . /usr/lib/brand/ipkg/common.ksh
  36 . /usr/lib/brand/shared/uninstall.ksh
  37 
  38 #
  39 # options processing
  40 #
  41 zonename=$1
  42 if [ -z "$zonename" ]; then
  43         printf "$f_abort\n" >&2
  44         exit $ZONE_SUBPROC_FATAL
  45 fi
  46 zonepath=$2
  47 if [ -z "$zonepath" ]; then
  48         printf "$f_abort" >&2
  49         exit $ZONE_SUBPROC_FATAL
  50 fi
  51 shift 2
  52 
  53 options="FhHnv"
  54 options_repeat=""
  55 options_seen=""
  56 
  57 opt_F=""
  58 opt_n=""
  59 opt_v=""
  60 
  61 # check for bad or duplicate options
  62 OPTIND=1
  63 while getopts $options OPT ; do
  64 case $OPT in
  65         \? ) usage_err ;; # invalid argument
  66         : ) usage_err ;; # argument expected
  67         * )
  68                 opt=`echo $OPT | sed 's/-\+//'`
  69                 if [ -n "$options_repeat" ]; then
  70                         echo $options_repeat | grep $opt >/dev/null
  71                         [ $? = 0 ] && break
  72                 fi
  73                 ( echo $options_seen | grep $opt >/dev/null ) &&
  74                         usage_err
  75                 options_seen="${options_seen}${opt}"
  76                 ;;
  77 esac
  78 done
  79 
  80 # check for a help request
  81 OPTIND=1
  82 while getopts :$options OPT ; do
  83 case $OPT in
  84         h|H ) usage
  85 esac
  86 done
  87 
  88 # process options
  89 OPTIND=1
  90 while getopts :$options OPT ; do
  91 case $OPT in
  92         F ) opt_F="-F" ;;
  93         n ) opt_n="-n" ;;
  94         v ) opt_v="-v" ;;
  95 esac
  96 done
  97 shift `expr $OPTIND - 1`
  98 
  99 [ $# -gt 0 ]  && usage_err
 100 
 101 #
 102 # main
 103 #
 104 zoneroot=$zonepath/root
 105 
 106 nop=""
 107 if [[ -n "$opt_n" ]]; then
 108         nop="echo"
 109         #
 110         # in '-n' mode we should never return success (since we haven't
 111         # actually done anything). so override ZONE_SUBPROC_OK here.
 112         #
 113         ZONE_SUBPROC_OK=$ZONE_SUBPROC_FATAL
 114 fi
 115 
 116 #
 117 # We want uninstall to work in the face of various problems, such as a
 118 # zone with no delegated root dataset or multiple active datasets, so we
 119 # don't use the common functions.  Instead, we do our own work here and
 120 # are tolerant of errors.
 121 #
 122 
 123 # get_current_gzbe
 124 CURRENT_GZBE=`/sbin/beadm list -H | /bin/nawk -F\; '{
 125         # Field 3 is the BE status.  'N' is the active BE.
 126         if ($3 ~ "N")
 127                 # Field 2 is the BE UUID
 128                 print $2
 129         }'`
 130 
 131 if [ -z "$CURRENT_GZBE" ]; then
 132         print "$f_no_gzbe"
 133 fi
 134 
 135 uninstall_get_zonepath_ds
 136 uninstall_get_zonepath_root_ds
 137 
 138 # find all the zone BEs datasets associated with this global zone BE.
 139 unset fs_all
 140 (( fs_all_c = 0 ))
 141 if [ -n "$CURRENT_GZBE" ]; then
 142         /sbin/zfs list -H -t filesystem -o $PROP_PARENT,name \
 143             -r $ZONEPATH_RDS |
 144             while IFS=" " read uid fs; do
 145 
 146                 # only look at filesystems directly below $ZONEPATH_RDS
 147                 [[ "$fs" != ~()($ZONEPATH_RDS/+([^/])) ]] &&
 148                         continue
 149 
 150                 # match by PROP_PARENT uuid
 151                 [[ "$uid" != ${CURRENT_GZBE} ]] &&
 152                         continue
 153 
 154                 fs_all[$fs_all_c]=$fs
 155                 (( fs_all_c = $fs_all_c + 1 ))
 156         done
 157 fi
 158 
 159 destroy_zone_datasets
 160 
 161 exit $ZONE_SUBPROC_OK