]> git.proxmox.com Git - mirror_zfs.git/blob - scripts/zloop.sh
Support -fsanitize=address with --enable-asan
[mirror_zfs.git] / scripts / zloop.sh
1 #!/bin/bash
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
23 BASE_DIR=$(dirname "$0")
24 SCRIPT_COMMON=common.sh
25 if [ -f "${BASE_DIR}/${SCRIPT_COMMON}" ]; then
26 . "${BASE_DIR}/${SCRIPT_COMMON}"
27 else
28 echo "Missing helper script ${SCRIPT_COMMON}" && exit 1
29 fi
30
31 # shellcheck disable=SC2034
32 PROG=zloop.sh
33 GDB=${GDB:-gdb}
34
35 DEFAULTWORKDIR=/var/tmp
36 DEFAULTCOREDIR=/var/tmp/zloop
37
38 function usage
39 {
40 echo -e "\n$0 [-t <timeout>] [ -s <vdev size> ] [-c <dump directory>]" \
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" \
52 " -s Size of vdev devices.\n" \
53 " -f Specify working directory for ztest vdev files.\n" \
54 " -c Specify a core dump directory to use.\n" \
55 " -h Print this help message.\n" \
56 "" >&2
57 }
58
59 function or_die
60 {
61 # shellcheck disable=SC2068
62 $@
63 # shellcheck disable=SC2181
64 if [[ $? -ne 0 ]]; then
65 # shellcheck disable=SC2145
66 echo "Command failed: $@"
67 exit 1
68 fi
69 }
70
71 # core file helpers
72 origcorepattern="$(cat /proc/sys/kernel/core_pattern)"
73 coreglob="$(egrep -o '^([^|%[:space:]]*)' /proc/sys/kernel/core_pattern)*"
74
75 if [[ $coreglob = "*" ]]; then
76 echo "Setting core file pattern..."
77 echo "core" > /proc/sys/kernel/core_pattern
78 coreglob="$(egrep -o '^([^|%[:space:]]*)' \
79 /proc/sys/kernel/core_pattern)*"
80 fi
81
82 function core_file
83 {
84 # shellcheck disable=SC2012 disable=2086
85 printf "%s" "$(ls -tr1 $coreglob 2> /dev/null | head -1)"
86 }
87
88 function core_prog
89 {
90 prog=$ZTEST
91 core_id=$($GDB --batch -c "$1" | grep "Core was generated by" | \
92 tr \' ' ')
93 # shellcheck disable=SC2076
94 if [[ "$core_id" =~ "zdb " ]]; then
95 prog=$ZDB
96 fi
97 printf "%s" "$prog"
98 }
99
100 function store_core
101 {
102 core="$(core_file)"
103 if [[ $ztrc -ne 0 ]] || [[ -f "$core" ]]; then
104 df -h "$workdir" >>ztest.out
105 coreid=$(date "+zloop-%y%m%d-%H%M%S")
106 foundcrashes=$((foundcrashes + 1))
107
108 dest=$coredir/$coreid
109 or_die mkdir -p "$dest"
110 or_die mkdir -p "$dest/vdev"
111
112 echo "*** ztest crash found - moving logs to $dest"
113
114 or_die mv ztest.history "$dest/"
115 or_die mv ztest.ddt "$dest/"
116 or_die mv ztest.out "$dest/"
117 or_die mv "$workdir/ztest*" "$dest/vdev/"
118 or_die mv "$workdir/zpool.cache" "$dest/vdev/"
119
120 # check for core
121 if [[ -f "$core" ]]; then
122 coreprog=$(core_prog "$core")
123 corestatus=$($GDB --batch --quiet \
124 -ex "set print thread-events off" \
125 -ex "printf \"*\n* Backtrace \n*\n\"" \
126 -ex "bt" \
127 -ex "printf \"*\n* Libraries \n*\n\"" \
128 -ex "info sharedlib" \
129 -ex "printf \"*\n* Threads (full) \n*\n\"" \
130 -ex "info threads" \
131 -ex "printf \"*\n* Backtraces \n*\n\"" \
132 -ex "thread apply all bt" \
133 -ex "printf \"*\n* Backtraces (full) \n*\n\"" \
134 -ex "thread apply all bt full" \
135 -ex "quit" "$coreprog" "$core" | grep -v "New LWP")
136
137 # Dump core + logs to stored directory
138 echo "$corestatus" >>"$dest/status"
139 or_die mv "$core" "$dest/"
140
141 # Record info in cores logfile
142 echo "*** core @ $coredir/$coreid/$core:" | \
143 tee -a ztest.cores
144 echo "$corestatus" | tee -a ztest.cores
145 echo "" | tee -a ztest.cores
146 fi
147 echo "continuing..."
148 fi
149 }
150
151 rngdpid=""
152 function on_exit
153 {
154 if [ -n "$rngdpid" ]; then
155 kill -9 "$rngdpid"
156 fi
157 }
158 trap on_exit EXIT
159
160 # parse arguments
161 # expected format: zloop [-t timeout] [-c coredir] [-- extra ztest args]
162 coredir=$DEFAULTCOREDIR
163 basedir=$DEFAULTWORKDIR
164 rundir="zloop-run"
165 timeout=0
166 size="512m"
167 while getopts ":ht:s:c:f:" opt; do
168 case $opt in
169 t ) [[ $OPTARG -gt 0 ]] && timeout=$OPTARG ;;
170 s ) [[ $OPTARG ]] && size=$OPTARG ;;
171 c ) [[ $OPTARG ]] && coredir=$OPTARG ;;
172 f ) [[ $OPTARG ]] && basedir=$(readlink -f "$OPTARG") ;;
173 h ) usage
174 exit 2
175 ;;
176 * ) echo "Invalid argument: -$OPTARG";
177 usage
178 exit 1
179 esac
180 done
181 # pass remaining arguments on to ztest
182 shift $((OPTIND - 1))
183
184 # enable core dumps
185 ulimit -c unlimited
186 export ASAN_OPTIONS=abort_on_error=1:disable_coredump=0
187
188 if [[ -f "$(core_file)" ]]; then
189 echo -n "There's a core dump here you might want to look at first... "
190 core_file
191 exit 1
192 fi
193
194 if [[ ! -d $coredir ]]; then
195 echo "core dump directory ($coredir) does not exist, creating it."
196 or_die mkdir -p "$coredir"
197 fi
198
199 if [[ ! -w $coredir ]]; then
200 echo "core dump directory ($coredir) is not writable."
201 exit 1
202 fi
203
204 or_die rm -f ztest.history
205 or_die rm -f ztest.ddt
206 or_die rm -f ztest.cores
207
208 # start rngd in the background so we don't run out of entropy
209 or_die read -r rngdpid < <(rngd -f -r /dev/urandom & echo $!)
210
211 ztrc=0 # ztest return value
212 foundcrashes=0 # number of crashes found so far
213 starttime=$(date +%s)
214 curtime=$starttime
215
216 # if no timeout was specified, loop forever.
217 while [[ $timeout -eq 0 ]] || [[ $curtime -le $((starttime + timeout)) ]]; do
218 zopt="-VVVVV"
219
220 # start each run with an empty directory
221 workdir="$basedir/$rundir"
222 or_die rm -rf "$workdir"
223 or_die mkdir "$workdir"
224
225 # switch between common arrangements & fully randomized
226 if [[ $((RANDOM % 2)) -eq 0 ]]; then
227 mirrors=2
228 raidz=0
229 parity=1
230 vdevs=2
231 else
232 mirrors=$(((RANDOM % 3) * 1))
233 parity=$(((RANDOM % 3) + 1))
234 raidz=$((((RANDOM % 9) + parity + 1) * (RANDOM % 2)))
235 vdevs=$(((RANDOM % 3) + 3))
236 fi
237 align=$(((RANDOM % 2) * 3 + 9))
238 runtime=$((RANDOM % 100))
239 passtime=$((RANDOM % (runtime / 3 + 1) + 10))
240
241 zopt="$zopt -m $mirrors"
242 zopt="$zopt -r $raidz"
243 zopt="$zopt -R $parity"
244 zopt="$zopt -v $vdevs"
245 zopt="$zopt -a $align"
246 zopt="$zopt -T $runtime"
247 zopt="$zopt -P $passtime"
248 zopt="$zopt -s $size"
249 zopt="$zopt -f $workdir"
250
251 # shellcheck disable=SC2124
252 cmd="$ZTEST $zopt $@"
253 desc="$(date '+%m/%d %T') $cmd"
254 echo "$desc" | tee -a ztest.history
255 echo "$desc" >>ztest.out
256 $cmd >>ztest.out 2>&1
257 ztrc=$?
258 egrep '===|WARNING' ztest.out >>ztest.history
259 $ZDB -U "$workdir/zpool.cache" -DD ztest >>ztest.ddt 2>&1
260
261 store_core
262
263 curtime=$(date +%s)
264 done
265
266 echo "zloop finished, $foundcrashes crashes found"
267
268 #restore core pattern
269 echo "$origcorepattern" > /proc/sys/kernel/core_pattern
270
271 uptime >>ztest.out
272
273 if [[ $foundcrashes -gt 0 ]]; then
274 exit 1
275 fi