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, 2016 by Delphix. All rights reserved.
16# Copyright 2014, OmniTI Computer Consulting, Inc. All rights reserved.
17# Copyright 2021 Tintri by DDN, Inc. All rights reserved.
18# Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
19#
20
21export PATH="/usr/bin"
22export NOINUSE_CHECK=1
23export STF_SUITE="/opt/zfs-tests"
24export COMMON="$STF_SUITE/runfiles/common.run"
25export STF_TOOLS="/opt/test-runner/stf"
26export PATHDIR=""
27runner="/opt/test-runner/bin/run"
28auto_detect=false
29
30if [[ -z "$TESTFAIL_CALLBACKS" ]] ; then
31	export TESTFAIL_CALLBACKS="$STF_SUITE/callbacks/zfs_dbgmsg"
32fi
33
34function fail
35{
36	echo $1
37	exit ${2:-1}
38}
39
40function find_disks
41{
42	typeset all_disks=$(echo '' | sudo -k format | awk \
43	    '/c[0-9]/ {print $2}')
44	typeset used_disks=$(zpool status | awk \
45	    '/c[0-9]+(t[0-9a-fA-F]+)?d[0-9]+/ {print $1}' | sed -E \
46	    's/(s|p)[0-9]+//g')
47
48	typeset disk used avail_disks
49	for disk in $all_disks; do
50		for used in $used_disks; do
51			[[ "$disk" = "$used" ]] && continue 2
52		done
53		[[ -n $avail_disks ]] && avail_disks="$avail_disks $disk"
54		[[ -z $avail_disks ]] && avail_disks="$disk"
55	done
56
57	echo $avail_disks
58}
59
60function find_rpool
61{
62	typeset ds=$(mount | awk '/^\/ / {print $3}')
63	echo ${ds%%/*}
64}
65
66function find_runfile
67{
68	typeset distro=
69	if [[ -d /opt/delphix && -h /etc/delphix/version ]]; then
70		distro=delphix
71	elif [[ 0 -ne $(grep -c OpenIndiana /etc/release 2>/dev/null) ]]; then
72		distro=openindiana
73	elif [[ 0 -ne $(grep -c OmniOS /etc/release 2>/dev/null) ]]; then
74		distro=omnios
75	fi
76
77	[[ -n $distro ]] && echo $COMMON,$STF_SUITE/runfiles/$distro.run
78}
79
80function verify_id
81{
82	[[ $(id -u) = "0" ]] && fail "This script must not be run as root."
83
84	sudo -k -n id >/dev/null 2>&1
85	[[ $? -eq 0 ]] || fail "User must be able to sudo without a password."
86}
87
88function verify_disks
89{
90	typeset disk
91	typeset path
92	for disk in $DISKS; do
93		case $disk in
94		/*) path=$disk;;
95		*) path=/dev/rdsk/${disk}s0
96		esac
97		sudo -k prtvtoc $path >/dev/null 2>&1
98		[[ $? -eq 0 ]] || return 1
99	done
100	return 0
101}
102
103function create_links
104{
105	typeset dir=$1
106	typeset file_list=$2
107
108	[[ -n $PATHDIR ]] || fail "PATHDIR wasn't correctly set"
109
110	for i in $file_list; do
111		[[ ! -e $PATHDIR/$i ]] || fail "$i already exists"
112		ln -s $dir/$i $PATHDIR/$i || fail "Couldn't link $i"
113	done
114
115}
116
117function constrain_path
118{
119	. $STF_SUITE/include/commands.cfg
120
121	PATHDIR=$(/usr/bin/mktemp -d /var/tmp/constrained_path.XXXX)
122	chmod 755 $PATHDIR || fail "Couldn't chmod $PATHDIR"
123
124	create_links "/usr/bin" "$USR_BIN_FILES"
125	create_links "/usr/sbin" "$USR_SBIN_FILES"
126	create_links "/sbin" "$SBIN_FILES"
127	create_links "/opt/zfs-tests/bin" "$ZFSTEST_FILES"
128
129	# Special case links
130	ln -s /usr/gnu/bin/dd $PATHDIR/gnu_dd
131}
132
133constrain_path
134export PATH=$PATHDIR
135
136verify_id
137while getopts ac:l:qT: c; do
138	case $c in
139	'a')
140		auto_detect=true
141		;;
142	'c')
143		runfile=$OPTARG
144		[[ -f $runfile ]] || fail "Cannot read file: $runfile"
145		if [[ -z $runfiles ]]; then
146			runfiles=$runfile
147		else
148			runfiles+=",$runfile"
149		fi
150		;;
151	'l')
152		logfile=$OPTARG
153		[[ -f $logfile ]] || fail "Cannot read file: $logfile"
154		xargs+=" -l $logfile"
155		;;
156	'q')
157		xargs+=" -q"
158		;;
159	'T')
160		xargs+=" -T $OPTARG"
161		;;
162	esac
163done
164shift $((OPTIND - 1))
165
166# If the user specified -a, then use free disks, otherwise use those in $DISKS.
167if $auto_detect; then
168	export DISKS=$(find_disks)
169elif [[ -z $DISKS ]]; then
170	fail "\$DISKS not set in env, and -a not specified."
171else
172	verify_disks || fail "Couldn't verify all the disks in \$DISKS"
173fi
174
175# Add the root pool to $KEEP according to its contents.
176# It's ok to list it twice.
177if [[ -z $KEEP ]]; then
178	KEEP="$(find_rpool)"
179else
180	KEEP+=" $(find_rpool)"
181fi
182
183export __ZFS_POOL_EXCLUDE="$KEEP"
184export KEEP="^$(echo $KEEP | sed 's/ /$|^/g')\$"
185
186[[ -z $runfiles ]] && runfiles=$(find_runfile)
187[[ -z $runfiles ]] && fail "Couldn't determine distro"
188
189. $STF_SUITE/include/default.cfg
190
191num_disks=$(echo $DISKS | awk '{print NF}')
192[[ $num_disks -lt 3 ]] && fail "Not enough disks to run ZFS Test Suite"
193
194# Ensure user has only basic privileges.
195ppriv -s EIP=basic -e $runner -c $runfiles $xargs
196ret=$?
197
198rm -rf $PATHDIR || fail "Couldn't remove $PATHDIR"
199
200exit $ret
201