]> git.ipfire.org Git - thirdparty/mdadm.git/blob - test
939410ce85e29a12612003d021d23f6b5fcefce1
[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 echo "Testing on linux-$(uname -r) kernel"
25
26 # Check whether to run multipath tests
27 modprobe multipath 2> /dev/null
28 if grep -s 'Personalities : .*multipath' > /dev/null /proc/mdstat ; then
29 MULTIPATH="yes"
30 fi
31 INTEGRITY=yes
32 DEVTYPE=loop
33 LVM_VOLGROUP=mdtest
34
35 # make sure to test local mdmon, not system one
36 export MDADM_NO_SYSTEMCTL=1
37
38 # assume md0, md1, md2 exist in /dev
39 md0=/dev/md0 md1=/dev/md1 md2=/dev/md2
40 mdp0=/dev/md_d0
41 mdp1=/dev/md_d1
42
43 # We test mdadm on loop-back block devices.
44 # dir for storing files should be settable by command line maybe
45 targetdir=/var/tmp
46 size=20000
47 # super0, round down to multiple of 64 and substract 64
48 mdsize0=19904
49 # super00 is nested, subtract 128
50 mdsize00=19840
51 # super1.0 round down to multiple of 2, subtract 8
52 mdsize1=19992
53 mdsize1a=19988
54 mdsize12=19988
55 # super1.2 for linear: round to multiple of 2, subtract 4
56 mdsize1_l=19996
57 mdsize2_l=19996
58 # subtract another 4 for bitmaps
59 mdsize1b=19988
60 mdsize11=19992
61 mdsize11a=19456
62 mdsize12=19988
63
64 # ddf needs bigger devices as 32Meg is reserved!
65 ddfsize=65536
66
67 config=/tmp/mdadm.conf
68
69 cleanup() {
70 udevadm settle
71 $mdadm -Ssq 2> /dev/null
72 case $DEVTYPE in
73 loop)
74 for d in 0 1 2 3 4 5 6 7 8 9 10 11 12 13
75 do
76 losetup -d /dev/loop$d ; # rm -f $targetdir/mdtest$d
77 rm -f /dev/disk/by-path/loop*
78 done
79 ;;
80 lvm)
81 for d in 0 1 2 3 4 5 6 7 8 9 10 11 12 13
82 do
83 eval "lvremove --quiet -f \$dev$d"
84 done
85 ;;
86 esac
87 }
88
89 ctrl_c() {
90 exitonerror=1
91 }
92
93 do_setup() {
94 trap cleanup 0 1 3 15
95 trap ctrl_c 2
96
97 # make sure there are no loop devices remaining.
98 # udev started things can sometimes prevent them being stopped
99 # immediately
100 while grep loop /proc/partitions > /dev/null 2>&1
101 do
102 mdadm -Ss
103 losetup -d /dev/loop[0-9]* 2> /dev/null
104 sleep 1
105 done
106 devlist=
107 for d in 0 1 2 3 4 5 6 7 8 9 10 11 12 13
108 do
109 sz=$size
110 if [ $d -gt 7 ]; then sz=$ddfsize ; fi
111 case $DEVTYPE in
112 loop)
113 [ -f $targetdir/mdtest$d ] || dd if=/dev/zero of=$targetdir/mdtest$d count=$sz bs=1K > /dev/null 2>&1
114 # make sure udev doesn't touch
115 mdadm --zero $targetdir/mdtest$d 2> /dev/null
116 [ -b /dev/loop$d ] || mknod /dev/loop$d b 7 $d
117 if [ $d -eq 7 ]
118 then
119 losetup /dev/loop$d $targetdir/mdtest6 # for multipath use
120 else
121 losetup /dev/loop$d $targetdir/mdtest$d
122 fi
123 eval dev$d=/dev/loop$d
124 eval file$d=$targetdir/mdtest$d
125 ;;
126 lvm)
127 unset MULTIPATH
128 eval dev$d=/dev/mapper/${LVM_VOLGROUP}-mdtest$d
129 if ! lvcreate --quiet -L ${sz}K -n mdtest$d $LVM_VOLGROUP; then
130 trap '' 0 # make sure lvremove is not called
131 eval echo error creating \$dev$d
132 exit 129
133 fi
134 ;;
135 ram)
136 unset MULTIPATH
137 eval dev$d=/dev/ram$d
138 ;;
139 esac
140 eval devlist=\"\$devlist \$dev$d\"
141 eval devlist$d=\"\$devlist\"
142 #" <-- add this quote to un-confuse vim syntax highlighting
143 done
144 path0=$dev6
145 path1=$dev7
146
147 ulimit -c unlimited
148 [ -f /proc/mdstat ] || modprobe md_mod
149 echo 2000 > /proc/sys/dev/raid/speed_limit_max
150 echo 0 > /sys/module/md_mod/parameters/start_ro
151 }
152
153 # mdadm always adds --quiet, and we want to see any unexpected messages
154 mdadm() {
155 rm -f $targetdir/stderr
156 case $* in
157 *-S* ) udevadm settle
158 p=`cat /proc/sys/dev/raid/speed_limit_max`
159 echo 20000 > /proc/sys/dev/raid/speed_limit_max
160 esac
161 case $* in
162 *-C* ) $mdadm 2> $targetdir/stderr --quiet "$@" --auto=yes;;
163 * ) $mdadm 2> $targetdir/stderr --quiet "$@"
164 esac
165 rv=$?
166 case $* in
167 *-S* ) udevadm settle
168 echo $p > /proc/sys/dev/raid/speed_limit_max
169 esac
170 cat >&2 $targetdir/stderr
171 return $rv
172 }
173
174 # check various things
175 check() {
176 case $1 in
177 spares )
178 spares=`tr '] ' '\012\012' < /proc/mdstat | grep -c '(S)' || exit 0`
179 if [ $spares -ne $2 ]
180 then
181 echo >&2 "ERROR expected $2 spares, found $spares"; exit 1;
182 fi
183 ;;
184 raid* | linear )
185 grep -s "active $1 " /proc/mdstat > /dev/null || {
186 echo >&2 "ERROR active $1 not found" ; cat /proc/mdstat ; exit 1;}
187 ;;
188 algorithm )
189 grep -s " algorithm $2 " /proc/mdstat > /dev/null || {
190 echo >&2 "ERROR algorithm $2 not found"; cat /proc/mdstat; exit 1;}
191 ;;
192 resync | recovery | reshape)
193 cnt=5
194 while ! grep -s $1 /proc/mdstat > /dev/null
195 do
196 if [ $cnt -gt 0 ] && grep -v idle /sys/block/md*/md/sync_action > /dev/null
197 then # Something isn't idle - wait a bit
198 sleep 0.5
199 cnt=$[cnt-1]
200 else
201 echo >&2 ERROR no $1 happening; cat /proc/mdstat; exit 1
202 fi
203 done
204 ;;
205
206 nosync )
207 sleep 0.5
208 if grep -s -E '(resync|recovery|reshape) *=' > /dev/null /proc/mdstat ; then
209 echo >&2 "ERROR resync or recovery is happening!"; cat /proc/mdstat ; exit 1;
210 fi
211 ;;
212
213 wait )
214 p=`cat /proc/sys/dev/raid/speed_limit_max`
215 echo 2000000 > /proc/sys/dev/raid/speed_limit_max
216 sleep 0.1
217 while grep -E '(resync|recovery|reshape|check|repair) *=' > /dev/null /proc/mdstat ||
218 grep -v idle > /dev/null /sys/block/md*/md/sync_action
219 do sleep 0.5;
220 done
221 echo $p > /proc/sys/dev/raid/speed_limit_max
222 ;;
223
224 state )
225 grep -s "blocks.*\[$2\]\$" /proc/mdstat > /dev/null || {
226 echo >&2 "ERROR state $2 not found!"; cat /proc/mdstat ; exit 1; }
227 sleep 0.5
228 ;;
229
230 bitmap )
231 grep -s bitmap > /dev/null /proc/mdstat || {
232 echo >&2 ERROR no bitmap ; cat /proc/mdstat ; exit 1; }
233 ;;
234 nobitmap )
235 if grep -s "bitmap" > /dev/null /proc/mdstat
236 then
237 echo >&2 ERROR bitmap present ; cat /proc/mdstat ; exit 1;
238 fi
239 ;;
240
241 * ) echo >&2 ERROR unknown check $1 ; exit 1;
242 esac
243 }
244
245 no_errors() {
246 if [ -s $targetdir/stderr ]
247 then echo Bad errors from mdadm: ; cat $targetdir/stderr; exit 2;
248 fi
249 }
250 # basic device test
251
252 testdev() {
253 udevadm settle
254 dev=$1
255 cnt=$2
256 dvsize=$3
257 chunk=$4
258 if [ -z "$5" ]; then
259 mkfs.ext3 -F -j $dev > /dev/null 2>&1 && fsck -fn $dev >&2
260 fi
261 dsize=$[dvsize/chunk]
262 dsize=$[dsize*chunk]
263 rasize=$[dsize*2*cnt]
264 # rasize is in sectors
265 if [ -n "$DEV_ROUND_K" ]; then
266 rasize=$[rasize/DEV_ROUND_K/2]
267 rasize=$[rasize*DEV_ROUND_K*2]
268 fi
269 if [ `/sbin/blockdev --getsize $dev` -eq 0 ]; then sleep 2 ; fi
270 _sz=`/sbin/blockdev --getsize $dev`
271 if [ $rasize -lt $_sz -o $[rasize*4/5] -gt $_sz ]
272 then
273 echo "ERROR: size is wrong for $dev: $cnt * $dvsize (chunk=$chunk) = $rasize, not $_sz"
274 exit 1
275 fi
276 }
277
278 fast_sync() {
279 echo 200000 > /proc/sys/dev/raid/speed_limit_max
280 }
281
282 rotest() {
283 dev=$1
284 fsck -fn $dev >&2
285 }
286
287 do_test() {
288 _script=$1
289 _basename=`basename $_script`
290 if [ -f "$_script" ]
291 then
292 rm -f $targetdir/stderr
293 # stop all arrays, just incase some script left an array active.
294 $mdadm -Ssq 2> /dev/null
295 mdadm --zero $devlist 2> /dev/null
296 mdadm --zero $devlist 2> /dev/null
297 # this might have been reset: restore the default.
298 echo 2000 > /proc/sys/dev/raid/speed_limit_max
299 # source script in a subshell, so it has access to our
300 # namespace, but cannot change it.
301 echo -ne "$_script... "
302 if ( set -ex ; . $_script ) &> $targetdir/log
303 then
304 echo "succeeded"
305 _fail=0
306 else
307 log=log
308 cat $targetdir/stderr >> $targetdir/log
309 echo "=======================dmesg=================" >> $targetdir/log
310 dmesg | tail -n 200 >> $targetdir/log
311 if [ $exitonerror == 0 ]; then
312 log=log-`basename $_script`
313 mv $targetdir/log $logdir/$log
314 fi
315 echo "FAILED - see $logdir/$log for details"
316 _fail=1
317 fi
318 if [ "$savelogs" == "1" ]; then
319 cp $targetdir/log $logdir/$_basename.log
320 fi
321 if [ "$_fail" == "1" -a "$exitonerror" == "1" ]; then
322 exit 1
323 fi
324 fi
325 }
326
327 do_help() {
328 echo "Usage: $0 [options]"
329 echo " Options:"
330 echo " --tests=<test1,test2,..> Comma separated list of tests to run"
331 echo " --disable-multipath Disable any tests involving multipath"
332 echo " --disable-integrity Disable slow tests of RAID[56] consistency"
333 echo " --logdir=<directory> Directory to save logfiles in"
334 echo " --save-logs Save all logs in <logdir>"
335 echo " --keep-going Don't stop on error, ie. run all tests"
336 echo " --dev=[loop|lvm|ram] Use loop devices (default), LVM, or RAM disk"
337 echo " --volgroup=<name> LVM volume group for LVM test"
338 echo " setup Setup test environment and exit"
339 echo " cleanup Cleanup test environment"
340 echo " <prefix> Run tests with <prefix>"
341 }
342
343 parse_args() {
344 for i in $*
345 do
346 case $i in
347 [0-9]*)
348 prefix=$i
349 ;;
350 setup)
351 echo "mdadm test environment setup"
352 do_setup
353 trap 0; exit 0
354 ;;
355 cleanup)
356 cleanup
357 exit 0
358 ;;
359 --tests=*)
360 TESTLIST=`expr "x$i" : 'x[^=]*=\(.*\)' | sed -e 's/,/ /g'`
361 ;;
362 --logdir=*)
363 logdir=`expr "x$i" : 'x[^=]*=\(.*\)'`
364 ;;
365 --save-logs)
366 savelogs=1
367 ;;
368 --keep-going | --no-error)
369 exitonerror=0
370 ;;
371 --disable-multipath)
372 unset MULTIPATH
373 ;;
374 --disable-integrity)
375 unset INTEGRITY
376 ;;
377 --dev=loop)
378 DEVTYPE=loop
379 ;;
380 --dev=lvm)
381 DEVTYPE=lvm
382 ;;
383 --dev=ram)
384 DEVTYPE=ram
385 ;;
386 --volgroup=*)
387 LVM_VOLGROUP=`expr "x$i" : 'x[^=]*=\(.*\)'`
388 ;;
389 --help)
390 do_help
391 exit 0;
392 ;;
393 -*)
394 echo " $0: Unknown argument: $i"
395 do_help
396 exit 0;
397 ;;
398 esac
399 done
400 }
401
402 logdir=$targetdir
403 parse_args $@
404
405 do_setup
406 mkdir -p $logdir
407
408 if [ "$savelogs" == "1" ]; then
409 echo "Saving logs to $logdir"
410 fi
411
412 if [ "x$TESTLIST" != "x" ]; then
413 for script in $TESTLIST
414 do
415 do_test $testdir/$script
416 done
417 else
418 for script in $testdir/$prefix $testdir/$prefix*[^~]
419 do
420 do_test $script
421 done
422 fi
423 exit 0