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