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