Merge remote-tracking branch 'remotes/dgibson/tags/ppc-for-5.0-20200108' into staging
[qemu/ar7.git] / tests / qemu-iotests / common.rc
blobd088392ab6f1bb93c1e4b18553e36fdaaf649205
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 SED=
21 for 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
27 done
28 if [ -z "$SED" ]; then
29 echo "$0: GNU sed not found"
30 exit 1
33 dd()
35 if [ "$HOSTOS" == "Linux" ]
36 then
37 command dd --help | grep noxfer > /dev/null 2>&1
39 if [ "$?" -eq 0 ]
40 then
41 command dd status=noxfer $@
42 else
43 command dd $@
45 else
46 command dd $@
50 # poke_file 'test.img' 512 '\xff\xfe'
51 poke_file()
53 printf "$3" | dd "of=$1" bs=1 "seek=$2" conv=notrunc &>/dev/null
56 # peek_file_le 'test.img' 512 2 => 65534
57 peek_file_le()
59 # Wrap in echo $() to strip spaces
60 echo $(od -j"$2" -N"$3" --endian=little -An -vtu"$3" "$1")
63 # peek_file_be 'test.img' 512 2 => 65279
64 peek_file_be()
66 # Wrap in echo $() to strip spaces
67 echo $(od -j"$2" -N"$3" --endian=big -An -vtu"$3" "$1")
70 # peek_file_raw 'test.img' 512 2 => '\xff\xfe'
71 peek_file_raw()
73 dd if="$1" bs=1 skip="$2" count="$3" status=none
77 if ! . ./common.config
78 then
79 echo "$0: failed to source common.config"
80 exit 1
83 # Set the variables to the empty string to turn Valgrind off
84 # for specific processes, e.g.
85 # $ VALGRIND_QEMU_IO= ./check -qcow2 -valgrind 015
87 : ${VALGRIND_QEMU_VM=$VALGRIND_QEMU}
88 : ${VALGRIND_QEMU_IMG=$VALGRIND_QEMU}
89 : ${VALGRIND_QEMU_IO=$VALGRIND_QEMU}
90 : ${VALGRIND_QEMU_NBD=$VALGRIND_QEMU}
91 : ${VALGRIND_QEMU_VXHS=$VALGRIND_QEMU}
93 # The Valgrind own parameters may be set with
94 # its environment variable VALGRIND_OPTS, e.g.
95 # $ VALGRIND_OPTS="--leak-check=yes" ./check -qcow2 -valgrind 015
97 _qemu_proc_exec()
99 local VALGRIND_LOGFILE="$1"
100 shift
101 if [[ "${VALGRIND_QEMU}" == "y" && "${NO_VALGRIND}" != "y" ]]; then
102 exec valgrind --log-file="${VALGRIND_LOGFILE}" --error-exitcode=99 "$@"
103 else
104 exec "$@"
108 _qemu_proc_valgrind_log()
110 local VALGRIND_LOGFILE="$1"
111 local RETVAL="$2"
112 if [[ "${VALGRIND_QEMU}" == "y" && "${NO_VALGRIND}" != "y" ]]; then
113 if [ $RETVAL == 99 ]; then
114 cat "${VALGRIND_LOGFILE}"
116 rm -f "${VALGRIND_LOGFILE}"
120 _qemu_wrapper()
122 local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
124 if [ -n "${QEMU_NEED_PID}" ]; then
125 echo $BASHPID > "${QEMU_TEST_DIR}/qemu-${_QEMU_HANDLE}.pid"
127 VALGRIND_QEMU="${VALGRIND_QEMU_VM}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
128 "$QEMU_PROG" $QEMU_OPTIONS "$@"
130 RETVAL=$?
131 _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
132 return $RETVAL
135 _qemu_img_wrapper()
137 local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
139 VALGRIND_QEMU="${VALGRIND_QEMU_IMG}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
140 "$QEMU_IMG_PROG" $QEMU_IMG_OPTIONS "$@"
142 RETVAL=$?
143 _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
144 return $RETVAL
147 _qemu_io_wrapper()
149 local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
150 local QEMU_IO_ARGS="$QEMU_IO_OPTIONS"
151 if [ "$IMGOPTSSYNTAX" = "true" ]; then
152 QEMU_IO_ARGS="--image-opts $QEMU_IO_ARGS"
153 if [ -n "$IMGKEYSECRET" ]; then
154 QEMU_IO_ARGS="--object secret,id=keysec0,data=$IMGKEYSECRET $QEMU_IO_ARGS"
158 VALGRIND_QEMU="${VALGRIND_QEMU_IO}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
159 "$QEMU_IO_PROG" $QEMU_IO_ARGS "$@"
161 RETVAL=$?
162 _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
163 return $RETVAL
166 _qemu_nbd_wrapper()
168 local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
170 VALGRIND_QEMU="${VALGRIND_QEMU_NBD}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
171 "$QEMU_NBD_PROG" --pid-file="${QEMU_TEST_DIR}/qemu-nbd.pid" \
172 $QEMU_NBD_OPTIONS "$@"
174 RETVAL=$?
175 _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
176 return $RETVAL
179 _qemu_vxhs_wrapper()
181 local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
183 echo $BASHPID > "${TEST_DIR}/qemu-vxhs.pid"
184 VALGRIND_QEMU="${VALGRIND_QEMU_VXHS}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
185 "$QEMU_VXHS_PROG" $QEMU_VXHS_OPTIONS "$@"
187 RETVAL=$?
188 _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
189 return $RETVAL
192 # Valgrind bug #409141 https://bugs.kde.org/show_bug.cgi?id=409141
193 # Until valgrind 3.16+ is ubiquitous, we must work around a hang in
194 # valgrind when issuing sigkill. Disable valgrind for this invocation.
195 _NO_VALGRIND()
197 NO_VALGRIND="y" "$@"
200 export QEMU=_qemu_wrapper
201 export QEMU_IMG=_qemu_img_wrapper
202 export QEMU_IO=_qemu_io_wrapper
203 export QEMU_NBD=_qemu_nbd_wrapper
204 export QEMU_VXHS=_qemu_vxhs_wrapper
206 if [ "$IMGOPTSSYNTAX" = "true" ]; then
207 DRIVER="driver=$IMGFMT"
208 QEMU_IMG_EXTRA_ARGS="--image-opts $QEMU_IMG_EXTRA_ARGS"
209 if [ -n "$IMGKEYSECRET" ]; then
210 QEMU_IMG_EXTRA_ARGS="--object secret,id=keysec0,data=$IMGKEYSECRET $QEMU_IMG_EXTRA_ARGS"
212 if [ "$IMGFMT" = "luks" ]; then
213 DRIVER="$DRIVER,key-secret=keysec0"
215 if [ "$IMGPROTO" = "file" ]; then
216 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
217 TEST_IMG="$DRIVER,file.filename=$TEST_DIR/t.$IMGFMT"
218 elif [ "$IMGPROTO" = "nbd" ]; then
219 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
220 TEST_IMG="$DRIVER,file.driver=nbd,file.type=unix"
221 TEST_IMG="$TEST_IMG,file.path=$SOCK_DIR/nbd"
222 elif [ "$IMGPROTO" = "ssh" ]; then
223 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
224 TEST_IMG="$DRIVER,file.driver=ssh,file.host=127.0.0.1,file.path=$TEST_IMG_FILE"
225 elif [ "$IMGPROTO" = "nfs" ]; then
226 TEST_DIR="$DRIVER,file.driver=nfs,file.filename=nfs://127.0.0.1/$TEST_DIR"
227 TEST_IMG=$TEST_DIR/t.$IMGFMT
228 else
229 TEST_IMG="$DRIVER,file.driver=$IMGPROTO,file.filename=$TEST_DIR/t.$IMGFMT"
231 else
232 QEMU_IMG_EXTRA_ARGS=
233 if [ "$IMGPROTO" = "file" ]; then
234 TEST_IMG=$TEST_DIR/t.$IMGFMT
235 elif [ "$IMGPROTO" = "nbd" ]; then
236 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
237 TEST_IMG="nbd+unix:///?socket=$SOCK_DIR/nbd"
238 elif [ "$IMGPROTO" = "ssh" ]; then
239 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
240 REMOTE_TEST_DIR="ssh://\\($USER@\\)\\?127.0.0.1\\(:[0-9]\\+\\)\\?$TEST_DIR"
241 TEST_IMG="ssh://127.0.0.1$TEST_IMG_FILE"
242 elif [ "$IMGPROTO" = "nfs" ]; then
243 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
244 REMOTE_TEST_DIR="nfs://127.0.0.1$TEST_DIR"
245 TEST_IMG="nfs://127.0.0.1$TEST_IMG_FILE"
246 elif [ "$IMGPROTO" = "vxhs" ]; then
247 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
248 TEST_IMG="vxhs://127.0.0.1:9999/t.$IMGFMT"
249 else
250 TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT
253 ORIG_TEST_IMG="$TEST_IMG"
255 if [ -z "$TEST_DIR" ]; then
256 TEST_DIR=$PWD/scratch
259 QEMU_TEST_DIR="${TEST_DIR}"
261 if [ ! -e "$TEST_DIR" ]; then
262 mkdir "$TEST_DIR"
265 if [ ! -d "$TEST_DIR" ]; then
266 echo "common.rc: Error: \$TEST_DIR ($TEST_DIR) is not a directory"
267 exit 1
270 if [ -z "$REMOTE_TEST_DIR" ]; then
271 REMOTE_TEST_DIR="$TEST_DIR"
274 if [ ! -d "$SAMPLE_IMG_DIR" ]; then
275 echo "common.rc: Error: \$SAMPLE_IMG_DIR ($SAMPLE_IMG_DIR) is not a directory"
276 exit 1
279 _use_sample_img()
281 SAMPLE_IMG_FILE="${1%\.bz2}"
282 TEST_IMG="$TEST_DIR/$SAMPLE_IMG_FILE"
283 bzcat "$SAMPLE_IMG_DIR/$1" > "$TEST_IMG"
284 if [ $? -ne 0 ]
285 then
286 echo "_use_sample_img error, cannot extract '$SAMPLE_IMG_DIR/$1'"
287 exit 1
291 _stop_nbd_server()
293 if [ -f "${QEMU_TEST_DIR}/qemu-nbd.pid" ]; then
294 local QEMU_NBD_PID
295 read QEMU_NBD_PID < "${QEMU_TEST_DIR}/qemu-nbd.pid"
296 kill ${QEMU_NBD_PID}
297 rm -f "${QEMU_TEST_DIR}/qemu-nbd.pid" "$SOCK_DIR/nbd"
301 # Gets the data_file value from IMGOPTS and replaces the '$TEST_IMG'
302 # pattern by '$1'
303 # Caution: The replacement is done with sed, so $1 must be escaped
304 # properly. (The delimiter is '#'.)
305 _get_data_file()
307 if ! echo "$IMGOPTS" | grep -q 'data_file='; then
308 return 1
311 echo "$IMGOPTS" | sed -e 's/.*data_file=\([^,]*\).*/\1/' \
312 | sed -e "s#\\\$TEST_IMG#$1#"
315 _make_test_img()
317 # extra qemu-img options can be added by tests
318 # at least one argument (the image size) needs to be added
319 local extra_img_options=""
320 local optstr=""
321 local img_name=""
322 local use_backing=0
323 local backing_file=""
324 local object_options=""
325 local opts_param=false
326 local misc_params=()
328 if [ -n "$TEST_IMG_FILE" ]; then
329 img_name=$TEST_IMG_FILE
330 else
331 img_name=$TEST_IMG
334 if [ -n "$IMGOPTS" ]; then
335 imgopts_expanded=$(echo "$IMGOPTS" | sed -e "s#\\\$TEST_IMG#$img_name#")
336 optstr=$(_optstr_add "$optstr" "$imgopts_expanded")
338 if [ -n "$IMGKEYSECRET" ]; then
339 object_options="--object secret,id=keysec0,data=$IMGKEYSECRET"
340 optstr=$(_optstr_add "$optstr" "key-secret=keysec0")
343 for param; do
344 if [ "$use_backing" = "1" -a -z "$backing_file" ]; then
345 backing_file=$param
346 continue
347 elif $opts_param; then
348 optstr=$(_optstr_add "$optstr" "$param")
349 opts_param=false
350 continue
353 case "$param" in
355 use_backing=1
359 opts_param=true
362 --no-opts)
363 optstr=""
367 misc_params=("${misc_params[@]}" "$param")
369 esac
370 done
372 if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then
373 optstr=$(_optstr_add "$optstr" "cluster_size=$CLUSTER_SIZE")
376 if [ -n "$optstr" ]; then
377 extra_img_options="-o $optstr $extra_img_options"
380 if [ $IMGPROTO = "nbd" ]; then
381 _stop_nbd_server
384 # XXX(hch): have global image options?
386 if [ $use_backing = 1 ]; then
387 $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options -b "$backing_file" "$img_name" "${misc_params[@]}" 2>&1
388 else
389 $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options "$img_name" "${misc_params[@]}" 2>&1
391 ) | _filter_img_create
393 # Start an NBD server on the image file, which is what we'll be talking to.
394 # Once NBD gains resize support, we may also want to use -f raw at the
395 # server and interpret format over NBD, but for now, the format is
396 # interpreted at the server and raw data sent over NBD.
397 if [ $IMGPROTO = "nbd" ]; then
398 # Pass a sufficiently high number to -e that should be enough for all
399 # tests
400 eval "$QEMU_NBD -v -t -k '$SOCK_DIR/nbd' -f $IMGFMT -e 42 -x '' $TEST_IMG_FILE >/dev/null &"
401 sleep 1 # FIXME: qemu-nbd needs to be listening before we continue
404 # Start QNIO server on image directory for vxhs protocol
405 if [ $IMGPROTO = "vxhs" ]; then
406 eval "$QEMU_VXHS -d $TEST_DIR > /dev/null &"
407 sleep 1 # Wait for server to come up.
411 _rm_test_img()
413 local img=$1
414 if [ "$IMGFMT" = "vmdk" ]; then
415 # Remove all the extents for vmdk
416 "$QEMU_IMG" info "$img" 2>/dev/null | grep 'filename:' | cut -f 2 -d: \
417 | xargs -I {} rm -f "{}"
418 elif [ "$IMGFMT" = "qcow2" ]; then
419 # Remove external data file
420 if data_file=$(_get_data_file "$img"); then
421 rm -f "$data_file"
424 rm -f "$img"
427 _cleanup_test_img()
429 case "$IMGPROTO" in
431 nbd)
432 _stop_nbd_server
433 rm -f "$TEST_IMG_FILE"
435 vxhs)
436 if [ -f "${TEST_DIR}/qemu-vxhs.pid" ]; then
437 local QEMU_VXHS_PID
438 read QEMU_VXHS_PID < "${TEST_DIR}/qemu-vxhs.pid"
439 kill ${QEMU_VXHS_PID} >/dev/null 2>&1
440 rm -f "${TEST_DIR}/qemu-vxhs.pid"
442 rm -f "$TEST_IMG_FILE"
445 file)
446 _rm_test_img "$TEST_DIR/t.$IMGFMT"
447 _rm_test_img "$TEST_DIR/t.$IMGFMT.orig"
448 _rm_test_img "$TEST_DIR/t.$IMGFMT.base"
449 if [ -n "$SAMPLE_IMG_FILE" ]
450 then
451 rm -f "$TEST_DIR/$SAMPLE_IMG_FILE"
452 SAMPLE_IMG_FILE=
453 TEST_IMG="$ORIG_TEST_IMG"
457 rbd)
458 rbd --no-progress rm "$TEST_DIR/t.$IMGFMT" > /dev/null
461 sheepdog)
462 collie vdi delete "$TEST_DIR/t.$IMGFMT"
465 esac
468 _check_test_img()
471 if [ "$IMGOPTSSYNTAX" = "true" ]; then
472 $QEMU_IMG check $QEMU_IMG_EXTRA_ARGS "$@" "$TEST_IMG" 2>&1
473 else
474 $QEMU_IMG check "$@" -f $IMGFMT "$TEST_IMG" 2>&1
476 ) | _filter_testdir | _filter_qemu_img_check
478 # return real qemu_img check status, to analyze in
479 # _check_test_img_ignore_leaks
480 return ${PIPESTATUS[0]}
483 _check_test_img_ignore_leaks()
485 out=$(_check_test_img "$@")
486 status=$?
487 if [ $status = 3 ]; then
488 # This must correspond to success output in dump_human_image_check()
489 echo "No errors were found on the image."
490 return 0
492 echo "$out"
493 return $status
496 _img_info()
498 if [[ "$1" == "--format-specific" ]]; then
499 local format_specific=1
500 shift
501 else
502 local format_specific=0
505 discard=0
506 regex_json_spec_start='^ *"format-specific": \{'
507 $QEMU_IMG info $QEMU_IMG_EXTRA_ARGS "$@" "$TEST_IMG" 2>&1 | \
508 sed -e "s#$REMOTE_TEST_DIR#TEST_DIR#g" \
509 -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
510 -e "s#$TEST_DIR#TEST_DIR#g" \
511 -e "s#$IMGFMT#IMGFMT#g" \
512 -e "/^disk size:/ D" \
513 -e "/actual-size/ D" | \
514 while IFS='' read -r line; do
515 if [[ $format_specific == 1 ]]; then
516 discard=0
517 elif [[ $line == "Format specific information:" ]]; then
518 discard=1
519 elif [[ $line =~ $regex_json_spec_start ]]; then
520 discard=2
521 regex_json_spec_end="^${line%%[^ ]*}\\},? *$"
523 if [[ $discard == 0 ]]; then
524 echo "$line"
525 elif [[ $discard == 1 && ! $line ]]; then
526 echo
527 discard=0
528 elif [[ $discard == 2 && $line =~ $regex_json_spec_end ]]; then
529 discard=0
531 done
534 # bail out, setting up .notrun file
536 _notrun()
538 echo "$*" >"$OUTPUT_DIR/$seq.notrun"
539 echo "$seq not run: $*"
540 status=0
541 exit
544 # bail out, setting up .casenotrun file
545 # The function _casenotrun() is used as a notifier. It is the
546 # caller's responsibility to make skipped a particular test.
548 _casenotrun()
550 echo " [case not run] $*" >>"$OUTPUT_DIR/$seq.casenotrun"
553 # just plain bail out
555 _fail()
557 echo "$*" | tee -a "$OUTPUT_DIR/$seq.full"
558 echo "(see $seq.full for details)"
559 status=1
560 exit 1
563 # tests whether $IMGFMT is one of the supported image formats for a test
565 _supported_fmt()
567 # "generic" is suitable for most image formats. For some formats it doesn't
568 # work, however (most notably read-only formats), so they can opt out by
569 # setting IMGFMT_GENERIC to false.
570 for f; do
571 if [ "$f" = "$IMGFMT" -o "$f" = "generic" -a "$IMGFMT_GENERIC" = "true" ]; then
572 return
574 done
576 _notrun "not suitable for this image format: $IMGFMT"
579 # tests whether $IMGFMT is one of the unsupported image format for a test
581 _unsupported_fmt()
583 for f; do
584 if [ "$f" = "$IMGFMT" ]; then
585 _notrun "not suitable for this image format: $IMGFMT"
587 done
590 # tests whether $IMGPROTO is one of the supported image protocols for a test
592 _supported_proto()
594 for f; do
595 if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then
596 return
598 done
600 _notrun "not suitable for this image protocol: $IMGPROTO"
603 # tests whether $IMGPROTO is specified as an unsupported image protocol for a test
605 _unsupported_proto()
607 for f; do
608 if [ "$f" = "$IMGPROTO" ]; then
609 _notrun "not suitable for this image protocol: $IMGPROTO"
610 return
612 done
615 # tests whether the host OS is one of the supported OSes for a test
617 _supported_os()
619 for h
621 if [ "$h" = "$HOSTOS" ]
622 then
623 return
625 done
627 _notrun "not suitable for this OS: $HOSTOS"
630 _supported_cache_modes()
632 for mode; do
633 if [ "$mode" = "$CACHEMODE" ]; then
634 return
636 done
637 _notrun "not suitable for cache mode: $CACHEMODE"
640 _default_cache_mode()
642 if $CACHEMODE_IS_DEFAULT; then
643 CACHEMODE="$1"
644 QEMU_IO="$QEMU_IO --cache $1"
645 return
649 _unsupported_imgopts()
651 for bad_opt
653 if echo "$IMGOPTS" | grep -q 2>/dev/null "$bad_opt"
654 then
655 _notrun "not suitable for image option: $bad_opt"
657 done
660 # this test requires that a specified command (executable) exists
662 _require_command()
664 if [ "$1" = "QEMU" ]; then
665 c=$QEMU_PROG
666 elif [ "$1" = "QEMU_IMG" ]; then
667 c=$QEMU_IMG_PROG
668 elif [ "$1" = "QEMU_IO" ]; then
669 c=$QEMU_IO_PROG
670 elif [ "$1" = "QEMU_NBD" ]; then
671 c=$QEMU_NBD_PROG
672 else
673 eval c=\$$1
675 [ -x "$c" ] || _notrun "$1 utility required, skipped this test"
678 # Check that a set of drivers has been whitelisted in the QEMU binary
680 _require_drivers()
682 available=$($QEMU -drive format=help | \
683 sed -e '/Supported formats:/!d' -e 's/Supported formats://')
684 for driver
686 if ! echo "$available" | grep -q " $driver\( \|$\)"; then
687 _notrun "$driver not available"
689 done
692 # Check that we have a file system that allows huge (but very sparse) files
694 _require_large_file()
696 if ! truncate --size="$1" "$TEST_IMG"; then
697 _notrun "file system on $TEST_DIR does not support large enough files"
699 rm "$TEST_IMG"
702 # make sure this script returns success
703 true