]>
Commit | Line | Data |
---|---|---|
908eaf68 | 1 | #!/bin/bash |
6bf19c94 CH |
2 | # |
3 | # Copyright (C) 2009 Red Hat, Inc. | |
4 | # Copyright (c) 2000-2006 Silicon Graphics, Inc. All Rights Reserved. | |
5 | # | |
6 | # This program is free software; you can redistribute it and/or modify | |
7 | # it under the terms of the GNU General Public License as published by | |
8 | # the Free Software Foundation; either version 2 of the License, or | |
9 | # (at your option) any later version. | |
10 | # | |
11 | # This program is distributed in the hope that it will be useful, | |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | # GNU General Public License for more details. | |
15 | # | |
16 | # You should have received a copy of the GNU General Public License | |
e8c212d6 | 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
6bf19c94 CH |
18 | # |
19 | ||
20 | dd() | |
21 | { | |
22 | if [ "$HOSTOS" == "Linux" ] | |
23 | then | |
24 | command dd --help | grep noxfer > /dev/null 2>&1 | |
25 | ||
26 | if [ "$?" -eq 0 ] | |
27 | then | |
28 | command dd status=noxfer $@ | |
29 | else | |
30 | command dd $@ | |
31 | fi | |
32 | else | |
33 | command dd $@ | |
34 | fi | |
35 | } | |
36 | ||
37 | # we need common.config | |
38 | if [ "$iam" != "check" ] | |
39 | then | |
40 | if ! . ./common.config | |
41 | then | |
42 | echo "$iam: failed to source common.config" | |
43 | exit 1 | |
44 | fi | |
45 | fi | |
46 | ||
47 | # make sure we have a standard umask | |
48 | umask 022 | |
49 | ||
9cdfa1b3 MK |
50 | if [ "$IMGPROTO" = "file" ]; then |
51 | TEST_IMG=$TEST_DIR/t.$IMGFMT | |
a9660664 NT |
52 | elif [ "$IMGPROTO" = "nbd" ]; then |
53 | TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT | |
54 | TEST_IMG="nbd:127.0.0.1:10810" | |
9cdfa1b3 MK |
55 | else |
56 | TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT | |
57 | fi | |
6bf19c94 | 58 | |
2f24e8fb KW |
59 | function valgrind_qemu_io() |
60 | { | |
61 | valgrind --log-file=/tmp/$$.valgrind --error-exitcode=99 $REAL_QEMU_IO "$@" | |
62 | if [ $? != 0 ]; then | |
63 | cat /tmp/$$.valgrind | |
64 | fi | |
65 | rm -f /tmp/$$.valgrind | |
66 | } | |
67 | ||
68 | ||
89004368 KW |
69 | _optstr_add() |
70 | { | |
71 | if [ -n "$1" ]; then | |
72 | echo "$1,$2" | |
73 | else | |
74 | echo "$2" | |
75 | fi | |
76 | } | |
77 | ||
78 | _set_default_imgopts() | |
79 | { | |
80 | if [ "$IMGFMT" == "qcow2" ] && ! (echo "$IMGOPTS" | grep "compat=" > /dev/null); then | |
81 | IMGOPTS=$(_optstr_add "$IMGOPTS" "compat=1.1") | |
82 | fi | |
83 | } | |
84 | ||
6bf19c94 CH |
85 | _make_test_img() |
86 | { | |
87 | # extra qemu-img options can be added by tests | |
88 | # at least one argument (the image size) needs to be added | |
21af8148 | 89 | local extra_img_options="" |
21af8148 | 90 | local image_size=$* |
89004368 | 91 | local optstr="" |
a9660664 NT |
92 | local img_name="" |
93 | ||
94 | if [ -n "$TEST_IMG_FILE" ]; then | |
95 | img_name=$TEST_IMG_FILE | |
96 | else | |
97 | img_name=$TEST_IMG | |
98 | fi | |
89004368 KW |
99 | |
100 | if [ -n "$IMGOPTS" ]; then | |
101 | optstr=$(_optstr_add "$optstr" "$IMGOPTS") | |
102 | fi | |
6bf19c94 | 103 | |
21af8148 SW |
104 | if [ "$1" = "-b" ]; then |
105 | extra_img_options="$1 $2" | |
106 | image_size=$3 | |
107 | fi | |
f5a4bbd9 | 108 | if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then |
89004368 KW |
109 | optstr=$(_optstr_add "$optstr" "cluster_size=$CLUSTER_SIZE") |
110 | fi | |
111 | ||
112 | if [ -n "$optstr" ]; then | |
113 | extra_img_options="-o $optstr $extra_img_options" | |
8fc1024c KW |
114 | fi |
115 | ||
6bf19c94 | 116 | # XXX(hch): have global image options? |
a9660664 | 117 | $QEMU_IMG create -f $IMGFMT $extra_img_options $img_name $image_size | \ |
353a41be KW |
118 | sed -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \ |
119 | -e "s#$TEST_DIR#TEST_DIR#g" \ | |
120 | -e "s#$IMGFMT#IMGFMT#g" \ | |
121 | -e "s# encryption=off##g" \ | |
122 | -e "s# cluster_size=[0-9]\\+##g" \ | |
123 | -e "s# table_size=[0-9]\\+##g" \ | |
124 | -e "s# compat='[^']*'##g" \ | |
125 | -e "s# compat6=\\(on\\|off\\)##g" \ | |
126 | -e "s# static=\\(on\\|off\\)##g" \ | |
127 | -e "s# lazy_refcounts=\\(on\\|off\\)##g" | |
a9660664 NT |
128 | |
129 | # Start an NBD server on the image file, which is what we'll be talking to | |
130 | if [ $IMGPROTO = "nbd" ]; then | |
131 | eval "$QEMU_NBD -v -t -b 127.0.0.1 -p 10810 $TEST_IMG_FILE &" | |
132 | QEMU_NBD_PID=$! | |
133 | sleep 1 # FIXME: qemu-nbd needs to be listening before we continue | |
134 | fi | |
6bf19c94 CH |
135 | } |
136 | ||
137 | _cleanup_test_img() | |
138 | { | |
9cdfa1b3 MK |
139 | case "$IMGPROTO" in |
140 | ||
a9660664 NT |
141 | nbd) |
142 | kill $QEMU_NBD_PID | |
143 | rm -f $TEST_IMG_FILE | |
144 | ;; | |
9cdfa1b3 MK |
145 | file) |
146 | rm -f $TEST_DIR/t.$IMGFMT | |
147 | rm -f $TEST_DIR/t.$IMGFMT.orig | |
148 | rm -f $TEST_DIR/t.$IMGFMT.base | |
149 | ;; | |
150 | ||
151 | rbd) | |
152 | rbd rm $TEST_DIR/t.$IMGFMT > /dev/null | |
153 | ;; | |
154 | ||
155 | sheepdog) | |
156 | collie vdi delete $TEST_DIR/t.$IMGFMT | |
157 | ;; | |
158 | ||
159 | esac | |
6bf19c94 CH |
160 | } |
161 | ||
162 | _check_test_img() | |
163 | { | |
e76a8e89 | 164 | $QEMU_IMG check -f $IMGFMT $TEST_IMG 2>&1 | \ |
a5126c75 | 165 | grep -v "fragmented$" | \ |
e76a8e89 | 166 | sed -e 's/qemu-img\: This image format does not support checks/No errors were found on the image./' |
6bf19c94 CH |
167 | } |
168 | ||
514d9da5 SH |
169 | _img_info() |
170 | { | |
171 | $QEMU_IMG info "$@" $TEST_IMG 2>&1 | \ | |
172 | sed -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \ | |
173 | -e "s#$TEST_DIR#TEST_DIR#g" \ | |
174 | -e "s#$IMGFMT#IMGFMT#g" \ | |
175 | -e "/^disk size:/ D" \ | |
176 | -e "/actual-size/ D" | |
177 | } | |
178 | ||
6bf19c94 CH |
179 | _get_pids_by_name() |
180 | { | |
181 | if [ $# -ne 1 ] | |
182 | then | |
183 | echo "Usage: _get_pids_by_name process-name" 1>&2 | |
184 | exit 1 | |
185 | fi | |
186 | ||
187 | # Algorithm ... all ps(1) variants have a time of the form MM:SS or | |
188 | # HH:MM:SS before the psargs field, use this as the search anchor. | |
189 | # | |
190 | # Matches with $1 (process-name) occur if the first psarg is $1 | |
191 | # or ends in /$1 ... the matching uses sed's regular expressions, | |
192 | # so passing a regex into $1 will work. | |
193 | ||
194 | ps $PS_ALL_FLAGS \ | |
195 | | sed -n \ | |
196 | -e 's/$/ /' \ | |
197 | -e 's/[ ][ ]*/ /g' \ | |
198 | -e 's/^ //' \ | |
199 | -e 's/^[^ ]* //' \ | |
200 | -e "/[0-9]:[0-9][0-9] *[^ ]*\/$1 /s/ .*//p" \ | |
201 | -e "/[0-9]:[0-9][0-9] *$1 /s/ .*//p" | |
202 | } | |
203 | ||
204 | # fqdn for localhost | |
205 | # | |
206 | _get_fqdn() | |
207 | { | |
208 | host=`hostname` | |
209 | $NSLOOKUP_PROG $host | $AWK_PROG '{ if ($1 == "Name:") print $2 }' | |
210 | } | |
211 | ||
212 | # check if run as root | |
213 | # | |
214 | _need_to_be_root() | |
215 | { | |
216 | id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'` | |
217 | if [ "$id" -ne 0 ] | |
218 | then | |
219 | echo "Arrgh ... you need to be root (not uid=$id) to run this test" | |
220 | exit 1 | |
221 | fi | |
222 | } | |
223 | ||
224 | ||
225 | # Do a command, log it to $seq.full, optionally test return status | |
226 | # and die if command fails. If called with one argument _do executes the | |
227 | # command, logs it, and returns its exit status. With two arguments _do | |
228 | # first prints the message passed in the first argument, and then "done" | |
229 | # or "fail" depending on the return status of the command passed in the | |
230 | # second argument. If the command fails and the variable _do_die_on_error | |
231 | # is set to "always" or the two argument form is used and _do_die_on_error | |
232 | # is set to "message_only" _do will print an error message to | |
233 | # $seq.out and exit. | |
234 | ||
235 | _do() | |
236 | { | |
237 | if [ $# -eq 1 ]; then | |
238 | _cmd=$1 | |
239 | elif [ $# -eq 2 ]; then | |
240 | _note=$1 | |
241 | _cmd=$2 | |
242 | echo -n "$_note... " | |
243 | else | |
244 | echo "Usage: _do [note] cmd" 1>&2 | |
245 | status=1; exit | |
246 | fi | |
247 | ||
248 | (eval "echo '---' \"$_cmd\"") >>$here/$seq.full | |
249 | (eval "$_cmd") >$tmp._out 2>&1; ret=$? | |
250 | cat $tmp._out >>$here/$seq.full | |
251 | if [ $# -eq 2 ]; then | |
252 | if [ $ret -eq 0 ]; then | |
253 | echo "done" | |
254 | else | |
255 | echo "fail" | |
256 | fi | |
257 | fi | |
258 | if [ $ret -ne 0 ] \ | |
259 | && [ "$_do_die_on_error" = "always" \ | |
260 | -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ] | |
261 | then | |
262 | [ $# -ne 2 ] && echo | |
263 | eval "echo \"$_cmd\" failed \(returned $ret\): see $seq.full" | |
264 | status=1; exit | |
265 | fi | |
266 | ||
267 | return $ret | |
268 | } | |
269 | ||
270 | # bail out, setting up .notrun file | |
271 | # | |
272 | _notrun() | |
273 | { | |
274 | echo "$*" >$seq.notrun | |
275 | echo "$seq not run: $*" | |
276 | status=0 | |
277 | exit | |
278 | } | |
279 | ||
280 | # just plain bail out | |
281 | # | |
282 | _fail() | |
283 | { | |
284 | echo "$*" | tee -a $here/$seq.full | |
285 | echo "(see $seq.full for details)" | |
286 | status=1 | |
287 | exit 1 | |
288 | } | |
289 | ||
290 | # tests whether $IMGFMT is one of the supported image formats for a test | |
291 | # | |
292 | _supported_fmt() | |
293 | { | |
294 | for f; do | |
295 | if [ "$f" = "$IMGFMT" -o "$f" = "generic" ]; then | |
296 | return | |
297 | fi | |
298 | done | |
299 | ||
300 | _notrun "not suitable for this image format: $IMGFMT" | |
301 | } | |
302 | ||
9cdfa1b3 MK |
303 | # tests whether $IMGPROTO is one of the supported image protocols for a test |
304 | # | |
305 | _supported_proto() | |
306 | { | |
307 | for f; do | |
308 | if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then | |
309 | return | |
310 | fi | |
311 | done | |
312 | ||
313 | _notrun "not suitable for this image protocol: $IMGPROTO" | |
314 | } | |
315 | ||
6bf19c94 CH |
316 | # tests whether the host OS is one of the supported OSes for a test |
317 | # | |
318 | _supported_os() | |
319 | { | |
320 | for h | |
321 | do | |
322 | if [ "$h" = "$HOSTOS" ] | |
323 | then | |
324 | return | |
325 | fi | |
326 | done | |
327 | ||
328 | _notrun "not suitable for this OS: $HOSTOS" | |
329 | } | |
330 | ||
166f3c7b SH |
331 | _unsupported_qemu_io_options() |
332 | { | |
333 | for bad_opt | |
334 | do | |
335 | for opt in $QEMU_IO_OPTIONS | |
336 | do | |
337 | if [ "$bad_opt" = "$opt" ] | |
338 | then | |
339 | _notrun "not suitable for qemu-io option: $bad_opt" | |
340 | fi | |
341 | done | |
342 | done | |
343 | } | |
344 | ||
6bf19c94 CH |
345 | # this test requires that a specified command (executable) exists |
346 | # | |
347 | _require_command() | |
348 | { | |
349 | [ -x "$1" ] || _notrun "$1 utility required, skipped this test" | |
350 | } | |
351 | ||
352 | _full_imgfmt_details() | |
353 | { | |
89004368 KW |
354 | if [ -n "$IMGOPTS" ]; then |
355 | echo "$IMGFMT ($IMGOPTS)" | |
356 | else | |
357 | echo "$IMGFMT" | |
358 | fi | |
6bf19c94 CH |
359 | } |
360 | ||
9cdfa1b3 MK |
361 | _full_imgproto_details() |
362 | { | |
363 | echo "$IMGPROTO" | |
364 | } | |
365 | ||
6bf19c94 CH |
366 | _full_platform_details() |
367 | { | |
368 | os=`uname -s` | |
369 | host=`hostname -s` | |
370 | kernel=`uname -r` | |
371 | platform=`uname -m` | |
372 | echo "$os/$platform $host $kernel" | |
373 | } | |
374 | ||
375 | _link_out_file() | |
376 | { | |
377 | if [ -z "$1" ]; then | |
378 | echo Error must pass \$seq. | |
379 | exit | |
380 | fi | |
381 | rm -f $1 | |
382 | if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then | |
383 | ln -s $1.irix $1 | |
384 | elif [ "`uname`" == "Linux" ]; then | |
385 | ln -s $1.linux $1 | |
386 | else | |
387 | echo Error test $seq does not run on the operating system: `uname` | |
388 | exit | |
389 | fi | |
390 | } | |
391 | ||
392 | _die() | |
393 | { | |
394 | echo $@ | |
395 | exit 1 | |
396 | } | |
397 | ||
398 | # make sure this script returns success | |
399 | /bin/true |