iotests: Give access to the qemu-storage-daemon
[qemu/kevin.git] / tests / qemu-iotests / common.rc
blob20589e59a5f19b74a55d19497e3813a239d9245d
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 # poke_file_le $img_filename $offset $byte_width $value
57 # Example: poke_file_le "$TEST_IMG" 512 2 65534
58 poke_file_le()
60 local img=$1 ofs=$2 len=$3 val=$4 str=''
62 while ((len--)); do
63 str+=$(printf '\\x%02x' $((val & 0xff)))
64 val=$((val >> 8))
65 done
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
72 poke_file_be()
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
81 peek_file_le()
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) ))
88 shift=$((shift + 8))
89 done
90 printf %llu $val
93 # peek_file_be 'test.img' 512 2 => 65279
94 peek_file_be()
96 local val=0 byte
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
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.
107 peek_file_raw()
109 dd if="$1" bs=1 skip="$2" count="$3" status=none
113 if ! . ./common.config
114 then
115 echo "$0: failed to source common.config"
116 exit 1
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_QSD=$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
133 _qemu_proc_exec()
135 local VALGRIND_LOGFILE="$1"
136 shift
137 if [[ "${VALGRIND_QEMU}" == "y" && "${NO_VALGRIND}" != "y" ]]; then
138 exec valgrind --log-file="${VALGRIND_LOGFILE}" --error-exitcode=99 "$@"
139 else
140 exec "$@"
144 _qemu_proc_valgrind_log()
146 local VALGRIND_LOGFILE="$1"
147 local RETVAL="$2"
148 if [[ "${VALGRIND_QEMU}" == "y" && "${NO_VALGRIND}" != "y" ]]; then
149 if [ $RETVAL == 99 ]; then
150 cat "${VALGRIND_LOGFILE}"
152 rm -f "${VALGRIND_LOGFILE}"
156 _qemu_wrapper()
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 "$@"
166 RETVAL=$?
167 _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
168 return $RETVAL
171 _qemu_img_wrapper()
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 "$@"
178 RETVAL=$?
179 _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
180 return $RETVAL
183 _qemu_io_wrapper()
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 "$@"
197 RETVAL=$?
198 _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
199 return $RETVAL
202 _qemu_nbd_wrapper()
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 "$@"
210 RETVAL=$?
211 _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
212 return $RETVAL
215 _qemu_storage_daemon_wrapper()
217 local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
219 if [ -n "${QSD_NEED_PID}" ]; then
220 echo $BASHPID > "${QEMU_TEST_DIR}/qemu-storage-daemon.pid"
222 VALGRIND_QEMU="${VALGRIND_QSD}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
223 "$QSD_PROG" $QSD_OPTIONS "$@"
225 RETVAL=$?
226 _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
227 return $RETVAL
230 # Valgrind bug #409141 https://bugs.kde.org/show_bug.cgi?id=409141
231 # Until valgrind 3.16+ is ubiquitous, we must work around a hang in
232 # valgrind when issuing sigkill. Disable valgrind for this invocation.
233 _NO_VALGRIND()
235 NO_VALGRIND="y" "$@"
238 export QEMU=_qemu_wrapper
239 export QEMU_IMG=_qemu_img_wrapper
240 export QEMU_IO=_qemu_io_wrapper
241 export QEMU_NBD=_qemu_nbd_wrapper
242 export QSD=_qemu_storage_daemon_wrapper
244 if [ "$IMGOPTSSYNTAX" = "true" ]; then
245 DRIVER="driver=$IMGFMT"
246 QEMU_IMG_EXTRA_ARGS="--image-opts $QEMU_IMG_EXTRA_ARGS"
247 if [ -n "$IMGKEYSECRET" ]; then
248 QEMU_IMG_EXTRA_ARGS="--object secret,id=keysec0,data=$IMGKEYSECRET $QEMU_IMG_EXTRA_ARGS"
250 if [ "$IMGFMT" = "luks" ]; then
251 DRIVER="$DRIVER,key-secret=keysec0"
253 if [ "$IMGPROTO" = "file" ]; then
254 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
255 TEST_IMG="$DRIVER,file.filename=$TEST_DIR/t.$IMGFMT"
256 elif [ "$IMGPROTO" = "nbd" ]; then
257 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
258 TEST_IMG="$DRIVER,file.driver=nbd,file.type=unix"
259 TEST_IMG="$TEST_IMG,file.path=$SOCK_DIR/nbd"
260 elif [ "$IMGPROTO" = "ssh" ]; then
261 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
262 TEST_IMG="$DRIVER,file.driver=ssh,file.host=127.0.0.1,file.path=$TEST_IMG_FILE"
263 elif [ "$IMGPROTO" = "nfs" ]; then
264 TEST_DIR="$DRIVER,file.driver=nfs,file.filename=nfs://127.0.0.1/$TEST_DIR"
265 TEST_IMG=$TEST_DIR/t.$IMGFMT
266 else
267 TEST_IMG="$DRIVER,file.driver=$IMGPROTO,file.filename=$TEST_DIR/t.$IMGFMT"
269 else
270 QEMU_IMG_EXTRA_ARGS=
271 if [ "$IMGPROTO" = "file" ]; then
272 TEST_IMG=$TEST_DIR/t.$IMGFMT
273 elif [ "$IMGPROTO" = "nbd" ]; then
274 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
275 TEST_IMG="nbd+unix:///?socket=$SOCK_DIR/nbd"
276 elif [ "$IMGPROTO" = "ssh" ]; then
277 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
278 REMOTE_TEST_DIR="ssh://\\($USER@\\)\\?127.0.0.1\\(:[0-9]\\+\\)\\?$TEST_DIR"
279 TEST_IMG="ssh://127.0.0.1$TEST_IMG_FILE"
280 elif [ "$IMGPROTO" = "nfs" ]; then
281 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
282 REMOTE_TEST_DIR="nfs://127.0.0.1$TEST_DIR"
283 TEST_IMG="nfs://127.0.0.1$TEST_IMG_FILE"
284 else
285 TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT
288 ORIG_TEST_IMG_FILE=$TEST_IMG_FILE
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
298 mkdir "$TEST_DIR"
301 if [ ! -d "$TEST_DIR" ]; then
302 echo "common.rc: Error: \$TEST_DIR ($TEST_DIR) is not a directory"
303 exit 1
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"
312 exit 1
315 _use_sample_img()
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
327 _stop_nbd_server()
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}
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'
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()
343 if ! echo "$IMGOPTS" | grep -q 'data_file='; then
344 return 1
347 echo "$IMGOPTS" | sed -e 's/.*data_file=\([^,]*\).*/\1/' \
348 | sed -e "s#\\\$TEST_IMG#$1#"
351 # Translate a $TEST_IMG to its corresponding $TEST_IMG_FILE for
352 # different protocols
353 _test_img_to_test_img_file()
355 case "$IMGPROTO" in
356 file)
357 echo "$1"
360 nfs)
361 echo "$1" | sed -e "s#nfs://127.0.0.1##"
364 ssh)
365 echo "$1" | \
366 sed -e "s#ssh://\\($USER@\\)\\?127.0.0.1\\(:[0-9]\\+\\)\\?##"
370 return 1
372 esac
375 _make_test_img()
377 # extra qemu-img options can be added by tests
378 # at least one argument (the image size) needs to be added
379 local extra_img_options=""
380 local optstr=""
381 local img_name=""
382 local use_backing=0
383 local backing_file=""
384 local object_options=""
385 local opts_param=false
386 local misc_params=()
388 if [ -z "$TEST_IMG_FILE" ]; then
389 img_name=$TEST_IMG
390 elif [ "$IMGOPTSSYNTAX" != "true" -a \
391 "$TEST_IMG_FILE" = "$ORIG_TEST_IMG_FILE" ]; then
392 # Handle cases of tests only updating TEST_IMG, but not TEST_IMG_FILE
393 img_name=$(_test_img_to_test_img_file "$TEST_IMG")
394 if [ "$?" != 0 ]; then
395 img_name=$TEST_IMG_FILE
397 else
398 # $TEST_IMG_FILE is not the default value, so it definitely has been
399 # modified by the test
400 img_name=$TEST_IMG_FILE
403 if [ -n "$IMGOPTS" ]; then
404 imgopts_expanded=$(echo "$IMGOPTS" | sed -e "s#\\\$TEST_IMG#$img_name#")
405 optstr=$(_optstr_add "$optstr" "$imgopts_expanded")
407 if [ -n "$IMGKEYSECRET" ]; then
408 object_options="--object secret,id=keysec0,data=$IMGKEYSECRET"
409 optstr=$(_optstr_add "$optstr" "key-secret=keysec0")
412 for param; do
413 if [ "$use_backing" = "1" -a -z "$backing_file" ]; then
414 backing_file=$param
415 continue
416 elif $opts_param; then
417 optstr=$(_optstr_add "$optstr" "$param")
418 opts_param=false
419 continue
422 case "$param" in
424 use_backing=1
428 opts_param=true
431 --no-opts)
432 optstr=""
436 misc_params=("${misc_params[@]}" "$param")
438 esac
439 done
441 if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then
442 optstr=$(_optstr_add "$optstr" "cluster_size=$CLUSTER_SIZE")
445 if [ -n "$optstr" ]; then
446 extra_img_options="-o $optstr $extra_img_options"
449 if [ $IMGPROTO = "nbd" ]; then
450 _stop_nbd_server
453 # XXX(hch): have global image options?
455 if [ $use_backing = 1 ]; then
456 $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options -b "$backing_file" "$img_name" "${misc_params[@]}" 2>&1
457 else
458 $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options "$img_name" "${misc_params[@]}" 2>&1
460 ) | _filter_img_create
462 # Start an NBD server on the image file, which is what we'll be talking to.
463 # Once NBD gains resize support, we may also want to use -f raw at the
464 # server and interpret format over NBD, but for now, the format is
465 # interpreted at the server and raw data sent over NBD.
466 if [ $IMGPROTO = "nbd" ]; then
467 # Pass a sufficiently high number to -e that should be enough for all
468 # tests
469 eval "$QEMU_NBD -v -t -k '$SOCK_DIR/nbd' -f $IMGFMT -e 42 -x '' $TEST_IMG_FILE >/dev/null &"
470 sleep 1 # FIXME: qemu-nbd needs to be listening before we continue
474 _rm_test_img()
476 local img=$1
477 if [ "$IMGFMT" = "vmdk" ]; then
478 # Remove all the extents for vmdk
479 "$QEMU_IMG" info "$img" 2>/dev/null | grep 'filename:' | cut -f 2 -d: \
480 | xargs -I {} rm -f "{}"
481 elif [ "$IMGFMT" = "qcow2" ]; then
482 # Remove external data file
483 if data_file=$(_get_data_file "$img"); then
484 rm -f "$data_file"
487 rm -f "$img"
490 _cleanup_test_img()
492 case "$IMGPROTO" in
494 nbd)
495 _stop_nbd_server
496 rm -f "$TEST_IMG_FILE"
499 file)
500 _rm_test_img "$TEST_DIR/t.$IMGFMT"
501 _rm_test_img "$TEST_DIR/t.$IMGFMT.orig"
502 _rm_test_img "$TEST_DIR/t.$IMGFMT.base"
503 if [ -n "$SAMPLE_IMG_FILE" ]
504 then
505 rm -f "$TEST_DIR/$SAMPLE_IMG_FILE"
506 SAMPLE_IMG_FILE=
507 TEST_IMG="$ORIG_TEST_IMG"
511 rbd)
512 rbd --no-progress rm "$TEST_DIR/t.$IMGFMT" > /dev/null
515 sheepdog)
516 collie vdi delete "$TEST_DIR/t.$IMGFMT"
519 esac
522 _check_test_img()
525 if [ "$IMGOPTSSYNTAX" = "true" ]; then
526 $QEMU_IMG check $QEMU_IMG_EXTRA_ARGS "$@" "$TEST_IMG" 2>&1
527 else
528 $QEMU_IMG check "$@" -f $IMGFMT "$TEST_IMG" 2>&1
530 ) | _filter_testdir | _filter_qemu_img_check
532 # return real qemu_img check status, to analyze in
533 # _check_test_img_ignore_leaks
534 return ${PIPESTATUS[0]}
537 _check_test_img_ignore_leaks()
539 out=$(_check_test_img "$@")
540 status=$?
541 if [ $status = 3 ]; then
542 # This must correspond to success output in dump_human_image_check()
543 echo "No errors were found on the image."
544 return 0
546 echo "$out"
547 return $status
550 _img_info()
552 if [[ "$1" == "--format-specific" ]]; then
553 local format_specific=1
554 shift
555 else
556 local format_specific=0
559 discard=0
560 regex_json_spec_start='^ *"format-specific": \{'
561 $QEMU_IMG info $QEMU_IMG_EXTRA_ARGS "$@" "$TEST_IMG" 2>&1 | \
562 sed -e "s#$REMOTE_TEST_DIR#TEST_DIR#g" \
563 -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
564 -e "s#$TEST_DIR#TEST_DIR#g" \
565 -e "s#$IMGFMT#IMGFMT#g" \
566 -e "/^disk size:/ D" \
567 -e "/actual-size/ D" | \
568 while IFS='' read -r line; do
569 if [[ $format_specific == 1 ]]; then
570 discard=0
571 elif [[ $line == "Format specific information:" ]]; then
572 discard=1
573 elif [[ $line =~ $regex_json_spec_start ]]; then
574 discard=2
575 regex_json_spec_end="^${line%%[^ ]*}\\},? *$"
577 if [[ $discard == 0 ]]; then
578 echo "$line"
579 elif [[ $discard == 1 && ! $line ]]; then
580 echo
581 discard=0
582 elif [[ $discard == 2 && $line =~ $regex_json_spec_end ]]; then
583 discard=0
585 done
588 # bail out, setting up .notrun file
590 _notrun()
592 echo "$*" >"$OUTPUT_DIR/$seq.notrun"
593 echo "$seq not run: $*"
594 status=0
595 exit
598 # bail out, setting up .casenotrun file
599 # The function _casenotrun() is used as a notifier. It is the
600 # caller's responsibility to make skipped a particular test.
602 _casenotrun()
604 echo " [case not run] $*" >>"$OUTPUT_DIR/$seq.casenotrun"
607 # just plain bail out
609 _fail()
611 echo "$*" | tee -a "$OUTPUT_DIR/$seq.full"
612 echo "(see $seq.full for details)"
613 status=1
614 exit 1
617 # tests whether $IMGFMT is one of the supported image formats for a test
619 _supported_fmt()
621 # "generic" is suitable for most image formats. For some formats it doesn't
622 # work, however (most notably read-only formats), so they can opt out by
623 # setting IMGFMT_GENERIC to false.
624 for f; do
625 if [ "$f" = "$IMGFMT" -o "$f" = "generic" -a "$IMGFMT_GENERIC" = "true" ]; then
626 if [ "$IMGFMT" = "luks" ]; then
627 _require_working_luks
629 return
631 done
633 _notrun "not suitable for this image format: $IMGFMT"
636 # tests whether $IMGFMT is one of the unsupported image format for a test
638 _unsupported_fmt()
640 for f; do
641 if [ "$f" = "$IMGFMT" ]; then
642 _notrun "not suitable for this image format: $IMGFMT"
644 done
647 # tests whether $IMGPROTO is one of the supported image protocols for a test
649 _supported_proto()
651 for f; do
652 if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then
653 return
655 done
657 _notrun "not suitable for this image protocol: $IMGPROTO"
660 # tests whether $IMGPROTO is specified as an unsupported image protocol for a test
662 _unsupported_proto()
664 for f; do
665 if [ "$f" = "$IMGPROTO" ]; then
666 _notrun "not suitable for this image protocol: $IMGPROTO"
667 return
669 done
672 # tests whether the host OS is one of the supported OSes for a test
674 _supported_os()
676 for h
678 if [ "$h" = "$HOSTOS" ]
679 then
680 return
682 done
684 _notrun "not suitable for this OS: $HOSTOS"
687 _supported_cache_modes()
689 for mode; do
690 if [ "$mode" = "$CACHEMODE" ]; then
691 return
693 done
694 _notrun "not suitable for cache mode: $CACHEMODE"
697 # Check whether the filesystem supports O_DIRECT
698 _check_o_direct()
700 $QEMU_IMG create -f raw "$TEST_IMG".test_o_direct 1M > /dev/null
701 out=$($QEMU_IO -f raw -t none -c quit "$TEST_IMG".test_o_direct 2>&1)
702 rm -f "$TEST_IMG".test_o_direct
704 [[ "$out" != *"O_DIRECT"* ]]
707 _require_o_direct()
709 if ! _check_o_direct; then
710 _notrun "file system on $TEST_DIR does not support O_DIRECT"
714 _check_cache_mode()
716 if [ $CACHEMODE == "none" ] || [ $CACHEMODE == "directsync" ]; then
717 _require_o_direct
721 _check_cache_mode
723 # $1 - cache mode to use by default
724 # $2 - (optional) cache mode to use by default if O_DIRECT is not supported
725 _default_cache_mode()
727 if $CACHEMODE_IS_DEFAULT; then
728 if [ -z "$2" ] || _check_o_direct; then
729 CACHEMODE="$1"
730 else
731 CACHEMODE="$2"
733 QEMU_IO="$QEMU_IO --cache $CACHEMODE"
734 _check_cache_mode
735 return
738 _supported_aio_modes()
740 for mode; do
741 if [ "$mode" = "$AIOMODE" ]; then
742 return
744 done
745 _notrun "not suitable for aio mode: $AIOMODE"
747 _default_aio_mode()
749 AIOMODE="$1"
750 QEMU_IO="$QEMU_IO --aio $1"
753 _unsupported_imgopts()
755 for bad_opt
757 if echo "$IMGOPTS" | grep -q 2>/dev/null "$bad_opt"
758 then
759 _notrun "not suitable for image option: $bad_opt"
761 done
764 # Caution: Overwrites $TEST_DIR/t.luks
765 _require_working_luks()
767 file="$TEST_DIR/t.luks"
769 output=$(
770 $QEMU_IMG create -f luks \
771 --object secret,id=sec0,data=hunter0 \
772 -o key-secret=sec0 \
773 -o iter-time=10 \
774 "$file" \
775 1M \
776 2>&1
778 status=$?
780 IMGFMT='luks' _rm_test_img "$file"
782 if [ $status != 0 ]; then
783 reason=$(echo "$output" | grep "$file:" | $SED -e "s#.*$file: *##")
784 if [ -z "$reason" ]; then
785 reason="Failed to create a LUKS image"
787 _notrun "$reason"
791 # this test requires that a specified command (executable) exists
793 _require_command()
795 if [ "$1" = "QEMU" ]; then
796 c=$QEMU_PROG
797 elif [ "$1" = "QEMU_IMG" ]; then
798 c=$QEMU_IMG_PROG
799 elif [ "$1" = "QEMU_IO" ]; then
800 c=$QEMU_IO_PROG
801 elif [ "$1" = "QEMU_NBD" ]; then
802 c=$QEMU_NBD_PROG
803 else
804 eval c=\$$1
806 [ -x "$c" ] || _notrun "$1 utility required, skipped this test"
809 # Check that a set of drivers has been whitelisted in the QEMU binary
811 _require_drivers()
813 available=$($QEMU -drive format=help | \
814 sed -e '/Supported formats:/!d' -e 's/Supported formats://')
815 for driver
817 if ! echo "$available" | grep -q " $driver\( \|$\)"; then
818 _notrun "$driver not available"
820 done
823 # Check that we have a file system that allows huge (but very sparse) files
825 _require_large_file()
827 if ! truncate --size="$1" "$TEST_IMG"; then
828 _notrun "file system on $TEST_DIR does not support large enough files"
830 rm "$TEST_IMG"
833 # Check that a set of devices is available in the QEMU binary
835 _require_devices()
837 available=$($QEMU -M none -device help | \
838 grep ^name | sed -e 's/^name "//' -e 's/".*$//')
839 for device
841 if ! echo "$available" | grep -q "$device" ; then
842 _notrun "$device not available"
844 done
847 # make sure this script returns success
848 true