1 #! /usr/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 #
  24 # Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  25 # Use is subject to license terms.
  26 #
  27 
  28 #typeset -ft $(typeset +f)
  29 
  30 . ${STF_TOOLS}/include/stf.kshlib
  31 lib_wait_time=${DEFAULT_WAIT_TIME:-30}
  32 
  33 library=gltest.kshlib
  34 
  35 ########################################################################
  36 # Function name: extract_assertion_info
  37 #
  38 # Purpose:
  39 #       Print out in a standard format, to stdout (and thus to the journal
  40 #       file) the assertion information.  This information is pulled from the
  41 #       standard assertion comment.
  42 #
  43 # Arguments:
  44 #       $1 = the file where the header information is.  Typically this
  45 #         is the test case source file.
  46 #
  47 # Return:  0 (always succeeds).
  48 #
  49 ########################################################################
  50 function extract_assertion_info
  51 {
  52 
  53         nawk -v comment_arg=$1 '
  54 
  55         BEGIN {
  56                 in_assertion            = 0;
  57                 turn_off_printing       = 0;
  58         }
  59 
  60         #
  61         # End of the .spec content.
  62         #
  63         /^# +end +__stf_assertion/ {
  64                 in_assertion = 0;
  65                 next;
  66         }
  67 
  68         #
  69         # Beginning of the .spec content.
  70         #
  71         /^# +start +__stf_assertion__/ {
  72                 in_assertion = 1;
  73                 next;
  74         }
  75 
  76         #
  77         #
  78         /^.+ASSERTION:/ && (in_assertion) {
  79                 a=substr($0, index($0, "ASSERTION:") + length("ASSERTION:"));
  80                 printf("--ASRT: %s\n\n", a);
  81                 turn_off_printing = 1;
  82                 next;
  83         }
  84 
  85         #
  86         # Actual assertion statement. STC calls this the description and 
  87         # requires one for every ASSERTION:.
  88         #
  89         /^.+DESCRIPTION:/ && (in_assertion) {
  90                 a=substr($0, index($0, "DESCRIPTION:") + length("DESCRIPTION:"));
  91                 printf("--DESC: %s\n", a);
  92                 turn_off_printing = 0;
  93                 next;
  94         }
  95 
  96         #
  97         # List of interfaces targeted by the current assertion. STC requires 
  98         # one of these for every ASSERTION:
  99         #
 100         /^.+INTERFACES:/ && (in_assertion) {
 101                 in_assertion = 0;
 102         }
 103 
 104         /^.+STRATEGY:/ && (in_assertion) {
 105                 in_assertion = 0;
 106         }
 107 
 108         #
 109         # Body of the assertion comments.
 110         #
 111         (in_assertion) && length && !(turn_off_printing) {
 112                 a=substr($0,index($0,"#")+1);
 113                 printf("%s\n", a);
 114         }
 115         (in_assertion) && (turn_off_printing)  {
 116                 next;
 117         }
 118 
 119         ' $1
 120 }
 121 
 122 ########################################################################
 123 #
 124 # Function name: service_exists
 125 #
 126 # Purpose:
 127 #       Verify that a service is defined within the repository
 128 #
 129 # Arguments:
 130 #       $1 = name of the service or service:instance to check
 131 #
 132 # Return:
 133 #       0 if service exists
 134 #       1 if the service does not exist
 135 #       2 if the arguments passed were invalid
 136 #
 137 ########################################################################
 138 function service_exists {
 139 
 140         typeset func_name=service_exists
 141 
 142         [[ $# -ne 1 ]] && {
 143                 echo "--DIAG: [$func_name]"
 144                 echo "function requires one argument - $# passed"
 145                 return 2
 146         }
 147 
 148         typeset service=$1
 149 
 150         /usr/sbin/svccfg select $service > /dev/null 2>&1
 151         ret=$?
 152         return $ret
 153 }
 154 
 155 ########################################################################
 156 #
 157 # Function name: service_check_state
 158 #
 159 # Purpose:
 160 #       Check if a service state is the state passed in 
 161 #
 162 # Arguments:
 163 #       [ -q ] don't complain if the state isn't reached
 164 #       $1 = name of the service:instance to check
 165 #       $2 = state to check for  (can be online, offline, degraded,
 166 #               maintenance, disabled, uninitialized)
 167 #
 168 # Return:
 169 #       0: if service is as asked
 170 #       1: if the service is not in the requested state
 171 #       2: an error occurred during the function
 172 #
 173 ########################################################################
 174 function service_check_state {
 175 
 176         typeset func_name=service_check_state
 177         typeset quiet=
 178 
 179         if [ -n "$1" -a "$1" = "-q" ]; then
 180                 quiet=1
 181                 shift
 182         fi
 183 
 184         [[ $# -ne 2 ]] && {
 185                 echo "--DIAG: [$func_name]"
 186                 echo "function requires two arguments - $# passed"
 187                 
 188                 return 2
 189         }
 190 
 191         typeset svcinst=$1
 192         typeset statetocheck=$2
 193 
 194         typeset state=
 195 
 196         service_exists $svcinst || {
 197                 echo "--DIAG: [$func_name]"
 198                 echo "entity $svcinst does not exist"
 199 
 200                 return 2
 201         }
 202 
 203         state=`svcprop -p restarter/state $svcinst`
 204         [[ -z $state ]] && {
 205                 echo "--DIAG: [$func_name]"
 206                 echo "svcprop did not return a state for instance $svcinst"
 207                 return 2
 208         }
 209 
 210         if [ "$state" != "$statetocheck" ]; then
 211                 [ -z "$quiet" ] && echo "--DIAG: [$func_name]"\
 212                     "instance $service returned state $state, not $statetocheck"
 213                 
 214                 return 1
 215         fi
 216 
 217         return 0
 218 }
 219 
 220 
 221 ########################################################################
 222 #
 223 # Function name: service_wait_state
 224 #
 225 # Purpose:
 226 #       Wait for a service to enter a certain state
 227 #
 228 # Arguments:
 229 #       $1 = name of a service:instance to check
 230 #       $2 = state that the service should transition to
 231 #       $3 (optional) = timeout to wait for the service to transition
 232 #               to the state.
 233 #
 234 # Return:
 235 #       0 if the service reaches the state by the timeout
 236 #       non-zero otherwise
 237 #
 238 ########################################################################
 239 function service_wait_state {
 240 
 241         typeset func_name=service_wait_state
 242 
 243         [[ $# -ne 2 && $# -ne 3 ]] && {
 244                 echo "--DIAG: [$func_name]"
 245                 echo "function requires two or three arguments - $# passed"
 246                 
 247                 return 2
 248         }
 249 
 250         typeset svcinst=$1
 251         typeset state=$2
 252         typeset wait_time=${3:-$lib_wait_time}
 253         typeset nsec=0
 254 
 255         while [ $nsec -le $wait_time ]; do
 256                 service_check_state -q $svcinst $state
 257                 [[ $? -eq 0 ]] && {
 258                         echo "--INFO: [$func_name]"
 259                         echo "instance $svcinst transitioned to state $state"
 260                         return 0
 261                 }
 262                 sleep 1
 263                 nsec=$((nsec + 1))
 264         done
 265         echo "--INFO: [$func_name]"
 266         echo "instance $svcinst did not transition to state $state"
 267         echo "within $wait_time seconds"
 268         return 1
 269 }
 270 
 271 ########################################################################
 272 #
 273 # Function name: update_result
 274 #
 275 # Purpose:
 276 #       This function accepts two results and determines what the overall
 277 #       result should be.  The first argument is the new result and the
 278 #       second argument is the current overall result.  The new result
 279 #       may or may not change the overall results.  For instance, if
 280 #       the current overall result is FAIL and the new result is 
 281 #       UNRESOLVED than the overall result remains FAIL.
 282 #       
 283 # Arguments:
 284 #       $1 = the new result
 285 #       $2 = the current overall result
 286 #
 287 # Return:
 288 #       Always returns 0 as an exit status.
 289 #       "Returns" the current overall result by echo'ing.  Therefore
 290 #       this routine should be used as follows (for example):
 291 #
 292 #       RESULT=$(update_result $STF_UNRESOLVED $RESULT)
 293 #
 294 ########################################################################
 295 function update_result
 296 {
 297         typeset result_1=$1
 298         typeset result_2=$2
 299 
 300         [[ ${result_1} -eq $STF_FAIL ]] &&
 301                 result_2=$STF_FAIL
 302         [[ ${result_1} -eq $STF_UNRESOLVED ]] && {
 303                 [[ ${result_2} -ne $STF_FAIL ]] &&
 304                         result_2=$STF_UNRESOLVED
 305         }
 306         echo $result_2
 307         return 0
 308 }
 309 
 310 ########################################################################
 311 #
 312 # Function name: print_result
 313 #
 314 # Purpose:
 315 #       Print out a result message "RSLT: result"
 316 #
 317 # Arguments:
 318 #       $1 - the result to print out (e.g. "PASS")
 319 #
 320 # Return:
 321 #       Always returns 0
 322 #
 323 ########################################################################
 324 function print_result
 325 {
 326         typeset result=$1
 327 
 328         case $result in
 329                 $STF_PASS) echo "--RSLT: PASS" ;;
 330                 $STF_FAIL) echo "--RSLT: FAIL" ;;
 331                 $STF_UNRESOLVED) echo "--RSLT: UNRESOLVED" ;;
 332                 *) echo "--RSLT: UNKNONWN" ;;
 333         esac
 334 
 335         return 0
 336 }
 337 
 338 ########################################################################
 339 #
 340 # Function name: service_delete
 341 #
 342 # Purpose:
 343 #       Deletes services and/or service instance from the repository
 344 #
 345 # Arguments:
 346 #       $1, $2, . . . - list of entities to delete
 347 #
 348 # Return:
 349 #       0 - deletion successful
 350 #       non-zero - deletion unsuccessful
 351 #
 352 ########################################################################
 353 function service_delete
 354 {
 355         typeset func_name=service_delete
 356 
 357         [[ $# -eq 0 ]] && {
 358                 echo "--DIAG: [$func_name]"
 359                 echo "function requires at least one service, none passed"
 360                 
 361                 return 1
 362         }
 363 
 364         typeset service=
 365         typeset ret=
 366 
 367         for service in $@; do
 368                 service_exists $service
 369                 [[ $? -eq 0 ]] && {
 370                         svccfg delete $service >/dev/null 2>&1 
 371                         ret=$?
 372                         [[ $ret -ne 0 ]]  && {
 373                                 echo "--DIAG: [$func_name]"
 374                                 echo "svccfg delete failed"
 375 
 376                                 return $ret
 377                         }
 378                 }
 379         done
 380         return 0
 381 }
 382 
 383 ########################################################################
 384 #
 385 # Function name: service_cleanup
 386 #
 387 # Purpose:
 388 #       Disable instances and remove them from the repository.  If
 389 #       no instances are specified (i.e. only a service is specified)
 390 #       all instances of that service are removed as well as the service.
 391 #
 392 # Arguments:
 393 #       $1: The service to delete it's instances and, if no instances
 394 #           are specified, to delete the service
 395 #       $2: A list of instances to specify (the service will not be deleted)
 396 #
 397 # Return:
 398 #       0 - all deletions were successful
 399 #       1 - the deletion was not successful
 400 #
 401 ########################################################################
 402 function service_cleanup
 403 {
 404         typeset func_name=service_cleanup
 405         typeset service=$1
 406 
 407         typeset delete_service=0
 408         typeset instance_list=
 409         typeset instance=
 410         typeset service_fmri=
 411         typeset fully_qualified=
 412 
 413         if [ $# -eq 0 ]; then
 414                 echo "--DIAG: [$func_name]"
 415                 echo "function requires at least one service, none passed"
 416 
 417                 return 1
 418         fi
 419 
 420         if [ $# -eq 1 ]; then
 421                 service_exists $service
 422                 [[ $? -ne 0 ]] && 
 423                         return 0
 424 
 425                 instance_list=$(svcs -a -H -o FMRI | grep $service)
 426                 fully_qualified=true
 427                 delete_service=1
 428         else
 429                 shift 
 430                 instance_list=$@
 431         fi
 432 
 433 
 434         for instance in $instance_list; do
 435                 if [ -z "$fully_qualified" ]; then
 436                         service_fmri=svc:/$service:$instance
 437                 else
 438                         service_fmri=$instance
 439                 fi
 440                 service_exists $service_fmri
 441                 if [ $? -eq 0 ]; then
 442 
 443                         typeset state=
 444                         typeset log=
 445 
 446                         svcadm disable $service_fmri
 447                         if [ $? -ne 0 ]; then
 448                                 echo "--DIAG: [$func_name]"
 449                                 echo "$service_fmri not transitioned to"
 450                                 echo "disabled state"
 451 
 452                                 return 1
 453                         fi
 454 
 455                         # clear maintenance state if it's set.  This
 456                         # should clean up processes.
 457                         typeset atstate=$(svcprop -p restarter/state \
 458                                         $service_fmri)
 459                         if [ "$atstate" = "maintenance" ]; then
 460                                 svcadm clear $service_fmri
 461                         fi
 462 
 463                         atstate=$(svcprop -p restarter/state $service_fmri)
 464                         if [ "$atstate" != offline -a "$atstate" != unknown \
 465                                 -a "$atstate" != maintenance \
 466                                 -a "$atstate" != - ]; then
 467                                 # Moan about it, but still continue
 468                                 # This will leave stray processes but
 469                                 # that's the cost.
 470                                 service_wait_state $service_fmri disabled
 471                         fi
 472                         svccfg delete -f $service_fmri
 473                         if [ $? -ne 0 ]; then
 474                                 echo "--DIAG: [$func_name]"
 475                                 echo "failure deleting instance $service_fmri"
 476 
 477                                 return 1
 478                         fi
 479 
 480                 fi
 481         done
 482         if [ $delete_service -ne 0 ]; then
 483                 svccfg delete -f $service 
 484                 if [ $? -ne 0 ]; then
 485                         echo "--DIAG: [$func_name]"
 486                         echo "failure deleting service $service"
 487 
 488                         return 1
 489                 fi
 490         fi
 491         return 0
 492 }
 493 
 494 
 495 ########################################################################
 496 #
 497 # Function name: service_import
 498 #
 499 # Purpose:
 500 #       Import service using specified service registration file
 501 #
 502 # Arguments:
 503 #       $1: The name of the service that will be imported
 504 #       $2: The service registration file to import 
 505 #
 506 # Return:
 507 #       0 - import was successful
 508 #       1 - import was not successful
 509 #
 510 ########################################################################
 511 function service_import
 512 {
 513         typeset func_name=service_import
 514         typeset service_name=$1
 515         typeset service_file=$2
 516 
 517         #
 518         # Check arguments, verify service name and file are specified
 519         #
 520         if [ $# -ne 2 ]; then
 521                 echo "--DIAG: [$func_name]"
 522                 echo "function requires service name and reg file"
 523 
 524                 return 1
 525         fi
 526 
 527         #
 528         # Import service
 529         #
 530         svccfg import $service_file > /dev/null 2>&1
 531         if [ $? -ne 0 ]; then
 532                 echo "--DIAG: [$func_name]"
 533                 echo "svccfg import $service_file failed"
 534 
 535                 return 1
 536         fi
 537 
 538         #
 539         # Verify service 
 540         #
 541         service_exists $service_name
 542         if [ $? -ne 0 ]; then
 543                 echo "--DIAG: [$func_name]"
 544                 echo "Service $service_name does not exist after import"
 545 
 546                 return 1
 547         fi
 548 
 549         return 0
 550 }
 551 
 552 
 553 ########################################################################
 554 #
 555 # Function name: check_gl_env
 556 #
 557 # Purpose:
 558 #       Determines if the GL environment is available.  This is
 559 #       to prevent the test from running if the environment is 
 560 #       unavailable or completely broken.
 561 #
 562 # Arguments: None
 563 #
 564 # Return:
 565 #       0 - if the repository exists
 566 #       non-zero - otherwise
 567 #
 568 ########################################################################
 569 function check_gl_env
 570 {
 571         typeset -i ret_value=0
 572         typeset ret=
 573 
 574         svccfg quit > /dev/null 2>&1
 575         ret=$?
 576         [[ $ret -ne 0 ]] && {
 577                 echo "--DIAG: [$library]"
 578                 echo "svccfg returned $ret, expected 0"
 579 
 580                 ret_value=1
 581         }
 582         test -n "`pgrep -z $(zonename) svc.startd`"
 583         ret=$?
 584         if [ $ret_value -eq 0 -a $ret -ne 0 ]; then
 585                 echo "--DIAG: [$library] svc.startd is not executing"
 586 
 587                 ret_value=2
 588         fi
 589         return $ret_value
 590 }
 591 
 592 ##############################################################
 593 #
 594 # Function Name: filename_to_property
 595 #
 596 # Purpose:
 597 #       convert a filename into a property name used in
 598 #       the md5 checksum.
 599 #
 600 # Parameters:
 601 #       $1 - The filename to convert into a property value
 602 #
 603 # Returns:
 604 #       A string, corresponding to the property name.
 605 #
 606 ##############################################################
 607 function filename_to_property {
 608         typeset file=$1
 609         typeset fc=${#file}
 610         typeset nfile=
 611         typeset sfile=
 612         typeset ch=
 613         typeset lch=
 614 
 615         # strip first character
 616         ch=${file##?}
 617         lch=${file%$ch}
 618         file=$ch
 619         sfile=$file
 620 
 621         if [ "$fc" -gt 255 ]; then
 622                 # do the .. thing
 623                 typeset atc=1
 624 
 625                 while [ $atc -lt 127 ]; do
 626                         ch=${sfile##?}
 627                         lch=${sfile%$ch}
 628                         sfile=$ch
 629                         nfile=$nfile$lch
 630                         atc=$((atc + 1))
 631                 done
 632                 nfile=$nfile..
 633                 while [ $atc -lt $((fc - 127)) ]; do
 634                         ch=${sfile##?}
 635                         lch=${sfile%$ch}
 636                         sfile=$ch
 637                         atc=$((atc + 1))
 638                 done
 639                 while [ $atc -lt $fc ]; do
 640                         ch=${sfile##?}
 641                         lch=${sfile%$ch}
 642                         sfile=$ch
 643                         nfile=$nfile$lch
 644                         atc=$((atc + 1))
 645                 done
 646         fi
 647         nfile=${nfile:-$file}
 648         file=`print -n $nfile | /bin/tr -c '[-][:alnum:]' '-[_*]'`
 649         echo $file
 650 }
 651 
 652 ########################################################################
 653 # 
 654 # Function Name: manifest_purgemd5
 655 # Purpose:
 656 #   purge the md5 checksum from the repository if one exists
 657 # Arguments:
 658 #       $1 - the filename who's manifest to remove.
 659 #
 660 # Returns:
 661 #       Don't Care
 662 # XXX alert: SVCCFG_CHECKHASH currently controls the creation of
 663 #       hash. Completely undocumented (again!)
 664 #
 665 ########################################################################
 666 function manifest_purgemd5 {
 667         typeset file=$1
 668         typeset fp=`filename_to_property $file`
 669 
 670         svccfg -f - >/dev/null 2>&1 <<-EOM
 671         select smf/manifest
 672         delprop $fp
 673         end
 674         EOM
 675 }
 676 
 677 ########################################################################
 678 #
 679 # Function Name: find_nontransient_pids
 680 # Purpose:
 681 #   find the non-transient pids for a process by name in a zone. If only
 682 #   one process is found then return that process; otherwise check for
 683 #   each process.
 684 # Arguments:
 685 #       $1 - the name of the process to initially pgrep.
 686 #
 687 # Returns:
 688 #       the number of pids found
 689 # Outputs:
 690 #       the pids that are remaining after the loop
 691 ########################################################################
 692 function find_nontransient_pids {
 693         typeset pids=$(pgrep -z $(zonename) $1)
 694         typeset output=
 695         typeset pid=
 696 
 697         set -- $pids
 698         typeset npids=$#
 699         if [ $npids = 1 ]; then
 700                 echo $pids
 701                 return 1
 702         fi
 703 
 704         npids=0
 705         for pid in $pids; do
 706                 typeset count=0
 707                 while [ $count -lt 5 ]; do
 708                         [ ! -d /proc/$pid ] && break
 709                         sleep 1
 710                         count=$((count + 1))
 711                 done
 712                 if [ $count -eq 10 ]; then
 713                         output="$output $pid"
 714                         npids=$((npids + 1))
 715                 fi
 716         done
 717         echo $output
 718         return $npids
 719 }
 720 
 721 
 722 ########################################################################
 723 #
 724 # Function Name: zone_create
 725 # Purpose:
 726 #       Create a zone with the supplied criteria.
 727 #
 728 # Arguments:
 729 #       $1 - The name of the local zone
 730 #       $2 - The directory of the zone
 731 #       $3 - "sparse" or "whole" type
 732 #
 733 # Returns:
 734 #       0 - Successfully created zone
 735 #       1 - Failed to create zone
 736 #
 737 # Outputs:
 738 #       none
 739 ########################################################################
 740 function zone_create {
 741 
 742         typeset zonename=$1
 743         typeset zonepath=$2
 744         typeset zonetype=$3
 745         typeset zonecfg=/tmp/zone_create.$$
 746         typeset zonesysid
 747         typeset zonehostname
 748         typeset status ret output
 749 
 750         # Verify all arguments were given
 751         if [[ -z "$zonename" || -z "$zonepath" || -z "$zonetype" ]]; then
 752                 echo "--DIAG: [$func_name]"
 753                 echo "All arguments were not specified"
 754 
 755                 return 1
 756         fi
 757 
 758         # Create command file for zone creation
 759         if [[ "$zonetype" == "sparse" ]]; then
 760                 cat > $zonecfg <<-EOM
 761                 create
 762                 set zonepath=$zonepath
 763                 set autoboot=true
 764                 commit
 765                 verify
 766                 exit
 767                 EOM
 768         elif [[ "$zonetype" == "whole" ]]; then
 769                 cat > $zonecfg <<-EOM
 770                 create -b
 771                 set zonepath=$zonepath
 772                 set autoboot=true
 773                 commit
 774                 verify
 775                 exit
 776                 EOM
 777         else
 778                 echo "--DIAG: [$func_name]"
 779                 echo "Zone type "$zonetype" is not whole or sparse"
 780 
 781                 return 1
 782         fi
 783 
 784         # Configure local zone
 785         output=$(zonecfg -z $zonename -f $zonecfg 2>&1)
 786         ret=$?
 787         if [[ $ret -ne 0 ]]; then
 788                 echo "--DIAG: [$func_name]"
 789                 echo "zonecfg failed with error code $ret"
 790                 echo "output='$output'"
 791 
 792                 return 1
 793         fi
 794 
 795         # Install local zone
 796         output=$(zoneadm -z $zonename install 2>&1)
 797         ret=$?
 798         if [[ $ret -ne 0 ]]; then
 799                 echo "--DIAG: [$func_name]"
 800                 echo "zoneadm install failed with error code $ret"
 801                 echo "output='$output'"
 802 
 803                 return 1
 804         fi
 805 
 806         # Auto configure the zone using sysidcfg
 807         zonesysid=$zonepath/root/etc/sysidcfg
 808         zonehostname="host$$"
 809 
 810         cat > $zonesysid <<-!
 811         terminal=xterm
 812         system_locale=C
 813         timezone=$TZ
 814         security_policy=NONE
 815         name_service=NONE
 816         root_password=l1admin
 817         network_interface=primary { hostname=$zonehostname }
 818         !
 819 
 820         # Boot local zone
 821         zone_boot $zonename > /dev/null 2>&1
 822         ret=$?
 823         if [[ $ret -ne 0 ]]; then
 824                 echo "--DIAG: [$func_name]"
 825                 echo "Failed to boot $zonename: return code $ret"
 826                 echo "output='$output'"
 827 
 828                 return 1
 829         fi
 830 
 831         # Cleanup
 832         rm -f $zonecfg
 833 
 834         return 0
 835 }
 836 
 837 
 838 
 839 
 840 ########################################################################
 841 #
 842 # Function Name: zone_halt
 843 # Purpose:
 844 #       Halt the specified zone 
 845 #
 846 # Arguments:
 847 #       $1 - The name of the local zone
 848 #
 849 # Returns:
 850 #       0 - Successfully halted zone
 851 #       1 - Failed to halt zone
 852 #
 853 # Outputs:
 854 #       none
 855 ########################################################################
 856 function zone_halt {
 857         
 858         typeset zonename=$1
 859         typeset status ret output
 860 
 861         # Verify zone name was given 
 862         if [[ -z "$zonename" ]]; then
 863                 echo "--DIAG: [$func_name]"
 864                 echo "Zone name was not supplied"
 865 
 866                 return 1
 867         fi
 868 
 869         # Check the status of the zone, if running, halt it
 870         status=$(zoneadm list -cv | egrep $zonename | awk '{print $3}')
 871 
 872         if [[ "$status" == "running" ]]; then
 873                 output=$(zoneadm -z $zonename halt 2>&1)
 874                 ret=$?
 875                 if [[ $ret -ne 0 ]]; then
 876                         echo "--DIAG: [$func_name]"
 877                         echo "Failed to halt $zonename: ret='$ret'"
 878                         echo "output='$output'"
 879 
 880                         return 1
 881                 fi
 882         fi
 883 
 884         # Verify zone is in the "installed" state
 885         status=$(zoneadm list -cv | egrep $zonename | awk '{print $3}')
 886 
 887         if [[ "$status" != "installed" ]]; then
 888                 echo "--DIAG: [$func_name]"
 889                 echo "Zone status unexpected: $status"
 890 
 891                 return 1
 892         fi
 893 
 894         return 0
 895 }
 896 
 897 
 898 ########################################################################
 899 #
 900 # Function Name: zone_boot
 901 # Purpose:
 902 #       Boot the specified zone 
 903 #
 904 # Arguments:
 905 #       $1 - The name of the local zone
 906 #
 907 # Returns:
 908 #       0 - Successfully booted zone
 909 #       1 - Failed to boot zone
 910 #
 911 # Outputs:
 912 #       none
 913 ########################################################################
 914 function zone_boot {
 915         
 916         typeset zonename=$1
 917         typeset status ret output
 918 
 919         # Verify zone name was given 
 920         if [[ -z "$zonename" ]]; then
 921                 echo "--DIAG: [$func_name]"
 922                 echo "Zone name was not supplied"
 923 
 924                 return 1
 925         fi
 926 
 927         # Check the status of the zone, if not running, boot it
 928         status=$(zoneadm list -cv | egrep $zonename | awk '{print $3}')
 929 
 930         if [[ "$status" != "running" ]]; then
 931                 output=$(zoneadm -z $zonename boot 2>&1)
 932                 ret=$?
 933                 if [[ $ret -ne 0 ]]; then
 934                         echo "--DIAG: [$func_name]"
 935                         echo "Failed to boot $zonename: ret='$ret'"
 936                         echo "output='$output'"
 937 
 938                         return 1
 939                 fi
 940 
 941                 # Sleep to allow zone to fully come up
 942                 sleep 180
 943 
 944 
 945                 # Verify zone is in the "running" state
 946                 status=$(zoneadm list -cv | egrep $zonename | awk '{print $3}')
 947 
 948                 if [[ "$status" != "running" ]]; then
 949                         echo "--DIAG: [$func_name]"
 950                         echo "Zone status unexpected: $status"
 951 
 952                         return 1
 953                 fi
 954         fi
 955 
 956         return 0
 957 }
 958 
 959 
 960 ########################################################################
 961 #
 962 # Function Name: zone_cleanup
 963 # Purpose:
 964 #       Destroy the zone with the supplied criteria.
 965 #
 966 # Arguments:
 967 #       $1 - The name of the local zone
 968 #
 969 # Returns:
 970 #       0 - Successfully removed zone
 971 #       1 - Failed to remove zone
 972 #
 973 # Outputs:
 974 #       none
 975 ########################################################################
 976 function zone_cleanup {
 977         
 978         typeset zonename=$1
 979         typeset zonesysid
 980         typeset status ret output
 981 
 982         # Verify all arguments were given
 983         if [[ -z "$zonename" ]]; then
 984                 echo "--DIAG: [$func_name]"
 985                 echo "All arguments were not supplied"
 986 
 987                 return 1
 988         fi
 989 
 990         # Halt the zone
 991         zone_halt $zonename
 992         if [[ $ret -ne 0 ]]; then
 993                 echo "--DIAG: [$func_name]"
 994                 echo "Failed to halt $zonename: ret='$ret'"
 995                 echo "output='$output'"
 996 
 997                 return 1
 998         fi
 999 
1000         # Uninstall the zone
1001         output=$(zoneadm -z $zonename uninstall -F 2>&1)
1002         ret=$?
1003         if [[ $ret -ne 0 ]]; then
1004                 echo "--DIAG: [$func_name]"
1005                 echo "Failed to uninstall $zonename: ret='$ret'"
1006                 echo "output='$output'"
1007 
1008                 return 1
1009         fi
1010 
1011         # Delete the zone configuration
1012         output=$(zonecfg -z $zonename delete -F 2>&1)
1013         ret=$?
1014         if [[ $ret -ne 0 ]]; then
1015                 echo "--DIAG: [$func_name]"
1016                 echo "Failed to delete $zonename: ret='$ret'"
1017                 echo "output='$output'"
1018 
1019                 return 1
1020         fi
1021 
1022         return 0
1023 }