]>
Commit | Line | Data |
---|---|---|
9c13b489 | 1 | #!/bin/bash |
541a0901 BB |
2 | |
3 | # | |
4 | # CDDL HEADER START | |
5 | # | |
6 | # This file and its contents are supplied under the terms of the | |
7 | # Common Development and Distribution License ("CDDL"), version 1.0. | |
8 | # You may only use this file in accordance with the terms of version | |
9 | # 1.0 of the CDDL. | |
10 | # | |
11 | # A full copy of the text of the CDDL should have accompanied this | |
12 | # source. A copy of the CDDL is also available via the Internet at | |
13 | # http://www.illumos.org/license/CDDL. | |
14 | # | |
15 | # CDDL HEADER END | |
16 | # | |
17 | ||
18 | # | |
19 | # Copyright (c) 2015 by Delphix. All rights reserved. | |
20 | # Copyright (C) 2016 Lawrence Livermore National Security, LLC. | |
21 | # | |
22 | ||
c8f9061f | 23 | BASE_DIR=$(dirname "$0") |
541a0901 | 24 | SCRIPT_COMMON=common.sh |
c8f9061f BB |
25 | if [ -f "${BASE_DIR}/${SCRIPT_COMMON}" ]; then |
26 | . "${BASE_DIR}/${SCRIPT_COMMON}" | |
541a0901 BB |
27 | else |
28 | echo "Missing helper script ${SCRIPT_COMMON}" && exit 1 | |
29 | fi | |
30 | ||
c552fbc5 | 31 | # shellcheck disable=SC2034 |
541a0901 | 32 | PROG=zloop.sh |
fed90353 | 33 | GDB=${GDB:-gdb} |
541a0901 BB |
34 | |
35 | DEFAULTWORKDIR=/var/tmp | |
36 | DEFAULTCOREDIR=/var/tmp/zloop | |
37 | ||
38 | function usage | |
39 | { | |
aea899a6 | 40 | echo -e "\n$0 [-t <timeout>] [ -s <vdev size> ] [-c <dump directory>]" \ |
541a0901 BB |
41 | "[ -- [extra ztest parameters]]\n" \ |
42 | "\n" \ | |
43 | " This script runs ztest repeatedly with randomized arguments.\n" \ | |
44 | " If a crash is encountered, the ztest logs, any associated\n" \ | |
45 | " vdev files, and core file (if one exists) are moved to the\n" \ | |
46 | " output directory ($DEFAULTCOREDIR by default). Any options\n" \ | |
47 | " after the -- end-of-options marker will be passed to ztest.\n" \ | |
48 | "\n" \ | |
49 | " Options:\n" \ | |
50 | " -t Total time to loop for, in seconds. If not provided,\n" \ | |
51 | " zloop runs forever.\n" \ | |
aea899a6 | 52 | " -s Size of vdev devices.\n" \ |
541a0901 BB |
53 | " -f Specify working directory for ztest vdev files.\n" \ |
54 | " -c Specify a core dump directory to use.\n" \ | |
cd0a89de BB |
55 | " -m Max number of core dumps to allow before exiting.\n" \ |
56 | " -l Create 'ztest.core.N' symlink to core directory.\n" \ | |
541a0901 BB |
57 | " -h Print this help message.\n" \ |
58 | "" >&2 | |
59 | } | |
60 | ||
61 | function or_die | |
62 | { | |
c552fbc5 | 63 | # shellcheck disable=SC2068 |
541a0901 | 64 | $@ |
c552fbc5 | 65 | # shellcheck disable=SC2181 |
541a0901 | 66 | if [[ $? -ne 0 ]]; then |
c552fbc5 | 67 | # shellcheck disable=SC2145 |
541a0901 BB |
68 | echo "Command failed: $@" |
69 | exit 1 | |
70 | fi | |
71 | } | |
72 | ||
20da0566 GN |
73 | # core file helpers |
74 | origcorepattern="$(cat /proc/sys/kernel/core_pattern)" | |
3da3488e | 75 | coreglob="$(grep -E -o '^([^|%[:space:]]*)' /proc/sys/kernel/core_pattern)*" |
20da0566 GN |
76 | |
77 | if [[ $coreglob = "*" ]]; then | |
78 | echo "Setting core file pattern..." | |
79 | echo "core" > /proc/sys/kernel/core_pattern | |
3da3488e | 80 | coreglob="$(grep -E -o '^([^|%[:space:]]*)' \ |
0c313d2f | 81 | /proc/sys/kernel/core_pattern)*" |
20da0566 GN |
82 | fi |
83 | ||
84 | function core_file | |
85 | { | |
c552fbc5 | 86 | # shellcheck disable=SC2012 disable=2086 |
20da0566 GN |
87 | printf "%s" "$(ls -tr1 $coreglob 2> /dev/null | head -1)" |
88 | } | |
89 | ||
0c313d2f GN |
90 | function core_prog |
91 | { | |
92 | prog=$ZTEST | |
c552fbc5 | 93 | core_id=$($GDB --batch -c "$1" | grep "Core was generated by" | \ |
0c313d2f | 94 | tr \' ' ') |
c552fbc5 | 95 | # shellcheck disable=SC2076 |
0c313d2f GN |
96 | if [[ "$core_id" =~ "zdb " ]]; then |
97 | prog=$ZDB | |
98 | fi | |
99 | printf "%s" "$prog" | |
100 | } | |
101 | ||
541a0901 BB |
102 | function store_core |
103 | { | |
20da0566 GN |
104 | core="$(core_file)" |
105 | if [[ $ztrc -ne 0 ]] || [[ -f "$core" ]]; then | |
5df5d06a | 106 | df -h "$workdir" >>ztest.out |
541a0901 | 107 | coreid=$(date "+zloop-%y%m%d-%H%M%S") |
c552fbc5 | 108 | foundcrashes=$((foundcrashes + 1)) |
541a0901 | 109 | |
cd0a89de BB |
110 | # zdb debugging |
111 | zdbcmd="$ZDB -U "$workdir/zpool.cache" -dddMmDDG ztest" | |
112 | zdbdebug=$($zdbcmd 2>&1) | |
113 | echo -e "$zdbcmd\n" >>ztest.zdb | |
114 | echo "$zdbdebug" >>ztest.zdb | |
115 | ||
541a0901 | 116 | dest=$coredir/$coreid |
c552fbc5 GDN |
117 | or_die mkdir -p "$dest" |
118 | or_die mkdir -p "$dest/vdev" | |
541a0901 | 119 | |
cd0a89de BB |
120 | if [[ $symlink -ne 0 ]]; then |
121 | or_die ln -sf "$dest" ztest.core.$foundcrashes | |
122 | fi | |
123 | ||
541a0901 BB |
124 | echo "*** ztest crash found - moving logs to $dest" |
125 | ||
c552fbc5 | 126 | or_die mv ztest.history "$dest/" |
cd0a89de | 127 | or_die mv ztest.zdb "$dest/" |
c552fbc5 GDN |
128 | or_die mv ztest.out "$dest/" |
129 | or_die mv "$workdir/ztest*" "$dest/vdev/" | |
8fb1ede1 BB |
130 | |
131 | if [[ -e "$workdir/zpool.cache" ]]; then | |
132 | or_die mv "$workdir/zpool.cache" "$dest/vdev/" | |
133 | fi | |
541a0901 BB |
134 | |
135 | # check for core | |
20da0566 | 136 | if [[ -f "$core" ]]; then |
c552fbc5 | 137 | coreprog=$(core_prog "$core") |
cd0a89de | 138 | coredebug=$($GDB --batch --quiet \ |
541a0901 BB |
139 | -ex "set print thread-events off" \ |
140 | -ex "printf \"*\n* Backtrace \n*\n\"" \ | |
141 | -ex "bt" \ | |
142 | -ex "printf \"*\n* Libraries \n*\n\"" \ | |
143 | -ex "info sharedlib" \ | |
144 | -ex "printf \"*\n* Threads (full) \n*\n\"" \ | |
145 | -ex "info threads" \ | |
146 | -ex "printf \"*\n* Backtraces \n*\n\"" \ | |
147 | -ex "thread apply all bt" \ | |
148 | -ex "printf \"*\n* Backtraces (full) \n*\n\"" \ | |
149 | -ex "thread apply all bt full" \ | |
cd0a89de BB |
150 | -ex "quit" "$coreprog" "$core" 2>&1 | \ |
151 | grep -v "New LWP") | |
541a0901 BB |
152 | |
153 | # Dump core + logs to stored directory | |
cd0a89de | 154 | echo "$coredebug" >>"$dest/ztest.gdb" |
c552fbc5 | 155 | or_die mv "$core" "$dest/" |
541a0901 BB |
156 | |
157 | # Record info in cores logfile | |
20da0566 | 158 | echo "*** core @ $coredir/$coreid/$core:" | \ |
541a0901 | 159 | tee -a ztest.cores |
541a0901 | 160 | fi |
cd0a89de BB |
161 | |
162 | if [[ $coremax -gt 0 ]] && | |
163 | [[ $foundcrashes -ge $coremax ]]; then | |
164 | echo "exiting... max $coremax allowed cores" | |
165 | exit 1 | |
166 | else | |
167 | echo "continuing..." | |
168 | fi | |
541a0901 BB |
169 | fi |
170 | } | |
171 | ||
172 | # parse arguments | |
173 | # expected format: zloop [-t timeout] [-c coredir] [-- extra ztest args] | |
174 | coredir=$DEFAULTCOREDIR | |
5df5d06a DB |
175 | basedir=$DEFAULTWORKDIR |
176 | rundir="zloop-run" | |
541a0901 | 177 | timeout=0 |
aea899a6 | 178 | size="512m" |
cd0a89de BB |
179 | coremax=0 |
180 | symlink=0 | |
181 | while getopts ":ht:m:s:c:f:l" opt; do | |
541a0901 BB |
182 | case $opt in |
183 | t ) [[ $OPTARG -gt 0 ]] && timeout=$OPTARG ;; | |
cd0a89de | 184 | m ) [[ $OPTARG -gt 0 ]] && coremax=$OPTARG ;; |
aea899a6 | 185 | s ) [[ $OPTARG ]] && size=$OPTARG ;; |
541a0901 | 186 | c ) [[ $OPTARG ]] && coredir=$OPTARG ;; |
5df5d06a | 187 | f ) [[ $OPTARG ]] && basedir=$(readlink -f "$OPTARG") ;; |
cd0a89de | 188 | l ) symlink=1 ;; |
541a0901 BB |
189 | h ) usage |
190 | exit 2 | |
191 | ;; | |
192 | * ) echo "Invalid argument: -$OPTARG"; | |
193 | usage | |
194 | exit 1 | |
195 | esac | |
196 | done | |
197 | # pass remaining arguments on to ztest | |
198 | shift $((OPTIND - 1)) | |
199 | ||
200 | # enable core dumps | |
201 | ulimit -c unlimited | |
fed90353 | 202 | export ASAN_OPTIONS=abort_on_error=1:disable_coredump=0 |
541a0901 | 203 | |
20da0566 GN |
204 | if [[ -f "$(core_file)" ]]; then |
205 | echo -n "There's a core dump here you might want to look at first... " | |
c552fbc5 | 206 | core_file |
cd0a89de | 207 | echo |
541a0901 BB |
208 | exit 1 |
209 | fi | |
210 | ||
211 | if [[ ! -d $coredir ]]; then | |
212 | echo "core dump directory ($coredir) does not exist, creating it." | |
c552fbc5 | 213 | or_die mkdir -p "$coredir" |
541a0901 BB |
214 | fi |
215 | ||
216 | if [[ ! -w $coredir ]]; then | |
217 | echo "core dump directory ($coredir) is not writable." | |
218 | exit 1 | |
219 | fi | |
220 | ||
221 | or_die rm -f ztest.history | |
cd0a89de | 222 | or_die rm -f ztest.zdb |
541a0901 BB |
223 | or_die rm -f ztest.cores |
224 | ||
225 | ztrc=0 # ztest return value | |
226 | foundcrashes=0 # number of crashes found so far | |
227 | starttime=$(date +%s) | |
228 | curtime=$starttime | |
229 | ||
230 | # if no timeout was specified, loop forever. | |
c552fbc5 | 231 | while [[ $timeout -eq 0 ]] || [[ $curtime -le $((starttime + timeout)) ]]; do |
8fb1ede1 | 232 | zopt="-G -VVVVV" |
541a0901 | 233 | |
5df5d06a DB |
234 | # start each run with an empty directory |
235 | workdir="$basedir/$rundir" | |
236 | or_die rm -rf "$workdir" | |
237 | or_die mkdir "$workdir" | |
238 | ||
541a0901 BB |
239 | # switch between common arrangements & fully randomized |
240 | if [[ $((RANDOM % 2)) -eq 0 ]]; then | |
241 | mirrors=2 | |
242 | raidz=0 | |
243 | parity=1 | |
244 | vdevs=2 | |
245 | else | |
246 | mirrors=$(((RANDOM % 3) * 1)) | |
247 | parity=$(((RANDOM % 3) + 1)) | |
248 | raidz=$((((RANDOM % 9) + parity + 1) * (RANDOM % 2))) | |
249 | vdevs=$(((RANDOM % 3) + 3)) | |
250 | fi | |
251 | align=$(((RANDOM % 2) * 3 + 9)) | |
252 | runtime=$((RANDOM % 100)) | |
253 | passtime=$((RANDOM % (runtime / 3 + 1) + 10)) | |
541a0901 BB |
254 | |
255 | zopt="$zopt -m $mirrors" | |
256 | zopt="$zopt -r $raidz" | |
257 | zopt="$zopt -R $parity" | |
258 | zopt="$zopt -v $vdevs" | |
259 | zopt="$zopt -a $align" | |
260 | zopt="$zopt -T $runtime" | |
261 | zopt="$zopt -P $passtime" | |
262 | zopt="$zopt -s $size" | |
263 | zopt="$zopt -f $workdir" | |
264 | ||
c552fbc5 | 265 | # shellcheck disable=SC2124 |
541a0901 BB |
266 | cmd="$ZTEST $zopt $@" |
267 | desc="$(date '+%m/%d %T') $cmd" | |
268 | echo "$desc" | tee -a ztest.history | |
269 | echo "$desc" >>ztest.out | |
270 | $cmd >>ztest.out 2>&1 | |
271 | ztrc=$? | |
3da3488e | 272 | grep -E '===|WARNING' ztest.out >>ztest.history |
541a0901 BB |
273 | |
274 | store_core | |
275 | ||
276 | curtime=$(date +%s) | |
277 | done | |
278 | ||
279 | echo "zloop finished, $foundcrashes crashes found" | |
280 | ||
20da0566 GN |
281 | #restore core pattern |
282 | echo "$origcorepattern" > /proc/sys/kernel/core_pattern | |
283 | ||
541a0901 BB |
284 | uptime >>ztest.out |
285 | ||
286 | if [[ $foundcrashes -gt 0 ]]; then | |
287 | exit 1 | |
288 | fi |