]> git.proxmox.com Git - mirror_zfs.git/blob - scripts/zfs-tests.sh
01fb2b28990782d6b3cb84e23c7d7f276f52184a
[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 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 . $STF_SUITE/include/default.cfg
33
34 PROG=zfs-tests.sh
35 SUDO=/usr/bin/sudo
36 SETENFORCE=/usr/sbin/setenforce
37 VERBOSE=
38 QUIET=
39 CLEANUP=1
40 CLEANUPALL=0
41 LOOPBACK=1
42 FILESIZE="2G"
43 RUNFILE=${RUNFILE:-"linux.run"}
44 FILEDIR=${FILEDIR:-/var/tmp}
45 DISKS=${DISKS:-""}
46
47 #
48 # Attempt to remove loopback devices and files which where created earlier
49 # by this script to run the test framework. The '-k' option may be passed
50 # to the script to suppress cleanup for debugging purposes.
51 #
52 cleanup() {
53 if [ $CLEANUP -eq 0 ]; then
54 return 0
55 fi
56
57 if [ $LOOPBACK -eq 1 ]; then
58 for TEST_LOOPBACK in ${LOOPBACKS}; do
59 LOOP_DEV=$(basename $TEST_LOOPBACK)
60 DM_DEV=$(${SUDO} ${DMSETUP} ls 2>/dev/null | \
61 grep ${LOOP_DEV} | cut -f1)
62
63 if [ -n "$DM_DEV" ]; then
64 ${SUDO} ${DMSETUP} remove ${DM_DEV} ||
65 echo "Failed to remove: ${DM_DEV}"
66 fi
67
68 if [ -n "${TEST_LOOPBACK}" ]; then
69 ${SUDO} ${LOSETUP} -d ${TEST_LOOPBACK} ||
70 echo "Failed to remove: ${TEST_LOOPBACK}"
71 fi
72 done
73 fi
74
75 for TEST_FILE in ${FILES}; do
76 rm -f ${TEST_FILE} &>/dev/null
77 done
78 }
79 trap cleanup EXIT
80
81 #
82 # Attempt to remove all testpools (testpool.XXX), unopened dm devices,
83 # loopback devices, and files. This is a useful way to cleanup a previous
84 # test run failure which has left the system in an unknown state. This can
85 # be dangerous and should only be used in a dedicated test environment.
86 #
87 cleanup_all() {
88 local TEST_POOLS=$(${SUDO} ${ZPOOL} list -H -o name | grep testpool)
89 local TEST_LOOPBACKS=$(${SUDO} ${LOSETUP} -a|grep file-vdev|cut -f1 -d:)
90 local TEST_FILES=$(ls /var/tmp/file-vdev* 2>/dev/null)
91
92 msg
93 msg "--- Cleanup ---"
94 msg "Removing pool(s): $(echo ${TEST_POOLS} | tr '\n' ' ')"
95 for TEST_POOL in $TEST_POOLS; do
96 ${SUDO} ${ZPOOL} destroy ${TEST_POOL}
97 done
98
99 msg "Removing dm(s): $(${SUDO} ${DMSETUP} ls |
100 grep loop | tr '\n' ' ')"
101 ${SUDO} ${DMSETUP} remove_all
102
103 msg "Removing loopback(s): $(echo ${TEST_LOOPBACKS} | tr '\n' ' ')"
104 for TEST_LOOPBACK in $TEST_LOOPBACKS; do
105 ${SUDO} ${LOSETUP} -d ${TEST_LOOPBACK}
106 done
107
108 msg "Removing files(s): $(echo ${TEST_FILES} | tr '\n' ' ')"
109 for TEST_FILE in $TEST_FILES; do
110 ${SUDO} rm -f ${TEST_FILE}
111 done
112 }
113
114 #
115 # Log a failure message, cleanup, and return an error.
116 #
117 fail() {
118 echo -e "${PROG}: $1" >&2
119 cleanup
120 exit 1
121 }
122
123 #
124 # Takes a name as the only arguments and looks for the following variations
125 # on that name. If one is found it is returned.
126 #
127 # $RUNFILEDIR/<name>
128 # $RUNFILEDIR/<name>.run
129 # <name>
130 # <name>.run
131 #
132 find_runfile() {
133 local NAME=$1
134 local RESULT=""
135
136 if [ -f "$RUNFILEDIR/$NAME" ]; then
137 RESULT="$RUNFILEDIR/$NAME"
138 elif [ -f "$RUNFILEDIR/$NAME.run" ]; then
139 RESULT="$RUNFILEDIR/$NAME.run"
140 elif [ -f "$NAME" ]; then
141 RESULT="$NAME"
142 elif [ -f "$NAME.run" ]; then
143 RESULT="$NAME.run"
144 fi
145
146 echo "$RESULT"
147 }
148
149 #
150 # Output a useful usage message.
151 #
152 usage() {
153 cat << EOF
154 USAGE:
155 $0 [hvqxkf] [-s SIZE] [-r RUNFILE]
156
157 DESCRIPTION:
158 ZFS Test Suite launch script
159
160 OPTIONS:
161 -h Show this message
162 -v Verbose zfs-tests.sh output
163 -q Quiet test-runner output
164 -x Remove all testpools, dm, lo, and files (unsafe)
165 -k Disable cleanup after test failure
166 -f Use files only, disables block device tests
167 -d DIR Use DIR for files and loopback devices
168 -s SIZE Use vdevs of SIZE (default: 2G)
169 -r RUNFILE Run tests in RUNFILE (default: linux.run)
170
171 EXAMPLES:
172 # Run the default (linux) suite of tests and output the configuration used.
173 $0 -v
174
175 # Run a smaller suite of tests designed to run more quickly.
176 $0 -r linux-fast
177
178 # Cleanup a previous run of the test suite prior to testing, run the
179 # default (linux) suite of tests and perform no cleanup on exit.
180 $0 -x
181
182 EOF
183 }
184
185 while getopts 'hvqxkfd:s:r:?' OPTION; do
186 case $OPTION in
187 h)
188 usage
189 exit 1
190 ;;
191 v)
192 VERBOSE=1
193 ;;
194 q)
195 QUIET="-q"
196 ;;
197 x)
198 CLEANUPALL=1
199 ;;
200 k)
201 CLEANUP=0
202 ;;
203 f)
204 LOOPBACK=0
205 ;;
206 d)
207 FILEDIR="$OPTARG"
208 ;;
209 s)
210 FILESIZE="$OPTARG"
211 ;;
212 r)
213 RUNFILE="$OPTARG"
214 ;;
215 ?)
216 usage
217 exit
218 ;;
219 esac
220 done
221
222 shift $((OPTIND-1))
223
224 FILES=${FILES:-"$FILEDIR/file-vdev0 $FILEDIR/file-vdev1 $FILEDIR/file-vdev2"}
225 LOOPBACKS=${LOOPBACKS:-""}
226
227 #
228 # Attempt to locate the runfile describing the test workload.
229 #
230 if [ -n "$RUNFILE" ]; then
231 SAVED_RUNFILE="$RUNFILE"
232 RUNFILE=$(find_runfile "$RUNFILE")
233 [ -z "$RUNFILE" ] && fail "Cannot find runfile: $SAVED_RUNFILE"
234 fi
235
236 if [ ! -r "$RUNFILE" ]; then
237 fail "Cannot read runfile: $RUNFILE"
238 fi
239
240 #
241 # This script should not be run as root. Instead the test user, which may
242 # be a normal user account, needs to be configured such that it can
243 # run commands via sudo passwordlessly.
244 #
245 if [ $(id -u) = "0" ]; then
246 fail "This script must not be run as root."
247 fi
248
249 if [ $(sudo whoami) != "root" ]; then
250 fail "Passwordless sudo access required."
251 fi
252
253 #
254 # Check if ksh exists
255 #
256 if [ -z "$(which ksh 2>/dev/null)" ]; then
257 fail "This test suite requires ksh."
258 fi
259
260 #
261 # Verify the ZFS module stack if loaded.
262 #
263 ${SUDO} ${ZFS_SH} &>/dev/null
264
265 #
266 # Attempt to cleanup all previous state for a new test run.
267 #
268 if [ $CLEANUPALL -ne 0 ]; then
269 cleanup_all
270 fi
271
272 #
273 # By default preserve any existing pools
274 #
275 if [ -z "${KEEP}" ]; then
276 KEEP=$(${SUDO} ${ZPOOL} list -H -o name)
277 if [ -z "${KEEP}" ]; then
278 KEEP="rpool"
279 fi
280 fi
281
282 msg
283 msg "--- Configuration ---"
284 msg "Runfile: $RUNFILE"
285 msg "STF_TOOLS: $STF_TOOLS"
286 msg "STF_SUITE: $STF_SUITE"
287
288 #
289 # No DISKS have been provided so a basic file or loopback based devices
290 # must be created for the test suite to use.
291 #
292 if [ -z "${DISKS}" ]; then
293 #
294 # Create sparse files for the test suite. These may be used
295 # directory or have loopback devices layered on them.
296 #
297 for TEST_FILE in ${FILES}; do
298 [ -f "$TEST_FILE" ] && fail "Failed file exists: ${TEST_FILE}"
299 truncate -s ${FILESIZE} ${TEST_FILE} ||
300 fail "Failed creating: ${TEST_FILE} ($?)"
301 DISKS="$DISKS$TEST_FILE "
302 done
303
304 #
305 # If requested setup loopback devices backed by the sparse files.
306 #
307 if [ $LOOPBACK -eq 1 ]; then
308 DISKS=""
309 check_loop_utils
310
311 for TEST_FILE in ${FILES}; do
312 TEST_LOOPBACK=$(${SUDO} ${LOSETUP} -f)
313 ${SUDO} ${LOSETUP} ${TEST_LOOPBACK} ${TEST_FILE} ||
314 fail "Failed: ${TEST_FILE} -> ${TEST_LOOPBACK}"
315 LOOPBACKS="${LOOPBACKS}${TEST_LOOPBACK} "
316 DISKS="$DISKS$(basename $TEST_LOOPBACK) "
317 done
318 fi
319 fi
320
321 NUM_DISKS=$(echo ${DISKS} | $AWK '{print NF}')
322 [ $NUM_DISKS -lt 3 ] && fail "Not enough disks ($NUM_DISKS/3 minimum)"
323
324 #
325 # Disable SELinux until the ZFS Test Suite has been updated accordingly.
326 #
327 if [ -x ${SETENFORCE} ]; then
328 ${SUDO} ${SETENFORCE} permissive &>/dev/null
329 fi
330
331 msg "FILEDIR: $FILEDIR"
332 msg "FILES: $FILES"
333 msg "LOOPBACKS: $LOOPBACKS"
334 msg "DISKS: $DISKS"
335 msg "NUM_DISKS: $NUM_DISKS"
336 msg "FILESIZE: $FILESIZE"
337 msg "Keep pool(s): $KEEP"
338 msg ""
339
340 export STF_TOOLS
341 export STF_SUITE
342 export DISKS
343 export KEEP
344
345 msg "${TEST_RUNNER} ${QUIET} -c ${RUNFILE} -i ${STF_SUITE}"
346 ${TEST_RUNNER} ${QUIET} -c ${RUNFILE} -i ${STF_SUITE}
347 RESULT=$?
348 echo
349
350 exit ${RESULT}