xref: /illumos-gate/usr/src/test/zfs-tests/cmd/scripts/zfstest.ksh (revision 2748bf5666cab238e5b0138f9baea36ee283f990)
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# Copyright 2026 Gordon W. Ross
20#
21
22export LC_ALL=C.UTF-8
23export PATH="/usr/bin"
24export NOINUSE_CHECK=1
25export STF_SUITE="/opt/zfs-tests"
26export COMMON="$STF_SUITE/runfiles/common.run"
27export STF_TOOLS="/opt/test-runner/stf"
28export PATHDIR=""
29runner="/opt/test-runner/bin/run"
30auto_detect=false
31
32if [[ -z "$TESTFAIL_CALLBACKS" ]] ; then
33	export TESTFAIL_CALLBACKS="$STF_SUITE/callbacks/zfs_dbgmsg"
34fi
35
36function fail
37{
38	echo $1
39	exit ${2:-1}
40}
41
42function find_disks
43{
44	typeset all_disks=$(echo '' | sudo -k format | awk \
45	    '/c[0-9]/ {print $2}')
46	typeset used_disks=$(zpool status | awk \
47	    '/c[0-9]+(t[0-9a-fA-F]+)?d[0-9]+/ {print $1}' | sed -E \
48	    's/(s|p)[0-9]+//g')
49
50	typeset disk used avail_disks
51	for disk in $all_disks; do
52		for used in $used_disks; do
53			[[ "$disk" == "$used" ]] && continue 2
54		done
55		[[ -n $avail_disks ]] && avail_disks="$avail_disks $disk"
56		[[ -z $avail_disks ]] && avail_disks="$disk"
57	done
58
59	echo $avail_disks
60}
61
62function find_rpool
63{
64	typeset ds=$(mount | awk '/^\/ / {print $3}')
65	echo ${ds%%/*}
66}
67
68function find_runfile
69{
70	typeset distro=
71	if [[ -d /opt/delphix && -h /etc/delphix/version ]]; then
72		distro=delphix
73	elif grep -qs OpenIndiana /etc/release; then
74		distro=openindiana
75	elif grep -qs OmniOS /etc/release; then
76		distro=omnios
77	fi
78
79	[[ -n $distro ]] && echo $COMMON,$STF_SUITE/runfiles/$distro.run
80}
81
82function verify_id
83{
84	[[ $(id -u) == "0" ]] && fail "This script must not be run as root."
85
86	sudo -k -n id >/dev/null 2>&1 ||
87		fail "User must be able to sudo without a password."
88}
89
90function verify_disks
91{
92	typeset disk
93	typeset path
94	typeset -lu expected_size
95	typeset -lu size
96	typeset -lu first_size=0
97	typeset first_disk=""
98
99	# Ensure disks are large enough for the tests: no less than 10GB
100	# and large enough for a crash dump plus overheads: the disk partition
101	# table (about 34k), zpool with 4.5MB for pool label and 128k for pool
102	# data, so we round up pool data + labels to 5MB.
103	expected_size=$(sudo -k -n dumpadm -epH)
104	(( expected_size = expected_size + 5 * 1024 * 1024 ))
105
106	if (( expected_size < 10 * 1024 * 1024 * 1024 )); then
107		(( expected_size = 10 * 1024 * 1024 * 1024 ))
108	fi
109
110	for disk in $DISKS; do
111		case $disk in
112		/*) path=$disk;;
113		*) path=/dev/rdsk/${disk}
114		esac
115		set -A disksize $(sudo -k prtvtoc $path 2>&1 |
116			awk '$3 == "bytes/sector" ||
117			    ($3 == "accessible" && $4 == "sectors") {print $2}')
118
119		if [[ (-n "${disksize[0]}") && (-n "${disksize[1]}") ]]; then
120			(( size = disksize[0] * disksize[1] ))
121		else
122			return 1
123		fi
124		if (( size <  expected_size )); then
125			(( size = expected_size / 1024 / 1024 / 1024 ))
126			fail "$disk is too small, need at least ${size}GB"
127		fi
128
129		if [[ -z $first_disk ]]; then
130			first_disk=$disk
131			first_size=$size
132			first_blocksize=${disksize[0]}
133		else
134			if (( size != first_size )); then
135				echo "Warning: $disk and $first_disk are" \
136				    "different sizes."
137				echo "Tests that mirror disks (e.g." \
138				    "zpool_scrub, zpool_resilver) require all"
139				echo "disks to be the same size and may fail" \
140				    "with 'device is too small'."
141			fi
142			if (( disksize[0] != first_blocksize )); then
143				echo "Warning: $disk and $first_disk have" \
144				    "different block sizes" \
145				    "(${disksize[0]} vs ${first_blocksize})."
146				echo "Some tests require all disks to present" \
147				    "the same block size and may fail."
148			fi
149		fi
150	done
151	return 0
152}
153
154function create_links
155{
156	typeset dir=$1
157	typeset file_list=$2
158
159	[[ -n $PATHDIR ]] || fail "PATHDIR wasn't correctly set"
160
161	for i in $file_list; do
162		[[ ! -e $PATHDIR/$i ]] || fail "$i already exists"
163		ln -s $dir/$i $PATHDIR/$i || fail "Couldn't link $i"
164	done
165
166}
167
168function constrain_path
169{
170	. $STF_SUITE/include/commands.cfg
171
172	PATHDIR=$(/usr/bin/mktemp -d /var/tmp/constrained_path.XXXX)
173	chmod 755 $PATHDIR || fail "Couldn't chmod $PATHDIR"
174
175	create_links "/usr/bin" "$USR_BIN_FILES"
176	create_links "/usr/sbin" "$USR_SBIN_FILES"
177	create_links "/sbin" "$SBIN_FILES"
178	create_links "/opt/zfs-tests/bin" "$ZFSTEST_FILES"
179
180	# Special case links
181	ln -s /usr/gnu/bin/dd $PATHDIR/gnu_dd
182}
183
184constrain_path
185export PATH=$PATHDIR
186
187verify_id
188while getopts ac:l:qT: c; do
189	case $c in
190	'a')
191		auto_detect=true
192		;;
193	'c')
194		runfile=$OPTARG
195		[[ -f $runfile ]] || fail "Cannot read file: $runfile"
196		if [[ -z $runfiles ]]; then
197			runfiles=$runfile
198		else
199			runfiles+=",$runfile"
200		fi
201		;;
202	'l')
203		logfile=$OPTARG
204		[[ -f $logfile ]] || fail "Cannot read file: $logfile"
205		xargs+=" -l $logfile"
206		;;
207	'q')
208		xargs+=" -q"
209		;;
210	'T')
211		xargs+=" -T $OPTARG"
212		;;
213	esac
214done
215shift $((OPTIND - 1))
216
217# If the user specified -a, then use free disks, otherwise use those in $DISKS.
218if $auto_detect; then
219	export DISKS=$(find_disks)
220elif [[ -z $DISKS ]]; then
221	fail "\$DISKS not set in env, and -a not specified."
222else
223	verify_disks || fail "Couldn't verify all the disks in \$DISKS"
224fi
225
226# Add the root pool to $KEEP according to its contents.
227# It's ok to list it twice.
228if [[ -z $KEEP ]]; then
229	KEEP="$(find_rpool)"
230else
231	KEEP+=" $(find_rpool)"
232fi
233
234export __ZFS_POOL_EXCLUDE="$KEEP"
235export KEEP="^$(echo $KEEP | sed 's/ /$|^/g')\$"
236
237[[ -z $runfiles ]] && runfiles=$(find_runfile)
238[[ -z $runfiles ]] && fail "Couldn't determine distro"
239
240. $STF_SUITE/include/default.cfg
241
242num_disks=$(echo $DISKS | awk '{print NF}')
243(( num_disks < 3 )) && fail "Not enough disks to run ZFS Test Suite"
244
245# Ensure user has only basic privileges.
246ppriv -s EIP=basic -e $runner -c $runfiles $xargs
247ret=$?
248
249rm -rf $PATHDIR || fail "Couldn't remove $PATHDIR"
250
251exit $ret
252