]> git.ipfire.org Git - thirdparty/mdadm.git/blob - test
mdadm/test: add new testcase for testing readonly/readwrite
[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' -o "$DEVTYPE" == 'disk' ]
77 then
78 if [ ! -z "$array" -a ${#array[@]} -ge 1 ]
79 then
80 md_disks=($($mdadm -D -Y ${array[@]} | grep "/dev/" | 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 disk )
118 $mdadm --zero ${disks[@]} &> /dev/null
119 ;;
120 esac
121 }
122
123 ctrl_c() {
124 exitonerror=1
125 }
126
127 do_setup() {
128 trap cleanup 0 1 3 15
129 trap ctrl_c 2
130
131 [ -d $logdir ] || mkdir -p $logdir
132 dmesg -c > /dev/null
133
134 devlist=
135 if [ "$DEVTYPE" == "loop" ]
136 then
137 # make sure there are no loop devices remaining.
138 # udev started things can sometimes prevent them being stopped
139 # immediately
140 while grep loop /proc/partitions > /dev/null 2>&1
141 do
142 $mdadm -Ssq
143 losetup -d /dev/loop[0-9]* 2> /dev/null
144 sleep 0.2
145 done
146 elif [ "$DEVTYPE" == "disk" ]
147 then
148 if [ ! -z "$disks" ]
149 then
150 for d in $(seq 0 ${#disks[@]})
151 do
152 eval "dev$d=${disks[$d]}"
153 eval devlist=\"\$devlist \$dev$d\"
154 eval devlist$d=\"\$devlist\"
155 done
156 $mdadm --zero ${disks[@]} &> /dev/null
157 else
158 echo "Forget to provide physical devices for disk mode."
159 exit 1
160 fi
161 fi
162 for d in 0 1 2 3 4 5 6 7 8 9 10 11 12 13
163 do
164 sz=$size
165 [ $d -gt 7 ] && sz=$ddfsize
166 case $DEVTYPE in
167 loop)
168 [ -f $targetdir/mdtest$d ] ||
169 dd if=/dev/zero of=$targetdir/mdtest$d count=$sz bs=1K > /dev/null 2>&1
170 # make sure udev doesn't touch
171 mdadm --zero $targetdir/mdtest$d 2> /dev/null
172 [ -b /dev/loop$d ] || mknod /dev/loop$d b 7 $d
173 if [ $d -eq 7 ]
174 then
175 losetup /dev/loop$d $targetdir/mdtest6 # for multipath use
176 else
177 losetup /dev/loop$d $targetdir/mdtest$d
178 fi
179 eval dev$d=/dev/loop$d
180 eval file$d=$targetdir/mdtest$d
181 ;;
182 lvm)
183 unset MULTIPATH
184 eval dev$d=/dev/mapper/${LVM_VOLGROUP}-mdtest$d
185 if ! lvcreate --quiet -L ${sz}K -n mdtest$d $LVM_VOLGROUP
186 then
187 trap '' 0 # make sure lvremove is not called
188 eval echo error creating \$dev$d
189 exit 129
190 fi
191 ;;
192 ram)
193 unset MULTIPATH
194 eval dev$d=/dev/ram$d
195 ;;
196 esac
197 eval devlist=\"\$devlist \$dev$d\"
198 eval devlist$d=\"\$devlist\"
199 #" <-- add this quote to un-confuse vim syntax highlighting
200 done
201 path0=$dev6
202 path1=$dev7
203 ulimit -c unlimited
204 [ -f /proc/mdstat ] || modprobe md_mod
205 echo 2000 > /proc/sys/dev/raid/speed_limit_max
206 echo 0 > /sys/module/md_mod/parameters/start_ro
207 }
208
209 # mdadm always adds --quiet, and we want to see any unexpected messages
210 mdadm() {
211 rm -f $targetdir/stderr
212 case $* in
213 *-S* )
214 udevadm settle
215 p=`cat /proc/sys/dev/raid/speed_limit_max`
216 echo 20000 > /proc/sys/dev/raid/speed_limit_max
217 ;;
218 esac
219 case $* in
220 *-C* | *--create* | *-B* | *--build* )
221 # clear superblock every time once creating or
222 # building arrays, because it's always creating
223 # and building array many times in a test case.
224 for args in $*
225 do
226 [[ $args =~ "/dev/" ]] && {
227 [[ $args =~ "md" ]] ||
228 $mdadm --zero $args > /dev/null
229 }
230 done
231 $mdadm 2> $targetdir/stderr --quiet "$@" --auto=yes
232 ;;
233 * )
234 $mdadm 2> $targetdir/stderr --quiet "$@"
235 ;;
236 esac
237 rv=$?
238 case $* in
239 *-S* )
240 udevadm settle
241 echo $p > /proc/sys/dev/raid/speed_limit_max
242 ;;
243 esac
244 cat >&2 $targetdir/stderr
245 return $rv
246 }
247
248 # check various things
249 check() {
250 case $1 in
251 spares )
252 spares=$(tr '] ' '\012\012' < /proc/mdstat | grep -c '(S)' || exit 0)
253 [ $spares -ne $2 ] &&
254 die "expected $2 spares, found $spares"
255 ;;
256 raid* | linear )
257 grep -sq "active $1 " /proc/mdstat ||
258 die "active $1 not found"
259 ;;
260 algorithm )
261 grep -sq " algorithm $2 " /proc/mdstat ||
262 die "algorithm $2 not found"
263 ;;
264 resync | recovery | reshape )
265 cnt=5
266 while ! grep -sq $1 /proc/mdstat
267 do
268 if [ $cnt -gt 0 ] && grep -v idle /sys/block/md*/md/sync_action > /dev/null
269 then # Something isn't idle - wait a bit
270 sleep 0.5
271 cnt=$[cnt-1]
272 else
273 die "no $1 happening"
274 fi
275 done
276 ;;
277 nosync )
278 sleep 0.5
279 # Since 4.2 we delay the close of recovery until there has been a chance for
280 # spares to be activated. That means that a recovery that finds nothing
281 # to do can still take a little longer than expected.
282 # add an extra check: is sync_completed shows the end is reached, assume
283 # there is no recovery.
284 if grep -sq -E '(resync|recovery|reshape) *=' /proc/mdstat
285 then
286 incomplete=`grep / /sys/block/md*/md/sync_completed 2> /dev/null | sed '/^ *\([0-9]*\) \/ \1/d'`
287 [ -n "$incomplete" ] &&
288 die "resync or recovery is happening!"
289 fi
290 ;;
291 wait )
292 p=`cat /proc/sys/dev/raid/speed_limit_max`
293 echo 2000000 > /proc/sys/dev/raid/speed_limit_max
294 sleep 0.1
295 while grep -Eq '(resync|recovery|reshape|check|repair) *=' /proc/mdstat ||
296 grep -v idle > /dev/null /sys/block/md*/md/sync_action
297 do
298 sleep 0.5
299 done
300 echo $p > /proc/sys/dev/raid/speed_limit_max
301 ;;
302 state )
303 grep -sq "blocks.*\[$2\]\$" /proc/mdstat ||
304 die "state $2 not found!"
305 sleep 0.5
306 ;;
307 bitmap )
308 grep -sq bitmap /proc/mdstat ||
309 die "no bitmap"
310 ;;
311 nobitmap )
312 grep -sq "bitmap" /proc/mdstat &&
313 die "bitmap present"
314 ;;
315 readonly )
316 grep -sq "read-only" /proc/mdstat ||
317 die "array is not read-only!"
318 ;;
319 inactive )
320 grep -sq "inactive" /proc/mdstat ||
321 die "array is not inactive!"
322 ;;
323 * )
324 die "unknown check $1"
325 ;;
326 esac
327 }
328
329 no_errors() {
330 if [ -s $targetdir/stderr ]
331 then
332 echo Bad errors from mdadm:
333 cat $targetdir/stderr
334 exit 2
335 fi
336 }
337
338 # basic device test
339 testdev() {
340 [ -b $1 ] || die "$1 isn't a block device."
341 [ "$DEVTYPE" == "disk" ] && return 0
342 udevadm settle
343 dev=$1
344 cnt=$2
345 dvsize=$3
346 chunk=$4
347 if [ -z "$5" ]
348 then
349 mkfs.ext3 -F -j $dev > /dev/null 2>&1 && fsck -fn $dev >&2
350 fi
351 dsize=$[dvsize/chunk]
352 dsize=$[dsize*chunk]
353 rasize=$[dsize*2*cnt]
354 # rasize is in sectors
355 if [ -n "$DEV_ROUND_K" ]
356 then
357 rasize=$[rasize/DEV_ROUND_K/2]
358 rasize=$[rasize*DEV_ROUND_K*2]
359 fi
360 [ `/sbin/blockdev --getsize $dev` -eq 0 ] && sleep 2
361 _sz=`/sbin/blockdev --getsize $dev`
362 [ $rasize -lt $_sz -o $[rasize*4/5] -gt $_sz ] &&
363 die "size is wrong for $dev: $cnt * $dvsize (chunk=$chunk) = $rasize, not $_sz"
364 return 0
365 }
366
367 rotest() {
368 dev=$1
369 fsck -fn $dev >&2
370 }
371
372 do_test() {
373 _script=$1
374 _basename=`basename $_script`
375 if [ -f "$_script" ]
376 then
377 rm -f $targetdir/stderr
378 # stop all arrays, just incase some script left an array active.
379 $mdadm -Ssq 2> /dev/null
380 mdadm --zero $devlist 2> /dev/null
381 # this might have been reset: restore the default.
382 echo 2000 > /proc/sys/dev/raid/speed_limit_max
383 # source script in a subshell, so it has access to our
384 # namespace, but cannot change it.
385 echo -ne "$_script... "
386 if ( set -ex ; . $_script ) &> $targetdir/log
387 then
388 dmesg | grep -iq "error\|call trace\|segfault" &&
389 die "dmesg prints errors when testing $_basename!"
390 echo "succeeded"
391 _fail=0
392 else
393 save_log fail
394 _fail=1
395 fi
396 [ "$savelogs" == "1" ] &&
397 mv -f $targetdir/log $logdir/$_basename.log
398 [ "$_fail" == "1" -a "$exitonerror" == "1" ] && exit 1
399 fi
400 }
401
402 do_help() {
403 cat <<-EOF
404 Usage: $0 [options]
405 Example for disk mode: ./test --dev=disk --disks=/dev/sda{2..15}
406 Options:
407 --tests=test1,test2,... Comma separated list of tests to run
408 --raidtype= raid0|linear|raid1|raid456|raid10|ddf|imsm
409 --disable-multipath Disable any tests involving multipath
410 --disable-integrity Disable slow tests of RAID[56] consistency
411 --logdir=directory Directory to save all logfiles in
412 --save-logs Usually use with --logdir together
413 --keep-going | --no-error Don't stop on error, ie. run all tests
414 --dev=loop|lvm|ram|disk Use loop devices (default), LVM, RAM or disk
415 --disks= Provide a bunch of physical devices for test
416 --volgroup=name LVM volume group for LVM test
417 setup Setup test environment and exit
418 cleanup Cleanup test environment
419 prefix Run tests with <prefix>
420 --help | -h Print this usage
421 EOF
422 }
423
424 parse_args() {
425 for i in $*
426 do
427 case $i in
428 [0-9][0-9] )
429 prefix=$i
430 ;;
431 setup )
432 echo "mdadm test environment setup"
433 do_setup
434 trap 0
435 exit 0
436 ;;
437 cleanup )
438 cleanup
439 exit 0
440 ;;
441 --tests=* )
442 TESTLIST=($(echo ${i##*=} | sed -e 's/,/ /g'))
443 ;;
444 --raidtype=* )
445 case ${i##*=} in
446 raid0 )
447 TESTLIST=($(ls $testdir | grep "[0-9][0-9]r0\|raid0"))
448 ;;
449 linear )
450 TESTLIST=($(ls $testdir | grep "linear"))
451 ;;
452 raid1 )
453 TESTLIST=($(ls $testdir | grep "[0-9][0-9]r1\|raid1" | grep -vi raid10))
454 ;;
455 raid456 )
456 TESTLIST=($(ls $testdir | grep "[0-9][0-9]r[4-6]\|raid[4-6]"))
457 ;;
458 raid10 )
459 TESTLIST=($(ls $testdir | grep "[0-9][0-9]r10\|raid10"))
460 ;;
461 ddf )
462 TESTLIST=($(ls $testdir | grep "[0-9][0-9]ddf"))
463 ;;
464 imsm )
465 TESTLIST=($(ls $testdir | grep "[0-9][0-9]imsm"))
466 ;;
467 * )
468 echo "Unknown argument: $i"
469 do_help
470 exit 1
471 ;;
472 esac
473 ;;
474 --logdir=* )
475 logdir="${i##*=}"
476 ;;
477 --save-logs )
478 savelogs=1
479 ;;
480 --keep-going | --no-error )
481 exitonerror=0
482 ;;
483 --disable-multipath )
484 unset MULTIPATH
485 ;;
486 --disable-integrity )
487 unset INTEGRITY
488 ;;
489 --dev=* )
490 case ${i##*=} in
491 loop )
492 DEVTYPE=loop
493 ;;
494 lvm )
495 DEVTYPE=lvm
496 ;;
497 ram )
498 DEVTYPE=ram
499 ;;
500 disk )
501 DEVTYPE=disk
502 ;;
503 * )
504 echo "Unknown argument: $i"
505 do_help
506 exit 1
507 ;;
508 esac
509 ;;
510 --disks=* )
511 disks=(${disks[*]} ${i##*=})
512 ;;
513 --volgroup=* )
514 LVM_VOLGROUP=`expr "x$i" : 'x[^=]*=\(.*\)'`
515 ;;
516 --help | -h )
517 do_help
518 exit 0
519 ;;
520 * )
521 echo " $0: Unknown argument: $i"
522 do_help
523 exit 1
524 ;;
525 esac
526 done
527 }
528
529 check_env() {
530 user=$(id -un)
531 [ "X$user" != "Xroot" ] && {
532 echo "test: testing can only be done as 'root'."
533 exit 1
534 }
535 [ -x "raid6check" -a -x $mdadm ] || {
536 echo "test: please run 'make everything' before perform testing."
537 exit 1
538 }
539 cmds=(mdadm lsblk df udevadm losetup mkfs.ext3 fsck seq)
540 for cmd in ${cmds[@]}
541 do
542 which $cmd > /dev/null || {
543 echo "$cmd command not found!"
544 exit 1
545 }
546 done
547 mdadm_src_ver="$($mdadm -V 2>&1)"
548 mdadm_sbin_ver="$($(which mdadm) -V 2>&1)"
549 if [ "$mdadm_src_ver" != "$mdadm_sbin_ver" ]
550 then
551 # it's nessesary to 'make install' mdadm to /SBIN/DIR,
552 # such as systemd/mdadm-grow-continue@.service, would
553 # run as an instance by systemd when reshape happens,
554 # thus ensure that the correct mdadm is in testing.
555 echo "test: please run 'make install' before testing."
556 exit 1
557 fi
558 if ! $(df -T . | grep -iq ext)
559 then
560 # 'external file' bitmap only supports with ext[2-4] file system
561 echo "test: please run test suite with ext[2-4] file system."
562 exit 1
563 fi
564 if $(lsblk -a | grep -iq raid)
565 then
566 # donot run mdadm -Ss directly if there are RAIDs working.
567 echo "test: please run test suite without running RAIDs environment."
568 exit 1
569 fi
570 # Check whether to run multipath tests
571 modprobe multipath 2> /dev/null
572 grep -sq 'Personalities : .*multipath' /proc/mdstat &&
573 MULTIPATH="yes"
574 }
575
576 main() {
577 check_env
578 do_setup
579
580 echo "Testing on linux-$(uname -r) kernel"
581 [ "$savelogs" == "1" ] &&
582 echo "Saving logs to $logdir"
583 if [ "x$TESTLIST" != "x" ]
584 then
585 for script in ${TESTLIST[@]}
586 do
587 do_test $testdir/$script
588 done
589 else
590 for script in $testdir/$prefix $testdir/$prefix*[^~]
591 do
592 do_test $script
593 done
594 fi
595
596 exit 0
597 }
598
599 parse_args $@
600 main