xref: /freebsd/sys/contrib/openzfs/scripts/zfs-tests.sh (revision 2c48331d28f16c0efce5a72a81e7d71668c4a158)
1eda14cbcSMatt Macy#!/bin/sh
2eda14cbcSMatt Macy#
3eda14cbcSMatt Macy# CDDL HEADER START
4eda14cbcSMatt Macy#
5eda14cbcSMatt Macy# The contents of this file are subject to the terms of the
6eda14cbcSMatt Macy# Common Development and Distribution License, Version 1.0 only
7eda14cbcSMatt Macy# (the "License").  You may not use this file except in compliance
8eda14cbcSMatt Macy# with the License.
9eda14cbcSMatt Macy#
10eda14cbcSMatt Macy# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11eda14cbcSMatt Macy# or http://www.opensolaris.org/os/licensing.
12eda14cbcSMatt Macy# See the License for the specific language governing permissions
13eda14cbcSMatt Macy# and limitations under the License.
14eda14cbcSMatt Macy#
15eda14cbcSMatt Macy# When distributing Covered Code, include this CDDL HEADER in each
16eda14cbcSMatt Macy# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17eda14cbcSMatt Macy# If applicable, add the following below this CDDL HEADER, with the
18eda14cbcSMatt Macy# fields enclosed by brackets "[]" replaced with your own identifying
19eda14cbcSMatt Macy# information: Portions Copyright [yyyy] [name of copyright owner]
20eda14cbcSMatt Macy#
21eda14cbcSMatt Macy# CDDL HEADER END
22eda14cbcSMatt Macy#
23eda14cbcSMatt Macy
24eda14cbcSMatt MacyBASE_DIR=$(dirname "$0")
25eda14cbcSMatt MacySCRIPT_COMMON=common.sh
26eda14cbcSMatt Macyif [ -f "${BASE_DIR}/${SCRIPT_COMMON}" ]; then
27eda14cbcSMatt Macy. "${BASE_DIR}/${SCRIPT_COMMON}"
28eda14cbcSMatt Macyelse
29eda14cbcSMatt Macyecho "Missing helper script ${SCRIPT_COMMON}" && exit 1
30eda14cbcSMatt Macyfi
31eda14cbcSMatt Macy
32eda14cbcSMatt MacyPROG=zfs-tests.sh
33eda14cbcSMatt MacyVERBOSE="no"
34eda14cbcSMatt MacyQUIET=""
35eda14cbcSMatt MacyCLEANUP="yes"
36eda14cbcSMatt MacyCLEANUPALL="no"
37eda14cbcSMatt MacyLOOPBACK="yes"
38eda14cbcSMatt MacySTACK_TRACER="no"
39eda14cbcSMatt MacyFILESIZE="4G"
40eda14cbcSMatt MacyDEFAULT_RUNFILES="common.run,$(uname | tr '[:upper:]' '[:lower:]').run"
41eda14cbcSMatt MacyRUNFILES=${RUNFILES:-$DEFAULT_RUNFILES}
42eda14cbcSMatt MacyFILEDIR=${FILEDIR:-/var/tmp}
43eda14cbcSMatt MacyDISKS=${DISKS:-""}
44eda14cbcSMatt MacySINGLETEST=""
45eda14cbcSMatt MacySINGLETESTUSER="root"
46eda14cbcSMatt MacyTAGS=""
47eda14cbcSMatt MacyITERATIONS=1
48eda14cbcSMatt MacyZFS_DBGMSG="$STF_SUITE/callbacks/zfs_dbgmsg.ksh"
49eda14cbcSMatt MacyZFS_DMESG="$STF_SUITE/callbacks/zfs_dmesg.ksh"
50eda14cbcSMatt MacyUNAME=$(uname -s)
51eda14cbcSMatt Macy
52eda14cbcSMatt Macy# Override some defaults if on FreeBSD
53eda14cbcSMatt Macyif [ "$UNAME" = "FreeBSD" ] ; then
54eda14cbcSMatt Macy	TESTFAIL_CALLBACKS=${TESTFAIL_CALLBACKS:-"$ZFS_DMESG"}
55eda14cbcSMatt Macy	LOSETUP=/sbin/mdconfig
56eda14cbcSMatt Macy	DMSETUP=/sbin/gpart
57eda14cbcSMatt Macyelse
58eda14cbcSMatt Macy	ZFS_MMP="$STF_SUITE/callbacks/zfs_mmp.ksh"
59eda14cbcSMatt Macy	TESTFAIL_CALLBACKS=${TESTFAIL_CALLBACKS:-"$ZFS_DBGMSG:$ZFS_DMESG:$ZFS_MMP"}
60eda14cbcSMatt Macy	LOSETUP=${LOSETUP:-/sbin/losetup}
61eda14cbcSMatt Macy	DMSETUP=${DMSETUP:-/sbin/dmsetup}
62eda14cbcSMatt Macyfi
63eda14cbcSMatt Macy
64eda14cbcSMatt Macy#
65eda14cbcSMatt Macy# Log an informational message when additional verbosity is enabled.
66eda14cbcSMatt Macy#
67eda14cbcSMatt Macymsg() {
68eda14cbcSMatt Macy	if [ "$VERBOSE" = "yes" ]; then
69eda14cbcSMatt Macy		echo "$@"
70eda14cbcSMatt Macy	fi
71eda14cbcSMatt Macy}
72eda14cbcSMatt Macy
73eda14cbcSMatt Macy#
74eda14cbcSMatt Macy# Log a failure message, cleanup, and return an error.
75eda14cbcSMatt Macy#
76eda14cbcSMatt Macyfail() {
77eda14cbcSMatt Macy	echo "$PROG: $1" >&2
78eda14cbcSMatt Macy	cleanup
79eda14cbcSMatt Macy	exit 1
80eda14cbcSMatt Macy}
81eda14cbcSMatt Macy
82eda14cbcSMatt Macycleanup_freebsd_loopback() {
83eda14cbcSMatt Macy	for TEST_LOOPBACK in ${LOOPBACKS}; do
84eda14cbcSMatt Macy		if [ -c "/dev/${TEST_LOOPBACK}" ]; then
85eda14cbcSMatt Macy			sudo "${LOSETUP}" -d -u "${TEST_LOOPBACK}" ||
86eda14cbcSMatt Macy			    echo "Failed to destroy: ${TEST_LOOPBACK}"
87eda14cbcSMatt Macy		fi
88eda14cbcSMatt Macy	done
89eda14cbcSMatt Macy}
90eda14cbcSMatt Macy
91eda14cbcSMatt Macycleanup_linux_loopback() {
92eda14cbcSMatt Macy	for TEST_LOOPBACK in ${LOOPBACKS}; do
93eda14cbcSMatt Macy		LOOP_DEV=$(basename "$TEST_LOOPBACK")
94eda14cbcSMatt Macy		DM_DEV=$(sudo "${DMSETUP}" ls 2>/dev/null | \
95eda14cbcSMatt Macy		    grep "${LOOP_DEV}" | cut -f1)
96eda14cbcSMatt Macy
97eda14cbcSMatt Macy		if [ -n "$DM_DEV" ]; then
98eda14cbcSMatt Macy			sudo "${DMSETUP}" remove "${DM_DEV}" ||
99eda14cbcSMatt Macy			    echo "Failed to remove: ${DM_DEV}"
100eda14cbcSMatt Macy		fi
101eda14cbcSMatt Macy
102eda14cbcSMatt Macy		if [ -n "${TEST_LOOPBACK}" ]; then
103eda14cbcSMatt Macy			sudo "${LOSETUP}" -d "${TEST_LOOPBACK}" ||
104eda14cbcSMatt Macy			    echo "Failed to remove: ${TEST_LOOPBACK}"
105eda14cbcSMatt Macy		fi
106eda14cbcSMatt Macy	done
107eda14cbcSMatt Macy}
108eda14cbcSMatt Macy
109eda14cbcSMatt Macy#
110eda14cbcSMatt Macy# Attempt to remove loopback devices and files which where created earlier
111eda14cbcSMatt Macy# by this script to run the test framework.  The '-k' option may be passed
112eda14cbcSMatt Macy# to the script to suppress cleanup for debugging purposes.
113eda14cbcSMatt Macy#
114eda14cbcSMatt Macycleanup() {
115eda14cbcSMatt Macy	if [ "$CLEANUP" = "no" ]; then
116eda14cbcSMatt Macy		return 0
117eda14cbcSMatt Macy	fi
118eda14cbcSMatt Macy
119eda14cbcSMatt Macy
120eda14cbcSMatt Macy	if [ "$LOOPBACK" = "yes" ]; then
121eda14cbcSMatt Macy		if [ "$UNAME" = "FreeBSD" ] ; then
122eda14cbcSMatt Macy			cleanup_freebsd_loopback
123eda14cbcSMatt Macy		else
124eda14cbcSMatt Macy			cleanup_linux_loopback
125eda14cbcSMatt Macy		fi
126eda14cbcSMatt Macy	fi
127eda14cbcSMatt Macy
128eda14cbcSMatt Macy	for TEST_FILE in ${FILES}; do
129eda14cbcSMatt Macy		rm -f "${TEST_FILE}" >/dev/null 2>&1
130eda14cbcSMatt Macy	done
131eda14cbcSMatt Macy
132eda14cbcSMatt Macy	if [ "$STF_PATH_REMOVE" = "yes" ] && [ -d "$STF_PATH" ]; then
133eda14cbcSMatt Macy		rm -Rf "$STF_PATH"
134eda14cbcSMatt Macy	fi
135eda14cbcSMatt Macy}
136eda14cbcSMatt Macytrap cleanup EXIT
137eda14cbcSMatt Macy
138eda14cbcSMatt Macy#
139eda14cbcSMatt Macy# Attempt to remove all testpools (testpool.XXX), unopened dm devices,
140eda14cbcSMatt Macy# loopback devices, and files.  This is a useful way to cleanup a previous
141eda14cbcSMatt Macy# test run failure which has left the system in an unknown state.  This can
142eda14cbcSMatt Macy# be dangerous and should only be used in a dedicated test environment.
143eda14cbcSMatt Macy#
144eda14cbcSMatt Macycleanup_all() {
145eda14cbcSMatt Macy	TEST_POOLS=$(sudo "$ZPOOL" list -H -o name | grep testpool)
146eda14cbcSMatt Macy	if [ "$UNAME" = "FreeBSD" ] ; then
147eda14cbcSMatt Macy		TEST_LOOPBACKS=$(sudo "${LOSETUP}" -l)
148eda14cbcSMatt Macy	else
149eda14cbcSMatt Macy		TEST_LOOPBACKS=$(sudo "${LOSETUP}" -a|grep file-vdev|cut -f1 -d:)
150eda14cbcSMatt Macy	fi
151eda14cbcSMatt Macy	TEST_FILES=$(ls /var/tmp/file-vdev* 2>/dev/null)
152eda14cbcSMatt Macy
153eda14cbcSMatt Macy	msg
154eda14cbcSMatt Macy	msg "--- Cleanup ---"
155eda14cbcSMatt Macy	msg "Removing pool(s):     $(echo "${TEST_POOLS}" | tr '\n' ' ')"
156eda14cbcSMatt Macy	for TEST_POOL in $TEST_POOLS; do
157eda14cbcSMatt Macy		sudo "$ZPOOL" destroy "${TEST_POOL}"
158eda14cbcSMatt Macy	done
159eda14cbcSMatt Macy
160eda14cbcSMatt Macy	if [ "$UNAME" != "FreeBSD" ] ; then
161eda14cbcSMatt Macy		msg "Removing dm(s):       $(sudo "${DMSETUP}" ls |
162eda14cbcSMatt Macy		    grep loop | tr '\n' ' ')"
163eda14cbcSMatt Macy		sudo "${DMSETUP}" remove_all
164eda14cbcSMatt Macy	fi
165eda14cbcSMatt Macy
166eda14cbcSMatt Macy	msg "Removing loopback(s): $(echo "${TEST_LOOPBACKS}" | tr '\n' ' ')"
167eda14cbcSMatt Macy	for TEST_LOOPBACK in $TEST_LOOPBACKS; do
168eda14cbcSMatt Macy		if [ "$UNAME" = "FreeBSD" ] ; then
169eda14cbcSMatt Macy			sudo "${LOSETUP}" -d -u "${TEST_LOOPBACK}"
170eda14cbcSMatt Macy		else
171eda14cbcSMatt Macy			sudo "${LOSETUP}" -d "${TEST_LOOPBACK}"
172eda14cbcSMatt Macy		fi
173eda14cbcSMatt Macy	done
174eda14cbcSMatt Macy
175eda14cbcSMatt Macy	msg "Removing files(s):    $(echo "${TEST_FILES}" | tr '\n' ' ')"
176eda14cbcSMatt Macy	for TEST_FILE in $TEST_FILES; do
177eda14cbcSMatt Macy		sudo rm -f "${TEST_FILE}"
178eda14cbcSMatt Macy	done
179eda14cbcSMatt Macy}
180eda14cbcSMatt Macy
181eda14cbcSMatt Macy#
182eda14cbcSMatt Macy# Takes a name as the only arguments and looks for the following variations
183eda14cbcSMatt Macy# on that name.  If one is found it is returned.
184eda14cbcSMatt Macy#
185eda14cbcSMatt Macy# $RUNFILE_DIR/<name>
186eda14cbcSMatt Macy# $RUNFILE_DIR/<name>.run
187eda14cbcSMatt Macy# <name>
188eda14cbcSMatt Macy# <name>.run
189eda14cbcSMatt Macy#
190eda14cbcSMatt Macyfind_runfile() {
191eda14cbcSMatt Macy	NAME=$1
192eda14cbcSMatt Macy	RESULT=""
193eda14cbcSMatt Macy
194eda14cbcSMatt Macy	if [ -f "$RUNFILE_DIR/$NAME" ]; then
195eda14cbcSMatt Macy		RESULT="$RUNFILE_DIR/$NAME"
196eda14cbcSMatt Macy	elif [ -f "$RUNFILE_DIR/$NAME.run" ]; then
197eda14cbcSMatt Macy		RESULT="$RUNFILE_DIR/$NAME.run"
198eda14cbcSMatt Macy	elif [ -f "$NAME" ]; then
199eda14cbcSMatt Macy		RESULT="$NAME"
200eda14cbcSMatt Macy	elif [ -f "$NAME.run" ]; then
201eda14cbcSMatt Macy		RESULT="$NAME.run"
202eda14cbcSMatt Macy	fi
203eda14cbcSMatt Macy
204eda14cbcSMatt Macy	echo "$RESULT"
205eda14cbcSMatt Macy}
206eda14cbcSMatt Macy
207eda14cbcSMatt Macy#
208eda14cbcSMatt Macy# Symlink file if it appears under any of the given paths.
209eda14cbcSMatt Macy#
210eda14cbcSMatt Macycreate_links() {
211eda14cbcSMatt Macy	dir_list="$1"
212eda14cbcSMatt Macy	file_list="$2"
213eda14cbcSMatt Macy
214eda14cbcSMatt Macy	[ -n "$STF_PATH" ] || fail "STF_PATH wasn't correctly set"
215eda14cbcSMatt Macy
216eda14cbcSMatt Macy	for i in $file_list; do
217eda14cbcSMatt Macy		for j in $dir_list; do
218eda14cbcSMatt Macy			[ ! -e "$STF_PATH/$i" ] || continue
219eda14cbcSMatt Macy
220eda14cbcSMatt Macy			if [ ! -d "$j/$i" ] && [ -e "$j/$i" ]; then
221eda14cbcSMatt Macy				ln -sf "$j/$i" "$STF_PATH/$i" || \
222eda14cbcSMatt Macy				    fail "Couldn't link $i"
223eda14cbcSMatt Macy				break
224eda14cbcSMatt Macy			fi
225eda14cbcSMatt Macy		done
226eda14cbcSMatt Macy
227eda14cbcSMatt Macy		[ ! -e "$STF_PATH/$i" ] && \
228eda14cbcSMatt Macy		    STF_MISSING_BIN="$STF_MISSING_BIN $i"
229eda14cbcSMatt Macy	done
230eda14cbcSMatt Macy	STF_MISSING_BIN=${STF_MISSING_BIN# }
231eda14cbcSMatt Macy}
232eda14cbcSMatt Macy
233eda14cbcSMatt Macy#
234eda14cbcSMatt Macy# Constrain the path to limit the available binaries to a known set.
235eda14cbcSMatt Macy# When running in-tree a top level ./bin/ directory is created for
236eda14cbcSMatt Macy# convenience, otherwise a temporary directory is used.
237eda14cbcSMatt Macy#
238eda14cbcSMatt Macyconstrain_path() {
239eda14cbcSMatt Macy	. "$STF_SUITE/include/commands.cfg"
240eda14cbcSMatt Macy
241eda14cbcSMatt Macy	# On FreeBSD, base system zfs utils are in /sbin and OpenZFS utils
242eda14cbcSMatt Macy	# install to /usr/local/sbin. To avoid testing the wrong utils we
243eda14cbcSMatt Macy	# need /usr/local to come before / in the path search order.
244eda14cbcSMatt Macy	SYSTEM_DIRS="/usr/local/bin /usr/local/sbin"
245eda14cbcSMatt Macy	SYSTEM_DIRS="$SYSTEM_DIRS /usr/bin /usr/sbin /bin /sbin"
246eda14cbcSMatt Macy
247eda14cbcSMatt Macy	if [ "$INTREE" = "yes" ]; then
248eda14cbcSMatt Macy		# Constrained path set to ./zfs/bin/
249eda14cbcSMatt Macy		STF_PATH="$BIN_DIR"
250eda14cbcSMatt Macy		STF_PATH_REMOVE="no"
251eda14cbcSMatt Macy		STF_MISSING_BIN=""
252eda14cbcSMatt Macy		if [ ! -d "$STF_PATH" ]; then
253eda14cbcSMatt Macy			mkdir "$STF_PATH"
254eda14cbcSMatt Macy			chmod 755 "$STF_PATH" || fail "Couldn't chmod $STF_PATH"
255eda14cbcSMatt Macy		fi
256eda14cbcSMatt Macy
257eda14cbcSMatt Macy		# Special case links for standard zfs utilities
258eda14cbcSMatt Macy		DIRS="$(find "$CMD_DIR" -type d \( ! -name .deps -a \
259eda14cbcSMatt Macy		    ! -name .libs \) -print | tr '\n' ' ')"
260eda14cbcSMatt Macy		create_links "$DIRS" "$ZFS_FILES"
261eda14cbcSMatt Macy
262eda14cbcSMatt Macy		# Special case links for zfs test suite utilities
263eda14cbcSMatt Macy		DIRS="$(find "$STF_SUITE" -type d \( ! -name .deps -a \
264eda14cbcSMatt Macy		    ! -name .libs \) -print | tr '\n' ' ')"
265eda14cbcSMatt Macy		create_links "$DIRS" "$ZFSTEST_FILES"
266eda14cbcSMatt Macy	else
267eda14cbcSMatt Macy		# Constrained path set to /var/tmp/constrained_path.*
268eda14cbcSMatt Macy		SYSTEMDIR=${SYSTEMDIR:-/var/tmp/constrained_path.XXXX}
269eda14cbcSMatt Macy		STF_PATH=$(mktemp -d "$SYSTEMDIR")
270eda14cbcSMatt Macy		STF_PATH_REMOVE="yes"
271eda14cbcSMatt Macy		STF_MISSING_BIN=""
272eda14cbcSMatt Macy
273eda14cbcSMatt Macy		chmod 755 "$STF_PATH" || fail "Couldn't chmod $STF_PATH"
274eda14cbcSMatt Macy
275eda14cbcSMatt Macy		# Special case links for standard zfs utilities
276eda14cbcSMatt Macy		create_links "$SYSTEM_DIRS" "$ZFS_FILES"
277eda14cbcSMatt Macy
278eda14cbcSMatt Macy		# Special case links for zfs test suite utilities
279eda14cbcSMatt Macy		create_links "$STF_SUITE/bin" "$ZFSTEST_FILES"
280eda14cbcSMatt Macy	fi
281eda14cbcSMatt Macy
282eda14cbcSMatt Macy	# Standard system utilities
283eda14cbcSMatt Macy	SYSTEM_FILES="$SYSTEM_FILES_COMMON"
284eda14cbcSMatt Macy	if [ "$UNAME" = "FreeBSD" ] ; then
285eda14cbcSMatt Macy		SYSTEM_FILES="$SYSTEM_FILES $SYSTEM_FILES_FREEBSD"
286eda14cbcSMatt Macy	else
287eda14cbcSMatt Macy		SYSTEM_FILES="$SYSTEM_FILES $SYSTEM_FILES_LINUX"
288eda14cbcSMatt Macy	fi
289eda14cbcSMatt Macy	create_links "$SYSTEM_DIRS" "$SYSTEM_FILES"
290eda14cbcSMatt Macy
291eda14cbcSMatt Macy	# Exceptions
292eda14cbcSMatt Macy	ln -fs "$STF_PATH/awk" "$STF_PATH/nawk"
293eda14cbcSMatt Macy	if [ "$UNAME" = "Linux" ] ; then
294eda14cbcSMatt Macy		ln -fs /sbin/fsck.ext4 "$STF_PATH/fsck"
295eda14cbcSMatt Macy		ln -fs /sbin/mkfs.ext4 "$STF_PATH/newfs"
296eda14cbcSMatt Macy		ln -fs "$STF_PATH/gzip" "$STF_PATH/compress"
297eda14cbcSMatt Macy		ln -fs "$STF_PATH/gunzip" "$STF_PATH/uncompress"
298eda14cbcSMatt Macy		ln -fs "$STF_PATH/exportfs" "$STF_PATH/share"
299eda14cbcSMatt Macy		ln -fs "$STF_PATH/exportfs" "$STF_PATH/unshare"
300eda14cbcSMatt Macy	elif [ "$UNAME" = "FreeBSD" ] ; then
301eda14cbcSMatt Macy		ln -fs /usr/local/bin/ksh93 "$STF_PATH/ksh"
302eda14cbcSMatt Macy	fi
303eda14cbcSMatt Macy}
304eda14cbcSMatt Macy
305eda14cbcSMatt Macy#
306eda14cbcSMatt Macy# Output a useful usage message.
307eda14cbcSMatt Macy#
308eda14cbcSMatt Macyusage() {
309eda14cbcSMatt Macycat << EOF
310eda14cbcSMatt MacyUSAGE:
311*2c48331dSMatt Macy$0 [-hvqxkfS] [-s SIZE] [-r RUNFILES] [-t PATH] [-u USER]
312eda14cbcSMatt Macy
313eda14cbcSMatt MacyDESCRIPTION:
314eda14cbcSMatt Macy	ZFS Test Suite launch script
315eda14cbcSMatt Macy
316eda14cbcSMatt MacyOPTIONS:
317eda14cbcSMatt Macy	-h          Show this message
318eda14cbcSMatt Macy	-v          Verbose zfs-tests.sh output
319eda14cbcSMatt Macy	-q          Quiet test-runner output
320eda14cbcSMatt Macy	-x          Remove all testpools, dm, lo, and files (unsafe)
321eda14cbcSMatt Macy	-k          Disable cleanup after test failure
322eda14cbcSMatt Macy	-f          Use files only, disables block device tests
323eda14cbcSMatt Macy	-S          Enable stack tracer (negative performance impact)
324eda14cbcSMatt Macy	-c          Only create and populate constrained path
325eda14cbcSMatt Macy	-n NFSFILE  Use the nfsfile to determine the NFS configuration
326eda14cbcSMatt Macy	-I NUM      Number of iterations
327eda14cbcSMatt Macy	-d DIR      Use DIR for files and loopback devices
328eda14cbcSMatt Macy	-s SIZE     Use vdevs of SIZE (default: 4G)
329eda14cbcSMatt Macy	-r RUNFILES Run tests in RUNFILES (default: ${DEFAULT_RUNFILES})
330eda14cbcSMatt Macy	-t PATH     Run single test at PATH relative to test suite
331eda14cbcSMatt Macy	-T TAGS     Comma separated list of tags (default: 'functional')
332eda14cbcSMatt Macy	-u USER     Run single test as USER (default: root)
333eda14cbcSMatt Macy
334eda14cbcSMatt MacyEXAMPLES:
335eda14cbcSMatt Macy# Run the default (linux) suite of tests and output the configuration used.
336eda14cbcSMatt Macy$0 -v
337eda14cbcSMatt Macy
338eda14cbcSMatt Macy# Run a smaller suite of tests designed to run more quickly.
339eda14cbcSMatt Macy$0 -r linux-fast
340eda14cbcSMatt Macy
341eda14cbcSMatt Macy# Run a single test
342eda14cbcSMatt Macy$0 -t tests/functional/cli_root/zfs_bookmark/zfs_bookmark_cliargs.ksh
343eda14cbcSMatt Macy
344eda14cbcSMatt Macy# Cleanup a previous run of the test suite prior to testing, run the
345eda14cbcSMatt Macy# default (linux) suite of tests and perform no cleanup on exit.
346eda14cbcSMatt Macy$0 -x
347eda14cbcSMatt Macy
348eda14cbcSMatt MacyEOF
349eda14cbcSMatt Macy}
350eda14cbcSMatt Macy
351eda14cbcSMatt Macywhile getopts 'hvqxkfScn:d:s:r:?t:T:u:I:' OPTION; do
352eda14cbcSMatt Macy	case $OPTION in
353eda14cbcSMatt Macy	h)
354eda14cbcSMatt Macy		usage
355eda14cbcSMatt Macy		exit 1
356eda14cbcSMatt Macy		;;
357eda14cbcSMatt Macy	v)
358eda14cbcSMatt Macy		# shellcheck disable=SC2034
359eda14cbcSMatt Macy		VERBOSE="yes"
360eda14cbcSMatt Macy		;;
361eda14cbcSMatt Macy	q)
362eda14cbcSMatt Macy		QUIET="yes"
363eda14cbcSMatt Macy		;;
364eda14cbcSMatt Macy	x)
365eda14cbcSMatt Macy		CLEANUPALL="yes"
366eda14cbcSMatt Macy		;;
367eda14cbcSMatt Macy	k)
368eda14cbcSMatt Macy		CLEANUP="no"
369eda14cbcSMatt Macy		;;
370eda14cbcSMatt Macy	f)
371eda14cbcSMatt Macy		LOOPBACK="no"
372eda14cbcSMatt Macy		;;
373eda14cbcSMatt Macy	S)
374eda14cbcSMatt Macy		STACK_TRACER="yes"
375eda14cbcSMatt Macy		;;
376eda14cbcSMatt Macy	c)
377eda14cbcSMatt Macy		constrain_path
378eda14cbcSMatt Macy		exit
379eda14cbcSMatt Macy		;;
380eda14cbcSMatt Macy	n)
381eda14cbcSMatt Macy		nfsfile=$OPTARG
382eda14cbcSMatt Macy		[ -f "$nfsfile" ] || fail "Cannot read file: $nfsfile"
383eda14cbcSMatt Macy		export NFS=1
384eda14cbcSMatt Macy		. "$nfsfile"
385eda14cbcSMatt Macy		;;
386eda14cbcSMatt Macy	d)
387eda14cbcSMatt Macy		FILEDIR="$OPTARG"
388eda14cbcSMatt Macy		;;
389eda14cbcSMatt Macy	I)
390eda14cbcSMatt Macy		ITERATIONS="$OPTARG"
391eda14cbcSMatt Macy		if [ "$ITERATIONS" -le 0 ]; then
392eda14cbcSMatt Macy			fail "Iterations must be greater than 0."
393eda14cbcSMatt Macy		fi
394eda14cbcSMatt Macy		;;
395eda14cbcSMatt Macy	s)
396eda14cbcSMatt Macy		FILESIZE="$OPTARG"
397eda14cbcSMatt Macy		;;
398eda14cbcSMatt Macy	r)
399eda14cbcSMatt Macy		RUNFILES="$OPTARG"
400eda14cbcSMatt Macy		;;
401eda14cbcSMatt Macy	t)
402eda14cbcSMatt Macy		if [ -n "$SINGLETEST" ]; then
403eda14cbcSMatt Macy			fail "-t can only be provided once."
404eda14cbcSMatt Macy		fi
405eda14cbcSMatt Macy		SINGLETEST="$OPTARG"
406eda14cbcSMatt Macy		;;
407eda14cbcSMatt Macy	T)
408eda14cbcSMatt Macy		TAGS="$OPTARG"
409eda14cbcSMatt Macy		;;
410eda14cbcSMatt Macy	u)
411eda14cbcSMatt Macy		SINGLETESTUSER="$OPTARG"
412eda14cbcSMatt Macy		;;
413eda14cbcSMatt Macy	?)
414eda14cbcSMatt Macy		usage
415eda14cbcSMatt Macy		exit
416eda14cbcSMatt Macy		;;
417eda14cbcSMatt Macy	esac
418eda14cbcSMatt Macydone
419eda14cbcSMatt Macy
420eda14cbcSMatt Macyshift $((OPTIND-1))
421eda14cbcSMatt Macy
422eda14cbcSMatt MacyFILES=${FILES:-"$FILEDIR/file-vdev0 $FILEDIR/file-vdev1 $FILEDIR/file-vdev2"}
423eda14cbcSMatt MacyLOOPBACKS=${LOOPBACKS:-""}
424eda14cbcSMatt Macy
425eda14cbcSMatt Macyif [ -n "$SINGLETEST" ]; then
426eda14cbcSMatt Macy	if [ -n "$TAGS" ]; then
427eda14cbcSMatt Macy		fail "-t and -T are mutually exclusive."
428eda14cbcSMatt Macy	fi
429eda14cbcSMatt Macy	RUNFILE_DIR="/var/tmp"
430eda14cbcSMatt Macy	RUNFILES="zfs-tests.$$.run"
431eda14cbcSMatt Macy	SINGLEQUIET="False"
432eda14cbcSMatt Macy
433eda14cbcSMatt Macy	if [ -n "$QUIET" ]; then
434eda14cbcSMatt Macy		SINGLEQUIET="True"
435eda14cbcSMatt Macy	fi
436eda14cbcSMatt Macy
437eda14cbcSMatt Macy	cat >$RUNFILE_DIR/$RUNFILES << EOF
438eda14cbcSMatt Macy[DEFAULT]
439eda14cbcSMatt Macypre =
440eda14cbcSMatt Macyquiet = $SINGLEQUIET
441eda14cbcSMatt Macypre_user = root
442eda14cbcSMatt Macyuser = $SINGLETESTUSER
443eda14cbcSMatt Macytimeout = 600
444eda14cbcSMatt Macypost_user = root
445eda14cbcSMatt Macypost =
446eda14cbcSMatt Macyoutputdir = /var/tmp/test_results
447eda14cbcSMatt MacyEOF
448eda14cbcSMatt Macy	SINGLETESTDIR=$(dirname "$SINGLETEST")
449eda14cbcSMatt Macy	SINGLETESTFILE=$(basename "$SINGLETEST")
450eda14cbcSMatt Macy	SETUPSCRIPT=
451eda14cbcSMatt Macy	CLEANUPSCRIPT=
452eda14cbcSMatt Macy
453eda14cbcSMatt Macy	if [ -f "$STF_SUITE/$SINGLETESTDIR/setup.ksh" ]; then
454eda14cbcSMatt Macy		SETUPSCRIPT="setup"
455eda14cbcSMatt Macy	fi
456eda14cbcSMatt Macy
457eda14cbcSMatt Macy	if [ -f "$STF_SUITE/$SINGLETESTDIR/cleanup.ksh" ]; then
458eda14cbcSMatt Macy		CLEANUPSCRIPT="cleanup"
459eda14cbcSMatt Macy	fi
460eda14cbcSMatt Macy
461eda14cbcSMatt Macy	cat >>$RUNFILE_DIR/$RUNFILES << EOF
462eda14cbcSMatt Macy
463eda14cbcSMatt Macy[$SINGLETESTDIR]
464eda14cbcSMatt Macytests = ['$SINGLETESTFILE']
465eda14cbcSMatt Macypre = $SETUPSCRIPT
466eda14cbcSMatt Macypost = $CLEANUPSCRIPT
467eda14cbcSMatt Macytags = ['functional']
468eda14cbcSMatt MacyEOF
469eda14cbcSMatt Macyfi
470eda14cbcSMatt Macy
471eda14cbcSMatt Macy#
472eda14cbcSMatt Macy# Use default tag if none was specified
473eda14cbcSMatt Macy#
474eda14cbcSMatt MacyTAGS=${TAGS:='functional'}
475eda14cbcSMatt Macy
476eda14cbcSMatt Macy#
477eda14cbcSMatt Macy# Attempt to locate the runfiles describing the test workload.
478eda14cbcSMatt Macy#
479eda14cbcSMatt MacyR=""
480eda14cbcSMatt MacyIFS=,
481eda14cbcSMatt Macyfor RUNFILE in $RUNFILES; do
482eda14cbcSMatt Macy	if [ -n "$RUNFILE" ]; then
483eda14cbcSMatt Macy		SAVED_RUNFILE="$RUNFILE"
484eda14cbcSMatt Macy		RUNFILE=$(find_runfile "$RUNFILE")
485eda14cbcSMatt Macy		[ -z "$RUNFILE" ] && fail "Cannot find runfile: $SAVED_RUNFILE"
486eda14cbcSMatt Macy		R="$R,$RUNFILE"
487eda14cbcSMatt Macy	fi
488eda14cbcSMatt Macy
489eda14cbcSMatt Macy	if [ ! -r "$RUNFILE" ]; then
490eda14cbcSMatt Macy		fail "Cannot read runfile: $RUNFILE"
491eda14cbcSMatt Macy	fi
492eda14cbcSMatt Macydone
493eda14cbcSMatt Macyunset IFS
494eda14cbcSMatt MacyRUNFILES=${R#,}
495eda14cbcSMatt Macy
496eda14cbcSMatt Macy#
497eda14cbcSMatt Macy# This script should not be run as root.  Instead the test user, which may
498eda14cbcSMatt Macy# be a normal user account, needs to be configured such that it can
499eda14cbcSMatt Macy# run commands via sudo passwordlessly.
500eda14cbcSMatt Macy#
501eda14cbcSMatt Macyif [ "$(id -u)" = "0" ]; then
502eda14cbcSMatt Macy	fail "This script must not be run as root."
503eda14cbcSMatt Macyfi
504eda14cbcSMatt Macy
505eda14cbcSMatt Macyif [ "$(sudo whoami)" != "root" ]; then
506eda14cbcSMatt Macy	fail "Passwordless sudo access required."
507eda14cbcSMatt Macyfi
508eda14cbcSMatt Macy
509eda14cbcSMatt Macy#
510eda14cbcSMatt Macy# Constrain the available binaries to a known set.
511eda14cbcSMatt Macy#
512eda14cbcSMatt Macyconstrain_path
513eda14cbcSMatt Macy
514eda14cbcSMatt Macy#
515eda14cbcSMatt Macy# Check if ksh exists
516eda14cbcSMatt Macy#
517eda14cbcSMatt Macyif [ "$UNAME" = "FreeBSD" ]; then
518eda14cbcSMatt Macy	sudo ln -fs /usr/local/bin/ksh93 /bin/ksh
519eda14cbcSMatt Macyfi
520eda14cbcSMatt Macy[ -e "$STF_PATH/ksh" ] || fail "This test suite requires ksh."
521eda14cbcSMatt Macy[ -e "$STF_SUITE/include/default.cfg" ] || fail \
522eda14cbcSMatt Macy    "Missing $STF_SUITE/include/default.cfg file."
523eda14cbcSMatt Macy
524eda14cbcSMatt Macy#
525eda14cbcSMatt Macy# Verify the ZFS module stack is loaded.
526eda14cbcSMatt Macy#
527eda14cbcSMatt Macyif [ "$STACK_TRACER" = "yes" ]; then
528eda14cbcSMatt Macy	sudo "${ZFS_SH}" -S >/dev/null 2>&1
529eda14cbcSMatt Macyelse
530eda14cbcSMatt Macy	sudo "${ZFS_SH}" >/dev/null 2>&1
531eda14cbcSMatt Macyfi
532eda14cbcSMatt Macy
533eda14cbcSMatt Macy#
534eda14cbcSMatt Macy# Attempt to cleanup all previous state for a new test run.
535eda14cbcSMatt Macy#
536eda14cbcSMatt Macyif [ "$CLEANUPALL" = "yes" ]; then
537eda14cbcSMatt Macy	cleanup_all
538eda14cbcSMatt Macyfi
539eda14cbcSMatt Macy
540eda14cbcSMatt Macy#
541eda14cbcSMatt Macy# By default preserve any existing pools
542eda14cbcSMatt Macy# NOTE: Since 'zpool list' outputs a newline-delimited list convert $KEEP from
543eda14cbcSMatt Macy# space-delimited to newline-delimited.
544eda14cbcSMatt Macy#
545eda14cbcSMatt Macyif [ -z "${KEEP}" ]; then
546eda14cbcSMatt Macy	KEEP="$(sudo "$ZPOOL" list -H -o name)"
547eda14cbcSMatt Macy	if [ -z "${KEEP}" ]; then
548eda14cbcSMatt Macy		KEEP="rpool"
549eda14cbcSMatt Macy	fi
550eda14cbcSMatt Macyelse
551eda14cbcSMatt Macy	KEEP="$(echo "$KEEP" | tr '[:blank:]' '\n')"
552eda14cbcSMatt Macyfi
553eda14cbcSMatt Macy
554eda14cbcSMatt Macy#
555eda14cbcSMatt Macy# NOTE: The following environment variables are undocumented
556eda14cbcSMatt Macy# and should be used for testing purposes only:
557eda14cbcSMatt Macy#
558eda14cbcSMatt Macy# __ZFS_POOL_EXCLUDE - don't iterate over the pools it lists
559eda14cbcSMatt Macy# __ZFS_POOL_RESTRICT - iterate only over the pools it lists
560eda14cbcSMatt Macy#
561eda14cbcSMatt Macy# See libzfs/libzfs_config.c for more information.
562eda14cbcSMatt Macy#
563eda14cbcSMatt Macyif [ "$UNAME" = "FreeBSD" ] ; then
564eda14cbcSMatt Macy	__ZFS_POOL_EXCLUDE="$(echo "$KEEP" | tr -s '\n' ' ')"
565eda14cbcSMatt Macyelse
566eda14cbcSMatt Macy	__ZFS_POOL_EXCLUDE="$(echo "$KEEP" | sed ':a;N;s/\n/ /g;ba')"
567eda14cbcSMatt Macyfi
568eda14cbcSMatt Macy
569eda14cbcSMatt Macy. "$STF_SUITE/include/default.cfg"
570eda14cbcSMatt Macy
571eda14cbcSMatt Macymsg
572eda14cbcSMatt Macymsg "--- Configuration ---"
573eda14cbcSMatt Macymsg "Runfiles:        $RUNFILES"
574eda14cbcSMatt Macymsg "STF_TOOLS:       $STF_TOOLS"
575eda14cbcSMatt Macymsg "STF_SUITE:       $STF_SUITE"
576eda14cbcSMatt Macymsg "STF_PATH:        $STF_PATH"
577eda14cbcSMatt Macy
578eda14cbcSMatt Macy#
579eda14cbcSMatt Macy# No DISKS have been provided so a basic file or loopback based devices
580eda14cbcSMatt Macy# must be created for the test suite to use.
581eda14cbcSMatt Macy#
582eda14cbcSMatt Macyif [ -z "${DISKS}" ]; then
583eda14cbcSMatt Macy	#
584eda14cbcSMatt Macy	# Create sparse files for the test suite.  These may be used
585eda14cbcSMatt Macy	# directory or have loopback devices layered on them.
586eda14cbcSMatt Macy	#
587eda14cbcSMatt Macy	for TEST_FILE in ${FILES}; do
588eda14cbcSMatt Macy		[ -f "$TEST_FILE" ] && fail "Failed file exists: ${TEST_FILE}"
589eda14cbcSMatt Macy		truncate -s "${FILESIZE}" "${TEST_FILE}" ||
590eda14cbcSMatt Macy		    fail "Failed creating: ${TEST_FILE} ($?)"
591eda14cbcSMatt Macy	done
592eda14cbcSMatt Macy
593eda14cbcSMatt Macy	#
594eda14cbcSMatt Macy	# If requested setup loopback devices backed by the sparse files.
595eda14cbcSMatt Macy	#
596eda14cbcSMatt Macy	if [ "$LOOPBACK" = "yes" ]; then
597eda14cbcSMatt Macy		test -x "$LOSETUP" || fail "$LOSETUP utility must be installed"
598eda14cbcSMatt Macy
599eda14cbcSMatt Macy		for TEST_FILE in ${FILES}; do
600eda14cbcSMatt Macy			if [ "$UNAME" = "FreeBSD" ] ; then
601eda14cbcSMatt Macy				MDDEVICE=$(sudo "${LOSETUP}" -a -t vnode -f "${TEST_FILE}")
602eda14cbcSMatt Macy				if [ -z "$MDDEVICE" ] ; then
603eda14cbcSMatt Macy					fail "Failed: ${TEST_FILE} -> loopback"
604eda14cbcSMatt Macy				fi
605eda14cbcSMatt Macy				DISKS="$DISKS $MDDEVICE"
606eda14cbcSMatt Macy				LOOPBACKS="$LOOPBACKS $MDDEVICE"
607eda14cbcSMatt Macy			else
608eda14cbcSMatt Macy				TEST_LOOPBACK=$(sudo "${LOSETUP}" -f)
609eda14cbcSMatt Macy				sudo "${LOSETUP}" "${TEST_LOOPBACK}" "${TEST_FILE}" ||
610eda14cbcSMatt Macy				    fail "Failed: ${TEST_FILE} -> ${TEST_LOOPBACK}"
611eda14cbcSMatt Macy				BASELOOPBACK=$(basename "$TEST_LOOPBACK")
612eda14cbcSMatt Macy				DISKS="$DISKS $BASELOOPBACK"
613eda14cbcSMatt Macy				LOOPBACKS="$LOOPBACKS $TEST_LOOPBACK"
614eda14cbcSMatt Macy			fi
615eda14cbcSMatt Macy		done
616eda14cbcSMatt Macy		DISKS=${DISKS# }
617eda14cbcSMatt Macy		LOOPBACKS=${LOOPBACKS# }
618eda14cbcSMatt Macy	else
619eda14cbcSMatt Macy		DISKS="$FILES"
620eda14cbcSMatt Macy	fi
621eda14cbcSMatt Macyfi
622eda14cbcSMatt Macy
623eda14cbcSMatt MacyNUM_DISKS=$(echo "${DISKS}" | awk '{print NF}')
624eda14cbcSMatt Macy[ "$NUM_DISKS" -lt 3 ] && fail "Not enough disks ($NUM_DISKS/3 minimum)"
625eda14cbcSMatt Macy
626eda14cbcSMatt Macy#
627eda14cbcSMatt Macy# Disable SELinux until the ZFS Test Suite has been updated accordingly.
628eda14cbcSMatt Macy#
629eda14cbcSMatt Macyif [ -x "$STF_PATH/setenforce" ]; then
630eda14cbcSMatt Macy	sudo setenforce permissive >/dev/null 2>&1
631eda14cbcSMatt Macyfi
632eda14cbcSMatt Macy
633eda14cbcSMatt Macy#
634eda14cbcSMatt Macy# Enable internal ZFS debug log and clear it.
635eda14cbcSMatt Macy#
636eda14cbcSMatt Macyif [ -e /sys/module/zfs/parameters/zfs_dbgmsg_enable ]; then
637eda14cbcSMatt Macy	sudo /bin/sh -c "echo 1 >/sys/module/zfs/parameters/zfs_dbgmsg_enable"
638eda14cbcSMatt Macy	sudo /bin/sh -c "echo 0 >/proc/spl/kstat/zfs/dbgmsg"
639eda14cbcSMatt Macyfi
640eda14cbcSMatt Macy
641eda14cbcSMatt Macymsg "FILEDIR:         $FILEDIR"
642eda14cbcSMatt Macymsg "FILES:           $FILES"
643eda14cbcSMatt Macymsg "LOOPBACKS:       $LOOPBACKS"
644eda14cbcSMatt Macymsg "DISKS:           $DISKS"
645eda14cbcSMatt Macymsg "NUM_DISKS:       $NUM_DISKS"
646eda14cbcSMatt Macymsg "FILESIZE:        $FILESIZE"
647eda14cbcSMatt Macymsg "ITERATIONS:      $ITERATIONS"
648eda14cbcSMatt Macymsg "TAGS:            $TAGS"
649eda14cbcSMatt Macymsg "STACK_TRACER:    $STACK_TRACER"
650eda14cbcSMatt Macymsg "Keep pool(s):    $KEEP"
651eda14cbcSMatt Macymsg "Missing util(s): $STF_MISSING_BIN"
652eda14cbcSMatt Macymsg ""
653eda14cbcSMatt Macy
654eda14cbcSMatt Macyexport STF_TOOLS
655eda14cbcSMatt Macyexport STF_SUITE
656eda14cbcSMatt Macyexport STF_PATH
657eda14cbcSMatt Macyexport DISKS
658eda14cbcSMatt Macyexport FILEDIR
659eda14cbcSMatt Macyexport KEEP
660eda14cbcSMatt Macyexport __ZFS_POOL_EXCLUDE
661eda14cbcSMatt Macyexport TESTFAIL_CALLBACKS
662eda14cbcSMatt Macyexport PATH=$STF_PATH
663eda14cbcSMatt Macy
664eda14cbcSMatt Macyif [ "$UNAME" = "FreeBSD" ] ; then
665eda14cbcSMatt Macy	mkdir -p "$FILEDIR" || true
666eda14cbcSMatt Macy	RESULTS_FILE=$(mktemp -u "${FILEDIR}/zts-results.XXXX")
667eda14cbcSMatt Macy	REPORT_FILE=$(mktemp -u "${FILEDIR}/zts-report.XXXX")
668eda14cbcSMatt Macyelse
669eda14cbcSMatt Macy	RESULTS_FILE=$(mktemp -u -t zts-results.XXXX -p "$FILEDIR")
670eda14cbcSMatt Macy	REPORT_FILE=$(mktemp -u -t zts-report.XXXX -p "$FILEDIR")
671eda14cbcSMatt Macyfi
672eda14cbcSMatt Macy
673eda14cbcSMatt Macy#
674eda14cbcSMatt Macy# Run all the tests as specified.
675eda14cbcSMatt Macy#
676eda14cbcSMatt Macymsg "${TEST_RUNNER} ${QUIET:+-q}" \
677eda14cbcSMatt Macy    "-c \"${RUNFILES}\"" \
678eda14cbcSMatt Macy    "-T \"${TAGS}\"" \
679eda14cbcSMatt Macy    "-i \"${STF_SUITE}\"" \
680eda14cbcSMatt Macy    "-I \"${ITERATIONS}\""
681eda14cbcSMatt Macy${TEST_RUNNER} ${QUIET:+-q} \
682eda14cbcSMatt Macy    -c "${RUNFILES}" \
683eda14cbcSMatt Macy    -T "${TAGS}" \
684eda14cbcSMatt Macy    -i "${STF_SUITE}" \
685eda14cbcSMatt Macy    -I "${ITERATIONS}" \
686eda14cbcSMatt Macy    2>&1 | tee "$RESULTS_FILE"
687eda14cbcSMatt Macy
688eda14cbcSMatt Macy#
689eda14cbcSMatt Macy# Analyze the results.
690eda14cbcSMatt Macy#
691eda14cbcSMatt Macy${ZTS_REPORT} "$RESULTS_FILE" >"$REPORT_FILE"
692eda14cbcSMatt MacyRESULT=$?
693eda14cbcSMatt Macycat "$REPORT_FILE"
694eda14cbcSMatt Macy
695eda14cbcSMatt MacyRESULTS_DIR=$(awk '/^Log directory/ { print $3 }' "$RESULTS_FILE")
696eda14cbcSMatt Macyif [ -d "$RESULTS_DIR" ]; then
697eda14cbcSMatt Macy	cat "$RESULTS_FILE" "$REPORT_FILE" >"$RESULTS_DIR/results"
698eda14cbcSMatt Macyfi
699eda14cbcSMatt Macy
700eda14cbcSMatt Macyrm -f "$RESULTS_FILE" "$REPORT_FILE"
701eda14cbcSMatt Macy
702eda14cbcSMatt Macyif [ -n "$SINGLETEST" ]; then
703eda14cbcSMatt Macy	rm -f "$RUNFILES" >/dev/null 2>&1
704eda14cbcSMatt Macyfi
705eda14cbcSMatt Macy
706eda14cbcSMatt Macyexit ${RESULT}
707