]> git.ipfire.org Git - thirdparty/mdadm.git/blob - tests/func.sh
mdadm/test: add do_clean to ensure each case only catch its own testlog
[thirdparty/mdadm.git] / tests / func.sh
1 #!/bin/bash
2
3 # We test mdadm on loop-back block devices.
4 # dir for storing files should be settable by command line maybe
5 size=20000
6 # super0, round down to multiple of 64 and substract 64
7 mdsize0=19904
8 # super00 is nested, subtract 128
9 mdsize00=19840
10 # super1.0 round down to multiple of 2, subtract 8
11 mdsize1=19992
12 mdsize1a=19988
13 mdsize12=19988
14 # super1.2 for linear: round to multiple of 2, subtract 4
15 mdsize1_l=19996
16 mdsize2_l=19996
17 # subtract another 4 for bitmaps
18 mdsize1b=19988
19 mdsize11=19992
20 mdsize11a=19456
21 mdsize12=19988
22
23 # ddf needs bigger devices as 32Meg is reserved!
24 ddfsize=65536
25
26 # $1 is optional parameter, it shows why to save log
27 save_log() {
28 status=$1
29 logfile="$status""$_basename".log
30
31 cat $targetdir/stderr >> $targetdir/log
32 cp $targetdir/log $logdir/$_basename.log
33 echo "## $HOSTNAME: saving dmesg." >> $logdir/$logfile
34 dmesg -c >> $logdir/$logfile
35 echo "## $HOSTNAME: saving proc mdstat." >> $logdir/$logfile
36 cat /proc/mdstat >> $logdir/$logfile
37 array=($(mdadm -Ds | cut -d' ' -f2))
38 [ "$1" == "fail" ] &&
39 echo "FAILED - see $logdir/$_basename.log and $logdir/$logfile for details"
40 if [ $DEVTYPE == 'lvm' ]
41 then
42 # not supported lvm type yet
43 echo
44 elif [ "$DEVTYPE" == 'loop' -o "$DEVTYPE" == 'disk' ]
45 then
46 if [ ! -z "$array" -a ${#array[@]} -ge 1 ]
47 then
48 echo "## $HOSTNAME: mdadm -D ${array[@]}" >> $logdir/$logfile
49 $mdadm -D ${array[@]} >> $logdir/$logfile
50 # ignore saving external(external file, imsm...) bitmap
51 cat /proc/mdstat | grep -q "linear\|external" && return 0
52 md_disks=($($mdadm -D -Y ${array[@]} | grep "/dev/" | cut -d'=' -f2))
53 cat /proc/mdstat | grep -q "bitmap"
54 if [ $? -eq 0 ]
55 then
56 echo "## $HOSTNAME: mdadm -X ${md_disks[@]}" >> $logdir/$logfile
57 $mdadm -X ${md_disks[@]} >> $logdir/$logfile
58 echo "## $HOSTNAME: mdadm -E ${md_disks[@]}" >> $logdir/$logfile
59 $mdadm -E ${md_disks[@]} >> $logdir/$logfile
60 fi
61 else
62 echo "## $HOSTNAME: no array assembled!" >> $logdir/$logfile
63 fi
64 fi
65 }
66
67 cleanup() {
68 udevadm settle
69 $mdadm -Ssq 2> /dev/null
70 case $DEVTYPE in
71 loop )
72 for d in 0 1 2 3 4 5 6 7 8 9 10 11 12 13
73 do
74 losetup -d /dev/loop$d &> /dev/null
75 rm -f /dev/disk/by-path/loop*
76 rm -f /var/tmp/mdtest$d
77 done
78 ;;
79 lvm )
80 for d in 0 1 2 3 4 5 6 7 8 9 10 11 12 13
81 do
82 eval "lvremove --quiet -f \$dev$d"
83 done
84 ;;
85 disk )
86 $mdadm --zero ${disks[@]} &> /dev/null
87 ;;
88 esac
89 }
90
91 do_clean()
92 {
93 mdadm -Ss > /dev/null
94 mdadm --zero $devlist 2> /dev/null
95 dmesg -c > /dev/null
96 }
97
98 check_env() {
99 user=$(id -un)
100 [ "X$user" != "Xroot" ] && {
101 echo "test: testing can only be done as 'root'."
102 exit 1
103 }
104 [ -x "raid6check" -a -x $mdadm ] || {
105 echo "test: please run 'make everything' before perform testing."
106 exit 1
107 }
108 cmds=(mdadm lsblk df udevadm losetup mkfs.ext3 fsck seq)
109 for cmd in ${cmds[@]}
110 do
111 which $cmd > /dev/null || {
112 echo "$cmd command not found!"
113 exit 1
114 }
115 done
116 mdadm_src_ver="$($mdadm -V 2>&1)"
117 mdadm_sbin_ver="$($(which mdadm) -V 2>&1)"
118 if [ "$mdadm_src_ver" != "$mdadm_sbin_ver" ]
119 then
120 # it's nessesary to 'make install' mdadm to /SBIN/DIR,
121 # such as systemd/mdadm-grow-continue@.service, would
122 # run as an instance by systemd when reshape happens,
123 # thus ensure that the correct mdadm is in testing.
124 echo "test: please run 'make install' before testing."
125 exit 1
126 fi
127 if ! $(df -T . | grep -iq ext)
128 then
129 # 'external file' bitmap only supports with ext[2-4] file system
130 echo "test: please run test suite with ext[2-4] file system."
131 exit 1
132 fi
133 if $(lsblk -a | grep -iq raid)
134 then
135 # donot run mdadm -Ss directly if there are RAIDs working.
136 echo "test: please run test suite without running RAIDs environment."
137 exit 1
138 fi
139 # Check whether to run multipath tests
140 modprobe multipath 2> /dev/null
141 grep -sq 'Personalities : .*multipath' /proc/mdstat &&
142 MULTIPATH="yes"
143 }
144
145 do_setup() {
146 trap cleanup 0 1 3 15
147 trap ctrl_c 2
148
149 check_env
150 [ -d $logdir ] || mkdir -p $logdir
151
152 devlist=
153 if [ "$DEVTYPE" == "loop" ]
154 then
155 # make sure there are no loop devices remaining.
156 # udev started things can sometimes prevent them being stopped
157 # immediately
158 while grep loop /proc/partitions > /dev/null 2>&1
159 do
160 $mdadm -Ssq
161 losetup -d /dev/loop[0-9]* 2> /dev/null
162 sleep 0.2
163 done
164 elif [ "$DEVTYPE" == "disk" ]
165 then
166 if [ ! -z "$disks" ]
167 then
168 for d in $(seq 0 ${#disks[@]})
169 do
170 eval "dev$d=${disks[$d]}"
171 eval devlist=\"\$devlist \$dev$d\"
172 eval devlist$d=\"\$devlist\"
173 done
174 $mdadm --zero ${disks[@]} &> /dev/null
175 else
176 echo "Forget to provide physical devices for disk mode."
177 exit 1
178 fi
179 fi
180 for d in 0 1 2 3 4 5 6 7 8 9 10 11 12 13
181 do
182 sz=$size
183 [ $d -gt 7 ] && sz=$ddfsize
184 case $DEVTYPE in
185 loop)
186 [ -f $targetdir/mdtest$d ] ||
187 dd if=/dev/zero of=$targetdir/mdtest$d count=$sz bs=1K > /dev/null 2>&1
188 # make sure udev doesn't touch
189 mdadm --zero $targetdir/mdtest$d 2> /dev/null
190 [ -b /dev/loop$d ] || mknod /dev/loop$d b 7 $d
191 if [ $d -eq 7 ]
192 then
193 losetup /dev/loop$d $targetdir/mdtest6 # for multipath use
194 else
195 losetup /dev/loop$d $targetdir/mdtest$d
196 fi
197 eval dev$d=/dev/loop$d
198 eval file$d=$targetdir/mdtest$d
199 ;;
200 lvm)
201 unset MULTIPATH
202 eval dev$d=/dev/mapper/${LVM_VOLGROUP}-mdtest$d
203 if ! lvcreate --quiet -L ${sz}K -n mdtest$d $LVM_VOLGROUP
204 then
205 trap '' 0 # make sure lvremove is not called
206 eval echo error creating \$dev$d
207 exit 129
208 fi
209 ;;
210 ram)
211 unset MULTIPATH
212 eval dev$d=/dev/ram$d
213 ;;
214 esac
215 eval devlist=\"\$devlist \$dev$d\"
216 eval devlist$d=\"\$devlist\"
217 #" <-- add this quote to un-confuse vim syntax highlighting
218 done
219 path0=$dev6
220 path1=$dev7
221 ulimit -c unlimited
222 [ -f /proc/mdstat ] || modprobe md_mod
223 echo 2000 > /proc/sys/dev/raid/speed_limit_max
224 echo 0 > /sys/module/md_mod/parameters/start_ro
225 }
226
227 # check various things
228 check() {
229 case $1 in
230 opposite_result )
231 if [ $? -eq 0 ]; then
232 die "This command shouldn't run successfully"
233 fi
234 spares )
235 spares=$(tr '] ' '\012\012' < /proc/mdstat | grep -c '(S)' || exit 0)
236 [ $spares -ne $2 ] &&
237 die "expected $2 spares, found $spares"
238 ;;
239 raid* | linear )
240 grep -sq "active $1 " /proc/mdstat ||
241 die "active $1 not found"
242 ;;
243 algorithm )
244 grep -sq " algorithm $2 " /proc/mdstat ||
245 die "algorithm $2 not found"
246 ;;
247 resync | recovery | reshape )
248 cnt=5
249 while ! grep -sq $1 /proc/mdstat
250 do
251 if [ $cnt -gt 0 ] && grep -v idle /sys/block/md*/md/sync_action > /dev/null
252 then # Something isn't idle - wait a bit
253 sleep 0.5
254 cnt=$[cnt-1]
255 else
256 die "no $1 happening"
257 fi
258 done
259 ;;
260 nosync )
261 sleep 0.5
262 # Since 4.2 we delay the close of recovery until there has been a chance for
263 # spares to be activated. That means that a recovery that finds nothing
264 # to do can still take a little longer than expected.
265 # add an extra check: is sync_completed shows the end is reached, assume
266 # there is no recovery.
267 if grep -sq -E '(resync|recovery|reshape) *=' /proc/mdstat
268 then
269 incomplete=`grep / /sys/block/md*/md/sync_completed 2> /dev/null | sed '/^ *\([0-9]*\) \/ \1/d'`
270 [ -n "$incomplete" ] &&
271 die "resync or recovery is happening!"
272 fi
273 ;;
274 wait )
275 p=`cat /proc/sys/dev/raid/speed_limit_max`
276 echo 2000000 > /proc/sys/dev/raid/speed_limit_max
277 sleep 0.1
278 while grep -Eq '(resync|recovery|reshape|check|repair) *=' /proc/mdstat ||
279 grep -v idle > /dev/null /sys/block/md*/md/sync_action
280 do
281 sleep 0.5
282 done
283 echo $p > /proc/sys/dev/raid/speed_limit_max
284 ;;
285 state )
286 grep -sq "blocks.*\[$2\]\$" /proc/mdstat ||
287 die "state $2 not found!"
288 sleep 0.5
289 ;;
290 bitmap )
291 grep -sq bitmap /proc/mdstat ||
292 die "no bitmap"
293 ;;
294 nobitmap )
295 grep -sq "bitmap" /proc/mdstat &&
296 die "bitmap present"
297 ;;
298 readonly )
299 grep -sq "read-only" /proc/mdstat ||
300 die "array is not read-only!"
301 ;;
302 inactive )
303 grep -sq "inactive" /proc/mdstat ||
304 die "array is not inactive!"
305 ;;
306 # It only can be used when there is only one raid
307 chunk )
308 chunk_size=`awk -F',' '/chunk/{print $2}' /proc/mdstat | awk -F'[a-z]' '{print $1}'`
309 if [ "$chunk_size" -ne "$2" ] ; then
310 die "chunksize should be $2, but it's $chunk_size"
311 fi
312 ;;
313 * )
314 die "unknown check $1"
315 ;;
316 esac
317 }
318
319 no_errors() {
320 if [ -s $targetdir/stderr ]
321 then
322 echo Bad errors from mdadm:
323 cat $targetdir/stderr
324 exit 2
325 fi
326 }
327
328 # basic device test
329 testdev() {
330 [ -b $1 ] || die "$1 isn't a block device."
331 [ "$DEVTYPE" == "disk" ] && return 0
332 udevadm settle
333 dev=$1
334 cnt=$2
335 dvsize=$3
336 chunk=$4
337 if [ -z "$5" ]
338 then
339 mkfs.ext3 -F -j $dev > /dev/null 2>&1 && fsck -fn $dev >&2
340 fi
341 dsize=$[dvsize/chunk]
342 dsize=$[dsize*chunk]
343 rasize=$[dsize*2*cnt]
344 # rasize is in sectors
345 if [ -n "$DEV_ROUND_K" ]
346 then
347 rasize=$[rasize/DEV_ROUND_K/2]
348 rasize=$[rasize*DEV_ROUND_K*2]
349 fi
350 [ `/sbin/blockdev --getsize $dev` -eq 0 ] && sleep 2
351 _sz=`/sbin/blockdev --getsize $dev`
352 [ $rasize -lt $_sz -o $[rasize*4/5] -gt $_sz ] &&
353 die "size is wrong for $dev: $cnt * $dvsize (chunk=$chunk) = $rasize, not $_sz"
354 return 0
355 }
356
357 rotest() {
358 dev=$1
359 fsck -fn $dev >&2
360 }