]> git.proxmox.com Git - mirror_qemu.git/blame - tests/qemu-iotests/common.rc
Merge remote-tracking branch 'remotes/vivier/tags/q800-for-5.0-pull-request' into...
[mirror_qemu.git] / tests / qemu-iotests / common.rc
CommitLineData
11a82d14 1#!/usr/bin/env 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
bde36af1
PMD
20SED=
21for sed in sed gsed; do
22 ($sed --version | grep 'GNU sed') > /dev/null 2>&1
23 if [ "$?" -eq 0 ]; then
24 SED=$sed
25 break
26 fi
27done
28if [ -z "$SED" ]; then
29 echo "$0: GNU sed not found"
30 exit 1
31fi
32
6bf19c94
CH
33dd()
34{
35 if [ "$HOSTOS" == "Linux" ]
79e40ab1
KW
36 then
37 command dd --help | grep noxfer > /dev/null 2>&1
38
39 if [ "$?" -eq 0 ]
40 then
41 command dd status=noxfer $@
42 else
43 command dd $@
44 fi
6bf19c94 45 else
79e40ab1 46 command dd $@
6bf19c94
CH
47 fi
48}
49
23ea2ecc
SH
50# poke_file 'test.img' 512 '\xff\xfe'
51poke_file()
52{
53 printf "$3" | dd "of=$1" bs=1 "seek=$2" conv=notrunc &>/dev/null
54}
55
fc8ba423
HR
56# peek_file_le 'test.img' 512 2 => 65534
57peek_file_le()
58{
69135eb3
EB
59 local val=0 shift=0 byte
60
61 # coreutils' od --endian is not portable, so manually assemble bytes.
62 for byte in $(od -j"$2" -N"$3" -An -v -tu1 "$1"); do
63 val=$(( val | (byte << shift) ))
64 shift=$((shift + 8))
65 done
66 printf %llu $val
fc8ba423
HR
67}
68
69# peek_file_be 'test.img' 512 2 => 65279
70peek_file_be()
71{
69135eb3
EB
72 local val=0 byte
73
74 # coreutils' od --endian is not portable, so manually assemble bytes.
75 for byte in $(od -j"$2" -N"$3" -An -v -tu1 "$1"); do
76 val=$(( (val << 8) | byte ))
77 done
78 printf %llu $val
fc8ba423
HR
79}
80
69135eb3
EB
81# peek_file_raw 'test.img' 512 2 => '\xff\xfe'. Do not use if the raw data
82# is likely to contain \0 or trailing \n.
fc8ba423
HR
83peek_file_raw()
84{
85 dd if="$1" bs=1 skip="$2" count="$3" status=none
86}
87
3817ce03
PB
88
89if ! . ./common.config
90 then
4e670492 91 echo "$0: failed to source common.config"
3817ce03 92 exit 1
6bf19c94
CH
93fi
94
036d8cbf
AS
95# Set the variables to the empty string to turn Valgrind off
96# for specific processes, e.g.
97# $ VALGRIND_QEMU_IO= ./check -qcow2 -valgrind 015
98
99: ${VALGRIND_QEMU_VM=$VALGRIND_QEMU}
100: ${VALGRIND_QEMU_IMG=$VALGRIND_QEMU}
101: ${VALGRIND_QEMU_IO=$VALGRIND_QEMU}
102: ${VALGRIND_QEMU_NBD=$VALGRIND_QEMU}
103: ${VALGRIND_QEMU_VXHS=$VALGRIND_QEMU}
104
105# The Valgrind own parameters may be set with
106# its environment variable VALGRIND_OPTS, e.g.
107# $ VALGRIND_OPTS="--leak-check=yes" ./check -qcow2 -valgrind 015
108
109_qemu_proc_exec()
110{
111 local VALGRIND_LOGFILE="$1"
112 shift
8af224d6 113 if [[ "${VALGRIND_QEMU}" == "y" && "${NO_VALGRIND}" != "y" ]]; then
036d8cbf
AS
114 exec valgrind --log-file="${VALGRIND_LOGFILE}" --error-exitcode=99 "$@"
115 else
116 exec "$@"
117 fi
118}
119
120_qemu_proc_valgrind_log()
121{
122 local VALGRIND_LOGFILE="$1"
123 local RETVAL="$2"
8af224d6 124 if [[ "${VALGRIND_QEMU}" == "y" && "${NO_VALGRIND}" != "y" ]]; then
036d8cbf
AS
125 if [ $RETVAL == 99 ]; then
126 cat "${VALGRIND_LOGFILE}"
127 fi
128 rm -f "${VALGRIND_LOGFILE}"
129 fi
130}
131
d1f2447a
PB
132_qemu_wrapper()
133{
036d8cbf 134 local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
d1f2447a
PB
135 (
136 if [ -n "${QEMU_NEED_PID}" ]; then
137 echo $BASHPID > "${QEMU_TEST_DIR}/qemu-${_QEMU_HANDLE}.pid"
138 fi
036d8cbf
AS
139 VALGRIND_QEMU="${VALGRIND_QEMU_VM}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
140 "$QEMU_PROG" $QEMU_OPTIONS "$@"
d1f2447a 141 )
036d8cbf
AS
142 RETVAL=$?
143 _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
144 return $RETVAL
d1f2447a
PB
145}
146
147_qemu_img_wrapper()
148{
036d8cbf
AS
149 local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
150 (
151 VALGRIND_QEMU="${VALGRIND_QEMU_IMG}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
152 "$QEMU_IMG_PROG" $QEMU_IMG_OPTIONS "$@"
153 )
154 RETVAL=$?
155 _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
156 return $RETVAL
d1f2447a
PB
157}
158
159_qemu_io_wrapper()
160{
161 local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
162 local QEMU_IO_ARGS="$QEMU_IO_OPTIONS"
163 if [ "$IMGOPTSSYNTAX" = "true" ]; then
164 QEMU_IO_ARGS="--image-opts $QEMU_IO_ARGS"
165 if [ -n "$IMGKEYSECRET" ]; then
166 QEMU_IO_ARGS="--object secret,id=keysec0,data=$IMGKEYSECRET $QEMU_IO_ARGS"
167 fi
168 fi
d1f2447a 169 (
036d8cbf
AS
170 VALGRIND_QEMU="${VALGRIND_QEMU_IO}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
171 "$QEMU_IO_PROG" $QEMU_IO_ARGS "$@"
d1f2447a
PB
172 )
173 RETVAL=$?
036d8cbf
AS
174 _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
175 return $RETVAL
d1f2447a
PB
176}
177
178_qemu_nbd_wrapper()
179{
036d8cbf
AS
180 local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
181 (
182 VALGRIND_QEMU="${VALGRIND_QEMU_NBD}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
183 "$QEMU_NBD_PROG" --pid-file="${QEMU_TEST_DIR}/qemu-nbd.pid" \
184 $QEMU_NBD_OPTIONS "$@"
185 )
186 RETVAL=$?
187 _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
188 return $RETVAL
d1f2447a
PB
189}
190
191_qemu_vxhs_wrapper()
192{
036d8cbf 193 local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
d1f2447a
PB
194 (
195 echo $BASHPID > "${TEST_DIR}/qemu-vxhs.pid"
036d8cbf
AS
196 VALGRIND_QEMU="${VALGRIND_QEMU_VXHS}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
197 "$QEMU_VXHS_PROG" $QEMU_VXHS_OPTIONS "$@"
d1f2447a 198 )
036d8cbf
AS
199 RETVAL=$?
200 _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
201 return $RETVAL
d1f2447a
PB
202}
203
8af224d6
AS
204# Valgrind bug #409141 https://bugs.kde.org/show_bug.cgi?id=409141
205# Until valgrind 3.16+ is ubiquitous, we must work around a hang in
206# valgrind when issuing sigkill. Disable valgrind for this invocation.
207_NO_VALGRIND()
208{
209 NO_VALGRIND="y" "$@"
210}
211
d1f2447a
PB
212export QEMU=_qemu_wrapper
213export QEMU_IMG=_qemu_img_wrapper
214export QEMU_IO=_qemu_io_wrapper
215export QEMU_NBD=_qemu_nbd_wrapper
216export QEMU_VXHS=_qemu_vxhs_wrapper
217
076003f5
DB
218if [ "$IMGOPTSSYNTAX" = "true" ]; then
219 DRIVER="driver=$IMGFMT"
cce293a2
PB
220 QEMU_IMG_EXTRA_ARGS="--image-opts $QEMU_IMG_EXTRA_ARGS"
221 if [ -n "$IMGKEYSECRET" ]; then
222 QEMU_IMG_EXTRA_ARGS="--object secret,id=keysec0,data=$IMGKEYSECRET $QEMU_IMG_EXTRA_ARGS"
223 fi
4e9b25fb
DB
224 if [ "$IMGFMT" = "luks" ]; then
225 DRIVER="$DRIVER,key-secret=keysec0"
226 fi
076003f5
DB
227 if [ "$IMGPROTO" = "file" ]; then
228 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
229 TEST_IMG="$DRIVER,file.filename=$TEST_DIR/t.$IMGFMT"
230 elif [ "$IMGPROTO" = "nbd" ]; then
231 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
eb4ea9aa
HR
232 TEST_IMG="$DRIVER,file.driver=nbd,file.type=unix"
233 TEST_IMG="$TEST_IMG,file.path=$SOCK_DIR/nbd"
076003f5
DB
234 elif [ "$IMGPROTO" = "ssh" ]; then
235 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
236 TEST_IMG="$DRIVER,file.driver=ssh,file.host=127.0.0.1,file.path=$TEST_IMG_FILE"
237 elif [ "$IMGPROTO" = "nfs" ]; then
238 TEST_DIR="$DRIVER,file.driver=nfs,file.filename=nfs://127.0.0.1/$TEST_DIR"
e5b77eec 239 TEST_IMG=$TEST_DIR/t.$IMGFMT
076003f5
DB
240 else
241 TEST_IMG="$DRIVER,file.driver=$IMGPROTO,file.filename=$TEST_DIR/t.$IMGFMT"
242 fi
9cdfa1b3 243else
cce293a2 244 QEMU_IMG_EXTRA_ARGS=
076003f5
DB
245 if [ "$IMGPROTO" = "file" ]; then
246 TEST_IMG=$TEST_DIR/t.$IMGFMT
247 elif [ "$IMGPROTO" = "nbd" ]; then
248 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
f3923a72 249 TEST_IMG="nbd+unix:///?socket=$SOCK_DIR/nbd"
076003f5
DB
250 elif [ "$IMGPROTO" = "ssh" ]; then
251 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
b8c1f901 252 REMOTE_TEST_DIR="ssh://\\($USER@\\)\\?127.0.0.1\\(:[0-9]\\+\\)\\?$TEST_DIR"
076003f5
DB
253 TEST_IMG="ssh://127.0.0.1$TEST_IMG_FILE"
254 elif [ "$IMGPROTO" = "nfs" ]; then
655ae6bb 255 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
8908b253 256 REMOTE_TEST_DIR="nfs://127.0.0.1$TEST_DIR"
655ae6bb 257 TEST_IMG="nfs://127.0.0.1$TEST_IMG_FILE"
ae0c0a3d
AM
258 elif [ "$IMGPROTO" = "vxhs" ]; then
259 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
260 TEST_IMG="vxhs://127.0.0.1:9999/t.$IMGFMT"
076003f5
DB
261 else
262 TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT
263 fi
9cdfa1b3 264fi
59fa68f3 265ORIG_TEST_IMG="$TEST_IMG"
6bf19c94 266
cce293a2 267if [ -z "$TEST_DIR" ]; then
e8d81a61 268 TEST_DIR=$PWD/scratch
cce293a2
PB
269fi
270
271QEMU_TEST_DIR="${TEST_DIR}"
272
273if [ ! -e "$TEST_DIR" ]; then
274 mkdir "$TEST_DIR"
275fi
276
277if [ ! -d "$TEST_DIR" ]; then
49cedf17 278 echo "common.rc: Error: \$TEST_DIR ($TEST_DIR) is not a directory"
cce293a2
PB
279 exit 1
280fi
281
8908b253
KW
282if [ -z "$REMOTE_TEST_DIR" ]; then
283 REMOTE_TEST_DIR="$TEST_DIR"
284fi
285
cce293a2 286if [ ! -d "$SAMPLE_IMG_DIR" ]; then
49cedf17 287 echo "common.rc: Error: \$SAMPLE_IMG_DIR ($SAMPLE_IMG_DIR) is not a directory"
cce293a2
PB
288 exit 1
289fi
290
85edbd37
JC
291_use_sample_img()
292{
293 SAMPLE_IMG_FILE="${1%\.bz2}"
294 TEST_IMG="$TEST_DIR/$SAMPLE_IMG_FILE"
295 bzcat "$SAMPLE_IMG_DIR/$1" > "$TEST_IMG"
296 if [ $? -ne 0 ]
297 then
298 echo "_use_sample_img error, cannot extract '$SAMPLE_IMG_DIR/$1'"
299 exit 1
300 fi
301}
302
2f9d4083
FZ
303_stop_nbd_server()
304{
305 if [ -f "${QEMU_TEST_DIR}/qemu-nbd.pid" ]; then
306 local QEMU_NBD_PID
307 read QEMU_NBD_PID < "${QEMU_TEST_DIR}/qemu-nbd.pid"
308 kill ${QEMU_NBD_PID}
f3923a72 309 rm -f "${QEMU_TEST_DIR}/qemu-nbd.pid" "$SOCK_DIR/nbd"
2f9d4083
FZ
310 fi
311}
312
1b35b85a
HR
313# Gets the data_file value from IMGOPTS and replaces the '$TEST_IMG'
314# pattern by '$1'
315# Caution: The replacement is done with sed, so $1 must be escaped
316# properly. (The delimiter is '#'.)
317_get_data_file()
318{
319 if ! echo "$IMGOPTS" | grep -q 'data_file='; then
320 return 1
321 fi
322
323 echo "$IMGOPTS" | sed -e 's/.*data_file=\([^,]*\).*/\1/' \
324 | sed -e "s#\\\$TEST_IMG#$1#"
325}
326
6bf19c94
CH
327_make_test_img()
328{
329 # extra qemu-img options can be added by tests
330 # at least one argument (the image size) needs to be added
21af8148 331 local extra_img_options=""
89004368 332 local optstr=""
a9660664 333 local img_name=""
0018c03f
JC
334 local use_backing=0
335 local backing_file=""
b7e875b2 336 local object_options=""
8b6d7be6 337 local opts_param=false
eea871d0 338 local misc_params=()
a9660664
NT
339
340 if [ -n "$TEST_IMG_FILE" ]; then
341 img_name=$TEST_IMG_FILE
342 else
343 img_name=$TEST_IMG
344 fi
89004368
KW
345
346 if [ -n "$IMGOPTS" ]; then
1b35b85a
HR
347 imgopts_expanded=$(echo "$IMGOPTS" | sed -e "s#\\\$TEST_IMG#$img_name#")
348 optstr=$(_optstr_add "$optstr" "$imgopts_expanded")
89004368 349 fi
b7e875b2
DB
350 if [ -n "$IMGKEYSECRET" ]; then
351 object_options="--object secret,id=keysec0,data=$IMGKEYSECRET"
352 optstr=$(_optstr_add "$optstr" "key-secret=keysec0")
353 fi
6bf19c94 354
eea871d0
HR
355 for param; do
356 if [ "$use_backing" = "1" -a -z "$backing_file" ]; then
357 backing_file=$param
358 continue
8b6d7be6
HR
359 elif $opts_param; then
360 optstr=$(_optstr_add "$optstr" "$param")
361 opts_param=false
362 continue
eea871d0
HR
363 fi
364
365 case "$param" in
366 -b)
367 use_backing=1
368 ;;
369
8b6d7be6
HR
370 -o)
371 opts_param=true
372 ;;
373
374 --no-opts)
375 optstr=""
376 ;;
377
eea871d0
HR
378 *)
379 misc_params=("${misc_params[@]}" "$param")
380 ;;
381 esac
382 done
383
f5a4bbd9 384 if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then
89004368
KW
385 optstr=$(_optstr_add "$optstr" "cluster_size=$CLUSTER_SIZE")
386 fi
387
388 if [ -n "$optstr" ]; then
389 extra_img_options="-o $optstr $extra_img_options"
8fc1024c
KW
390 fi
391
2f9d4083
FZ
392 if [ $IMGPROTO = "nbd" ]; then
393 _stop_nbd_server
394 fi
395
6bf19c94 396 # XXX(hch): have global image options?
0018c03f
JC
397 (
398 if [ $use_backing = 1 ]; then
eea871d0 399 $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options -b "$backing_file" "$img_name" "${misc_params[@]}" 2>&1
0018c03f 400 else
eea871d0 401 $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options "$img_name" "${misc_params[@]}" 2>&1
0018c03f 402 fi
6ffb4cb6 403 ) | _filter_img_create
a9660664 404
f3923a72
EB
405 # Start an NBD server on the image file, which is what we'll be talking to.
406 # Once NBD gains resize support, we may also want to use -f raw at the
407 # server and interpret format over NBD, but for now, the format is
408 # interpreted at the server and raw data sent over NBD.
a9660664 409 if [ $IMGPROTO = "nbd" ]; then
03a0aa37
HR
410 # Pass a sufficiently high number to -e that should be enough for all
411 # tests
f3923a72 412 eval "$QEMU_NBD -v -t -k '$SOCK_DIR/nbd' -f $IMGFMT -e 42 -x '' $TEST_IMG_FILE >/dev/null &"
a9660664
NT
413 sleep 1 # FIXME: qemu-nbd needs to be listening before we continue
414 fi
ae0c0a3d
AM
415
416 # Start QNIO server on image directory for vxhs protocol
417 if [ $IMGPROTO = "vxhs" ]; then
418 eval "$QEMU_VXHS -d $TEST_DIR > /dev/null &"
419 sleep 1 # Wait for server to come up.
420 fi
6bf19c94
CH
421}
422
487c1910
FZ
423_rm_test_img()
424{
425 local img=$1
426 if [ "$IMGFMT" = "vmdk" ]; then
427 # Remove all the extents for vmdk
c5575274 428 "$QEMU_IMG" info "$img" 2>/dev/null | grep 'filename:' | cut -f 2 -d: \
487c1910 429 | xargs -I {} rm -f "{}"
1b35b85a
HR
430 elif [ "$IMGFMT" = "qcow2" ]; then
431 # Remove external data file
432 if data_file=$(_get_data_file "$img"); then
433 rm -f "$data_file"
434 fi
487c1910 435 fi
c5575274 436 rm -f "$img"
487c1910
FZ
437}
438
6bf19c94
CH
439_cleanup_test_img()
440{
9cdfa1b3
MK
441 case "$IMGPROTO" in
442
a9660664 443 nbd)
2f9d4083 444 _stop_nbd_server
fef9c191 445 rm -f "$TEST_IMG_FILE"
a9660664 446 ;;
ae0c0a3d
AM
447 vxhs)
448 if [ -f "${TEST_DIR}/qemu-vxhs.pid" ]; then
449 local QEMU_VXHS_PID
450 read QEMU_VXHS_PID < "${TEST_DIR}/qemu-vxhs.pid"
451 kill ${QEMU_VXHS_PID} >/dev/null 2>&1
452 rm -f "${TEST_DIR}/qemu-vxhs.pid"
453 fi
454 rm -f "$TEST_IMG_FILE"
455 ;;
456
9cdfa1b3 457 file)
487c1910
FZ
458 _rm_test_img "$TEST_DIR/t.$IMGFMT"
459 _rm_test_img "$TEST_DIR/t.$IMGFMT.orig"
460 _rm_test_img "$TEST_DIR/t.$IMGFMT.base"
85edbd37
JC
461 if [ -n "$SAMPLE_IMG_FILE" ]
462 then
463 rm -f "$TEST_DIR/$SAMPLE_IMG_FILE"
59fa68f3
KW
464 SAMPLE_IMG_FILE=
465 TEST_IMG="$ORIG_TEST_IMG"
85edbd37 466 fi
9cdfa1b3
MK
467 ;;
468
469 rbd)
9147d019 470 rbd --no-progress rm "$TEST_DIR/t.$IMGFMT" > /dev/null
9cdfa1b3
MK
471 ;;
472
473 sheepdog)
fef9c191 474 collie vdi delete "$TEST_DIR/t.$IMGFMT"
9cdfa1b3
MK
475 ;;
476
477 esac
6bf19c94
CH
478}
479
480_check_test_img()
481{
076003f5
DB
482 (
483 if [ "$IMGOPTSSYNTAX" = "true" ]; then
484 $QEMU_IMG check $QEMU_IMG_EXTRA_ARGS "$@" "$TEST_IMG" 2>&1
485 else
486 $QEMU_IMG check "$@" -f $IMGFMT "$TEST_IMG" 2>&1
487 fi
86ce1f6e 488 ) | _filter_testdir | _filter_qemu_img_check
ee1e66d9
VSO
489
490 # return real qemu_img check status, to analyze in
491 # _check_test_img_ignore_leaks
492 return ${PIPESTATUS[0]}
493}
494
495_check_test_img_ignore_leaks()
496{
497 out=$(_check_test_img "$@")
498 status=$?
499 if [ $status = 3 ]; then
500 # This must correspond to success output in dump_human_image_check()
501 echo "No errors were found on the image."
502 return 0
503 fi
504 echo "$out"
505 return $status
6bf19c94
CH
506}
507
514d9da5
SH
508_img_info()
509{
e800e5d4
KW
510 if [[ "$1" == "--format-specific" ]]; then
511 local format_specific=1
512 shift
513 else
514 local format_specific=0
515 fi
516
4c2e9465
HR
517 discard=0
518 regex_json_spec_start='^ *"format-specific": \{'
d06195e6 519 $QEMU_IMG info $QEMU_IMG_EXTRA_ARGS "$@" "$TEST_IMG" 2>&1 | \
8908b253
KW
520 sed -e "s#$REMOTE_TEST_DIR#TEST_DIR#g" \
521 -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
514d9da5
SH
522 -e "s#$TEST_DIR#TEST_DIR#g" \
523 -e "s#$IMGFMT#IMGFMT#g" \
524 -e "/^disk size:/ D" \
4c2e9465 525 -e "/actual-size/ D" | \
6dd6d7ab 526 while IFS='' read -r line; do
e800e5d4
KW
527 if [[ $format_specific == 1 ]]; then
528 discard=0
529 elif [[ $line == "Format specific information:" ]]; then
4c2e9465
HR
530 discard=1
531 elif [[ $line =~ $regex_json_spec_start ]]; then
532 discard=2
533 regex_json_spec_end="^${line%%[^ ]*}\\},? *$"
534 fi
535 if [[ $discard == 0 ]]; then
536 echo "$line"
537 elif [[ $discard == 1 && ! $line ]]; then
538 echo
539 discard=0
540 elif [[ $discard == 2 && $line =~ $regex_json_spec_end ]]; then
541 discard=0
542 fi
543 done
514d9da5
SH
544}
545
6bf19c94
CH
546# bail out, setting up .notrun file
547#
548_notrun()
549{
e8f8624d 550 echo "$*" >"$OUTPUT_DIR/$seq.notrun"
6bf19c94
CH
551 echo "$seq not run: $*"
552 status=0
553 exit
554}
555
5ff1c2c8
AS
556# bail out, setting up .casenotrun file
557# The function _casenotrun() is used as a notifier. It is the
558# caller's responsibility to make skipped a particular test.
559#
560_casenotrun()
561{
562 echo " [case not run] $*" >>"$OUTPUT_DIR/$seq.casenotrun"
563}
564
6bf19c94
CH
565# just plain bail out
566#
567_fail()
568{
e8f8624d 569 echo "$*" | tee -a "$OUTPUT_DIR/$seq.full"
6bf19c94
CH
570 echo "(see $seq.full for details)"
571 status=1
572 exit 1
573}
574
575# tests whether $IMGFMT is one of the supported image formats for a test
576#
577_supported_fmt()
578{
47f73da0
SH
579 # "generic" is suitable for most image formats. For some formats it doesn't
580 # work, however (most notably read-only formats), so they can opt out by
581 # setting IMGFMT_GENERIC to false.
6bf19c94 582 for f; do
89e91181 583 if [ "$f" = "$IMGFMT" -o "$f" = "generic" -a "$IMGFMT_GENERIC" = "true" ]; then
79e40ab1
KW
584 return
585 fi
6bf19c94
CH
586 done
587
588 _notrun "not suitable for this image format: $IMGFMT"
589}
590
b4a2caa4
NS
591# tests whether $IMGFMT is one of the unsupported image format for a test
592#
593_unsupported_fmt()
594{
595 for f; do
596 if [ "$f" = "$IMGFMT" ]; then
597 _notrun "not suitable for this image format: $IMGFMT"
598 fi
599 done
600}
601
9cdfa1b3
MK
602# tests whether $IMGPROTO is one of the supported image protocols for a test
603#
604_supported_proto()
605{
606 for f; do
79e40ab1
KW
607 if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then
608 return
609 fi
9cdfa1b3
MK
610 done
611
612 _notrun "not suitable for this image protocol: $IMGPROTO"
613}
614
dfac03dc
JC
615# tests whether $IMGPROTO is specified as an unsupported image protocol for a test
616#
617_unsupported_proto()
618{
619 for f; do
620 if [ "$f" = "$IMGPROTO" ]; then
621 _notrun "not suitable for this image protocol: $IMGPROTO"
622 return
623 fi
624 done
625}
626
6bf19c94
CH
627# tests whether the host OS is one of the supported OSes for a test
628#
629_supported_os()
630{
631 for h
632 do
79e40ab1
KW
633 if [ "$h" = "$HOSTOS" ]
634 then
635 return
636 fi
6bf19c94
CH
637 done
638
639 _notrun "not suitable for this OS: $HOSTOS"
640}
641
f210a83c 642_supported_cache_modes()
166f3c7b 643{
f210a83c
FZ
644 for mode; do
645 if [ "$mode" = "$CACHEMODE" ]; then
646 return
647 fi
166f3c7b 648 done
f210a83c
FZ
649 _notrun "not suitable for cache mode: $CACHEMODE"
650}
651
652_default_cache_mode()
653{
654 if $CACHEMODE_IS_DEFAULT; then
655 CACHEMODE="$1"
656 QEMU_IO="$QEMU_IO --cache $1"
657 return
658 fi
166f3c7b 659}
7156ca48
AM
660_supported_aio_modes()
661{
662 for mode; do
663 if [ "$mode" = "$AIOMODE" ]; then
664 return
665 fi
666 done
667 _notrun "not suitable for aio mode: $AIOMODE"
668}
669_default_aio_mode()
670{
671 AIOMODE="$1"
672 QEMU_IO="$QEMU_IO --aio $1"
673}
166f3c7b 674
2c77f52e
FZ
675_unsupported_imgopts()
676{
677 for bad_opt
678 do
679 if echo "$IMGOPTS" | grep -q 2>/dev/null "$bad_opt"
680 then
681 _notrun "not suitable for image option: $bad_opt"
682 fi
683 done
684}
685
6bf19c94
CH
686# this test requires that a specified command (executable) exists
687#
688_require_command()
689{
934659c4
HR
690 if [ "$1" = "QEMU" ]; then
691 c=$QEMU_PROG
692 elif [ "$1" = "QEMU_IMG" ]; then
693 c=$QEMU_IMG_PROG
694 elif [ "$1" = "QEMU_IO" ]; then
695 c=$QEMU_IO_PROG
696 elif [ "$1" = "QEMU_NBD" ]; then
697 c=$QEMU_NBD_PROG
698 else
699 eval c=\$$1
700 fi
9c468a01 701 [ -x "$c" ] || _notrun "$1 utility required, skipped this test"
6bf19c94
CH
702}
703
21b43d00
TH
704# Check that a set of drivers has been whitelisted in the QEMU binary
705#
706_require_drivers()
707{
708 available=$($QEMU -drive format=help | \
709 sed -e '/Supported formats:/!d' -e 's/Supported formats://')
710 for driver
711 do
712 if ! echo "$available" | grep -q " $driver\( \|$\)"; then
713 _notrun "$driver not available"
714 fi
715 done
716}
717
059f708d
TH
718# Check that we have a file system that allows huge (but very sparse) files
719#
720_require_large_file()
721{
722 if ! truncate --size="$1" "$TEST_IMG"; then
723 _notrun "file system on $TEST_DIR does not support large enough files"
724 fi
725 rm "$TEST_IMG"
726}
727
9bdabfbe
TH
728# Check that a set of devices is available in the QEMU binary
729#
730_require_devices()
731{
732 available=$($QEMU -M none -device help | \
733 grep ^name | sed -e 's/^name "//' -e 's/".*$//')
734 for device
735 do
736 if ! echo "$available" | grep -q "$device" ; then
737 _notrun "$device not available"
738 fi
739 done
740}
741
6bf19c94 742# make sure this script returns success
a2d9c0c4 743true