target/i386/kvm: Free xsave_buf when destroying vCPU
[qemu/rayw.git] / tests / qemu-iotests / common.rc
blob227e0a5be91743efbd2f49c837209c1bd4b36f62
1 #!/usr/bin/env bash
3 # Copyright (C) 2009 Red Hat, Inc.
4 # Copyright (c) 2000-2006 Silicon Graphics, Inc. All Rights Reserved.
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.
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.
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/>.
20 # bail out, setting up .notrun file
21 _notrun()
23 echo "$*" >"$TEST_DIR/$seq.notrun"
24 echo "$seq not run: $*"
25 status=0
26 exit
29 if ! command -v gsed >/dev/null 2>&1; then
30 if sed --version 2>&1 | grep -v 'not GNU sed' | grep 'GNU sed' > /dev/null;
31 then
32 gsed()
34 sed "$@"
36 else
37 gsed()
39 _notrun "GNU sed not available"
44 dd()
46 if [ "$HOSTOS" == "Linux" ]
47 then
48 command dd --help | grep noxfer > /dev/null 2>&1
50 if [ "$?" -eq 0 ]
51 then
52 command dd status=noxfer $@
53 else
54 command dd $@
56 else
57 command dd $@
61 # poke_file 'test.img' 512 '\xff\xfe'
62 poke_file()
64 printf "$3" | dd "of=$1" bs=1 "seek=$2" conv=notrunc &>/dev/null
67 # poke_file_le $img_filename $offset $byte_width $value
68 # Example: poke_file_le "$TEST_IMG" 512 2 65534
69 poke_file_le()
71 local img=$1 ofs=$2 len=$3 val=$4 str=''
73 while ((len--)); do
74 str+=$(printf '\\x%02x' $((val & 0xff)))
75 val=$((val >> 8))
76 done
78 poke_file "$img" "$ofs" "$str"
81 # poke_file_be $img_filename $offset $byte_width $value
82 # Example: poke_file_be "$TEST_IMG" 512 2 65279
83 poke_file_be()
85 local img=$1 ofs=$2 len=$3 val=$4
86 local str=$(printf "%0$((len * 2))x\n" $val | sed 's/\(..\)/\\x\1/g')
88 poke_file "$img" "$ofs" "$str"
91 # peek_file_le 'test.img' 512 2 => 65534
92 peek_file_le()
94 local val=0 shift=0 byte
96 # coreutils' od --endian is not portable, so manually assemble bytes.
97 for byte in $(od -j"$2" -N"$3" -An -v -tu1 "$1"); do
98 val=$(( val | (byte << shift) ))
99 shift=$((shift + 8))
100 done
101 printf %llu $val
104 # peek_file_be 'test.img' 512 2 => 65279
105 peek_file_be()
107 local val=0 byte
109 # coreutils' od --endian is not portable, so manually assemble bytes.
110 for byte in $(od -j"$2" -N"$3" -An -v -tu1 "$1"); do
111 val=$(( (val << 8) | byte ))
112 done
113 printf %llu $val
116 # peek_file_raw 'test.img' 512 2 => '\xff\xfe'. Do not use if the raw data
117 # is likely to contain \0 or trailing \n.
118 peek_file_raw()
120 dd if="$1" bs=1 skip="$2" count="$3" status=none
123 config=common.config
124 test -f $config || config=../common.config
125 if ! test -f $config
126 then
127 echo "$0: failed to find common.config"
128 exit 1
130 if ! . $config
131 then
132 echo "$0: failed to source common.config"
133 exit 1
136 # Set the variables to the empty string to turn Valgrind off
137 # for specific processes, e.g.
138 # $ VALGRIND_QEMU_IO= ./check -qcow2 -valgrind 015
140 : ${VALGRIND_QEMU_VM=$VALGRIND_QEMU}
141 : ${VALGRIND_QEMU_IMG=$VALGRIND_QEMU}
142 : ${VALGRIND_QEMU_IO=$VALGRIND_QEMU}
143 : ${VALGRIND_QEMU_NBD=$VALGRIND_QEMU}
144 : ${VALGRIND_QSD=$VALGRIND_QEMU}
146 # The Valgrind own parameters may be set with
147 # its environment variable VALGRIND_OPTS, e.g.
148 # $ VALGRIND_OPTS="--leak-check=yes" ./check -qcow2 -valgrind 015
150 _qemu_proc_exec()
152 local VALGRIND_LOGFILE="$1"
153 shift
154 if [[ "${VALGRIND_QEMU}" == "y" && "${NO_VALGRIND}" != "y" ]]; then
155 exec valgrind --log-file="${VALGRIND_LOGFILE}" --error-exitcode=99 "$@"
156 else
157 exec "$@"
161 _qemu_proc_valgrind_log()
163 local VALGRIND_LOGFILE="$1"
164 local RETVAL="$2"
165 if [[ "${VALGRIND_QEMU}" == "y" && "${NO_VALGRIND}" != "y" ]]; then
166 if [ $RETVAL == 99 ]; then
167 cat "${VALGRIND_LOGFILE}"
169 rm -f "${VALGRIND_LOGFILE}"
173 _qemu_wrapper()
175 local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
177 if [ -n "${QEMU_NEED_PID}" ]; then
178 echo $BASHPID > "${QEMU_TEST_DIR}/qemu-${_QEMU_HANDLE}.pid"
181 GDB=""
182 if [ -n "${GDB_OPTIONS}" ]; then
183 GDB="gdbserver ${GDB_OPTIONS}"
186 VALGRIND_QEMU="${VALGRIND_QEMU_VM}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
187 $GDB "$QEMU_PROG" $QEMU_OPTIONS "$@"
189 RETVAL=$?
190 _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
191 return $RETVAL
194 _qemu_img_wrapper()
196 local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
198 VALGRIND_QEMU="${VALGRIND_QEMU_IMG}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
199 "$QEMU_IMG_PROG" $QEMU_IMG_OPTIONS "$@"
201 RETVAL=$?
202 _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
203 return $RETVAL
206 _qemu_io_wrapper()
208 local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
209 local QEMU_IO_ARGS="$QEMU_IO_OPTIONS"
210 if [ "$IMGOPTSSYNTAX" = "true" ]; then
211 QEMU_IO_ARGS="--image-opts $QEMU_IO_ARGS"
212 if [ -n "$IMGKEYSECRET" ]; then
213 QEMU_IO_ARGS="--object secret,id=keysec0,data=$IMGKEYSECRET $QEMU_IO_ARGS"
217 VALGRIND_QEMU="${VALGRIND_QEMU_IO}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
218 "$QEMU_IO_PROG" $QEMU_IO_ARGS "$@"
220 RETVAL=$?
221 _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
222 return $RETVAL
225 _qemu_nbd_wrapper()
227 local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
229 VALGRIND_QEMU="${VALGRIND_QEMU_NBD}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
230 "$QEMU_NBD_PROG" --pid-file="${QEMU_TEST_DIR}/qemu-nbd.pid" \
231 $QEMU_NBD_OPTIONS "$@"
233 RETVAL=$?
234 _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
235 return $RETVAL
238 _qemu_storage_daemon_wrapper()
240 local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
242 if [ -n "${QSD_NEED_PID}" ]; then
243 echo $BASHPID > "${QEMU_TEST_DIR}/qemu-storage-daemon.pid"
245 VALGRIND_QEMU="${VALGRIND_QSD}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
246 "$QSD_PROG" $QSD_OPTIONS "$@"
248 RETVAL=$?
249 _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
250 return $RETVAL
253 # Valgrind bug #409141 https://bugs.kde.org/show_bug.cgi?id=409141
254 # Until valgrind 3.16+ is ubiquitous, we must work around a hang in
255 # valgrind when issuing sigkill. Disable valgrind for this invocation.
256 _NO_VALGRIND()
258 NO_VALGRIND="y" "$@"
261 export QEMU=_qemu_wrapper
262 export QEMU_IMG=_qemu_img_wrapper
263 export QEMU_IO=_qemu_io_wrapper
264 export QEMU_NBD=_qemu_nbd_wrapper
265 export QSD=_qemu_storage_daemon_wrapper
267 if [ "$IMGOPTSSYNTAX" = "true" ]; then
268 DRIVER="driver=$IMGFMT"
269 QEMU_IMG_EXTRA_ARGS="--image-opts $QEMU_IMG_EXTRA_ARGS"
270 if [ -n "$IMGKEYSECRET" ]; then
271 QEMU_IMG_EXTRA_ARGS="--object secret,id=keysec0,data=$IMGKEYSECRET $QEMU_IMG_EXTRA_ARGS"
273 if [ "$IMGFMT" = "luks" ]; then
274 DRIVER="$DRIVER,key-secret=keysec0"
276 if [ "$IMGPROTO" = "file" ]; then
277 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
278 TEST_IMG="$DRIVER,file.filename=$TEST_DIR/t.$IMGFMT"
279 elif [ "$IMGPROTO" = "nbd" ]; then
280 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
281 TEST_IMG="$DRIVER,file.driver=nbd,file.type=unix"
282 TEST_IMG="$TEST_IMG,file.path=$SOCK_DIR/nbd"
283 elif [ "$IMGPROTO" = "fuse" ]; then
284 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
285 TEST_IMG="$DRIVER,file.filename=$SOCK_DIR/fuse-t.$IMGFMT"
286 elif [ "$IMGPROTO" = "ssh" ]; then
287 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
288 TEST_IMG="$DRIVER,file.driver=ssh,file.host=127.0.0.1,file.path=$TEST_IMG_FILE"
289 elif [ "$IMGPROTO" = "nfs" ]; then
290 TEST_DIR="$DRIVER,file.driver=nfs,file.filename=nfs://127.0.0.1/$TEST_DIR"
291 TEST_IMG=$TEST_DIR/t.$IMGFMT
292 else
293 TEST_IMG="$DRIVER,file.driver=$IMGPROTO,file.filename=$TEST_DIR/t.$IMGFMT"
295 else
296 QEMU_IMG_EXTRA_ARGS=
297 if [ "$IMGPROTO" = "file" ]; then
298 TEST_IMG=$TEST_DIR/t.$IMGFMT
299 elif [ "$IMGPROTO" = "nbd" ]; then
300 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
301 TEST_IMG="nbd+unix:///?socket=$SOCK_DIR/nbd"
302 elif [ "$IMGPROTO" = "fuse" ]; then
303 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
304 TEST_IMG="$SOCK_DIR/fuse-t.$IMGFMT"
305 elif [ "$IMGPROTO" = "ssh" ]; then
306 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
307 REMOTE_TEST_DIR="ssh://\\($USER@\\)\\?127.0.0.1\\(:[0-9]\\+\\)\\?$TEST_DIR"
308 TEST_IMG="ssh://127.0.0.1$TEST_IMG_FILE"
309 elif [ "$IMGPROTO" = "nfs" ]; then
310 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
311 REMOTE_TEST_DIR="nfs://127.0.0.1$TEST_DIR"
312 TEST_IMG="nfs://127.0.0.1$TEST_IMG_FILE"
313 else
314 TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT
317 ORIG_TEST_IMG_FILE=$TEST_IMG_FILE
318 ORIG_TEST_IMG="$TEST_IMG"
320 FUSE_PIDS=()
321 FUSE_EXPORTS=()
323 if [ -z "$TEST_DIR" ]; then
324 TEST_DIR=$PWD/scratch
327 QEMU_TEST_DIR="${TEST_DIR}"
329 if [ ! -e "$TEST_DIR" ]; then
330 mkdir "$TEST_DIR"
333 if [ ! -d "$TEST_DIR" ]; then
334 echo "common.rc: Error: \$TEST_DIR ($TEST_DIR) is not a directory"
335 exit 1
338 if [ -z "$REMOTE_TEST_DIR" ]; then
339 REMOTE_TEST_DIR="$TEST_DIR"
342 if [ ! -d "$SAMPLE_IMG_DIR" ]; then
343 echo "common.rc: Error: \$SAMPLE_IMG_DIR ($SAMPLE_IMG_DIR) is not a directory"
344 exit 1
347 _use_sample_img()
349 SAMPLE_IMG_FILE="${1%\.bz2}"
350 TEST_IMG="$TEST_DIR/$SAMPLE_IMG_FILE"
351 bzcat "$SAMPLE_IMG_DIR/$1" > "$TEST_IMG"
352 if [ $? -ne 0 ]
353 then
354 echo "_use_sample_img error, cannot extract '$SAMPLE_IMG_DIR/$1'"
355 exit 1
359 _stop_nbd_server()
361 if [ -f "${QEMU_TEST_DIR}/qemu-nbd.pid" ]; then
362 local QEMU_NBD_PID
363 read QEMU_NBD_PID < "${QEMU_TEST_DIR}/qemu-nbd.pid"
364 kill ${QEMU_NBD_PID}
365 rm -f "${QEMU_TEST_DIR}/qemu-nbd.pid" "$SOCK_DIR/nbd"
369 # Gets the data_file value from IMGOPTS and replaces the '$TEST_IMG'
370 # pattern by '$1'
371 # Caution: The replacement is done with sed, so $1 must be escaped
372 # properly. (The delimiter is '#'.)
373 _get_data_file()
375 if ! echo "$IMGOPTS" | grep -q 'data_file='; then
376 return 1
379 echo "$IMGOPTS" | sed -e 's/.*data_file=\([^,]*\).*/\1/' \
380 | sed -e "s#\\\$TEST_IMG#$1#"
383 # Translate a $TEST_IMG to its corresponding $TEST_IMG_FILE for
384 # different protocols
385 _test_img_to_test_img_file()
387 case "$IMGPROTO" in
388 file)
389 echo "$1"
392 fuse)
393 echo "$1" | sed -e "s#$SOCK_DIR/fuse-#$TEST_DIR/#"
396 nfs)
397 echo "$1" | sed -e "s#nfs://127.0.0.1##"
400 ssh)
401 echo "$1" | \
402 sed -e "s#ssh://\\($USER@\\)\\?127.0.0.1\\(:[0-9]\\+\\)\\?##"
406 return 1
408 esac
411 _make_test_img()
413 # extra qemu-img options can be added by tests
414 # at least one argument (the image size) needs to be added
415 local extra_img_options=""
416 local optstr=""
417 local img_name=""
418 local use_backing=0
419 local backing_file=""
420 local object_options=""
421 local opts_param=false
422 local misc_params=()
424 if [[ $IMGPROTO == fuse && $TEST_IMG == $SOCK_DIR/fuse-* ]]; then
425 # The caller may be trying to overwrite an existing image
426 _rm_test_img "$TEST_IMG"
429 if [ -z "$TEST_IMG_FILE" ]; then
430 img_name=$TEST_IMG
431 elif [ "$IMGOPTSSYNTAX" != "true" -a \
432 "$TEST_IMG_FILE" = "$ORIG_TEST_IMG_FILE" ]; then
433 # Handle cases of tests only updating TEST_IMG, but not TEST_IMG_FILE
434 img_name=$(_test_img_to_test_img_file "$TEST_IMG")
435 if [ "$?" != 0 ]; then
436 img_name=$TEST_IMG_FILE
438 else
439 # $TEST_IMG_FILE is not the default value, so it definitely has been
440 # modified by the test
441 img_name=$TEST_IMG_FILE
444 if [ -n "$IMGOPTS" ]; then
445 imgopts_expanded=$(echo "$IMGOPTS" | sed -e "s#\\\$TEST_IMG#$img_name#")
446 optstr=$(_optstr_add "$optstr" "$imgopts_expanded")
448 if [ -n "$IMGKEYSECRET" ]; then
449 object_options="--object secret,id=keysec0,data=$IMGKEYSECRET"
450 optstr=$(_optstr_add "$optstr" "key-secret=keysec0")
453 for param; do
454 if [ "$use_backing" = "1" -a -z "$backing_file" ]; then
455 backing_file=$param
456 continue
457 elif $opts_param; then
458 optstr=$(_optstr_add "$optstr" "$param")
459 opts_param=false
460 continue
463 case "$param" in
465 use_backing=1
469 opts_param=true
472 --no-opts)
473 optstr=""
477 misc_params=("${misc_params[@]}" "$param")
479 esac
480 done
482 if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then
483 optstr=$(_optstr_add "$optstr" "cluster_size=$CLUSTER_SIZE")
486 if [ -n "$optstr" ]; then
487 extra_img_options="-o $optstr $extra_img_options"
490 if [ $IMGPROTO = "nbd" ]; then
491 _stop_nbd_server
494 # XXX(hch): have global image options?
496 if [ $use_backing = 1 ]; then
497 $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options -b "$backing_file" "$img_name" "${misc_params[@]}" 2>&1
498 else
499 $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options "$img_name" "${misc_params[@]}" 2>&1
501 ) | _filter_img_create
503 # Start an NBD server on the image file, which is what we'll be talking to.
504 # Once NBD gains resize support, we may also want to use -f raw at the
505 # server and interpret format over NBD, but for now, the format is
506 # interpreted at the server and raw data sent over NBD.
507 if [ $IMGPROTO = "nbd" ]; then
508 # Pass a sufficiently high number to -e that should be enough for all
509 # tests
510 eval "$QEMU_NBD -v -t -k '$SOCK_DIR/nbd' -f $IMGFMT -e 42 -x '' $TEST_IMG_FILE >/dev/null &"
511 sleep 1 # FIXME: qemu-nbd needs to be listening before we continue
514 if [ $IMGPROTO = "fuse" -a -f "$img_name" ]; then
515 local export_mp
516 local pid
517 local pidfile
518 local timeout
520 export_mp=$(echo "$img_name" | sed -e "s#$TEST_DIR/#$SOCK_DIR/fuse-#")
521 if ! echo "$export_mp" | grep -q "^$SOCK_DIR"; then
522 echo 'Cannot use FUSE exports with images outside of TEST_DIR' >&2
523 return 1
526 touch "$export_mp"
527 rm -f "$SOCK_DIR/fuse-output"
529 # Usually, users would export formatted nodes. But we present fuse as a
530 # protocol-level driver here, so we have to leave the format to the
531 # client.
532 # Switch off allow-other, because in general we do not need it for
533 # iotests. The default allow-other=auto has the downside of printing a
534 # fusermount error on its first attempt if allow_other is not
535 # permissible, which we would need to filter.
536 QSD_NEED_PID=y $QSD \
537 --blockdev file,node-name=export-node,filename=$img_name,discard=unmap \
538 --export fuse,id=fuse-export,node-name=export-node,mountpoint="$export_mp",writable=on,growable=on,allow-other=off \
541 pidfile="$QEMU_TEST_DIR/qemu-storage-daemon.pid"
543 # Wait for the PID file
544 while [ ! -f "$pidfile" ]; do
545 sleep 0.5
546 done
548 pid=$(cat "$pidfile")
549 rm -f "$pidfile"
551 FUSE_PIDS+=($pid)
552 FUSE_EXPORTS+=("$export_mp")
556 _rm_test_img()
558 local img=$1
560 if [[ $IMGPROTO == fuse && $img == $SOCK_DIR/fuse-* ]]; then
561 # Drop a FUSE export
562 local df_output
563 local i
564 local image_file
565 local index=''
566 local timeout
568 for i in "${!FUSE_EXPORTS[@]}"; do
569 if [ "${FUSE_EXPORTS[i]}" = "$img" ]; then
570 index=$i
571 break
573 done
575 if [ -z "$index" ]; then
576 # Probably gone already
577 return 0
580 kill "${FUSE_PIDS[index]}"
582 # Wait until the mount is gone
583 timeout=10 # *0.5 s
584 while true; do
585 # Will show the mount point; if the mount is still there,
586 # it will be $img.
587 df_output=$(df "$img" 2>/dev/null)
589 # But df may also show an error ("Transpoint endpoint not
590 # connected"), so retry in such cases
591 if [ -n "$df_output" ]; then
592 if ! echo "$df_output" | grep -q "$img"; then
593 break
597 sleep 0.5
599 timeout=$((timeout - 1))
600 if [ "$timeout" = 0 ]; then
601 echo 'Failed to take down FUSE export' >&2
602 return 1
604 done
606 rm -f "$img"
608 unset "FUSE_PIDS[$index]"
609 unset "FUSE_EXPORTS[$index]"
611 image_file=$(echo "$img" | sed -e "s#$SOCK_DIR/fuse-#$TEST_DIR/#")
612 _rm_test_img "$image_file"
613 return
616 if [ "$IMGFMT" = "vmdk" ]; then
617 # Remove all the extents for vmdk
618 "$QEMU_IMG" info "$img" 2>/dev/null | grep 'filename:' | cut -f 2 -d: \
619 | xargs -I {} rm -f "{}"
620 elif [ "$IMGFMT" = "qcow2" ]; then
621 # Remove external data file
622 if data_file=$(_get_data_file "$img"); then
623 rm -f "$data_file"
626 rm -f "$img"
629 _cleanup_test_img()
631 case "$IMGPROTO" in
633 nbd)
634 _stop_nbd_server
635 rm -f "$TEST_IMG_FILE"
638 fuse)
639 local mp
641 for mp in "${FUSE_EXPORTS[@]}"; do
642 _rm_test_img "$mp"
643 done
645 FUSE_PIDS=()
646 FUSE_EXPORTS=()
649 file)
650 _rm_test_img "$TEST_DIR/t.$IMGFMT"
651 _rm_test_img "$TEST_DIR/t.$IMGFMT.orig"
652 _rm_test_img "$TEST_DIR/t.$IMGFMT.base"
653 if [ -n "$SAMPLE_IMG_FILE" ]
654 then
655 rm -f "$TEST_DIR/$SAMPLE_IMG_FILE"
656 SAMPLE_IMG_FILE=
657 TEST_IMG="$ORIG_TEST_IMG"
661 rbd)
662 rbd --no-progress rm "$TEST_DIR/t.$IMGFMT" > /dev/null
665 esac
668 _check_test_img()
671 if [ "$IMGOPTSSYNTAX" = "true" ]; then
672 $QEMU_IMG check $QEMU_IMG_EXTRA_ARGS "$@" "$TEST_IMG" 2>&1
673 else
674 $QEMU_IMG check "$@" -f $IMGFMT "$TEST_IMG" 2>&1
676 ) | _filter_testdir | _filter_qemu_img_check
678 # return real qemu_img check status, to analyze in
679 # _check_test_img_ignore_leaks
680 return ${PIPESTATUS[0]}
683 _check_test_img_ignore_leaks()
685 out=$(_check_test_img "$@")
686 status=$?
687 if [ $status = 3 ]; then
688 # This must correspond to success output in dump_human_image_check()
689 echo "No errors were found on the image."
690 return 0
692 echo "$out"
693 return $status
696 _img_info()
698 if [[ "$1" == "--format-specific" ]]; then
699 local format_specific=1
700 shift
701 else
702 local format_specific=0
705 discard=0
706 regex_json_spec_start='^ *"format-specific": \{'
707 $QEMU_IMG info $QEMU_IMG_EXTRA_ARGS "$@" "$TEST_IMG" 2>&1 | \
708 sed -e "s#$REMOTE_TEST_DIR#TEST_DIR#g" \
709 -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
710 -e "s#$TEST_DIR#TEST_DIR#g" \
711 -e "s#$SOCK_DIR/fuse-#TEST_DIR/#g" \
712 -e "s#$IMGFMT#IMGFMT#g" \
713 -e 's/\(compression type: \)\(zlib\|zstd\)/\1COMPRESSION_TYPE/' \
714 -e "/^disk size:/ D" \
715 -e "/actual-size/ D" | \
716 while IFS='' read -r line; do
717 if [[ $format_specific == 1 ]]; then
718 discard=0
719 elif [[ $line == "Format specific information:" ]]; then
720 discard=1
721 elif [[ $line =~ $regex_json_spec_start ]]; then
722 discard=2
723 regex_json_spec_end="^${line%%[^ ]*}\\},? *$"
725 if [[ $discard == 0 ]]; then
726 echo "$line"
727 elif [[ $discard == 1 && ! $line ]]; then
728 echo
729 discard=0
730 elif [[ $discard == 2 && $line =~ $regex_json_spec_end ]]; then
731 discard=0
733 done
736 # bail out, setting up .casenotrun file
737 # The function _casenotrun() is used as a notifier. It is the
738 # caller's responsibility to make skipped a particular test.
740 _casenotrun()
742 echo " [case not run] $*" >>"$TEST_DIR/$seq.casenotrun"
745 # just plain bail out
747 _fail()
749 echo "$*" | tee -a "$TEST_DIR/$seq.full"
750 echo "(see $seq.full for details)"
751 status=1
752 exit 1
755 # tests whether $IMGFMT is one of the supported image formats for a test
757 _supported_fmt()
759 # "generic" is suitable for most image formats. For some formats it doesn't
760 # work, however (most notably read-only formats), so they can opt out by
761 # setting IMGFMT_GENERIC to false.
762 for f; do
763 if [ "$f" = "$IMGFMT" -o "$f" = "generic" -a "$IMGFMT_GENERIC" = "true" ]; then
764 if [ "$IMGFMT" = "luks" ]; then
765 _require_working_luks
767 return
769 done
771 _notrun "not suitable for this image format: $IMGFMT"
774 # tests whether $IMGFMT is one of the unsupported image format for a test
776 _unsupported_fmt()
778 for f; do
779 if [ "$f" = "$IMGFMT" ]; then
780 _notrun "not suitable for this image format: $IMGFMT"
782 done
785 # tests whether $IMGPROTO is one of the supported image protocols for a test
787 _supported_proto()
789 for f; do
790 if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then
791 return
793 done
795 _notrun "not suitable for this image protocol: $IMGPROTO"
798 # tests whether $IMGPROTO is specified as an unsupported image protocol for a test
800 _unsupported_proto()
802 for f; do
803 if [ "$f" = "$IMGPROTO" ]; then
804 _notrun "not suitable for this image protocol: $IMGPROTO"
805 return
807 done
810 # tests whether the host OS is one of the supported OSes for a test
812 _supported_os()
814 for h
816 if [ "$h" = "$HOSTOS" ]
817 then
818 return
820 done
822 _notrun "not suitable for this OS: $HOSTOS"
825 _supported_cache_modes()
827 for mode; do
828 if [ "$mode" = "$CACHEMODE" ]; then
829 return
831 done
832 _notrun "not suitable for cache mode: $CACHEMODE"
835 # Check whether the filesystem supports O_DIRECT
836 _check_o_direct()
838 testfile="$TEST_DIR"/_check_o_direct
839 $QEMU_IMG create -f raw "$testfile" 1M > /dev/null
840 out=$($QEMU_IO -f raw -t none -c quit "$testfile" 2>&1)
841 rm -f "$testfile"
843 [[ "$out" != *"O_DIRECT"* ]]
846 _require_o_direct()
848 if ! _check_o_direct; then
849 _notrun "file system on $TEST_DIR does not support O_DIRECT"
853 _check_cache_mode()
855 if [ $CACHEMODE == "none" ] || [ $CACHEMODE == "directsync" ]; then
856 _require_o_direct
860 _check_cache_mode
862 # $1 - cache mode to use by default
863 # $2 - (optional) cache mode to use by default if O_DIRECT is not supported
864 _default_cache_mode()
866 if $CACHEMODE_IS_DEFAULT; then
867 if [ -z "$2" ] || _check_o_direct; then
868 CACHEMODE="$1"
869 else
870 CACHEMODE="$2"
872 QEMU_IO="$QEMU_IO --cache $CACHEMODE"
873 _check_cache_mode
874 return
877 _supported_aio_modes()
879 for mode; do
880 if [ "$mode" = "$AIOMODE" ]; then
881 return
883 done
884 _notrun "not suitable for aio mode: $AIOMODE"
886 _default_aio_mode()
888 AIOMODE="$1"
889 QEMU_IO="$QEMU_IO --aio $1"
892 _unsupported_imgopts()
894 for bad_opt
896 # Add a space so tests can match for whitespace that marks the
897 # end of an option (\b or \> are not portable)
898 if echo "$IMGOPTS " | grep -q 2>/dev/null "$bad_opt"
899 then
900 _notrun "not suitable for image option: $bad_opt"
902 done
905 # Caution: Overwrites $TEST_DIR/t.luks
906 _require_working_luks()
908 file="$TEST_DIR/t.luks"
910 output=$(
911 $QEMU_IMG create -f luks \
912 --object secret,id=sec0,data=hunter0 \
913 -o key-secret=sec0 \
914 -o iter-time=10 \
915 "$file" \
916 1M \
917 2>&1
919 status=$?
921 IMGFMT='luks' _rm_test_img "$file"
923 if [ $status != 0 ]; then
924 reason=$(echo "$output" | grep "$file:" | sed -e "s#.*$file: *##")
925 if [ -z "$reason" ]; then
926 reason="Failed to create a LUKS image"
928 _notrun "$reason"
932 # this test requires that a specified command (executable) exists
934 _require_command()
936 if [ "$1" = "QEMU" ]; then
937 c=$QEMU_PROG
938 elif [ "$1" = "QEMU_IMG" ]; then
939 c=$QEMU_IMG_PROG
940 elif [ "$1" = "QEMU_IO" ]; then
941 c=$QEMU_IO_PROG
942 elif [ "$1" = "QEMU_NBD" ]; then
943 c=$QEMU_NBD_PROG
944 else
945 eval c=\$$1
947 [ -x "$c" ] || _notrun "$1 utility required, skipped this test"
950 # Check that a set of drivers has been whitelisted in the QEMU binary
952 _require_drivers()
954 available=$($QEMU -drive format=help | \
955 sed -e '/Supported formats:/!d' -e 's/Supported formats://')
956 for driver
958 if ! echo "$available" | grep -q " $driver\( \|$\)"; then
959 _notrun "$driver not available"
961 done
964 # Check that we have a file system that allows huge (but very sparse) files
966 _require_large_file()
968 if ! truncate --size="$1" "$TEST_IMG"; then
969 _notrun "file system on $TEST_DIR does not support large enough files"
971 rm "$TEST_IMG"
974 # Check that a set of devices is available in the QEMU binary
976 _require_devices()
978 available=$($QEMU -M none -device help | \
979 grep ^name | sed -e 's/^name "//' -e 's/".*$//')
980 for device
982 if ! echo "$available" | grep -q "$device" ; then
983 _notrun "$device not available"
985 done
988 _require_one_device_of()
990 available=$($QEMU -M none -device help | \
991 grep ^name | sed -e 's/^name "//' -e 's/".*$//')
992 for device
994 if echo "$available" | grep -q "$device" ; then
995 return
997 done
998 _notrun "$* not available"
1001 _qcow2_dump_header()
1003 if [[ "$1" == "--no-filter-compression" ]]; then
1004 local filter_compression=0
1005 shift
1006 else
1007 local filter_compression=1
1010 img="$1"
1011 if [ -z "$img" ]; then
1012 img="$TEST_IMG"
1015 if [[ $filter_compression == 0 ]]; then
1016 $PYTHON qcow2.py "$img" dump-header
1017 else
1018 $PYTHON qcow2.py "$img" dump-header | _filter_qcow2_compression_type_bit
1022 # make sure this script returns success
1023 true