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