]> Git Repo - qemu.git/blob - tests/qemu-iotests/common.rc
Merge tag 'block-pull-request' into staging
[qemu.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 [ "$IMGOPTSSYNTAX" = "true" ]; then
57     DRIVER="driver=$IMGFMT"
58     if [ "$IMGFMT" = "luks" ]; then
59         DRIVER="$DRIVER,key-secret=keysec0"
60     fi
61     if [ "$IMGPROTO" = "file" ]; then
62         TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
63         TEST_IMG="$DRIVER,file.filename=$TEST_DIR/t.$IMGFMT"
64     elif [ "$IMGPROTO" = "nbd" ]; then
65         TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
66         TEST_IMG="$DRIVER,file.driver=nbd,file.host=127.0.0.1,file.port=10810"
67     elif [ "$IMGPROTO" = "ssh" ]; then
68         TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
69         TEST_IMG="$DRIVER,file.driver=ssh,file.host=127.0.0.1,file.path=$TEST_IMG_FILE"
70     elif [ "$IMGPROTO" = "nfs" ]; then
71         TEST_DIR="$DRIVER,file.driver=nfs,file.filename=nfs://127.0.0.1/$TEST_DIR"
72         TEST_IMG=$TEST_DIR/t.$IMGFMT
73     else
74         TEST_IMG="$DRIVER,file.driver=$IMGPROTO,file.filename=$TEST_DIR/t.$IMGFMT"
75     fi
76 else
77     if [ "$IMGPROTO" = "file" ]; then
78         TEST_IMG=$TEST_DIR/t.$IMGFMT
79     elif [ "$IMGPROTO" = "nbd" ]; then
80         TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
81         TEST_IMG="nbd:127.0.0.1:10810"
82     elif [ "$IMGPROTO" = "ssh" ]; then
83         TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
84         TEST_IMG="ssh://127.0.0.1$TEST_IMG_FILE"
85     elif [ "$IMGPROTO" = "nfs" ]; then
86         TEST_DIR="nfs://127.0.0.1/$TEST_DIR"
87         TEST_IMG=$TEST_DIR/t.$IMGFMT
88     elif [ "$IMGPROTO" = "vxhs" ]; then
89         TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
90         TEST_IMG="vxhs://127.0.0.1:9999/t.$IMGFMT"
91     else
92         TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT
93     fi
94 fi
95
96 _optstr_add()
97 {
98     if [ -n "$1" ]; then
99         echo "$1,$2"
100     else
101         echo "$2"
102     fi
103 }
104
105 _set_default_imgopts()
106 {
107     if [ "$IMGFMT" == "qcow2" ] && ! (echo "$IMGOPTS" | grep "compat=" > /dev/null); then
108         IMGOPTS=$(_optstr_add "$IMGOPTS" "compat=1.1")
109     fi
110 }
111
112 _use_sample_img()
113 {
114     SAMPLE_IMG_FILE="${1%\.bz2}"
115     TEST_IMG="$TEST_DIR/$SAMPLE_IMG_FILE"
116     bzcat "$SAMPLE_IMG_DIR/$1" > "$TEST_IMG"
117     if [ $? -ne 0 ]
118     then
119         echo "_use_sample_img error, cannot extract '$SAMPLE_IMG_DIR/$1'"
120         exit 1
121     fi
122 }
123
124 _make_test_img()
125 {
126     # extra qemu-img options can be added by tests
127     # at least one argument (the image size) needs to be added
128     local extra_img_options=""
129     local image_size=$*
130     local optstr=""
131     local img_name=""
132     local use_backing=0
133     local backing_file=""
134     local object_options=""
135
136     if [ -n "$TEST_IMG_FILE" ]; then
137         img_name=$TEST_IMG_FILE
138     else
139         img_name=$TEST_IMG
140     fi
141
142     if [ -n "$IMGOPTS" ]; then
143         optstr=$(_optstr_add "$optstr" "$IMGOPTS")
144     fi
145     if [ -n "$IMGKEYSECRET" ]; then
146         object_options="--object secret,id=keysec0,data=$IMGKEYSECRET"
147         optstr=$(_optstr_add "$optstr" "key-secret=keysec0")
148     fi
149
150     if [ "$1" = "-b" ]; then
151         use_backing=1
152         backing_file=$2
153         image_size=$3
154     fi
155     if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then
156         optstr=$(_optstr_add "$optstr" "cluster_size=$CLUSTER_SIZE")
157     fi
158
159     if [ -n "$optstr" ]; then
160         extra_img_options="-o $optstr $extra_img_options"
161     fi
162
163     # XXX(hch): have global image options?
164     (
165      if [ $use_backing = 1 ]; then
166         $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options -b "$backing_file" "$img_name" $image_size 2>&1
167      else
168         $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options "$img_name" $image_size 2>&1
169      fi
170     ) | _filter_img_create
171
172     # Start an NBD server on the image file, which is what we'll be talking to
173     if [ $IMGPROTO = "nbd" ]; then
174         # Pass a sufficiently high number to -e that should be enough for all
175         # tests
176         eval "$QEMU_NBD -v -t -b 127.0.0.1 -p 10810 -f $IMGFMT -e 42  $TEST_IMG_FILE >/dev/null &"
177         sleep 1 # FIXME: qemu-nbd needs to be listening before we continue
178     fi
179
180     # Start QNIO server on image directory for vxhs protocol
181     if [ $IMGPROTO = "vxhs" ]; then
182         eval "$QEMU_VXHS -d  $TEST_DIR > /dev/null &"
183         sleep 1 # Wait for server to come up.
184     fi
185 }
186
187 _rm_test_img()
188 {
189     local img=$1
190     if [ "$IMGFMT" = "vmdk" ]; then
191         # Remove all the extents for vmdk
192         "$QEMU_IMG" info "$img" 2>/dev/null | grep 'filename:' | cut -f 2 -d: \
193             | xargs -I {} rm -f "{}"
194     fi
195     rm -f "$img"
196 }
197
198 _cleanup_test_img()
199 {
200     case "$IMGPROTO" in
201
202         nbd)
203             if [ -f "${QEMU_TEST_DIR}/qemu-nbd.pid" ]; then
204                 local QEMU_NBD_PID
205                 read QEMU_NBD_PID < "${QEMU_TEST_DIR}/qemu-nbd.pid"
206                 kill ${QEMU_NBD_PID}
207                 rm -f "${QEMU_TEST_DIR}/qemu-nbd.pid"
208             fi
209             rm -f "$TEST_IMG_FILE"
210             ;;
211         vxhs)
212             if [ -f "${TEST_DIR}/qemu-vxhs.pid" ]; then
213                 local QEMU_VXHS_PID
214                 read QEMU_VXHS_PID < "${TEST_DIR}/qemu-vxhs.pid"
215                 kill ${QEMU_VXHS_PID} >/dev/null 2>&1
216                 rm -f "${TEST_DIR}/qemu-vxhs.pid"
217             fi
218             rm -f "$TEST_IMG_FILE"
219             ;;
220
221         file)
222             _rm_test_img "$TEST_DIR/t.$IMGFMT"
223             _rm_test_img "$TEST_DIR/t.$IMGFMT.orig"
224             _rm_test_img "$TEST_DIR/t.$IMGFMT.base"
225             if [ -n "$SAMPLE_IMG_FILE" ]
226             then
227                 rm -f "$TEST_DIR/$SAMPLE_IMG_FILE"
228             fi
229             ;;
230
231         rbd)
232             rbd --no-progress rm "$TEST_DIR/t.$IMGFMT" > /dev/null
233             ;;
234
235         sheepdog)
236             collie vdi delete "$TEST_DIR/t.$IMGFMT"
237             ;;
238
239     esac
240 }
241
242 _check_test_img()
243 {
244     (
245         if [ "$IMGOPTSSYNTAX" = "true" ]; then
246             $QEMU_IMG check $QEMU_IMG_EXTRA_ARGS "$@" "$TEST_IMG" 2>&1
247         else
248             $QEMU_IMG check "$@" -f $IMGFMT "$TEST_IMG" 2>&1
249         fi
250     ) | _filter_testdir | _filter_qemu_img_check
251 }
252
253 _img_info()
254 {
255     if [[ "$1" == "--format-specific" ]]; then
256         local format_specific=1
257         shift
258     else
259         local format_specific=0
260     fi
261
262     discard=0
263     regex_json_spec_start='^ *"format-specific": \{'
264     $QEMU_IMG info "$@" "$TEST_IMG" 2>&1 | \
265         sed -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
266             -e "s#$TEST_DIR#TEST_DIR#g" \
267             -e "s#$IMGFMT#IMGFMT#g" \
268             -e "/^disk size:/ D" \
269             -e "/actual-size/ D" | \
270         while IFS='' read line; do
271             if [[ $format_specific == 1 ]]; then
272                 discard=0
273             elif [[ $line == "Format specific information:" ]]; then
274                 discard=1
275             elif [[ $line =~ $regex_json_spec_start ]]; then
276                 discard=2
277                 regex_json_spec_end="^${line%%[^ ]*}\\},? *$"
278             fi
279             if [[ $discard == 0 ]]; then
280                 echo "$line"
281             elif [[ $discard == 1 && ! $line ]]; then
282                 echo
283                 discard=0
284             elif [[ $discard == 2 && $line =~ $regex_json_spec_end ]]; then
285                 discard=0
286             fi
287         done
288 }
289
290 _get_pids_by_name()
291 {
292     if [ $# -ne 1 ]
293     then
294         echo "Usage: _get_pids_by_name process-name" 1>&2
295         exit 1
296     fi
297
298     # Algorithm ... all ps(1) variants have a time of the form MM:SS or
299     # HH:MM:SS before the psargs field, use this as the search anchor.
300     #
301     # Matches with $1 (process-name) occur if the first psarg is $1
302     # or ends in /$1 ... the matching uses sed's regular expressions,
303     # so passing a regex into $1 will work.
304
305     ps $PS_ALL_FLAGS \
306     | sed -n \
307         -e 's/$/ /' \
308         -e 's/[         ][         ]*/ /g' \
309         -e 's/^ //' \
310         -e 's/^[^ ]* //' \
311         -e "/[0-9]:[0-9][0-9]  *[^ ]*\/$1 /s/ .*//p" \
312         -e "/[0-9]:[0-9][0-9]  *$1 /s/ .*//p"
313 }
314
315 # fqdn for localhost
316 #
317 _get_fqdn()
318 {
319     host=`hostname`
320     $NSLOOKUP_PROG $host | $AWK_PROG '{ if ($1 == "Name:") print $2 }'
321 }
322
323 # check if run as root
324 #
325 _need_to_be_root()
326 {
327     id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'`
328     if [ "$id" -ne 0 ]
329     then
330         echo "Arrgh ... you need to be root (not uid=$id) to run this test"
331         exit 1
332     fi
333 }
334
335 # bail out, setting up .notrun file
336 #
337 _notrun()
338 {
339     echo "$*" >"$OUTPUT_DIR/$seq.notrun"
340     echo "$seq not run: $*"
341     status=0
342     exit
343 }
344
345 # just plain bail out
346 #
347 _fail()
348 {
349     echo "$*" | tee -a "$OUTPUT_DIR/$seq.full"
350     echo "(see $seq.full for details)"
351     status=1
352     exit 1
353 }
354
355 # tests whether $IMGFMT is one of the supported image formats for a test
356 #
357 _supported_fmt()
358 {
359     # "generic" is suitable for most image formats. For some formats it doesn't
360     # work, however (most notably read-only formats), so they can opt out by
361     # setting IMGFMT_GENERIC to false.
362     for f; do
363         if [ "$f" = "$IMGFMT" -o "$f" = "generic" -a "$IMGFMT_GENERIC" = "true" ]; then
364             return
365         fi
366     done
367
368     _notrun "not suitable for this image format: $IMGFMT"
369 }
370
371 # tests whether $IMGFMT is one of the unsupported image format for a test
372 #
373 _unsupported_fmt()
374 {
375     for f; do
376         if [ "$f" = "$IMGFMT" ]; then
377             _notrun "not suitable for this image format: $IMGFMT"
378         fi
379     done
380 }
381
382 # tests whether $IMGPROTO is one of the supported image protocols for a test
383 #
384 _supported_proto()
385 {
386     for f; do
387         if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then
388             return
389         fi
390     done
391
392     _notrun "not suitable for this image protocol: $IMGPROTO"
393 }
394
395 # tests whether $IMGPROTO is specified as an unsupported image protocol for a test
396 #
397 _unsupported_proto()
398 {
399     for f; do
400         if [ "$f" = "$IMGPROTO" ]; then
401             _notrun "not suitable for this image protocol: $IMGPROTO"
402             return
403         fi
404     done
405 }
406
407 # tests whether the host OS is one of the supported OSes for a test
408 #
409 _supported_os()
410 {
411     for h
412     do
413         if [ "$h" = "$HOSTOS" ]
414         then
415             return
416         fi
417     done
418
419     _notrun "not suitable for this OS: $HOSTOS"
420 }
421
422 _supported_cache_modes()
423 {
424     for mode; do
425         if [ "$mode" = "$CACHEMODE" ]; then
426             return
427         fi
428     done
429     _notrun "not suitable for cache mode: $CACHEMODE"
430 }
431
432 _default_cache_mode()
433 {
434     if $CACHEMODE_IS_DEFAULT; then
435         CACHEMODE="$1"
436         QEMU_IO="$QEMU_IO --cache $1"
437         return
438     fi
439 }
440
441 _unsupported_imgopts()
442 {
443     for bad_opt
444     do
445         if echo "$IMGOPTS" | grep -q 2>/dev/null "$bad_opt"
446         then
447             _notrun "not suitable for image option: $bad_opt"
448         fi
449     done
450 }
451
452 # this test requires that a specified command (executable) exists
453 #
454 _require_command()
455 {
456     if [ "$1" = "QEMU" ]; then
457         c=$QEMU_PROG
458     elif [ "$1" = "QEMU_IMG" ]; then
459         c=$QEMU_IMG_PROG
460     elif [ "$1" = "QEMU_IO" ]; then
461         c=$QEMU_IO_PROG
462     elif [ "$1" = "QEMU_NBD" ]; then
463         c=$QEMU_NBD_PROG
464     else
465         eval c=\$$1
466     fi
467     [ -x "$c" ] || _notrun "$1 utility required, skipped this test"
468 }
469
470 _full_imgfmt_details()
471 {
472     if [ -n "$IMGOPTS" ]; then
473         echo "$IMGFMT ($IMGOPTS)"
474     else
475         echo "$IMGFMT"
476     fi
477 }
478
479 _full_imgproto_details()
480 {
481     echo "$IMGPROTO"
482 }
483
484 _full_platform_details()
485 {
486     os=`uname -s`
487     host=`hostname -s`
488     kernel=`uname -r`
489     platform=`uname -m`
490     echo "$os/$platform $host $kernel"
491 }
492
493 _link_out_file()
494 {
495    if [ -z "$1" ]; then
496       echo Error must pass \$seq.
497       exit
498    fi
499    rm -f $1
500    if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then
501       ln -s $1.irix $1
502    elif [ "`uname`" == "Linux" ]; then
503       ln -s $1.linux $1
504    else
505       echo Error test $seq does not run on the operating system: `uname`
506       exit
507    fi
508 }
509
510 _die()
511 {
512         echo $@
513         exit 1
514 }
515
516 # make sure this script returns success
517 true
This page took 0.051946 seconds and 4 git commands to generate.