1 #!/usr/bin/ksh
   2 
   3 #
   4 # This file and its contents are supplied under the terms of the
   5 # Common Development and Distribution License ("CDDL"), version 1.0.
   6 # You may only use this file in accordance with the terms of version
   7 # 1.0 of the CDDL.
   8 #
   9 # A full copy of the text of the CDDL should have accompanied this
  10 # source.  A copy of the CDDL is also available via the Internet at
  11 # http://www.illumos.org/license/CDDL.
  12 #
  13 
  14 #
  15 # Copyright (c) 2012, 2015 by Delphix. All rights reserved.
  16 # Copyright 2014, OmniTI Computer Consulting, Inc. All rights reserved.
  17 # Copyright 2016 Nexenta Systems, Inc.
  18 #
  19 
  20 export NOINUSE_CHECK=1
  21 export STF_SUITE="/opt/zfs-tests"
  22 export STF_TOOLS="/opt/test-runner/stf"
  23 runner="/opt/test-runner/bin/run"
  24 auto_detect=false
  25 
  26 function fail
  27 {
  28         echo $1
  29         exit ${2:-1}
  30 }
  31 
  32 function find_disks
  33 {
  34         typeset all_disks=$(echo '' | sudo -k /usr/sbin/format | awk \
  35             '/c[0-9]/ {print $2}')
  36         typeset used_disks=$(/sbin/zpool status | awk \
  37             '/c[0-9]*t[0-9a-f]*d[0-9]/ {print $1}' | sed 's/s[0-9]//g')
  38 
  39         typeset disk used avail_disks
  40         for disk in $all_disks; do
  41                 for used in $used_disks; do
  42                         [[ "$disk" = "$used" ]] && continue 2
  43                 done
  44                 [[ -n $avail_disks ]] && avail_disks="$avail_disks $disk"
  45                 [[ -z $avail_disks ]] && avail_disks="$disk"
  46         done
  47 
  48         echo $avail_disks
  49 }
  50 
  51 function find_rpool
  52 {
  53         typeset ds=$(/usr/sbin/mount | awk '/^\/ / {print $3}')
  54         echo ${ds%%/*}
  55 }
  56 
  57 function find_runfile
  58 {
  59         typeset distro=
  60         if [[ -d /opt/delphix && -h /etc/delphix/version ]]; then
  61                 distro=delphix
  62         elif [[ 0 -ne $(grep -c OpenIndiana /etc/release 2>/dev/null) ]]; then
  63                 distro=openindiana
  64         elif [[ 0 -ne $(grep -c OmniOS /etc/release 2>/dev/null) ]]; then
  65                 distro=omnios
  66         fi
  67 
  68         [[ -n $distro ]] && echo $STF_SUITE/runfiles/$distro.run
  69 }
  70 
  71 function verify_id
  72 {
  73         [[ $(id -u) = "0" ]] && fail "This script must not be run as root."
  74 
  75         sudo -k -n id >/dev/null 2>&1
  76         [[ $? -eq 0 ]] || fail "User must be able to sudo without a password."
  77 }
  78 
  79 function verify_disks
  80 {
  81         typeset disk
  82         for disk in $DISKS; do
  83                 sudo -k /usr/sbin/prtvtoc /dev/rdsk/${disk}s0 >/dev/null 2>&1
  84                 [[ $? -eq 0 ]] || return 1
  85         done
  86         return 0
  87 }
  88 
  89 verify_id
  90 
  91 while getopts ac:q c; do
  92         case $c in
  93         'a')
  94                 auto_detect=true
  95                 ;;
  96         'c')
  97                 runfile=$OPTARG
  98                 [[ -f $runfile ]] || fail "Cannot read file: $runfile"
  99                 ;;
 100         'q')
 101                 quiet='-q'
 102                 ;;
 103         esac
 104 done
 105 shift $((OPTIND - 1))
 106 
 107 # If the user specified -a, then use free disks, otherwise use those in $DISKS.
 108 if $auto_detect; then
 109         export DISKS=$(find_disks)
 110 elif [[ -z $DISKS ]]; then
 111         fail "\$DISKS not set in env, and -a not specified."
 112 else
 113         verify_disks || fail "Couldn't verify all the disks in \$DISKS"
 114 fi
 115 
 116 # Add the root pool to $KEEP according to its contents.
 117 # It's ok to list it twice.
 118 if [[ -z $KEEP ]]; then
 119         KEEP="$(find_rpool)"
 120 else
 121         KEEP+=" $(find_rpool)"
 122 fi
 123 
 124 export __ZFS_POOL_EXCLUDE="$KEEP"
 125 export KEEP="^$(echo $KEEP | sed 's/ /$|^/g')\$"
 126 
 127 [[ -z $runfile ]] && runfile=$(find_runfile)
 128 [[ -z $runfile ]] && fail "Couldn't determine distro"
 129 
 130 . $STF_SUITE/include/default.cfg
 131 
 132 num_disks=$(echo $DISKS | awk '{print NF}')
 133 [[ $num_disks -lt 3 ]] && fail "Not enough disks to run ZFS Test Suite"
 134 
 135 # Ensure user has only basic privileges.
 136 /usr/bin/ppriv -s EIP=basic -e $runner $quiet -c $runfile
 137 
 138 exit $?