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/>.
21 for sed in sed gsed
; do
22 ($sed --version |
grep 'GNU sed') > /dev
/null
2>&1
23 if [ "$?" -eq 0 ]; then
28 if [ -z "$SED" ]; then
29 echo "$0: GNU sed not found"
35 if [ "$HOSTOS" == "Linux" ]
37 command dd --help |
grep noxfer
> /dev
/null
2>&1
41 command dd status
=noxfer $@
50 # poke_file 'test.img' 512 '\xff\xfe'
53 printf "$3" |
dd "of=$1" bs
=1 "seek=$2" conv
=notrunc
&>/dev
/null
56 # poke_file_le $img_filename $offset $byte_width $value
57 # Example: poke_file_le "$TEST_IMG" 512 2 65534
60 local img
=$1 ofs
=$2 len
=$3 val
=$4 str
=''
63 str
+=$
(printf '\\x%02x' $
((val
& 0xff)))
67 poke_file
"$img" "$ofs" "$str"
70 # poke_file_be $img_filename $offset $byte_width $value
71 # Example: poke_file_be "$TEST_IMG" 512 2 65279
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')
77 poke_file
"$img" "$ofs" "$str"
80 # peek_file_le 'test.img' 512 2 => 65534
83 local val
=0 shift=0 byte
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) ))
93 # peek_file_be 'test.img' 512 2 => 65279
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 ))
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.
109 dd if="$1" bs=1 skip="$2" count="$3" status=none
113 if ! . ./common.config
115 echo "$0: failed to source common.config"
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
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}
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
135 local VALGRIND_LOGFILE="$1"
137 if [[ "${VALGRIND_QEMU}" == "y" && "${NO_VALGRIND}" != "y" ]]; then
138 exec valgrind --log-file="${VALGRIND_LOGFILE}" --error-exitcode=99 "$@"
144 _qemu_proc_valgrind_log()
146 local VALGRIND_LOGFILE="$1"
148 if [[ "${VALGRIND_QEMU}" == "y" && "${NO_VALGRIND}" != "y" ]]; then
149 if [ $RETVAL == 99 ]; then
150 cat "${VALGRIND_LOGFILE}"
152 rm -f "${VALGRIND_LOGFILE}"
158 local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
160 if [ -n "${QEMU_NEED_PID}" ]; then
161 echo $BASHPID > "${QEMU_TEST_DIR}/qemu-${_QEMU_HANDLE}.pid"
163 VALGRIND_QEMU="${VALGRIND_QEMU_VM}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
164 "$QEMU_PROG" $QEMU_OPTIONS "$@"
167 _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
173 local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
175 VALGRIND_QEMU="${VALGRIND_QEMU_IMG}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
176 "$QEMU_IMG_PROG" $QEMU_IMG_OPTIONS "$@"
179 _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
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"
194 VALGRIND_QEMU="${VALGRIND_QEMU_IO}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
195 "$QEMU_IO_PROG" $QEMU_IO_ARGS "$@"
198 _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
204 local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
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 "$@"
211 _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
217 local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
219 echo $BASHPID > "${TEST_DIR}/qemu-vxhs.pid"
220 VALGRIND_QEMU="${VALGRIND_QEMU_VXHS}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
221 "$QEMU_VXHS_PROG" $QEMU_VXHS_OPTIONS "$@"
224 _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
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.
236 export QEMU=_qemu_wrapper
237 export QEMU_IMG=_qemu_img_wrapper
238 export QEMU_IO=_qemu_io_wrapper
239 export QEMU_NBD=_qemu_nbd_wrapper
240 export QEMU_VXHS=_qemu_vxhs_wrapper
242 if [ "$IMGOPTSSYNTAX" = "true" ]; then
243 DRIVER="driver=$IMGFMT"
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"
248 if [ "$IMGFMT" = "luks" ]; then
249 DRIVER="$DRIVER,key-secret=keysec0"
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
256 TEST_IMG="$DRIVER,file.driver=nbd,file.type=unix"
257 TEST_IMG="$TEST_IMG,file.path=$SOCK_DIR/nbd"
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"
263 TEST_IMG=$TEST_DIR/t.$IMGFMT
265 TEST_IMG="$DRIVER,file.driver=$IMGPROTO,file.filename=$TEST_DIR/t.$IMGFMT"
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
273 TEST_IMG="nbd+unix:///?socket=$SOCK_DIR/nbd"
274 elif [ "$IMGPROTO" = "ssh" ]; then
275 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
276 REMOTE_TEST_DIR="ssh://\\($USER@\\)\\?127.0.0.1\\(:[0-9]\\+\\)\\?$TEST_DIR"
277 TEST_IMG="ssh://127.0.0.1$TEST_IMG_FILE"
278 elif [ "$IMGPROTO" = "nfs" ]; then
279 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
280 REMOTE_TEST_DIR="nfs://127.0.0.1$TEST_DIR"
281 TEST_IMG="nfs://127.0.0.1$TEST_IMG_FILE"
282 elif [ "$IMGPROTO" = "vxhs" ]; then
283 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
284 TEST_IMG="vxhs://127.0.0.1:9999/t.$IMGFMT"
286 TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT
289 ORIG_TEST_IMG="$TEST_IMG"
291 if [ -z "$TEST_DIR" ]; then
292 TEST_DIR=$PWD/scratch
295 QEMU_TEST_DIR="${TEST_DIR}"
297 if [ ! -e "$TEST_DIR" ]; then
301 if [ ! -d "$TEST_DIR" ]; then
302 echo "common.rc: Error: \$TEST_DIR ($TEST_DIR) is not a directory"
306 if [ -z "$REMOTE_TEST_DIR" ]; then
307 REMOTE_TEST_DIR="$TEST_DIR"
310 if [ ! -d "$SAMPLE_IMG_DIR" ]; then
311 echo "common.rc: Error: \$SAMPLE_IMG_DIR ($SAMPLE_IMG_DIR) is not a directory"
317 SAMPLE_IMG_FILE="${1%\.bz2}"
318 TEST_IMG="$TEST_DIR/$SAMPLE_IMG_FILE"
319 bzcat "$SAMPLE_IMG_DIR/$1" > "$TEST_IMG"
322 echo "_use_sample_img error, cannot extract '$SAMPLE_IMG_DIR/$1'"
329 if [ -f "${QEMU_TEST_DIR}/qemu-nbd.pid" ]; then
331 read QEMU_NBD_PID < "${QEMU_TEST_DIR}/qemu-nbd.pid"
333 rm -f "${QEMU_TEST_DIR}/qemu-nbd.pid" "$SOCK_DIR/nbd"
337 # Gets the data_file value from IMGOPTS and replaces the '$TEST_IMG'
339 # Caution: The replacement is done with sed, so $1 must be escaped
340 # properly. (The delimiter is '#'.)
343 if ! echo "$IMGOPTS" | grep -q 'data_file='; then
347 echo "$IMGOPTS" | sed -e 's/.*data_file=\([^,]*\).*/\1/' \
348 | sed -e "s#\\\$TEST_IMG#$1#"
353 # extra qemu-img options can be added by tests
354 # at least one argument (the image size) needs to be added
355 local extra_img_options=""
359 local backing_file=""
360 local object_options=""
361 local opts_param=false
364 if [ -n "$TEST_IMG_FILE" ]; then
365 img_name=$TEST_IMG_FILE
370 if [ -n "$IMGOPTS" ]; then
371 imgopts_expanded=$(echo "$IMGOPTS" | sed -e "s#\\\$TEST_IMG#$img_name#")
372 optstr=$(_optstr_add "$optstr" "$imgopts_expanded")
374 if [ -n "$IMGKEYSECRET" ]; then
375 object_options="--object secret,id=keysec0,data=$IMGKEYSECRET"
376 optstr=$(_optstr_add "$optstr" "key-secret=keysec0")
380 if [ "$use_backing" = "1" -a -z "$backing_file" ]; then
383 elif $opts_param; then
384 optstr=$(_optstr_add "$optstr" "$param")
403 misc_params=("${misc_params[@]}" "$param")
408 if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then
409 optstr=$(_optstr_add "$optstr" "cluster_size=$CLUSTER_SIZE")
412 if [ -n "$optstr" ]; then
413 extra_img_options="-o $optstr $extra_img_options"
416 if [ $IMGPROTO = "nbd" ]; then
420 # XXX(hch): have global image options?
422 if [ $use_backing = 1 ]; then
423 $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options -b "$backing_file" "$img_name" "${misc_params[@]}" 2>&1
425 $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options "$img_name" "${misc_params[@]}" 2>&1
427 ) | _filter_img_create
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.
433 if [ $IMGPROTO = "nbd" ]; then
434 # Pass a sufficiently high number to -e that should be enough for all
436 eval "$QEMU_NBD -v -t -k '$SOCK_DIR/nbd' -f $IMGFMT -e 42 -x '' $TEST_IMG_FILE >/dev/null &"
437 sleep 1 # FIXME: qemu-nbd needs to be listening before we continue
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.
450 if [ "$IMGFMT" = "vmdk" ]; then
451 # Remove all the extents for vmdk
452 "$QEMU_IMG" info "$img" 2>/dev/null | grep 'filename:' | cut -f 2 -d: \
453 | xargs -I {} rm -f "{}"
454 elif [ "$IMGFMT" = "qcow2" ]; then
455 # Remove external data file
456 if data_file=$(_get_data_file "$img"); then
469 rm -f "$TEST_IMG_FILE"
472 if [ -f "${TEST_DIR}/qemu-vxhs.pid" ]; then
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"
478 rm -f "$TEST_IMG_FILE"
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"
485 if [ -n "$SAMPLE_IMG_FILE" ]
487 rm -f "$TEST_DIR/$SAMPLE_IMG_FILE"
489 TEST_IMG="$ORIG_TEST_IMG"
494 rbd --no-progress rm "$TEST_DIR/t.$IMGFMT" > /dev/null
498 collie vdi delete "$TEST_DIR/t.$IMGFMT"
507 if [ "$IMGOPTSSYNTAX" = "true" ]; then
508 $QEMU_IMG check $QEMU_IMG_EXTRA_ARGS "$@" "$TEST_IMG" 2>&1
510 $QEMU_IMG check "$@" -f $IMGFMT "$TEST_IMG" 2>&1
512 ) | _filter_testdir | _filter_qemu_img_check
514 # return real qemu_img check status, to analyze in
515 # _check_test_img_ignore_leaks
516 return ${PIPESTATUS[0]}
519 _check_test_img_ignore_leaks()
521 out=$(_check_test_img "$@")
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."
534 if [[ "$1" == "--format-specific" ]]; then
535 local format_specific=1
538 local format_specific=0
542 regex_json_spec_start='^ *"format-specific": \{'
543 $QEMU_IMG info $QEMU_IMG_EXTRA_ARGS "$@" "$TEST_IMG" 2>&1 | \
544 sed -e "s#$REMOTE_TEST_DIR#TEST_DIR#g" \
545 -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
546 -e "s#$TEST_DIR#TEST_DIR#g" \
547 -e "s#$IMGFMT#IMGFMT#g" \
548 -e "/^disk size:/ D" \
549 -e "/actual-size/ D" | \
550 while IFS='' read -r line; do
551 if [[ $format_specific == 1 ]]; then
553 elif [[ $line == "Format specific information:" ]]; then
555 elif [[ $line =~ $regex_json_spec_start ]]; then
557 regex_json_spec_end="^${line%%[^ ]*}\\},? *$"
559 if [[ $discard == 0 ]]; then
561 elif [[ $discard == 1 && ! $line ]]; then
564 elif [[ $discard == 2 && $line =~ $regex_json_spec_end ]]; then
570 # bail out, setting up .notrun file
574 echo "$*" >"$OUTPUT_DIR/$seq.notrun"
575 echo "$seq not run: $*"
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.
586 echo " [case not run] $*" >>"$OUTPUT_DIR/$seq.casenotrun"
589 # just plain bail out
593 echo "$*" | tee -a "$OUTPUT_DIR/$seq.full"
594 echo "(see $seq.full for details)"
599 # tests whether $IMGFMT is one of the supported image formats for a test
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.
607 if [ "$f" = "$IMGFMT" -o "$f" = "generic" -a "$IMGFMT_GENERIC" = "true" ]; then
608 if [ "$IMGFMT" = "luks" ]; then
609 _require_working_luks
615 _notrun "not suitable for this image format: $IMGFMT"
618 # tests whether $IMGFMT is one of the unsupported image format for a test
623 if [ "$f" = "$IMGFMT" ]; then
624 _notrun "not suitable for this image format: $IMGFMT"
629 # tests whether $IMGPROTO is one of the supported image protocols for a test
634 if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then
639 _notrun "not suitable for this image protocol: $IMGPROTO"
642 # tests whether $IMGPROTO is specified as an unsupported image protocol for a test
647 if [ "$f" = "$IMGPROTO" ]; then
648 _notrun "not suitable for this image protocol: $IMGPROTO"
654 # tests whether the host OS is one of the supported OSes for a test
660 if [ "$h" = "$HOSTOS" ]
666 _notrun "not suitable for this OS: $HOSTOS"
669 _supported_cache_modes()
672 if [ "$mode" = "$CACHEMODE" ]; then
676 _notrun "not suitable for cache mode: $CACHEMODE"
679 # Check whether the filesystem supports O_DIRECT
682 $QEMU_IMG create -f raw "$TEST_IMG".test_o_direct 1M > /dev/null
683 out=$($QEMU_IO -f raw -t none -c quit "$TEST_IMG".test_o_direct 2>&1)
684 rm -f "$TEST_IMG".test_o_direct
686 [[ "$out" != *"O_DIRECT"* ]]
691 if ! _check_o_direct; then
692 _notrun "file system on $TEST_DIR does not support O_DIRECT"
698 if [ $CACHEMODE == "none" ] || [ $CACHEMODE == "directsync" ]; then
705 # $1 - cache mode to use by default
706 # $2 - (optional) cache mode to use by default if O_DIRECT is not supported
707 _default_cache_mode()
709 if $CACHEMODE_IS_DEFAULT; then
710 if [ -z "$2" ] || _check_o_direct; then
715 QEMU_IO="$QEMU_IO --cache $CACHEMODE"
720 _supported_aio_modes()
723 if [ "$mode" = "$AIOMODE" ]; then
727 _notrun "not suitable for aio mode: $AIOMODE"
732 QEMU_IO="$QEMU_IO --aio $1"
735 _unsupported_imgopts()
739 if echo "$IMGOPTS" | grep -q 2>/dev/null "$bad_opt"
741 _notrun "not suitable for image option: $bad_opt"
746 # Caution: Overwrites $TEST_DIR/t.luks
747 _require_working_luks()
749 file="$TEST_DIR/t.luks"
752 $QEMU_IMG create -f luks \
753 --object secret,id=sec0,data=hunter0 \
762 IMGFMT='luks' _rm_test_img "$file"
764 if [ $status != 0 ]; then
765 reason=$(echo "$output" | grep "$file:" | $SED -e "s#.*$file: *##")
766 if [ -z "$reason" ]; then
767 reason="Failed to create a LUKS image"
773 # this test requires that a specified command (executable) exists
777 if [ "$1" = "QEMU" ]; then
779 elif [ "$1" = "QEMU_IMG" ]; then
781 elif [ "$1" = "QEMU_IO" ]; then
783 elif [ "$1" = "QEMU_NBD" ]; then
788 [ -x "$c" ] || _notrun "$1 utility required, skipped this test"
791 # Check that a set of drivers has been whitelisted in the QEMU binary
795 available=$($QEMU -drive format=help | \
796 sed -e '/Supported formats:/!d' -e 's/Supported formats://')
799 if ! echo "$available" | grep -q " $driver\( \|$\)"; then
800 _notrun "$driver not available"
805 # Check that we have a file system that allows huge (but very sparse) files
807 _require_large_file()
809 if ! truncate --size="$1" "$TEST_IMG"; then
810 _notrun "file system on $TEST_DIR does not support large enough files"
815 # Check that a set of devices is available in the QEMU binary
819 available=$($QEMU -M none -device help | \
820 grep ^name | sed -e 's/^name "//' -e 's/".*$//')
823 if ! echo "$available" | grep -q "$device" ; then
824 _notrun "$device not available"
829 # make sure this script returns success