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