7290 ZFS test suite needs to control what utilities it can run
[unleashed.git] / usr / src / test / zfs-tests / cmd / scripts / zfstest.ksh
blob5bbf4f02d16e73c23301f15293917dfd59401673
1 #!/usr/bin/ksh
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.
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.
15 # Copyright (c) 2012, 2016 by Delphix. All rights reserved.
16 # Copyright 2014, OmniTI Computer Consulting, Inc. All rights reserved.
17 # Copyright 2016 Nexenta Systems, Inc.
20 export PATH="/usr/bin"
21 export NOINUSE_CHECK=1
22 export STF_SUITE="/opt/zfs-tests"
23 export STF_TOOLS="/opt/test-runner/stf"
24 export PATHDIR=""
25 runner="/opt/test-runner/bin/run"
26 auto_detect=false
28 if [[ -z "$TESTFAIL_CALLBACKS" ]] ; then
29 export TESTFAIL_CALLBACKS="$STF_SUITE/callbacks/zfs_dbgmsg.ksh"
32 function fail
34 echo $1
35 exit ${2:-1}
38 function find_disks
40 typeset all_disks=$(echo '' | sudo -k format | awk \
41 '/c[0-9]/ {print $2}')
42 typeset used_disks=$(zpool status | awk \
43 '/c[0-9]*t[0-9a-f]*d[0-9]/ {print $1}' | sed 's/s[0-9]//g')
45 typeset disk used avail_disks
46 for disk in $all_disks; do
47 for used in $used_disks; do
48 [[ "$disk" = "$used" ]] && continue 2
49 done
50 [[ -n $avail_disks ]] && avail_disks="$avail_disks $disk"
51 [[ -z $avail_disks ]] && avail_disks="$disk"
52 done
54 echo $avail_disks
57 function find_rpool
59 typeset ds=$(mount | awk '/^\/ / {print $3}')
60 echo ${ds%%/*}
63 function find_runfile
65 typeset distro=
66 if [[ -d /opt/delphix && -h /etc/delphix/version ]]; then
67 distro=delphix
68 elif [[ 0 -ne $(grep -c OpenIndiana /etc/release 2>/dev/null) ]]; then
69 distro=openindiana
70 elif [[ 0 -ne $(grep -c OmniOS /etc/release 2>/dev/null) ]]; then
71 distro=omnios
74 [[ -n $distro ]] && echo $STF_SUITE/runfiles/$distro.run
77 function verify_id
79 [[ $(id -u) = "0" ]] && fail "This script must not be run as root."
81 sudo -k -n id >/dev/null 2>&1
82 [[ $? -eq 0 ]] || fail "User must be able to sudo without a password."
85 function verify_disks
87 typeset disk
88 for disk in $DISKS; do
89 sudo -k prtvtoc /dev/rdsk/${disk}s0 >/dev/null 2>&1
90 [[ $? -eq 0 ]] || return 1
91 done
92 return 0
95 function create_links
97 typeset dir=$1
98 typeset file_list=$2
100 [[ -n $PATHDIR ]] || fail "PATHDIR wasn't correctly set"
102 for i in $file_list; do
103 [[ ! -e $PATHDIR/$i ]] || fail "$i already exists"
104 ln -s $dir/$i $PATHDIR/$i || fail "Couldn't link $i"
105 done
109 function constrain_path
111 . $STF_SUITE/include/commands.cfg
113 PATHDIR=$(/usr/bin/mktemp -d /var/tmp/constrained_path.XXXX)
114 chmod 755 $PATHDIR || fail "Couldn't chmod $PATHDIR"
116 create_links "/usr/bin" "$USR_BIN_FILES"
117 create_links "/usr/sbin" "$USR_SBIN_FILES"
118 create_links "/sbin" "$SBIN_FILES"
119 create_links "/opt/zfs-tests/bin" "$ZFSTEST_FILES"
121 # Special case links
122 ln -s /usr/gnu/bin/dd $PATHDIR/gnu_dd
125 constrain_path
126 export PATH=$PATHDIR
128 verify_id
129 while getopts ac:q c; do
130 case $c in
131 'a')
132 auto_detect=true
134 'c')
135 runfile=$OPTARG
136 [[ -f $runfile ]] || fail "Cannot read file: $runfile"
138 'q')
139 quiet='-q'
141 esac
142 done
143 shift $((OPTIND - 1))
145 # If the user specified -a, then use free disks, otherwise use those in $DISKS.
146 if $auto_detect; then
147 export DISKS=$(find_disks)
148 elif [[ -z $DISKS ]]; then
149 fail "\$DISKS not set in env, and -a not specified."
150 else
151 verify_disks || fail "Couldn't verify all the disks in \$DISKS"
154 # Add the root pool to $KEEP according to its contents.
155 # It's ok to list it twice.
156 if [[ -z $KEEP ]]; then
157 KEEP="$(find_rpool)"
158 else
159 KEEP+=" $(find_rpool)"
162 export __ZFS_POOL_EXCLUDE="$KEEP"
163 export KEEP="^$(echo $KEEP | sed 's/ /$|^/g')\$"
165 [[ -z $runfile ]] && runfile=$(find_runfile)
166 [[ -z $runfile ]] && fail "Couldn't determine distro"
168 . $STF_SUITE/include/default.cfg
170 num_disks=$(echo $DISKS | awk '{print NF}')
171 [[ $num_disks -lt 3 ]] && fail "Not enough disks to run ZFS Test Suite"
173 # Ensure user has only basic privileges.
174 ppriv -s EIP=basic -e $runner $quiet -c $runfile
175 ret=$?
177 rm -rf $PATHDIR || fail "Couldn't remove $PATHDIR"
179 exit $ret