]> git.ipfire.org Git - thirdparty/mdadm.git/blob - tests/func.sh
mdadm/tests: Don't run mknod before losetup
[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 $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 if $(lsblk -a | grep -iq raid)
117 then
118 # donot run mdadm -Ss directly if there are RAIDs working.
119 echo "test: please run test suite without running RAIDs environment."
120 exit 1
121 fi
122 # Check whether to run multipath tests
123 modprobe multipath 2> /dev/null
124 grep -sq 'Personalities : .*multipath' /proc/mdstat &&
125 MULTIPATH="yes"
126 }
127
128 do_setup() {
129 trap cleanup 0 1 3 15
130 trap ctrl_c 2
131
132 check_env
133 [ -d $logdir ] || mkdir -p $logdir
134
135 devlist=
136 if [ "$DEVTYPE" == "loop" ]
137 then
138 # make sure there are no loop devices remaining.
139 # udev started things can sometimes prevent them being stopped
140 # immediately
141 while grep loop /proc/partitions > /dev/null 2>&1
142 do
143 $mdadm -Ssq
144 losetup -d /dev/loop[0-9]* 2> /dev/null
145 sleep 0.2
146 done
147 elif [ "$DEVTYPE" == "disk" ]
148 then
149 if [ ! -z "$disks" ]
150 then
151 for d in $(seq 0 ${#disks[@]})
152 do
153 eval "dev$d=${disks[$d]}"
154 eval devlist=\"\$devlist \$dev$d\"
155 eval devlist$d=\"\$devlist\"
156 done
157 $mdadm --zero ${disks[@]} &> /dev/null
158 else
159 echo "Forget to provide physical devices for disk mode."
160 exit 1
161 fi
162 fi
163 for d in 0 1 2 3 4 5 6 7 8 9 10 11 12 13
164 do
165 sz=$size
166 [ $d -gt 7 ] && sz=$ddfsize
167 case $DEVTYPE in
168 loop)
169 [ -f $targetdir/mdtest$d ] ||
170 dd if=/dev/zero of=$targetdir/mdtest$d count=$sz bs=1K > /dev/null 2>&1
171 # make sure udev doesn't touch
172 mdadm --zero $targetdir/mdtest$d 2> /dev/null
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 # check various things
210 check() {
211 case $1 in
212 opposite_result )
213 if [ $? -eq 0 ]; then
214 die "This command shouldn't run successfully"
215 fi
216 ;;
217 spares )
218 spares=$(tr '] ' '\012\012' < /proc/mdstat | grep -c '(S)' || exit 0)
219 [ $spares -ne $2 ] &&
220 die "expected $2 spares, found $spares"
221 ;;
222 raid* | linear )
223 grep -sq "active $1 " /proc/mdstat ||
224 die "active $1 not found"
225 ;;
226 algorithm )
227 grep -sq " algorithm $2 " /proc/mdstat ||
228 die "algorithm $2 not found"
229 ;;
230 resync | recovery | reshape )
231 cnt=5
232 while ! grep -sq $1 /proc/mdstat
233 do
234 if [ $cnt -gt 0 ] && grep -v idle /sys/block/md*/md/sync_action > /dev/null
235 then # Something isn't idle - wait a bit
236 sleep 0.5
237 cnt=$[cnt-1]
238 else
239 die "no $1 happening"
240 fi
241 done
242 ;;
243 nosync )
244 sleep 0.5
245 # Since 4.2 we delay the close of recovery until there has been a chance for
246 # spares to be activated. That means that a recovery that finds nothing
247 # to do can still take a little longer than expected.
248 # add an extra check: is sync_completed shows the end is reached, assume
249 # there is no recovery.
250 if grep -sq -E '(resync|recovery|reshape) *=' /proc/mdstat
251 then
252 incomplete=`grep / /sys/block/md*/md/sync_completed 2> /dev/null | sed '/^ *\([0-9]*\) \/ \1/d'`
253 [ -n "$incomplete" ] &&
254 die "resync or recovery is happening!"
255 fi
256 ;;
257 wait )
258 p=`cat /proc/sys/dev/raid/speed_limit_max`
259 echo 2000000 > /proc/sys/dev/raid/speed_limit_max
260 sleep 0.1
261 while grep -Eq '(resync|recovery|reshape|check|repair) *=' /proc/mdstat ||
262 grep -v idle > /dev/null /sys/block/md*/md/sync_action
263 do
264 sleep 0.5
265 done
266 echo $p > /proc/sys/dev/raid/speed_limit_max
267 ;;
268 state )
269 grep -sq "blocks.*\[$2\]\$" /proc/mdstat ||
270 die "state $2 not found!"
271 sleep 0.5
272 ;;
273 bitmap )
274 grep -sq bitmap /proc/mdstat ||
275 die "no bitmap"
276 ;;
277 nobitmap )
278 grep -sq "bitmap" /proc/mdstat &&
279 die "bitmap present"
280 ;;
281 readonly )
282 grep -sq "read-only" /proc/mdstat ||
283 die "array is not read-only!"
284 ;;
285 inactive )
286 grep -sq "inactive" /proc/mdstat ||
287 die "array is not inactive!"
288 ;;
289 # It only can be used when there is only one raid
290 chunk )
291 chunk_size=`awk -F',' '/chunk/{print $2}' /proc/mdstat | awk -F'[a-z]' '{print $1}'`
292 if [ "$chunk_size" -ne "$2" ] ; then
293 die "chunksize should be $2, but it's $chunk_size"
294 fi
295 ;;
296 * )
297 die "unknown check $1"
298 ;;
299 esac
300 }
301
302 no_errors() {
303 if [ -s $targetdir/stderr ]
304 then
305 echo Bad errors from mdadm:
306 cat $targetdir/stderr
307 exit 2
308 fi
309 }
310
311 # basic device test
312 testdev() {
313 [ -b $1 ] || die "$1 isn't a block device."
314 [ "$DEVTYPE" == "disk" ] && return 0
315 udevadm settle
316 dev=$1
317 cnt=$2
318 dvsize=$3
319 chunk=$4
320 if [ -z "$5" ]
321 then
322 mkfs.ext3 -F -j $dev > /dev/null 2>&1 && fsck -fn $dev >&2
323 fi
324 dsize=$[dvsize/chunk]
325 dsize=$[dsize*chunk]
326 rasize=$[dsize*2*cnt]
327 # rasize is in sectors
328 if [ -n "$DEV_ROUND_K" ]
329 then
330 rasize=$[rasize/DEV_ROUND_K/2]
331 rasize=$[rasize*DEV_ROUND_K*2]
332 fi
333 [ `/sbin/blockdev --getsize $dev` -eq 0 ] && sleep 2
334 _sz=`/sbin/blockdev --getsize $dev`
335 [ $rasize -lt $_sz -o $[rasize*4/5] -gt $_sz ] &&
336 die "size is wrong for $dev: $cnt * $dvsize (chunk=$chunk) = $rasize, not $_sz"
337 return 0
338 }
339
340 rotest() {
341 dev=$1
342 fsck -fn $dev >&2
343 }