]> git.proxmox.com Git - mirror_zfs-debian.git/blob - scripts/zfs-tests.sh
1d959ae330c6ff33e4046abc0f2329613a7b5026
[mirror_zfs-debian.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 basedir=$(dirname "$0")
24
25 SCRIPT_COMMON=common.sh
26 if [ -f "${basedir}/${SCRIPT_COMMON}" ]; then
27 . "${basedir}/${SCRIPT_COMMON}"
28 else
29 echo "Missing helper script ${SCRIPT_COMMON}" && exit 1
30 fi
31
32 PROG=zfs-tests.sh
33 VERBOSE=
34 QUIET=
35 CLEANUP=1
36 CLEANUPALL=0
37 LOOPBACK=1
38 FILESIZE="4G"
39 RUNFILE=${RUNFILE:-"linux.run"}
40 FILEDIR=${FILEDIR:-/var/tmp}
41 DISKS=${DISKS:-""}
42 SINGLETEST=()
43 SINGLETESTUSER="root"
44 ZFS_DBGMSG="$STF_SUITE/callbacks/zfs_dbgmsg.ksh"
45 ZFS_DMESG="$STF_SUITE/callbacks/zfs_dmesg.ksh"
46 ZFS_MMP="$STF_SUITE/callbacks/zfs_mmp.ksh"
47 TESTFAIL_CALLBACKS=${TESTFAIL_CALLBACKS:-"$ZFS_DBGMSG:$ZFS_DMESG:$ZFS_MMP"}
48
49 #
50 # Attempt to remove loopback devices and files which where created earlier
51 # by this script to run the test framework. The '-k' option may be passed
52 # to the script to suppress cleanup for debugging purposes.
53 #
54 cleanup() {
55 if [ $CLEANUP -eq 0 ]; then
56 return 0
57 fi
58
59 if [ $LOOPBACK -eq 1 ]; then
60 for TEST_LOOPBACK in ${LOOPBACKS}; do
61 LOOP_DEV=$(basename "$TEST_LOOPBACK")
62 DM_DEV=$(sudo "${DMSETUP}" ls 2>/dev/null | \
63 grep "${LOOP_DEV}" | cut -f1)
64
65 if [ -n "$DM_DEV" ]; then
66 sudo "${DMSETUP}" remove "${DM_DEV}" ||
67 echo "Failed to remove: ${DM_DEV}"
68 fi
69
70 if [ -n "${TEST_LOOPBACK}" ]; then
71 sudo "${LOSETUP}" -d "${TEST_LOOPBACK}" ||
72 echo "Failed to remove: ${TEST_LOOPBACK}"
73 fi
74 done
75 fi
76
77 for TEST_FILE in ${FILES}; do
78 rm -f "${TEST_FILE}" &>/dev/null
79 done
80
81 # Preserve in-tree symlinks to aid debugging.
82 if [ -z "${INTREE}" ] && [ -d "$STF_PATH" ]; then
83 rm -Rf "$STF_PATH"
84 fi
85 }
86 trap cleanup EXIT
87
88 #
89 # Attempt to remove all testpools (testpool.XXX), unopened dm devices,
90 # loopback devices, and files. This is a useful way to cleanup a previous
91 # test run failure which has left the system in an unknown state. This can
92 # be dangerous and should only be used in a dedicated test environment.
93 #
94 cleanup_all() {
95 local TEST_POOLS
96 TEST_POOLS=$(sudo "$ZPOOL" list -H -o name | grep testpool)
97 local TEST_LOOPBACKS
98 TEST_LOOPBACKS=$(sudo "${LOSETUP}" -a|grep file-vdev|cut -f1 -d:)
99 local TEST_FILES
100 TEST_FILES=$(ls /var/tmp/file-vdev* 2>/dev/null)
101
102 msg
103 msg "--- Cleanup ---"
104 msg "Removing pool(s): $(echo "${TEST_POOLS}" | tr '\n' ' ')"
105 for TEST_POOL in $TEST_POOLS; do
106 sudo "$ZPOOL" destroy "${TEST_POOL}"
107 done
108
109 msg "Removing dm(s): $(sudo "${DMSETUP}" ls |
110 grep loop | tr '\n' ' ')"
111 sudo "${DMSETUP}" remove_all
112
113 msg "Removing loopback(s): $(echo "${TEST_LOOPBACKS}" | tr '\n' ' ')"
114 for TEST_LOOPBACK in $TEST_LOOPBACKS; do
115 sudo "${LOSETUP}" -d "${TEST_LOOPBACK}"
116 done
117
118 msg "Removing files(s): $(echo "${TEST_FILES}" | tr '\n' ' ')"
119 for TEST_FILE in $TEST_FILES; do
120 sudo rm -f "${TEST_FILE}"
121 done
122 }
123
124 #
125 # Log a failure message, cleanup, and return an error.
126 #
127 fail() {
128 echo -e "${PROG}: $1" >&2
129 cleanup
130 exit 1
131 }
132
133 #
134 # Takes a name as the only arguments and looks for the following variations
135 # on that name. If one is found it is returned.
136 #
137 # $RUNFILEDIR/<name>
138 # $RUNFILEDIR/<name>.run
139 # <name>
140 # <name>.run
141 #
142 find_runfile() {
143 local NAME=$1
144 local RESULT=""
145
146 if [ -f "$RUNFILEDIR/$NAME" ]; then
147 RESULT="$RUNFILEDIR/$NAME"
148 elif [ -f "$RUNFILEDIR/$NAME.run" ]; then
149 RESULT="$RUNFILEDIR/$NAME.run"
150 elif [ -f "$NAME" ]; then
151 RESULT="$NAME"
152 elif [ -f "$NAME.run" ]; then
153 RESULT="$NAME.run"
154 fi
155
156 echo "$RESULT"
157 }
158
159 #
160 # Symlink file if it appears under any of the given paths.
161 #
162 create_links() {
163 local dir_list="$1"
164 local file_list="$2"
165
166 [ -n "$STF_PATH" ] || fail "STF_PATH wasn't correctly set"
167
168 for i in $file_list; do
169 for j in $dir_list; do
170 [ ! -e "$STF_PATH/$i" ] || continue
171
172 if [ ! -d "$j/$i" ] && [ -e "$j/$i" ]; then
173 ln -s "$j/$i" "$STF_PATH/$i" || \
174 fail "Couldn't link $i"
175 break
176 fi
177 done
178
179 [ ! -e "$STF_PATH/$i" ] && STF_MISSING_BIN="$STF_MISSING_BIN$i "
180 done
181 }
182
183 #
184 # Constrain the path to limit the available binaries to a known set.
185 # When running in-tree a top level ./bin/ directory is created for
186 # convenience, otherwise a temporary directory is used.
187 #
188 constrain_path() {
189 . "$STF_SUITE/include/commands.cfg"
190
191 if [ -n "${INTREE}" ]; then
192 STF_PATH="$BUILDDIR/bin"
193 if [ ! -d "$STF_PATH" ]; then
194 mkdir "$STF_PATH"
195 fi
196 else
197 SYSTEMDIR=${SYSTEMDIR:-/var/tmp/constrained_path.XXXX}
198 STF_PATH=$(/bin/mktemp -d "$SYSTEMDIR")
199 fi
200
201 STF_MISSING_BIN=""
202 chmod 755 "$STF_PATH" || fail "Couldn't chmod $STF_PATH"
203
204 # Standard system utilities
205 create_links "/bin /usr/bin /sbin /usr/sbin" "$SYSTEM_FILES"
206
207 if [ -z "${INTREE}" ]; then
208 # Special case links for standard zfs utilities
209 create_links "/bin /usr/bin /sbin /usr/sbin" "$ZFS_FILES"
210
211 # Special case links for zfs test suite utilties
212 create_links "$TESTSDIR/bin" "$ZFSTEST_FILES"
213 else
214 # Special case links for standard zfs utilities
215 DIRS="$(find "$CMDDIR" -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 utilties
220 DIRS="$(find "$TESTSDIR" -type d \( ! -name .deps -a \
221 ! -name .libs \) -print | tr '\n' ' ')"
222 create_links "$DIRS" "$ZFSTEST_FILES"
223 fi
224
225 # Exceptions
226 ln -fs "$STF_PATH/awk" "$STF_PATH/nawk"
227 ln -fs /sbin/fsck.ext4 "$STF_PATH/fsck"
228 ln -fs /sbin/mkfs.ext4 "$STF_PATH/newfs"
229 ln -fs "$STF_PATH/gzip" "$STF_PATH/compress"
230 ln -fs "$STF_PATH/gunzip" "$STF_PATH/uncompress"
231 ln -fs "$STF_PATH/exportfs" "$STF_PATH/share"
232 ln -fs "$STF_PATH/exportfs" "$STF_PATH/unshare"
233 }
234
235 #
236 # Output a useful usage message.
237 #
238 usage() {
239 cat << EOF
240 USAGE:
241 $0 [hvqxkf] [-s SIZE] [-r RUNFILE] [-t PATH] [-u USER]
242
243 DESCRIPTION:
244 ZFS Test Suite launch script
245
246 OPTIONS:
247 -h Show this message
248 -v Verbose zfs-tests.sh output
249 -q Quiet test-runner output
250 -x Remove all testpools, dm, lo, and files (unsafe)
251 -k Disable cleanup after test failure
252 -f Use files only, disables block device tests
253 -d DIR Use DIR for files and loopback devices
254 -s SIZE Use vdevs of SIZE (default: 4G)
255 -r RUNFILE Run tests in RUNFILE (default: linux.run)
256 -t PATH Run single test at PATH relative to test suite
257 -u USER Run single test as USER (default: root)
258
259 EXAMPLES:
260 # Run the default (linux) suite of tests and output the configuration used.
261 $0 -v
262
263 # Run a smaller suite of tests designed to run more quickly.
264 $0 -r linux-fast
265
266 # Cleanup a previous run of the test suite prior to testing, run the
267 # default (linux) suite of tests and perform no cleanup on exit.
268 $0 -x
269
270 EOF
271 }
272
273 while getopts 'hvqxkfd:s:r:?t:u:' OPTION; do
274 case $OPTION in
275 h)
276 usage
277 exit 1
278 ;;
279 v)
280 # shellcheck disable=SC2034
281 VERBOSE=1
282 ;;
283 q)
284 QUIET="-q"
285 ;;
286 x)
287 CLEANUPALL=1
288 ;;
289 k)
290 CLEANUP=0
291 ;;
292 f)
293 LOOPBACK=0
294 ;;
295 d)
296 FILEDIR="$OPTARG"
297 ;;
298 s)
299 FILESIZE="$OPTARG"
300 ;;
301 r)
302 RUNFILE="$OPTARG"
303 ;;
304 t)
305 if [ ${#SINGLETEST[@]} -ne 0 ]; then
306 fail "-t can only be provided once."
307 fi
308 SINGLETEST+=("$OPTARG")
309 ;;
310 u)
311 SINGLETESTUSER="$OPTARG"
312 ;;
313 ?)
314 usage
315 exit
316 ;;
317 esac
318 done
319
320 shift $((OPTIND-1))
321
322 FILES=${FILES:-"$FILEDIR/file-vdev0 $FILEDIR/file-vdev1 $FILEDIR/file-vdev2"}
323 LOOPBACKS=${LOOPBACKS:-""}
324
325 if [ ${#SINGLETEST[@]} -ne 0 ]; then
326 RUNFILEDIR="/var/tmp"
327 RUNFILE="zfs-tests.$$.run"
328 SINGLEQUIET="False"
329
330 if [ -n "$QUIET" ]; then
331 SINGLEQUIET="True"
332 fi
333
334 cat >$RUNFILEDIR/$RUNFILE << EOF
335 [DEFAULT]
336 pre =
337 quiet = $SINGLEQUIET
338 pre_user = root
339 user = $SINGLETESTUSER
340 timeout = 600
341 post_user = root
342 post =
343 outputdir = /var/tmp/test_results
344 EOF
345 for t in "${SINGLETEST[@]}"
346 do
347 SINGLETESTDIR=$(dirname "$t")
348 SINGLETESTFILE=$(basename "$t")
349 SETUPSCRIPT=
350 CLEANUPSCRIPT=
351
352 if [ -f "$STF_SUITE/$SINGLETESTDIR/setup.ksh" ]; then
353 SETUPSCRIPT="setup"
354 fi
355
356 if [ -f "$STF_SUITE/$SINGLETESTDIR/cleanup.ksh" ]; then
357 CLEANUPSCRIPT="cleanup"
358 fi
359
360 cat >>$RUNFILEDIR/$RUNFILE << EOF
361
362 [$SINGLETESTDIR]
363 tests = ['$SINGLETESTFILE']
364 pre = $SETUPSCRIPT
365 post = $CLEANUPSCRIPT
366 EOF
367 done
368 fi
369
370 #
371 # Attempt to locate the runfile describing the test workload.
372 #
373 if [ -n "$RUNFILE" ]; then
374 SAVED_RUNFILE="$RUNFILE"
375 RUNFILE=$(find_runfile "$RUNFILE")
376 [ -z "$RUNFILE" ] && fail "Cannot find runfile: $SAVED_RUNFILE"
377 fi
378
379 if [ ! -r "$RUNFILE" ]; then
380 fail "Cannot read runfile: $RUNFILE"
381 fi
382
383 #
384 # This script should not be run as root. Instead the test user, which may
385 # be a normal user account, needs to be configured such that it can
386 # run commands via sudo passwordlessly.
387 #
388 if [ "$(id -u)" = "0" ]; then
389 fail "This script must not be run as root."
390 fi
391
392 if [ "$(sudo whoami)" != "root" ]; then
393 fail "Passwordless sudo access required."
394 fi
395
396 #
397 # Constain the available binaries to a known set.
398 #
399 constrain_path
400
401 #
402 # Check if ksh exists
403 #
404 [ -e "$STF_PATH/ksh" ] || fail "This test suite requires ksh."
405 [ -e "$STF_SUITE/include/default.cfg" ] || fail \
406 "Missing $STF_SUITE/include/default.cfg file."
407
408 #
409 # Verify the ZFS module stack if loaded.
410 #
411 sudo "${ZFS_SH}" &>/dev/null
412
413 #
414 # Attempt to cleanup all previous state for a new test run.
415 #
416 if [ $CLEANUPALL -ne 0 ]; then
417 cleanup_all
418 fi
419
420 #
421 # By default preserve any existing pools
422 #
423 if [ -z "${KEEP}" ]; then
424 KEEP=$(sudo "$ZPOOL" list -H -o name)
425 if [ -z "${KEEP}" ]; then
426 KEEP="rpool"
427 fi
428 fi
429
430 __ZFS_POOL_EXCLUDE="$(echo $KEEP | sed ':a;N;s/\n/ /g;ba')"
431
432 . "$STF_SUITE/include/default.cfg"
433
434 msg
435 msg "--- Configuration ---"
436 msg "Runfile: $RUNFILE"
437 msg "STF_TOOLS: $STF_TOOLS"
438 msg "STF_SUITE: $STF_SUITE"
439 msg "STF_PATH: $STF_PATH"
440
441 #
442 # No DISKS have been provided so a basic file or loopback based devices
443 # must be created for the test suite to use.
444 #
445 if [ -z "${DISKS}" ]; then
446 #
447 # Create sparse files for the test suite. These may be used
448 # directory or have loopback devices layered on them.
449 #
450 for TEST_FILE in ${FILES}; do
451 [ -f "$TEST_FILE" ] && fail "Failed file exists: ${TEST_FILE}"
452 truncate -s "${FILESIZE}" "${TEST_FILE}" ||
453 fail "Failed creating: ${TEST_FILE} ($?)"
454 DISKS="$DISKS$TEST_FILE "
455 done
456
457 #
458 # If requested setup loopback devices backed by the sparse files.
459 #
460 if [ $LOOPBACK -eq 1 ]; then
461 DISKS=""
462 check_loop_utils
463
464 for TEST_FILE in ${FILES}; do
465 TEST_LOOPBACK=$(sudo "${LOSETUP}" -f)
466 sudo "${LOSETUP}" "${TEST_LOOPBACK}" "${TEST_FILE}" ||
467 fail "Failed: ${TEST_FILE} -> ${TEST_LOOPBACK}"
468 LOOPBACKS="${LOOPBACKS}${TEST_LOOPBACK} "
469 BASELOOPBACKS=$(basename "$TEST_LOOPBACK")
470 DISKS="$DISKS$BASELOOPBACKS "
471 done
472 fi
473 fi
474
475 NUM_DISKS=$(echo "${DISKS}" | $AWK '{print NF}')
476 [ "$NUM_DISKS" -lt 3 ] && fail "Not enough disks ($NUM_DISKS/3 minimum)"
477
478 #
479 # Disable SELinux until the ZFS Test Suite has been updated accordingly.
480 #
481 if [ -x "$STF_PATH/setenforce" ]; then
482 sudo setenforce permissive &>/dev/null
483 fi
484
485 #
486 # Enable internal ZFS debug log and clear it.
487 #
488 if [ -e /sys/module/zfs/parameters/zfs_dbgmsg_enable ]; then
489 sudo /bin/sh -c "echo 1 >/sys/module/zfs/parameters/zfs_dbgmsg_enable"
490 sudo /bin/sh -c "echo 0 >/proc/spl/kstat/zfs/dbgmsg"
491 fi
492
493 msg "FILEDIR: $FILEDIR"
494 msg "FILES: $FILES"
495 msg "LOOPBACKS: $LOOPBACKS"
496 msg "DISKS: $DISKS"
497 msg "NUM_DISKS: $NUM_DISKS"
498 msg "FILESIZE: $FILESIZE"
499 msg "Keep pool(s): $KEEP"
500 msg "Missing util(s): $STF_MISSING_BIN"
501 msg ""
502
503 export STF_TOOLS
504 export STF_SUITE
505 export STF_PATH
506 export DISKS
507 export KEEP
508 export __ZFS_POOL_EXCLUDE
509 export TESTFAIL_CALLBACKS
510 export PATH=$STF_PATH
511
512 msg "${TEST_RUNNER} ${QUIET} -c ${RUNFILE} -i ${STF_SUITE}"
513 ${TEST_RUNNER} ${QUIET} -c "${RUNFILE}" -i "${STF_SUITE}"
514 RESULT=$?
515 echo
516
517 if [ ${#SINGLETEST[@]} -ne 0 ]; then
518 rm -f "$RUNFILE" &>/dev/null
519 fi
520
521 exit ${RESULT}