1 #
   2 # CDDL HEADER START
   3 #
   4 # The contents of this file are subject to the terms of the
   5 # Common Development and Distribution License (the "License").
   6 # You may not use this file except in compliance with the License.
   7 #
   8 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9 # or http://www.opensolaris.org/os/licensing.
  10 # See the License for the specific language governing permissions
  11 # and limitations under the License.
  12 #
  13 # When distributing Covered Code, include this CDDL HEADER in each
  14 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15 # If applicable, add the following below this CDDL HEADER, with the
  16 # fields enclosed by brackets "[]" replaced with your own identifying
  17 # information: Portions Copyright [yyyy] [name of copyright owner]
  18 #
  19 # CDDL HEADER END
  20 #
  21 
  22 #
  23 # Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  24 # Use is subject to license terms.
  25 #
  26 # ident "@(#)zones_common.ksh   1.5     09/08/23 SMI"
  27 #
  28 
  29 #
  30 # Common functions to create and work with zones.
  31 #
  32 
  33 unset GZONEINTERFACE
  34 unset GZONEDEVICE
  35 unset LZONEINTERFACE
  36 ZONECFGFILE=$SHR_TMPDIR/share_tests_zonecfg.file
  37 ZONELOGFILE=$SHR_TMPDIR/share_tests_zonecfg.log
  38 ZONETMPFILE=$SHR_TMPDIR/share_tests_zone.tmp
  39 
  40 #
  41 # NAME
  42 #       zone_boot
  43 #
  44 # DESCRIPTION
  45 #       Boot the zone and monitor the zone to make sure all smf 
  46 #       functions have started.  Need to do the check twice to 
  47 #       make sure that if the system reboots do to init state 
  48 #       changes from the first boot after install, the system 
  49 #       handles this properly.
  50 #
  51 function zone_boot {
  52         typeset zb_zone_name=$1
  53 
  54         $ZONEADM -z $zb_zone_name boot > $ZONELOGFILE 2>&1
  55         if (( $? != 0 )); then
  56                 cti_reportfile $ZONELOGFILE "zone<$zb_zone_name> boot failed"
  57                 return 1
  58         fi
  59 
  60         typeset -i zb_zone_booted=0
  61         typeset -i zb_zone_sendm=0
  62         while (( $zb_zone_booted < 120 )); do
  63                 #
  64                 # Give the zone a second to get some work done and then
  65                 # check.
  66                 #
  67                 sleep 5
  68                 #
  69                 # Disable sendmail (but only do it once) to save some
  70                 # time on the zone boot.  This can be removed and
  71                 # use of the exclude package when 4963323 rfe is
  72                 # applied.
  73                 #
  74                 if (( $zb_zone_sendm == 0 )); then
  75                         $ZLOGIN $zb_zone_name /usr/sbin/svcadm \
  76                             disable sendmail > /dev/null 2>&1
  77                         (( $? == 0 )) && zb_zone_sendm=1
  78                 fi
  79                 zlogin $zb_zone_name /bin/svcs multi-user > $ZONETMPFILE 2>&1
  80                 grep online $ZONETMPFILE > /dev/null 2>&1
  81                 if (( $? == 0 )); then
  82                         #
  83                         # Sleep for a bit to make sure we are not in the
  84                         # middle of a reboot, before attempting to get the
  85                         # second status.
  86                         #
  87                         sleep 5
  88                         return 0
  89                 else
  90                         zb_zone_booted=$((zb_zone_booted + 1))
  91                 fi
  92         done
  93 
  94         cti_reportfile $ZONETMPFILE \
  95                 "timed out<10 minutes>: multi-user has not been online"
  96         return 1
  97 }
  98 
  99 #
 100 # NAME
 101 #       zones_min_setup
 102 #
 103 # DESCRIPTION
 104 #       Create a file system with minimal setup.  No additional file systems
 105 #       or networks configured.
 106 #
 107 function zones_min_setup {
 108         typeset zms_zone_name=$1
 109         typeset zms_zone_path=$2
 110 
 111         if [[ $TET_ZONE_LEAVE == True ]]; then
 112                 $ZONEADM -z $zms_zone_name list > /dev/null 2>&1
 113                 if (( $? == 0 )); then
 114                         zone_boot $zms_zone_name
 115                         return $?
 116                 fi
 117         fi
 118 
 119         tet_infoline "Setting up a minimal zone to test sharemgr"
 120 
 121         # set perms on the paths
 122         tet_infoline " - $CHMOD 700 $zms_zone_path"
 123         $CHMOD 700 $zms_zone_path > $ZONELOGFILE 2>&1
 124         if (( $? != 0 )); then
 125                 cti_reportfile $ZONELOGFILE "chmod $zms_zone_path failed"
 126                 return 1
 127         fi
 128         typeset ppath=$(dirname $zms_zone_path)
 129         tet_infoline " - $CHMOD 755 $ppath"
 130         $CHMOD 755 $ppath > $ZONELOGFILE 2>&1
 131         if (( $? != 0 )); then
 132                 cti_reportfile $ZONELOGFILE "chmod $ppath failed"
 133                 return 1
 134         fi
 135 
 136         # Create the config file
 137         $CAT << __EOF__ > $ZONECFGFILE
 138 create
 139 set zonepath=$zms_zone_path
 140 set autoboot=false
 141 commit
 142 __EOF__
 143 
 144         tet_infoline " Creating zone using following config file:"
 145         infofile "  " $ZONECFGFILE
 146         # Call zonecfg and check if the zone is configured as expected
 147         tet_infoline " - $ZONECFG -z $zms_zone_name -f $ZONECFGFILE"
 148         $ZONECFG -z $zms_zone_name -f $ZONECFGFILE > $ZONELOGFILE 2>&1
 149         if (( $? != 0 )); then
 150                 cti_reportfile $ZONELOGFILE "zonecfg $zms_zone_name failed"
 151                 return 1
 152         fi
 153         $ZONECFG -z $zms_zone_name verify >> $ZONELOGFILE 2>&1
 154         if (( $? != 0 )); then
 155                 cti_reportfile $ZONELOGFILE \
 156                         "wrong in the configuration of zone $zms_zone_name"
 157                 return 1
 158         fi
 159         $ZONEADM -z $zms_zone_name list -p >> $ZONELOGFILE 2>&1
 160         typeset cur_stat=$(tail -1 $ZONELOGFILE | awk -F: '{print $3}')
 161         if (( $? != 0 )) || [[ $cur_stat != configured ]]; then
 162                 cti_reportfile $ZONELOGFILE \
 163                         "wrong state: expect <configured> but got <$cur_stat>"
 164                 return 1
 165         fi
 166 
 167         # Call zone install and check if the zone is installed as expected
 168         tet_infoline " - $ZONEADM -z $zms_zone_name install"
 169         $ZONEADM -z $zms_zone_name install > $ZONELOGFILE 2>&1 &
 170         typeset -i zi_ret=$? zi_pid=$!
 171         pgrep zoneadm | grep -w $zi_pid > /dev/null 2>&1
 172         if (( $? != 0 || $zi_ret != 0 )); then
 173                 cti_reportfile $ZONELOGFILE "install zone $zms_zone_name failed"
 174                 return 1
 175         fi
 176         wait_now 3600 "! pgrep zoneadm | grep -w $zi_pid > /dev/null 2>&1" 60
 177         if (( $? != 0 )); then
 178                 cti_reportfile $ZONELOGFILE \
 179                     "timed out<1 hour>: install zone<$zms_zone_name> failed"
 180                 tet_infoline "please have a check and do cleanup manually"
 181                 kill -9 $zi_pid
 182                 return 1
 183         fi
 184         $ZONEADM -z $zms_zone_name list -p >> $ZONELOGFILE 2>&1
 185         cur_stat=$(tail -1 $ZONELOGFILE | awk -F: '{print $3}')
 186         if (( $? != 0 )) || [[ $cur_stat != installed ]]; then
 187                 cti_reportfile $ZONELOGFILE \
 188                         "wrong state: expect <installed> but got <$cur_stat>"
 189                 return 1
 190         fi
 191 
 192         # Call zone ready and check if the zone is ready as expected
 193         # this step is must for opensolaris
 194         tet_infoline " - $ZONEADM -z $zms_zone_name ready"
 195         $ZONEADM -z $zms_zone_name ready > $ZONELOGFILE 2>&1
 196         if (( $? != 0 )); then
 197                 cti_reportfile $ZONELOGFILE "ready zone $zms_zone_name failed"
 198                 return 1
 199         fi
 200         $ZONEADM -z $zms_zone_name list -p >> $ZONELOGFILE 2>&1
 201         cur_stat=$(tail -1 $ZONELOGFILE | awk -F: '{print $3}')
 202         if (( $? != 0 )) || [[ $cur_stat != ready ]]; then
 203                 cti_reportfile $ZONELOGFILE \
 204                         "wrong state: expect <ready> but got <$cur_stat>"
 205                 return 1
 206         fi
 207 
 208         # Setup the sysidcfg
 209         zms_sysidcfg="$zms_zone_path/root/etc/sysidcfg"
 210         zms_password=`$GREP "root:" /etc/shadow | $AWK -F':' '{print $2}'`
 211         echo "system_locale=C" > $zms_sysidcfg 2>$ZONELOGFILE
 212         if (( $? != 0 )); then
 213                 cti_reportfile $ZONELOGFILE "create file $zms_sysidcfg failed"
 214                 return 1
 215         fi
 216         echo "terminal=xterm" >> $zms_sysidcfg
 217         echo "network_interface=none {" >> $zms_sysidcfg
 218         echo "hostname=$zms_zone_name" >> $zms_sysidcfg
 219         echo "}" >> $zms_sysidcfg
 220         echo "name_service=NONE" >> $zms_sysidcfg
 221         # here set fixed root password to sysidcfg for now until bug 6863728
 222         # is fixed
 223         echo "root_password=wqm70K1OlX8Xk" >> $zms_sysidcfg
 224         echo "security_policy=NONE" >> $zms_sysidcfg
 225         echo "timezone=$TZ" >> $zms_sysidcfg
 226         echo "nfs4_domain=dynamic" >> $zms_sysidcfg
 227 
 228         # Call zone boot and check if the zone is running
 229         tet_infoline " - $ZONEADM -z $zms_zone_name boot"
 230         zone_boot $zms_zone_name
 231         if (( $? != 0 )); then
 232                 tet_infoline "boot zone<$zms_zone_name> failed"
 233                 return 1
 234         fi
 235         $ZONEADM -z $zms_zone_name list -p >> $ZONELOGFILE 2>&1
 236         cur_stat=$(tail -1 $ZONELOGFILE | awk -F: '{print $3}')
 237         if (( $? != 0 )) || [[ $cur_stat != running ]]; then
 238                 cti_reportfile $ZONELOGFILE \
 239                         "wrong state: expect <running> but got <$cur_stat>"
 240                 return 1
 241         fi
 242 
 243         # Install sharemgr
 244         tet_infoline " - $CP $SHAREMGR $zms_zone_path/root"
 245         $CP $SHAREMGR $zms_zone_path/root > $ZONELOGFILE 2>&1
 246         if (( $? != 0 )); then
 247                 cti_reportfile $ZONELOGFILE \
 248                         "cp $SHAREMGR $zms_zone_path/root failed"
 249                 return 1
 250         fi
 251 
 252         return 0
 253 }
 254 
 255 #
 256 # NAME
 257 #       zones_min_cleanup
 258 #
 259 # DESCRIPTION
 260 #       Remove the minimum zone created in the zones_min_setup() function.
 261 #
 262 function zones_min_cleanup {
 263         zmc_zone_name=$1
 264         zmc_zone_path=$2
 265 
 266         if [[ $TET_ZONE_LEAVE == True ]]; then
 267                 $ZONEADM -z $zmc_zone_name halt > $ZONELOGFILE 2>&1
 268                 return
 269         fi
 270 
 271         tet_infoline "Cleaning up a minimal zone to test sharemgr"
 272 
 273         # Call halt of zone
 274         tet_infoline " - $ZONEADM -z $zmc_zone_name halt"
 275         $ZONEADM -z $zmc_zone_name halt > $ZONELOGFILE 2>&1
 276         typeset -i zc_ret=$?
 277 
 278         # Call uninstall of zone
 279         tet_infoline " - $ZONEADM -z $zmc_zone_name uninstall -F"
 280         $ZONEADM -z $zmc_zone_name uninstall -F >> $ZONELOGFILE 2>&1
 281         zc_ret=$((zc_ret + $?))
 282 
 283         # Remove the zone
 284         tet_infoline " - $ZONECFG -z $zmc_zone_name delete -F"
 285         $ZONECFG -z $zmc_zone_name delete -F >> $ZONELOGFILE 2>&1
 286         zc_ret=$((zc_ret + $?))
 287         (( $zc_ret != 0 )) && cti_reportfile $ZONELOGFILE \
 288                 "WARNING: find error during zone cleanup"
 289 
 290         $RM -rf $zmc_zone_path > /dev/null 2>&1
 291 
 292         [[ -n $GZONEDEVICE ]] && $IFCONFIG $GZONEDEVICE unplumb
 293 }