]> git.ipfire.org Git - thirdparty/mdadm.git/blob - test
mdadm/test: Add '--raidtype=' to run different raidlevel cases
[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 --raidtype= raid0|linear|raid1|raid456|raid10|ddf|imsm
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
401 }
402
403 parse_args() {
404 for i in $*
405 do
406 case $i in
407 [0-9][0-9] )
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=* )
421 TESTLIST=($(echo ${i##*=} | sed -e 's/,/ /g'))
422 ;;
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 ;;
453 --logdir=* )
454 logdir="${i##*=}"
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 ;;
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
485 ;;
486 --volgroup=* )
487 LVM_VOLGROUP=`expr "x$i" : 'x[^=]*=\(.*\)'`
488 ;;
489 --help | -h )
490 do_help
491 exit 0
492 ;;
493 * )
494 echo " $0: Unknown argument: $i"
495 do_help
496 exit 1
497 ;;
498 esac
499 done
500 }
501
502 check_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 }
548
549 main() {
550 check_env
551 do_setup
552
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
568
569 exit 0
570 }
571
572 parse_args $@
573 main