]> git.proxmox.com Git - mirror_zfs-debian.git/blame - scripts/zloop.sh
Prepare upload of 0.7.3-1
[mirror_zfs-debian.git] / scripts / zloop.sh
CommitLineData
cae5b340
AX
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
23basedir=$(dirname "$0")
24
25SCRIPT_COMMON=common.sh
26if [ -f "${basedir}/${SCRIPT_COMMON}" ]; then
27 . "${basedir}/${SCRIPT_COMMON}"
28else
29 echo "Missing helper script ${SCRIPT_COMMON}" && exit 1
30fi
31
32# shellcheck disable=SC2034
33PROG=zloop.sh
34
35DEFAULTWORKDIR=/var/tmp
36DEFAULTCOREDIR=/var/tmp/zloop
37
38function usage
39{
8ec27e97 40 echo -e "\n$0 [-t <timeout>] [ -s <vdev size> ] [-c <dump directory>]" \
cae5b340
AX
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" \
8ec27e97 52 " -s Size of vdev devices.\n" \
cae5b340
AX
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
59function 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
72origcorepattern="$(cat /proc/sys/kernel/core_pattern)"
73coreglob="$(egrep -o '^([^|%[:space:]]*)' /proc/sys/kernel/core_pattern)*"
74
75if [[ $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)*"
80fi
81
82function core_file
83{
84 # shellcheck disable=SC2012 disable=2086
85 printf "%s" "$(ls -tr1 $coreglob 2> /dev/null | head -1)"
86}
87
88function 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
100function store_core
101{
102 core="$(core_file)"
103 if [[ $ztrc -ne 0 ]] || [[ -f "$core" ]]; then
104 coreid=$(date "+zloop-%y%m%d-%H%M%S")
105 foundcrashes=$((foundcrashes + 1))
106
107 dest=$coredir/$coreid
108 or_die mkdir -p "$dest"
109 or_die mkdir -p "$dest/vdev"
110
111 echo "*** ztest crash found - moving logs to $dest"
112
113 or_die mv ztest.history "$dest/"
114 or_die mv ztest.ddt "$dest/"
115 or_die mv ztest.out "$dest/"
116 or_die mv "$workdir/ztest*" "$dest/vdev/"
117 or_die mv "$workdir/zpool.cache" "$dest/vdev/"
118
119 # check for core
120 if [[ -f "$core" ]]; then
121 coreprog=$(core_prog "$core")
122 corestatus=$($GDB --batch --quiet \
123 -ex "set print thread-events off" \
124 -ex "printf \"*\n* Backtrace \n*\n\"" \
125 -ex "bt" \
126 -ex "printf \"*\n* Libraries \n*\n\"" \
127 -ex "info sharedlib" \
128 -ex "printf \"*\n* Threads (full) \n*\n\"" \
129 -ex "info threads" \
130 -ex "printf \"*\n* Backtraces \n*\n\"" \
131 -ex "thread apply all bt" \
132 -ex "printf \"*\n* Backtraces (full) \n*\n\"" \
133 -ex "thread apply all bt full" \
134 -ex "quit" "$coreprog" "$core" | grep -v "New LWP")
135
136 # Dump core + logs to stored directory
137 echo "$corestatus" >>"$dest/status"
138 or_die mv "$core" "$dest/"
139
140 # Record info in cores logfile
141 echo "*** core @ $coredir/$coreid/$core:" | \
142 tee -a ztest.cores
143 echo "$corestatus" | tee -a ztest.cores
144 echo "" | tee -a ztest.cores
145 fi
146 echo "continuing..."
147 fi
148}
149
150# parse arguments
151# expected format: zloop [-t timeout] [-c coredir] [-- extra ztest args]
152coredir=$DEFAULTCOREDIR
153workdir=$DEFAULTWORKDIR
154timeout=0
8ec27e97
AX
155size="512m"
156while getopts ":ht:s:c:f:" opt; do
cae5b340
AX
157 case $opt in
158 t ) [[ $OPTARG -gt 0 ]] && timeout=$OPTARG ;;
8ec27e97 159 s ) [[ $OPTARG ]] && size=$OPTARG ;;
cae5b340
AX
160 c ) [[ $OPTARG ]] && coredir=$OPTARG ;;
161 f ) [[ $OPTARG ]] && workdir=$(readlink -f "$OPTARG") ;;
162 h ) usage
163 exit 2
164 ;;
165 * ) echo "Invalid argument: -$OPTARG";
166 usage
167 exit 1
168 esac
169done
170# pass remaining arguments on to ztest
171shift $((OPTIND - 1))
172
173# enable core dumps
174ulimit -c unlimited
175
176if [[ -f "$(core_file)" ]]; then
177 echo -n "There's a core dump here you might want to look at first... "
178 core_file
179 exit 1
180fi
181
182if [[ ! -d $coredir ]]; then
183 echo "core dump directory ($coredir) does not exist, creating it."
184 or_die mkdir -p "$coredir"
185fi
186
187if [[ ! -w $coredir ]]; then
188 echo "core dump directory ($coredir) is not writable."
189 exit 1
190fi
191
192or_die rm -f ztest.history
193or_die rm -f ztest.ddt
194or_die rm -f ztest.cores
195
196ztrc=0 # ztest return value
197foundcrashes=0 # number of crashes found so far
198starttime=$(date +%s)
199curtime=$starttime
200
201# if no timeout was specified, loop forever.
202while [[ $timeout -eq 0 ]] || [[ $curtime -le $((starttime + timeout)) ]]; do
203 zopt="-VVVVV"
204
205 # switch between common arrangements & fully randomized
206 if [[ $((RANDOM % 2)) -eq 0 ]]; then
207 mirrors=2
208 raidz=0
209 parity=1
210 vdevs=2
211 else
212 mirrors=$(((RANDOM % 3) * 1))
213 parity=$(((RANDOM % 3) + 1))
214 raidz=$((((RANDOM % 9) + parity + 1) * (RANDOM % 2)))
215 vdevs=$(((RANDOM % 3) + 3))
216 fi
217 align=$(((RANDOM % 2) * 3 + 9))
218 runtime=$((RANDOM % 100))
219 passtime=$((RANDOM % (runtime / 3 + 1) + 10))
cae5b340
AX
220
221 zopt="$zopt -m $mirrors"
222 zopt="$zopt -r $raidz"
223 zopt="$zopt -R $parity"
224 zopt="$zopt -v $vdevs"
225 zopt="$zopt -a $align"
226 zopt="$zopt -T $runtime"
227 zopt="$zopt -P $passtime"
228 zopt="$zopt -s $size"
229 zopt="$zopt -f $workdir"
230
231 # shellcheck disable=SC2124
232 cmd="$ZTEST $zopt $@"
233 desc="$(date '+%m/%d %T') $cmd"
234 echo "$desc" | tee -a ztest.history
235 echo "$desc" >>ztest.out
236 $cmd >>ztest.out 2>&1
237 ztrc=$?
238 egrep '===|WARNING' ztest.out >>ztest.history
239 $ZDB -U "$workdir/zpool.cache" -DD ztest >>ztest.ddt
240
241 store_core
242
243 curtime=$(date +%s)
244done
245
246echo "zloop finished, $foundcrashes crashes found"
247
248#restore core pattern
249echo "$origcorepattern" > /proc/sys/kernel/core_pattern
250
251uptime >>ztest.out
252
253if [[ $foundcrashes -gt 0 ]]; then
254 exit 1
255fi