]> git.ipfire.org Git - thirdparty/mdadm.git/blob - test
3f8515fe17e14fdc51cf1d22fb73621d34f8a535
[thirdparty/mdadm.git] / test
1 #!/bin/bash
2 #
3 # run test suite for mdadm
4 user=`id -un`
5 if [ " $user" != " root" ]
6 then echo >&2 "test: testing can only be done as 'root'."
7 exit 1;
8 fi
9
10 prefix='[0-9][0-9]'
11
12 dir=`pwd`
13 mdadm=$dir/mdadm
14 if [ \! -x $mdadm ]
15 then
16 echo >&2 "test: $mdadm isn't usable."
17 fi
18
19 testdir="tests"
20 logdir="$testdir/logs"
21 logsave=0
22 exitonerror=1
23
24 # Check whether to run multipath tests
25 modprobe multipath 2> /dev/null
26 if grep -s 'Personalities : .*multipath' > /dev/null /proc/mdstat ; then
27 MULTIPATH="yes"
28 fi
29
30 # assume md0, md1, md2 exist in /dev
31 md0=/dev/md0 md1=/dev/md1 md2=/dev/md2
32 mdp0=/dev/md_d0
33 mdp1=/dev/md_d1
34
35 # We test mdadm on loop-back block devices.
36 # dir for storing files should be settable by command line maybe
37 targetdir=/var/tmp
38 size=20000
39 # super0, round down to multiple of 64 and substract 64
40 mdsize0=19904
41 # super00 is nested, subtract 128
42 mdsize00=19840
43 # super1.0 round down to multiple of 2, subtract 8
44 mdsize1=19992
45 mdsize1a=19988
46 mdsize12=19988
47 # super1.2 for linear: round to multiple of 2, subtract 4
48 mdsize1_l=19996
49 mdsize2_l=19996
50 # subtract another 4 for bitmaps
51 mdsize1b=19988
52 mdsize11=19992
53 mdsize11a=19456
54 mdsize12=19988
55
56 # ddf needs bigger devices as 32Meg is reserved!
57 ddfsize=65536
58
59 config=/tmp/mdadm.conf
60
61 cleanup() {
62 udevadm settle
63 $mdadm -Ssq 2> /dev/null
64 for d in 0 1 2 3 4 5 6 7 8 9 10 11 12
65 do
66 losetup -d /dev/loop$d ; # rm -f $targetdir/mdtest$d
67 rm -f /dev/disk/by-path/loop*
68 done
69 }
70
71 ctrl-c() {
72 exitonerror=1
73 }
74
75 do_setup() {
76 trap cleanup 0 1 3 15
77 trap ctrl-c 2
78
79 devlist=
80 for d in 0 1 2 3 4 5 6 7 8 9 10 11 12
81 do
82 sz=$size
83 if [ $d -gt 7 ]; then sz=$ddfsize ; fi
84 [ -f $targetdir/mdtest$d ] || dd if=/dev/zero of=$targetdir/mdtest$d count=$sz bs=1K > /dev/null 2>&1
85 [ -b /dev/loop$d ] || mknod /dev/loop$d b 7 $d
86 if [ $d -eq 7 ]
87 then
88 losetup /dev/loop$d $targetdir/mdtest6 # for multipath use
89 else
90 losetup /dev/loop$d $targetdir/mdtest$d
91 fi
92 eval dev$d=/dev/loop$d
93 eval file$d=$targetdir/mdtest$d
94 eval devlist=\"\$devlist \$dev$d\"
95 #" <-- add this quote to un-confuse vim syntax highlighting
96 done
97 path0=$dev6
98 path1=$dev7
99
100 ulimit -c unlimited
101 [ -f /proc/mdstat ] || modprobe md_mod
102 echo 2000 > /proc/sys/dev/raid/speed_limit_max
103 echo 0 > /sys/module/md_mod/parameters/start_ro
104 }
105
106 # mdadm always adds --quiet, and we want to see any unexpected messages
107 mdadm() {
108 rm -f $targetdir/stderr
109 case $* in
110 *-S* ) udevadm settle;;
111 esac
112 case $* in
113 *-C* ) $mdadm 2> $targetdir/stderr --quiet "$@" --auto=yes;;
114 * ) $mdadm 2> $targetdir/stderr --quiet "$@"
115 esac
116 rv=$?
117 case $* in
118 *-S* ) udevadm settle;;
119 esac
120 cat >&2 $targetdir/stderr
121 return $rv
122 }
123
124 # check various things
125 check() {
126 case $1 in
127 spares )
128 spares=`tr '] ' '\012\012' < /proc/mdstat | grep -c '(S)' || exit 0`
129 if [ $spares -ne $2 ]
130 then
131 echo >&2 "ERROR expected $2 spares, found $spares"; exit 1;
132 fi
133 ;;
134 raid* | linear )
135 grep -s "active $1 " /proc/mdstat > /dev/null || {
136 echo >&2 "ERROR active $1 not found" ; cat /proc/mdstat ; exit 1;}
137 ;;
138 resync | recovery | reshape)
139 sleep 0.5
140 grep -s $1 /proc/mdstat > /dev/null || {
141 echo >&2 ERROR no $1 happening; cat /proc/mdstat; exit 1; }
142 ;;
143
144 nosync )
145 sleep 0.5
146 if grep -s -E '(resync|recovery|reshape) *=' > /dev/null /proc/mdstat ; then
147 echo >&2 "ERROR resync or recovery is happening!"; cat /proc/mdstat ; exit 1;
148 fi
149 ;;
150
151 wait )
152 sleep 0.1
153 while grep -E '(resync|recovery|reshape|check|repair) *=' > /dev/null /proc/mdstat
154 do sleep 2;
155 done
156 ;;
157
158 state )
159 grep -s "blocks.*\[$2\]\$" /proc/mdstat > /dev/null || {
160 echo >&2 "ERROR state $2 not found!"; cat /proc/mdstat ; exit 1; }
161 sleep 0.5
162 ;;
163
164 bitmap )
165 grep -s bitmap > /dev/null /proc/mdstat || {
166 echo >&2 ERROR no bitmap ; cat /proc/mdstat ; exit 1; }
167 ;;
168 nobitmap )
169 if grep -s "bitmap" > /dev/null /proc/mdstat
170 then
171 echo >&2 ERROR bitmap present ; cat /proc/mdstat ; exit 1;
172 fi
173 ;;
174
175 * ) echo >&2 ERROR unknown check $1 ; exit 1;
176 esac
177 }
178
179 no_errors() {
180 if [ -s $targetdir/stderr ]
181 then echo Bad errors from mdadm: ; cat $targetdir/stderr; exit 2;
182 fi
183 }
184 # basic device test
185
186 testdev() {
187 udevadm settle
188 dev=$1
189 cnt=$2
190 dvsize=$3
191 chunk=$4
192 if [ -z "$5" ]; then
193 mkfs -j $dev > /dev/null 2>&1 && fsck -fn $dev >&2
194 fi
195 dsize=$[dvsize/chunk]
196 dsize=$[dsize*chunk]
197 rasize=$[dsize*2*cnt]
198 # rasize is in sectors
199 if [ -n "$DEV_ROUND_K" ]; then
200 rasize=$[rasize/DEV_ROUND_K/2]
201 rasize=$[rasize*DEV_ROUND_K*2]
202 fi
203 if [ `/sbin/blockdev --getsize $dev` -eq 0 ]; then sleep 2 ; fi
204 _sz=`/sbin/blockdev --getsize $dev`
205 if [ $rasize -lt $_sz -o $[rasize*4/5] -gt $_sz ]
206 then
207 echo "ERROR: size is wrong for $dev: $cnt * $dvsize (chunk=$chunk) = $rasize, not $_sz"
208 exit 1
209 fi
210 }
211
212 rotest() {
213 dev=$1
214 fsck -fn $dev >&2
215 }
216
217 do_test() {
218 _script=$1
219 _basename=`basename $_script`
220 if [ -f "$_script" ]
221 then
222 rm -f $targetdir/stderr
223 # stop all arrays, just incase some script left an array active.
224 $mdadm -Ssq 2> /dev/null
225 mdadm --zero $devlist 2> /dev/null
226 mdadm --zero $devlist 2> /dev/null
227 # source script in a subshell, so it has access to our
228 # namespace, but cannot change it.
229 echo -ne "$_script... "
230 if ( set -ex ; . $_script ) &> $targetdir/log
231 then
232 echo "succeeded"
233 _fail=0
234 else
235 log=log
236 if [ $exitonerror == 0 ]; then
237 log=log-`basename $_script`
238 mv $targetdir/log $targetdir/$log
239 fi
240 echo "FAILED - see $targetdir/$log for details"
241 _fail=1
242 fi
243 if [ "$savelogs" == "1" ]; then
244 cp $targetdir/log $logdir/$_basename.log
245 fi
246 if [ "$_fail" == "1" -a "$exitonerror" == "1" ]; then
247 exit 1
248 fi
249 fi
250 }
251
252 do_help() {
253 echo "Usage: $0 [options]"
254 echo " Options:"
255 echo " --tests=<test1,test2,..> Comma separated list of tests to run"
256 echo " --disable-multipath Disable any tests involving multipath"
257 echo " --logdir=<directory> Directory to save logfiles in"
258 echo " --save-logs Save all logs in <logdir>"
259 echo " --keep-going Don't stop on error, ie. run all tests"
260 echo " setup Setup test environment and exit"
261 echo " cleanup Cleanup test environment"
262 echo " <prefix> Run tests with <prefix>"
263 }
264
265 parse_args() {
266 for i in $*
267 do
268 case $i in
269 [0-9]*)
270 prefix=$i
271 ;;
272 setup)
273 echo "mdadm test environment setup"
274 do_setup
275 trap 0; exit 0
276 ;;
277 cleanup)
278 cleanup
279 exit 0
280 ;;
281 --tests=*)
282 TESTLIST=`expr "x$i" : 'x[^=]*=\(.*\)' | sed -e 's/,/ /g'`
283 ;;
284 --logdir=*)
285 logdir=`expr "x$i" : 'x[^=]*=\(.*\)'`
286 ;;
287 --save-logs)
288 savelogs=1
289 if [ ! -d $logdir ] ; then
290 mkdir $logdir
291 if [ $? -ne 0 ] ; then
292 exit 1;
293 fi
294 fi
295 ;;
296 --keep-going | --no-error)
297 exitonerror=0
298 ;;
299 --disable-multipath)
300 unset MULTIPATH
301 ;;
302 --help)
303 do_help
304 exit 0;
305 ;;
306 -*)
307 echo " $0: Unknown argument: $i"
308 do_help
309 exit 0;
310 ;;
311 esac
312 done
313 }
314
315 parse_args $@
316
317 do_setup
318
319 if [ "$savelogs" == "1" ]; then
320 echo "Saving logs to $logdir"
321 fi
322
323 if [ "x$TESTLIST" != "x" ]; then
324 for script in $TESTLIST
325 do
326 do_test $testdir/$script
327 done
328 else
329 for script in $testdir/$prefix $testdir/$prefix*[^~]
330 do
331 do_test $script
332 done
333 fi
334 exit 0