]> git.ipfire.org Git - thirdparty/mdadm.git/blame - test
mdadm/test: Add '--raidtype=' to run different raidlevel cases
[thirdparty/mdadm.git] / test
CommitLineData
23b7d3b9 1#!/bin/bash
5e7519fa
NB
2#
3# run test suite for mdadm
20d10b4b 4dir=$(pwd)
5e7519fa 5mdadm=$dir/mdadm
60381555 6testdir="tests"
20d10b4b
ZL
7targetdir="/var/tmp"
8logdir="$targetdir"
9config=/tmp/mdadm.conf
60381555 10
20d10b4b
ZL
11savelogs=0
12exitonerror=1
13prefix='[0-9][0-9]'
98a92cff 14
20d10b4b 15# use loop devices by default if doesn't specify --dev
7d8a70bf 16DEVTYPE=loop
20d10b4b 17INTEGRITY=yes
7d8a70bf 18LVM_VOLGROUP=mdtest
04c1ca5f 19
9540cc24
N
20# make sure to test local mdmon, not system one
21export MDADM_NO_SYSTEMCTL=1
22
5e7519fa 23# assume md0, md1, md2 exist in /dev
662c349a
ZL
24md0=/dev/md0
25md1=/dev/md1
26md2=/dev/md2
1f48664b
NB
27mdp0=/dev/md_d0
28mdp1=/dev/md_d1
5e7519fa
NB
29
30# We test mdadm on loop-back block devices.
31# dir for storing files should be settable by command line maybe
5e7519fa 32size=20000
69646c14 33# super0, round down to multiple of 64 and substract 64
5e7519fa 34mdsize0=19904
1c203a4b
NB
35# super00 is nested, subtract 128
36mdsize00=19840
69646c14 37# super1.0 round down to multiple of 2, subtract 8
1bf4e2d9 38mdsize1=19992
ebe6ea0c
N
39mdsize1a=19988
40mdsize12=19988
41# super1.2 for linear: round to multiple of 2, subtract 4
3c8b2739
N
42mdsize1_l=19996
43mdsize2_l=19996
69646c14
NB
44# subtract another 4 for bitmaps
45mdsize1b=19988
46mdsize11=19992
3c8b2739 47mdsize11a=19456
69646c14 48mdsize12=19988
5e7519fa 49
d50683f9
N
50# ddf needs bigger devices as 32Meg is reserved!
51ddfsize=65536
52
20d10b4b
ZL
53# $1 is optional parameter, it shows why to save log
54save_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
93die() {
94 echo -e "\n\tERROR: $* \n"
95 save_log fail
96 exit 2
97}
ed02d9cc 98
5e7519fa 99cleanup() {
2952742d 100 udevadm settle
4f8a3e5b 101 $mdadm -Ssq 2> /dev/null
662c349a 102 case $DEVTYPE in
20d10b4b 103 loop )
662c349a
ZL
104 for d in 0 1 2 3 4 5 6 7 8 9 10 11 12 13
105 do
20d10b4b 106 losetup -d /dev/loop$d
662c349a 107 rm -f /dev/disk/by-path/loop*
20d10b4b 108 rm -f /var/tmp/mdtest$d
662c349a
ZL
109 done
110 ;;
20d10b4b 111 lvm )
662c349a
ZL
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
5e7519fa
NB
118}
119
d5a221a5 120ctrl_c() {
4d5995c4
GE
121 exitonerror=1
122}
123
572d7091 124do_setup() {
662c349a
ZL
125 trap cleanup 0 1 3 15
126 trap ctrl_c 2
5e7519fa 127
20d10b4b
ZL
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
662c349a
ZL
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
20d10b4b 147 [ $d -gt 7 ] && sz=$ddfsize
662c349a
ZL
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
572d7091 189}
0aa389dc 190
dab6685f
NB
191# mdadm always adds --quiet, and we want to see any unexpected messages
192mdadm() {
662c349a
ZL
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
20d10b4b
ZL
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
662c349a
ZL
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
dab6685f
NB
228}
229
230# check various things
231check() {
662c349a
ZL
232 case $1 in
233 spares )
20d10b4b
ZL
234 spares=$(tr '] ' '\012\012' < /proc/mdstat | grep -c '(S)' || exit 0)
235 [ $spares -ne $2 ] &&
236 die "expected $2 spares, found $spares"
662c349a
ZL
237 ;;
238 raid* | linear )
20d10b4b
ZL
239 grep -sq "active $1 " /proc/mdstat ||
240 die "active $1 not found"
662c349a
ZL
241 ;;
242 algorithm )
20d10b4b
ZL
243 grep -sq " algorithm $2 " /proc/mdstat ||
244 die "algorithm $2 not found"
662c349a
ZL
245 ;;
246 resync | recovery | reshape )
247 cnt=5
20d10b4b 248 while ! grep -sq $1 /proc/mdstat
662c349a
ZL
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
20d10b4b 255 die "no $1 happening"
662c349a
ZL
256 fi
257 done
258 ;;
259 nosync )
a76b3a34 260 sleep 0.5
662c349a
ZL
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.
20d10b4b 266 if grep -sq -E '(resync|recovery|reshape) *=' /proc/mdstat
662c349a
ZL
267 then
268 incomplete=`grep / /sys/block/md*/md/sync_completed 2> /dev/null | sed '/^ *\([0-9]*\) \/ \1/d'`
20d10b4b
ZL
269 [ -n "$incomplete" ] &&
270 die "resync or recovery is happening!"
662c349a
ZL
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
20d10b4b 277 while grep -Eq '(resync|recovery|reshape|check|repair) *=' /proc/mdstat ||
662c349a
ZL
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 )
20d10b4b
ZL
285 grep -sq "blocks.*\[$2\]\$" /proc/mdstat ||
286 die "state $2 not found!"
662c349a
ZL
287 sleep 0.5
288 ;;
289 bitmap )
20d10b4b
ZL
290 grep -sq bitmap /proc/mdstat ||
291 die "no bitmap"
662c349a
ZL
292 ;;
293 nobitmap )
20d10b4b
ZL
294 grep -sq "bitmap" /proc/mdstat &&
295 die "bitmap present"
662c349a
ZL
296 ;;
297 readonly )
20d10b4b
ZL
298 grep -sq "read-only" /proc/mdstat ||
299 die "array is not read-only!"
662c349a
ZL
300 ;;
301 inactive )
20d10b4b
ZL
302 grep -sq "inactive" /proc/mdstat ||
303 die "array is not inactive!"
662c349a
ZL
304 ;;
305 * )
20d10b4b 306 die "unknown check $1"
662c349a
ZL
307 ;;
308 esac
dab6685f
NB
309}
310
bf4fb153 311no_errors() {
662c349a
ZL
312 if [ -s $targetdir/stderr ]
313 then
314 echo Bad errors from mdadm:
315 cat $targetdir/stderr
316 exit 2
317 fi
bf4fb153 318}
dab6685f 319
662c349a 320# basic device test
dab6685f 321testdev() {
20d10b4b 322 [ -b $1 ] || die "$1 isn't a block device."
662c349a
ZL
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
20d10b4b 341 [ `/sbin/blockdev --getsize $dev` -eq 0 ] && sleep 2
662c349a 342 _sz=`/sbin/blockdev --getsize $dev`
20d10b4b
ZL
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
6fd2a36f
N
346}
347
c13c45e9 348rotest() {
662c349a
ZL
349 dev=$1
350 fsck -fn $dev >&2
c13c45e9
NB
351}
352
4e5ce543 353do_test() {
662c349a
ZL
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
662c349a
ZL
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
20d10b4b
ZL
369 dmesg | grep -iq "error\|call trace\|segfault" &&
370 die "dmesg prints errors when testing $_basename!"
662c349a
ZL
371 echo "succeeded"
372 _fail=0
373 else
20d10b4b 374 save_log fail
662c349a
ZL
375 _fail=1
376 fi
20d10b4b
ZL
377 [ "$savelogs" == "1" ] &&
378 mv -f $targetdir/log $logdir/$_basename.log
379 [ "$_fail" == "1" -a "$exitonerror" == "1" ] && exit 1
662c349a 380 fi
4e5ce543
JS
381}
382
383do_help() {
20d10b4b
ZL
384 cat <<-EOF
385 Usage: $0 [options]
386 Options:
387 --tests=test1,test2,... Comma separated list of tests to run
8403b202 388 --raidtype= raid0|linear|raid1|raid456|raid10|ddf|imsm
20d10b4b
ZL
389 --disable-multipath Disable any tests involving multipath
390 --disable-integrity Disable slow tests of RAID[56] consistency
391 --logdir=directory Directory to save all logfiles in
392 --save-logs Usually use with --logdir together
393 --keep-going | --no-error Don't stop on error, ie. run all tests
394 --dev=loop|lvm|ram Use loop devices (default), LVM, or RAM disk
395 --volgroup=name LVM volume group for LVM test
396 setup Setup test environment and exit
397 cleanup Cleanup test environment
398 prefix Run tests with <prefix>
399 --help | -h Print this usage
400 EOF
4e5ce543
JS
401}
402
403parse_args() {
662c349a
ZL
404 for i in $*
405 do
406 case $i in
20d10b4b 407 [0-9][0-9] )
662c349a
ZL
408 prefix=$i
409 ;;
410 setup )
411 echo "mdadm test environment setup"
412 do_setup
413 trap 0
414 exit 0
415 ;;
416 cleanup )
417 cleanup
418 exit 0
419 ;;
420 --tests=* )
20d10b4b 421 TESTLIST=($(echo ${i##*=} | sed -e 's/,/ /g'))
662c349a 422 ;;
8403b202
ZL
423 --raidtype=* )
424 case ${i##*=} in
425 raid0 )
426 TESTLIST=($(ls $testdir | grep "[0-9][0-9]r0\|raid0"))
427 ;;
428 linear )
429 TESTLIST=($(ls $testdir | grep "linear"))
430 ;;
431 raid1 )
432 TESTLIST=($(ls $testdir | grep "[0-9][0-9]r1\|raid1" | grep -vi raid10))
433 ;;
434 raid456 )
435 TESTLIST=($(ls $testdir | grep "[0-9][0-9]r[4-6]\|raid[4-6]"))
436 ;;
437 raid10 )
438 TESTLIST=($(ls $testdir | grep "[0-9][0-9]r10\|raid10"))
439 ;;
440 ddf )
441 TESTLIST=($(ls $testdir | grep "[0-9][0-9]ddf"))
442 ;;
443 imsm )
444 TESTLIST=($(ls $testdir | grep "[0-9][0-9]imsm"))
445 ;;
446 * )
447 echo "Unknown argument: $i"
448 do_help
449 exit 1
450 ;;
451 esac
452 ;;
662c349a 453 --logdir=* )
20d10b4b 454 logdir="${i##*=}"
662c349a
ZL
455 ;;
456 --save-logs )
457 savelogs=1
458 ;;
459 --keep-going | --no-error )
460 exitonerror=0
461 ;;
462 --disable-multipath )
463 unset MULTIPATH
464 ;;
465 --disable-integrity )
466 unset INTEGRITY
467 ;;
20d10b4b
ZL
468 --dev=* )
469 case ${i##*=} in
470 loop )
471 DEVTYPE=loop
472 ;;
473 lvm )
474 DEVTYPE=lvm
475 ;;
476 ram )
477 DEVTYPE=ram
478 ;;
479 * )
480 echo "Unknown argument: $i"
481 do_help
482 exit 1
483 ;;
484 esac
662c349a
ZL
485 ;;
486 --volgroup=* )
487 LVM_VOLGROUP=`expr "x$i" : 'x[^=]*=\(.*\)'`
488 ;;
20d10b4b 489 --help | -h )
662c349a
ZL
490 do_help
491 exit 0
492 ;;
20d10b4b 493 * )
662c349a
ZL
494 echo " $0: Unknown argument: $i"
495 do_help
20d10b4b 496 exit 1
662c349a
ZL
497 ;;
498 esac
499 done
4e5ce543
JS
500}
501
20d10b4b
ZL
502check_env() {
503 user=$(id -un)
504 [ "X$user" != "Xroot" ] && {
505 echo "test: testing can only be done as 'root'."
506 exit 1
507 }
508 [ -x "raid6check" -a -x $mdadm ] || {
509 echo "test: please run 'make everything' before perform testing."
510 exit 1
511 }
512 cmds=(mdadm lsblk df udevadm losetup mkfs.ext3 fsck)
513 for cmd in ${cmds[@]}
514 do
515 which $cmd > /dev/null || {
516 echo "$cmd command not found!"
517 exit 1
518 }
519 done
520 mdadm_src_ver="$($mdadm -V 2>&1)"
521 mdadm_sbin_ver="$($(which mdadm) -V 2>&1)"
522 if [ "$mdadm_src_ver" != "$mdadm_sbin_ver" ]
523 then
524 # it's nessesary to 'make install' mdadm to /SBIN/DIR,
525 # such as systemd/mdadm-grow-continue@.service, would
526 # run as an instance by systemd when reshape happens,
527 # thus ensure that the correct mdadm is in testing.
528 echo "test: please run 'make install' before testing."
529 exit 1
530 fi
531 if ! $(df -T . | grep -iq ext)
532 then
533 # 'external file' bitmap only supports with ext[2-4] file system
534 echo "test: please run test suite with ext[2-4] file system."
535 exit 1
536 fi
537 if $(lsblk -a | grep -iq raid)
538 then
539 # donot run mdadm -Ss directly if there are RAIDs working.
540 echo "test: please run test suite without running RAIDs environment."
541 exit 1
542 fi
543 # Check whether to run multipath tests
544 modprobe multipath 2> /dev/null
545 grep -sq 'Personalities : .*multipath' /proc/mdstat &&
546 MULTIPATH="yes"
547}
4e5ce543 548
20d10b4b
ZL
549main() {
550 check_env
551 do_setup
572d7091 552
20d10b4b
ZL
553 echo "Testing on linux-$(uname -r) kernel"
554 [ "$savelogs" == "1" ] &&
555 echo "Saving logs to $logdir"
556 if [ "x$TESTLIST" != "x" ]
557 then
558 for script in ${TESTLIST[@]}
559 do
560 do_test $testdir/$script
561 done
562 else
563 for script in $testdir/$prefix $testdir/$prefix*[^~]
564 do
565 do_test $script
566 done
567 fi
60381555 568
20d10b4b
ZL
569 exit 0
570}
571
572parse_args $@
573main