]> git.ipfire.org Git - thirdparty/mdadm.git/blob - test
mdadm/test: Refactor and revamp 'test' script
[thirdparty/mdadm.git] / test
1 #!/bin/bash
2 #
3 # run test suite for mdadm
4 dir=$(pwd)
5 mdadm=$dir/mdadm
6 testdir="tests"
7 targetdir="/var/tmp"
8 logdir="$targetdir"
9 config=/tmp/mdadm.conf
10
11 savelogs=0
12 exitonerror=1
13 prefix='[0-9][0-9]'
14
15 # use loop devices by default if doesn't specify --dev
16 DEVTYPE=loop
17 INTEGRITY=yes
18 LVM_VOLGROUP=mdtest
19
20 # make sure to test local mdmon, not system one
21 export MDADM_NO_SYSTEMCTL=1
22
23 # assume md0, md1, md2 exist in /dev
24 md0=/dev/md0
25 md1=/dev/md1
26 md2=/dev/md2
27 mdp0=/dev/md_d0
28 mdp1=/dev/md_d1
29
30 # We test mdadm on loop-back block devices.
31 # dir for storing files should be settable by command line maybe
32 size=20000
33 # super0, round down to multiple of 64 and substract 64
34 mdsize0=19904
35 # super00 is nested, subtract 128
36 mdsize00=19840
37 # super1.0 round down to multiple of 2, subtract 8
38 mdsize1=19992
39 mdsize1a=19988
40 mdsize12=19988
41 # super1.2 for linear: round to multiple of 2, subtract 4
42 mdsize1_l=19996
43 mdsize2_l=19996
44 # subtract another 4 for bitmaps
45 mdsize1b=19988
46 mdsize11=19992
47 mdsize11a=19456
48 mdsize12=19988
49
50 # ddf needs bigger devices as 32Meg is reserved!
51 ddfsize=65536
52
53 # $1 is optional parameter, it shows why to save log
54 save_log() {
55 status=$1
56 logfile="$status""$_basename".log
57
58 cat $targetdir/stderr >> $targetdir/log
59 cp $targetdir/log $logdir/$_basename.log
60 echo "## $HOSTNAME: saving dmesg." >> $logdir/$logfile
61 dmesg -c >> $logdir/$logfile
62 $mdadm -As 2> /dev/null
63 echo "## $HOSTNAME: saving proc mdstat." >> $logdir/$logfile
64 cat /proc/mdstat >> $logdir/$logfile
65 array=($(mdadm -Ds | cut -d' ' -f2))
66 echo "## $HOSTNAME: mdadm -D ${array[@]}" >> $logdir/$logfile
67 $mdadm -D ${array[@]} >> $logdir/$logfile
68 [ "$1" == "fail" ] &&
69 echo "FAILED - see $logdir/$_basename.log and $logdir/$logfile for details"
70 # ignore saving external(external file, imsm...) bitmap
71 cat /proc/mdstat | grep -q "linear\|external" && return 0
72 if [ $DEVTYPE == 'lvm' ]
73 then
74 # not supported lvm type yet
75 echo
76 elif [ $DEVTYPE == 'loop' ]
77 then
78 if [ ! -z ${array[@]} -a ${#array[@]} -ge 1 ]
79 then
80 md_disks=($($mdadm -D -Y ${array[@]} | grep "/dev/$DEVTYPE" | cut -d'=' -f2))
81 cat /proc/mdstat | grep -q "bitmap"
82 if [ $? -eq 0 ]
83 then
84 echo "## $HOSTNAME: mdadm -X ${md_disks[@]}" >> $logdir/$logfile
85 $mdadm -X ${md_disks[@]} >> $logdir/$logfile
86 fi
87 else
88 echo "## $HOSTNAME: no array assembled!" >> $logdir/$logfile
89 fi
90 fi
91 }
92
93 die() {
94 echo -e "\n\tERROR: $* \n"
95 save_log fail
96 exit 2
97 }
98
99 cleanup() {
100 udevadm settle
101 $mdadm -Ssq 2> /dev/null
102 case $DEVTYPE in
103 loop )
104 for d in 0 1 2 3 4 5 6 7 8 9 10 11 12 13
105 do
106 losetup -d /dev/loop$d
107 rm -f /dev/disk/by-path/loop*
108 rm -f /var/tmp/mdtest$d
109 done
110 ;;
111 lvm )
112 for d in 0 1 2 3 4 5 6 7 8 9 10 11 12 13
113 do
114 eval "lvremove --quiet -f \$dev$d"
115 done
116 ;;
117 esac
118 }
119
120 ctrl_c() {
121 exitonerror=1
122 }
123
124 do_setup() {
125 trap cleanup 0 1 3 15
126 trap ctrl_c 2
127
128 [ -d $logdir ] || mkdir -p $logdir
129 dmesg -c > /dev/null
130
131 if [ "$DEVTYPE" == "loop" ]
132 then
133 # make sure there are no loop devices remaining.
134 # udev started things can sometimes prevent them being stopped
135 # immediately
136 while grep loop /proc/partitions > /dev/null 2>&1
137 do
138 $mdadm -Ssq
139 losetup -d /dev/loop[0-9]* 2> /dev/null
140 sleep 0.2
141 done
142 fi
143 devlist=
144 for d in 0 1 2 3 4 5 6 7 8 9 10 11 12 13
145 do
146 sz=$size
147 [ $d -gt 7 ] && sz=$ddfsize
148 case $DEVTYPE in
149 loop)
150 [ -f $targetdir/mdtest$d ] ||
151 dd if=/dev/zero of=$targetdir/mdtest$d count=$sz bs=1K > /dev/null 2>&1
152 # make sure udev doesn't touch
153 mdadm --zero $targetdir/mdtest$d 2> /dev/null
154 [ -b /dev/loop$d ] || mknod /dev/loop$d b 7 $d
155 if [ $d -eq 7 ]
156 then
157 losetup /dev/loop$d $targetdir/mdtest6 # for multipath use
158 else
159 losetup /dev/loop$d $targetdir/mdtest$d
160 fi
161 eval dev$d=/dev/loop$d
162 eval file$d=$targetdir/mdtest$d
163 ;;
164 lvm)
165 unset MULTIPATH
166 eval dev$d=/dev/mapper/${LVM_VOLGROUP}-mdtest$d
167 if ! lvcreate --quiet -L ${sz}K -n mdtest$d $LVM_VOLGROUP
168 then
169 trap '' 0 # make sure lvremove is not called
170 eval echo error creating \$dev$d
171 exit 129
172 fi
173 ;;
174 ram)
175 unset MULTIPATH
176 eval dev$d=/dev/ram$d
177 ;;
178 esac
179 eval devlist=\"\$devlist \$dev$d\"
180 eval devlist$d=\"\$devlist\"
181 #" <-- add this quote to un-confuse vim syntax highlighting
182 done
183 path0=$dev6
184 path1=$dev7
185 ulimit -c unlimited
186 [ -f /proc/mdstat ] || modprobe md_mod
187 echo 2000 > /proc/sys/dev/raid/speed_limit_max
188 echo 0 > /sys/module/md_mod/parameters/start_ro
189 }
190
191 # mdadm always adds --quiet, and we want to see any unexpected messages
192 mdadm() {
193 rm -f $targetdir/stderr
194 case $* in
195 *-S* )
196 udevadm settle
197 p=`cat /proc/sys/dev/raid/speed_limit_max`
198 echo 20000 > /proc/sys/dev/raid/speed_limit_max
199 ;;
200 esac
201 case $* in
202 *-C* | *--create* | *-B* | *--build* )
203 # clear superblock every time once creating or
204 # building arrays, because it's always creating
205 # and building array many times in a test case.
206 for args in $*
207 do
208 [[ $args =~ "/dev/" ]] && {
209 [[ $args =~ "md" ]] ||
210 $mdadm --zero $args > /dev/null
211 }
212 done
213 $mdadm 2> $targetdir/stderr --quiet "$@" --auto=yes
214 ;;
215 * )
216 $mdadm 2> $targetdir/stderr --quiet "$@"
217 ;;
218 esac
219 rv=$?
220 case $* in
221 *-S* )
222 udevadm settle
223 echo $p > /proc/sys/dev/raid/speed_limit_max
224 ;;
225 esac
226 cat >&2 $targetdir/stderr
227 return $rv
228 }
229
230 # check various things
231 check() {
232 case $1 in
233 spares )
234 spares=$(tr '] ' '\012\012' < /proc/mdstat | grep -c '(S)' || exit 0)
235 [ $spares -ne $2 ] &&
236 die "expected $2 spares, found $spares"
237 ;;
238 raid* | linear )
239 grep -sq "active $1 " /proc/mdstat ||
240 die "active $1 not found"
241 ;;
242 algorithm )
243 grep -sq " algorithm $2 " /proc/mdstat ||
244 die "algorithm $2 not found"
245 ;;
246 resync | recovery | reshape )
247 cnt=5
248 while ! grep -sq $1 /proc/mdstat
249 do
250 if [ $cnt -gt 0 ] && grep -v idle /sys/block/md*/md/sync_action > /dev/null
251 then # Something isn't idle - wait a bit
252 sleep 0.5
253 cnt=$[cnt-1]
254 else
255 die "no $1 happening"
256 fi
257 done
258 ;;
259 nosync )
260 sleep 0.5
261 # Since 4.2 we delay the close of recovery until there has been a chance for
262 # spares to be activated. That means that a recovery that finds nothing
263 # to do can still take a little longer than expected.
264 # add an extra check: is sync_completed shows the end is reached, assume
265 # there is no recovery.
266 if grep -sq -E '(resync|recovery|reshape) *=' /proc/mdstat
267 then
268 incomplete=`grep / /sys/block/md*/md/sync_completed 2> /dev/null | sed '/^ *\([0-9]*\) \/ \1/d'`
269 [ -n "$incomplete" ] &&
270 die "resync or recovery is happening!"
271 fi
272 ;;
273 wait )
274 p=`cat /proc/sys/dev/raid/speed_limit_max`
275 echo 2000000 > /proc/sys/dev/raid/speed_limit_max
276 sleep 0.1
277 while grep -Eq '(resync|recovery|reshape|check|repair) *=' /proc/mdstat ||
278 grep -v idle > /dev/null /sys/block/md*/md/sync_action
279 do
280 sleep 0.5
281 done
282 echo $p > /proc/sys/dev/raid/speed_limit_max
283 ;;
284 state )
285 grep -sq "blocks.*\[$2\]\$" /proc/mdstat ||
286 die "state $2 not found!"
287 sleep 0.5
288 ;;
289 bitmap )
290 grep -sq bitmap /proc/mdstat ||
291 die "no bitmap"
292 ;;
293 nobitmap )
294 grep -sq "bitmap" /proc/mdstat &&
295 die "bitmap present"
296 ;;
297 readonly )
298 grep -sq "read-only" /proc/mdstat ||
299 die "array is not read-only!"
300 ;;
301 inactive )
302 grep -sq "inactive" /proc/mdstat ||
303 die "array is not inactive!"
304 ;;
305 * )
306 die "unknown check $1"
307 ;;
308 esac
309 }
310
311 no_errors() {
312 if [ -s $targetdir/stderr ]
313 then
314 echo Bad errors from mdadm:
315 cat $targetdir/stderr
316 exit 2
317 fi
318 }
319
320 # basic device test
321 testdev() {
322 [ -b $1 ] || die "$1 isn't a block device."
323 udevadm settle
324 dev=$1
325 cnt=$2
326 dvsize=$3
327 chunk=$4
328 if [ -z "$5" ]
329 then
330 mkfs.ext3 -F -j $dev > /dev/null 2>&1 && fsck -fn $dev >&2
331 fi
332 dsize=$[dvsize/chunk]
333 dsize=$[dsize*chunk]
334 rasize=$[dsize*2*cnt]
335 # rasize is in sectors
336 if [ -n "$DEV_ROUND_K" ]
337 then
338 rasize=$[rasize/DEV_ROUND_K/2]
339 rasize=$[rasize*DEV_ROUND_K*2]
340 fi
341 [ `/sbin/blockdev --getsize $dev` -eq 0 ] && sleep 2
342 _sz=`/sbin/blockdev --getsize $dev`
343 [ $rasize -lt $_sz -o $[rasize*4/5] -gt $_sz ] &&
344 die "size is wrong for $dev: $cnt * $dvsize (chunk=$chunk) = $rasize, not $_sz"
345 return 0
346 }
347
348 rotest() {
349 dev=$1
350 fsck -fn $dev >&2
351 }
352
353 do_test() {
354 _script=$1
355 _basename=`basename $_script`
356 if [ -f "$_script" ]
357 then
358 rm -f $targetdir/stderr
359 # stop all arrays, just incase some script left an array active.
360 $mdadm -Ssq 2> /dev/null
361 mdadm --zero $devlist 2> /dev/null
362 # this might have been reset: restore the default.
363 echo 2000 > /proc/sys/dev/raid/speed_limit_max
364 # source script in a subshell, so it has access to our
365 # namespace, but cannot change it.
366 echo -ne "$_script... "
367 if ( set -ex ; . $_script ) &> $targetdir/log
368 then
369 dmesg | grep -iq "error\|call trace\|segfault" &&
370 die "dmesg prints errors when testing $_basename!"
371 echo "succeeded"
372 _fail=0
373 else
374 save_log fail
375 _fail=1
376 fi
377 [ "$savelogs" == "1" ] &&
378 mv -f $targetdir/log $logdir/$_basename.log
379 [ "$_fail" == "1" -a "$exitonerror" == "1" ] && exit 1
380 fi
381 }
382
383 do_help() {
384 cat <<-EOF
385 Usage: $0 [options]
386 Options:
387 --tests=test1,test2,... Comma separated list of tests to run
388 --disable-multipath Disable any tests involving multipath
389 --disable-integrity Disable slow tests of RAID[56] consistency
390 --logdir=directory Directory to save all logfiles in
391 --save-logs Usually use with --logdir together
392 --keep-going | --no-error Don't stop on error, ie. run all tests
393 --dev=loop|lvm|ram Use loop devices (default), LVM, or RAM disk
394 --volgroup=name LVM volume group for LVM test
395 setup Setup test environment and exit
396 cleanup Cleanup test environment
397 prefix Run tests with <prefix>
398 --help | -h Print this usage
399 EOF
400 }
401
402 parse_args() {
403 for i in $*
404 do
405 case $i in
406 [0-9][0-9] )
407 prefix=$i
408 ;;
409 setup )
410 echo "mdadm test environment setup"
411 do_setup
412 trap 0
413 exit 0
414 ;;
415 cleanup )
416 cleanup
417 exit 0
418 ;;
419 --tests=* )
420 TESTLIST=($(echo ${i##*=} | sed -e 's/,/ /g'))
421 ;;
422 --logdir=* )
423 logdir="${i##*=}"
424 ;;
425 --save-logs )
426 savelogs=1
427 ;;
428 --keep-going | --no-error )
429 exitonerror=0
430 ;;
431 --disable-multipath )
432 unset MULTIPATH
433 ;;
434 --disable-integrity )
435 unset INTEGRITY
436 ;;
437 --dev=* )
438 case ${i##*=} in
439 loop )
440 DEVTYPE=loop
441 ;;
442 lvm )
443 DEVTYPE=lvm
444 ;;
445 ram )
446 DEVTYPE=ram
447 ;;
448 * )
449 echo "Unknown argument: $i"
450 do_help
451 exit 1
452 ;;
453 esac
454 ;;
455 --volgroup=* )
456 LVM_VOLGROUP=`expr "x$i" : 'x[^=]*=\(.*\)'`
457 ;;
458 --help | -h )
459 do_help
460 exit 0
461 ;;
462 * )
463 echo " $0: Unknown argument: $i"
464 do_help
465 exit 1
466 ;;
467 esac
468 done
469 }
470
471 check_env() {
472 user=$(id -un)
473 [ "X$user" != "Xroot" ] && {
474 echo "test: testing can only be done as 'root'."
475 exit 1
476 }
477 [ -x "raid6check" -a -x $mdadm ] || {
478 echo "test: please run 'make everything' before perform testing."
479 exit 1
480 }
481 cmds=(mdadm lsblk df udevadm losetup mkfs.ext3 fsck)
482 for cmd in ${cmds[@]}
483 do
484 which $cmd > /dev/null || {
485 echo "$cmd command not found!"
486 exit 1
487 }
488 done
489 mdadm_src_ver="$($mdadm -V 2>&1)"
490 mdadm_sbin_ver="$($(which mdadm) -V 2>&1)"
491 if [ "$mdadm_src_ver" != "$mdadm_sbin_ver" ]
492 then
493 # it's nessesary to 'make install' mdadm to /SBIN/DIR,
494 # such as systemd/mdadm-grow-continue@.service, would
495 # run as an instance by systemd when reshape happens,
496 # thus ensure that the correct mdadm is in testing.
497 echo "test: please run 'make install' before testing."
498 exit 1
499 fi
500 if ! $(df -T . | grep -iq ext)
501 then
502 # 'external file' bitmap only supports with ext[2-4] file system
503 echo "test: please run test suite with ext[2-4] file system."
504 exit 1
505 fi
506 if $(lsblk -a | grep -iq raid)
507 then
508 # donot run mdadm -Ss directly if there are RAIDs working.
509 echo "test: please run test suite without running RAIDs environment."
510 exit 1
511 fi
512 # Check whether to run multipath tests
513 modprobe multipath 2> /dev/null
514 grep -sq 'Personalities : .*multipath' /proc/mdstat &&
515 MULTIPATH="yes"
516 }
517
518 main() {
519 check_env
520 do_setup
521
522 echo "Testing on linux-$(uname -r) kernel"
523 [ "$savelogs" == "1" ] &&
524 echo "Saving logs to $logdir"
525 if [ "x$TESTLIST" != "x" ]
526 then
527 for script in ${TESTLIST[@]}
528 do
529 do_test $testdir/$script
530 done
531 else
532 for script in $testdir/$prefix $testdir/$prefix*[^~]
533 do
534 do_test $script
535 done
536 fi
537
538 exit 0
539 }
540
541 parse_args $@
542 main