]> rtime.felk.cvut.cz Git - lisovros/qemu_apohw.git/blob - tests/qemu-iotests/common.rc
qemu-iotests: add ./check -cloop support
[lisovros/qemu_apohw.git] / tests / qemu-iotests / common.rc
1 #!/bin/bash
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
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
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 # poke_file 'test.img' 512 '\xff\xfe'
38 poke_file()
39 {
40     printf "$3" | dd "of=$1" bs=1 "seek=$2" conv=notrunc &>/dev/null
41 }
42
43 # we need common.config
44 if [ "$iam" != "check" ]
45 then
46     if ! . ./common.config
47         then
48         echo "$iam: failed to source common.config"
49         exit 1
50     fi
51 fi
52
53 # make sure we have a standard umask
54 umask 022
55
56 if [ "$IMGPROTO" = "file" ]; then
57     TEST_IMG=$TEST_DIR/t.$IMGFMT
58 elif [ "$IMGPROTO" = "nbd" ]; then
59     TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
60     TEST_IMG="nbd:127.0.0.1:10810"
61 elif [ "$IMGPROTO" = "ssh" ]; then
62     TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
63     TEST_IMG="ssh://127.0.0.1$TEST_IMG_FILE"
64 elif [ "$IMGPROTO" = "nfs" ]; then
65     TEST_DIR="nfs://127.0.0.1/$TEST_DIR"
66     TEST_IMG=$TEST_DIR/t.$IMGFMT
67 else
68     TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT
69 fi
70
71 function valgrind_qemu_io()
72 {
73     valgrind --log-file=/tmp/$$.valgrind --error-exitcode=99 $REAL_QEMU_IO "$@"
74     if [ $? != 0 ]; then
75         cat /tmp/$$.valgrind
76     fi
77     rm -f /tmp/$$.valgrind
78 }
79
80
81 _optstr_add()
82 {
83     if [ -n "$1" ]; then
84         echo "$1,$2"
85     else
86         echo "$2"
87     fi
88 }
89
90 _set_default_imgopts()
91 {
92     if [ "$IMGFMT" == "qcow2" ] && ! (echo "$IMGOPTS" | grep "compat=" > /dev/null); then
93         IMGOPTS=$(_optstr_add "$IMGOPTS" "compat=1.1")
94     fi
95 }
96
97 _use_sample_img()
98 {
99     SAMPLE_IMG_FILE="${1%\.bz2}"
100     TEST_IMG="$TEST_DIR/$SAMPLE_IMG_FILE"
101     bzcat "$SAMPLE_IMG_DIR/$1" > "$TEST_IMG"
102     if [ $? -ne 0 ]
103     then
104         echo "_use_sample_img error, cannot extract '$SAMPLE_IMG_DIR/$1'"
105         exit 1
106     fi
107 }
108
109 _make_test_img()
110 {
111     # extra qemu-img options can be added by tests
112     # at least one argument (the image size) needs to be added
113     local extra_img_options=""
114     local image_size=$*
115     local optstr=""
116     local img_name=""
117     local use_backing=0
118     local backing_file=""
119
120     if [ -n "$TEST_IMG_FILE" ]; then
121         img_name=$TEST_IMG_FILE
122     else
123         img_name=$TEST_IMG
124     fi
125
126     if [ -n "$IMGOPTS" ]; then
127         optstr=$(_optstr_add "$optstr" "$IMGOPTS")
128     fi
129
130     if [ "$1" = "-b" ]; then
131         use_backing=1
132         backing_file=$2
133         image_size=$3
134     fi
135     if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then
136         optstr=$(_optstr_add "$optstr" "cluster_size=$CLUSTER_SIZE")
137     fi
138
139     if [ -n "$optstr" ]; then
140         extra_img_options="-o $optstr $extra_img_options"
141     fi
142
143     # XXX(hch): have global image options?
144     (
145      if [ $use_backing = 1 ]; then
146         $QEMU_IMG create -f $IMGFMT $extra_img_options -b "$backing_file" "$img_name" $image_size 2>&1
147      else
148         $QEMU_IMG create -f $IMGFMT $extra_img_options "$img_name" $image_size 2>&1
149      fi
150     ) | \
151         sed -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
152             -e "s#$TEST_DIR#TEST_DIR#g" \
153             -e "s#$IMGFMT#IMGFMT#g" \
154             -e "s# encryption=off##g" \
155             -e "s# cluster_size=[0-9]\\+##g" \
156             -e "s# table_size=[0-9]\\+##g" \
157             -e "s# compat='[^']*'##g" \
158             -e "s# compat6=\\(on\\|off\\)##g" \
159             -e "s# static=\\(on\\|off\\)##g" \
160             -e "s# zeroed_grain=\\(on\\|off\\)##g" \
161             -e "s# subformat='[^']*'##g" \
162             -e "s# adapter_type='[^']*'##g" \
163             -e "s# lazy_refcounts=\\(on\\|off\\)##g" \
164             -e "s# block_size=[0-9]\\+##g" \
165             -e "s# block_state_zero=\\(on\\|off\\)##g" \
166             -e "s# log_size=[0-9]\\+##g"
167
168     # Start an NBD server on the image file, which is what we'll be talking to
169     if [ $IMGPROTO = "nbd" ]; then
170         eval "$QEMU_NBD -v -t -b 127.0.0.1 -p 10810  $TEST_IMG_FILE &"
171         QEMU_NBD_PID=$!
172         sleep 1 # FIXME: qemu-nbd needs to be listening before we continue
173     fi
174 }
175
176 _rm_test_img()
177 {
178     local img=$1
179     if [ "$IMGFMT" = "vmdk" ]; then
180         # Remove all the extents for vmdk
181         $QEMU_IMG info $img 2>/dev/null | grep 'filename:' | cut -f 2 -d: \
182             | xargs -I {} rm -f "{}"
183     fi
184     rm -f $img
185 }
186
187 _cleanup_test_img()
188 {
189     case "$IMGPROTO" in
190
191         nbd)
192             kill $QEMU_NBD_PID
193             rm -f "$TEST_IMG_FILE"
194             ;;
195         file)
196             _rm_test_img "$TEST_DIR/t.$IMGFMT"
197             _rm_test_img "$TEST_DIR/t.$IMGFMT.orig"
198             _rm_test_img "$TEST_DIR/t.$IMGFMT.base"
199             if [ -n "$SAMPLE_IMG_FILE" ]
200             then
201                 rm -f "$TEST_DIR/$SAMPLE_IMG_FILE"
202             fi
203             ;;
204
205         rbd)
206             rbd --no-progress rm "$TEST_DIR/t.$IMGFMT" > /dev/null
207             ;;
208
209         sheepdog)
210             collie vdi delete "$TEST_DIR/t.$IMGFMT"
211             ;;
212
213     esac
214 }
215
216 _check_test_img()
217 {
218     $QEMU_IMG check "$@" -f $IMGFMT "$TEST_IMG" 2>&1 | _filter_testdir | \
219         sed -e '/allocated.*fragmented.*compressed clusters/d' \
220             -e 's/qemu-img: This image format does not support checks/No errors were found on the image./' \
221             -e '/Image end offset: [0-9]\+/d'
222 }
223
224 _img_info()
225 {
226     discard=0
227     regex_json_spec_start='^ *"format-specific": \{'
228     $QEMU_IMG info "$@" "$TEST_IMG" 2>&1 | \
229         sed -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
230             -e "s#$TEST_DIR#TEST_DIR#g" \
231             -e "s#$IMGFMT#IMGFMT#g" \
232             -e "/^disk size:/ D" \
233             -e "/actual-size/ D" | \
234         while IFS='' read line; do
235             if [[ $line == "Format specific information:" ]]; then
236                 discard=1
237             elif [[ $line =~ $regex_json_spec_start ]]; then
238                 discard=2
239                 regex_json_spec_end="^${line%%[^ ]*}\\},? *$"
240             fi
241             if [[ $discard == 0 ]]; then
242                 echo "$line"
243             elif [[ $discard == 1 && ! $line ]]; then
244                 echo
245                 discard=0
246             elif [[ $discard == 2 && $line =~ $regex_json_spec_end ]]; then
247                 discard=0
248             fi
249         done
250 }
251
252 _get_pids_by_name()
253 {
254     if [ $# -ne 1 ]
255     then
256         echo "Usage: _get_pids_by_name process-name" 1>&2
257         exit 1
258     fi
259
260     # Algorithm ... all ps(1) variants have a time of the form MM:SS or
261     # HH:MM:SS before the psargs field, use this as the search anchor.
262     #
263     # Matches with $1 (process-name) occur if the first psarg is $1
264     # or ends in /$1 ... the matching uses sed's regular expressions,
265     # so passing a regex into $1 will work.
266
267     ps $PS_ALL_FLAGS \
268     | sed -n \
269         -e 's/$/ /' \
270         -e 's/[         ][         ]*/ /g' \
271         -e 's/^ //' \
272         -e 's/^[^ ]* //' \
273         -e "/[0-9]:[0-9][0-9]  *[^ ]*\/$1 /s/ .*//p" \
274         -e "/[0-9]:[0-9][0-9]  *$1 /s/ .*//p"
275 }
276
277 # fqdn for localhost
278 #
279 _get_fqdn()
280 {
281     host=`hostname`
282     $NSLOOKUP_PROG $host | $AWK_PROG '{ if ($1 == "Name:") print $2 }'
283 }
284
285 # check if run as root
286 #
287 _need_to_be_root()
288 {
289     id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'`
290     if [ "$id" -ne 0 ]
291     then
292         echo "Arrgh ... you need to be root (not uid=$id) to run this test"
293         exit 1
294     fi
295 }
296
297
298 # Do a command, log it to $seq.full, optionally test return status
299 # and die if command fails. If called with one argument _do executes the
300 # command, logs it, and returns its exit status. With two arguments _do
301 # first prints the message passed in the first argument, and then "done"
302 # or "fail" depending on the return status of the command passed in the
303 # second argument. If the command fails and the variable _do_die_on_error
304 # is set to "always" or the two argument form is used and _do_die_on_error
305 # is set to "message_only" _do will print an error message to
306 # $seq.out and exit.
307
308 _do()
309 {
310     if [ $# -eq 1 ]; then
311         _cmd=$1
312     elif [ $# -eq 2 ]; then
313         _note=$1
314         _cmd=$2
315         echo -n "$_note... "
316     else
317         echo "Usage: _do [note] cmd" 1>&2
318         status=1; exit
319     fi
320
321     (eval "echo '---' \"$_cmd\"") >>$here/$seq.full
322     (eval "$_cmd") >$tmp._out 2>&1; ret=$?
323     cat $tmp._out >>$here/$seq.full
324     if [ $# -eq 2 ]; then
325         if [ $ret -eq 0 ]; then
326             echo "done"
327         else
328             echo "fail"
329         fi
330     fi
331     if [ $ret -ne 0  ] \
332         && [ "$_do_die_on_error" = "always" \
333             -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
334     then
335         [ $# -ne 2 ] && echo
336         eval "echo \"$_cmd\" failed \(returned $ret\): see $seq.full"
337         status=1; exit
338     fi
339
340     return $ret
341 }
342
343 # bail out, setting up .notrun file
344 #
345 _notrun()
346 {
347     echo "$*" >$seq.notrun
348     echo "$seq not run: $*"
349     status=0
350     exit
351 }
352
353 # just plain bail out
354 #
355 _fail()
356 {
357     echo "$*" | tee -a $here/$seq.full
358     echo "(see $seq.full for details)"
359     status=1
360     exit 1
361 }
362
363 # tests whether $IMGFMT is one of the supported image formats for a test
364 #
365 _supported_fmt()
366 {
367     # "generic" is suitable for most image formats. For some formats it doesn't
368     # work, however (most notably read-only formats), so they can opt out by
369     # setting IMGFMT_GENERIC to false.
370     for f; do
371         if [ "$f" = "$IMGFMT" -o "$f" = "generic" -a "$IMGFMT_GENERIC" = "true" ]; then
372             return
373         fi
374     done
375
376     _notrun "not suitable for this image format: $IMGFMT"
377 }
378
379 # tests whether $IMGPROTO is one of the supported image protocols for a test
380 #
381 _supported_proto()
382 {
383     for f; do
384         if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then
385             return
386         fi
387     done
388
389     _notrun "not suitable for this image protocol: $IMGPROTO"
390 }
391
392 # tests whether the host OS is one of the supported OSes for a test
393 #
394 _supported_os()
395 {
396     for h
397     do
398         if [ "$h" = "$HOSTOS" ]
399         then
400             return
401         fi
402     done
403
404     _notrun "not suitable for this OS: $HOSTOS"
405 }
406
407 _supported_cache_modes()
408 {
409     for mode; do
410         if [ "$mode" = "$CACHEMODE" ]; then
411             return
412         fi
413     done
414     _notrun "not suitable for cache mode: $CACHEMODE"
415 }
416
417 _default_cache_mode()
418 {
419     if $CACHEMODE_IS_DEFAULT; then
420         CACHEMODE="$1"
421         QEMU_IO="$QEMU_IO --cache $1"
422         return
423     fi
424 }
425
426 _unsupported_imgopts()
427 {
428     for bad_opt
429     do
430         if echo "$IMGOPTS" | grep -q 2>/dev/null "$bad_opt"
431         then
432             _notrun "not suitable for image option: $bad_opt"
433         fi
434     done
435 }
436
437 # this test requires that a specified command (executable) exists
438 #
439 _require_command()
440 {
441     eval c=\$$1
442     [ -x "$c" ] || _notrun "$1 utility required, skipped this test"
443 }
444
445 _full_imgfmt_details()
446 {
447     if [ -n "$IMGOPTS" ]; then
448         echo "$IMGFMT ($IMGOPTS)"
449     else
450         echo "$IMGFMT"
451     fi
452 }
453
454 _full_imgproto_details()
455 {
456     echo "$IMGPROTO"
457 }
458
459 _full_platform_details()
460 {
461     os=`uname -s`
462     host=`hostname -s`
463     kernel=`uname -r`
464     platform=`uname -m`
465     echo "$os/$platform $host $kernel"
466 }
467
468 _link_out_file()
469 {
470    if [ -z "$1" ]; then
471       echo Error must pass \$seq.
472       exit
473    fi
474    rm -f $1
475    if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then
476       ln -s $1.irix $1
477    elif [ "`uname`" == "Linux" ]; then
478       ln -s $1.linux $1
479    else
480       echo Error test $seq does not run on the operating system: `uname`
481       exit
482    fi
483 }
484
485 _die()
486 {
487         echo $@
488         exit 1
489 }
490
491 # make sure this script returns success
492 /bin/true