Docs/RCU: Correct sample code of qatomic_rcu_set
[qemu/ar7.git] / tests / qemu-iotests / common.rc
blob29354654cc45fd188089bcd416792999ad2b0bd8
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" = "fuse" ]; then
261 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
262 TEST_IMG="$DRIVER,file.filename=$SOCK_DIR/fuse-t.$IMGFMT"
263 elif [ "$IMGPROTO" = "ssh" ]; then
264 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
265 TEST_IMG="$DRIVER,file.driver=ssh,file.host=127.0.0.1,file.path=$TEST_IMG_FILE"
266 elif [ "$IMGPROTO" = "nfs" ]; then
267 TEST_DIR="$DRIVER,file.driver=nfs,file.filename=nfs://127.0.0.1/$TEST_DIR"
268 TEST_IMG=$TEST_DIR/t.$IMGFMT
269 else
270 TEST_IMG="$DRIVER,file.driver=$IMGPROTO,file.filename=$TEST_DIR/t.$IMGFMT"
272 else
273 QEMU_IMG_EXTRA_ARGS=
274 if [ "$IMGPROTO" = "file" ]; then
275 TEST_IMG=$TEST_DIR/t.$IMGFMT
276 elif [ "$IMGPROTO" = "nbd" ]; then
277 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
278 TEST_IMG="nbd+unix:///?socket=$SOCK_DIR/nbd"
279 elif [ "$IMGPROTO" = "fuse" ]; then
280 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
281 TEST_IMG="$SOCK_DIR/fuse-t.$IMGFMT"
282 elif [ "$IMGPROTO" = "ssh" ]; then
283 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
284 REMOTE_TEST_DIR="ssh://\\($USER@\\)\\?127.0.0.1\\(:[0-9]\\+\\)\\?$TEST_DIR"
285 TEST_IMG="ssh://127.0.0.1$TEST_IMG_FILE"
286 elif [ "$IMGPROTO" = "nfs" ]; then
287 TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
288 REMOTE_TEST_DIR="nfs://127.0.0.1$TEST_DIR"
289 TEST_IMG="nfs://127.0.0.1$TEST_IMG_FILE"
290 else
291 TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT
294 ORIG_TEST_IMG_FILE=$TEST_IMG_FILE
295 ORIG_TEST_IMG="$TEST_IMG"
297 FUSE_PIDS=()
298 FUSE_EXPORTS=()
300 if [ -z "$TEST_DIR" ]; then
301 TEST_DIR=$PWD/scratch
304 QEMU_TEST_DIR="${TEST_DIR}"
306 if [ ! -e "$TEST_DIR" ]; then
307 mkdir "$TEST_DIR"
310 if [ ! -d "$TEST_DIR" ]; then
311 echo "common.rc: Error: \$TEST_DIR ($TEST_DIR) is not a directory"
312 exit 1
315 if [ -z "$REMOTE_TEST_DIR" ]; then
316 REMOTE_TEST_DIR="$TEST_DIR"
319 if [ ! -d "$SAMPLE_IMG_DIR" ]; then
320 echo "common.rc: Error: \$SAMPLE_IMG_DIR ($SAMPLE_IMG_DIR) is not a directory"
321 exit 1
324 _use_sample_img()
326 SAMPLE_IMG_FILE="${1%\.bz2}"
327 TEST_IMG="$TEST_DIR/$SAMPLE_IMG_FILE"
328 bzcat "$SAMPLE_IMG_DIR/$1" > "$TEST_IMG"
329 if [ $? -ne 0 ]
330 then
331 echo "_use_sample_img error, cannot extract '$SAMPLE_IMG_DIR/$1'"
332 exit 1
336 _stop_nbd_server()
338 if [ -f "${QEMU_TEST_DIR}/qemu-nbd.pid" ]; then
339 local QEMU_NBD_PID
340 read QEMU_NBD_PID < "${QEMU_TEST_DIR}/qemu-nbd.pid"
341 kill ${QEMU_NBD_PID}
342 rm -f "${QEMU_TEST_DIR}/qemu-nbd.pid" "$SOCK_DIR/nbd"
346 # Gets the data_file value from IMGOPTS and replaces the '$TEST_IMG'
347 # pattern by '$1'
348 # Caution: The replacement is done with sed, so $1 must be escaped
349 # properly. (The delimiter is '#'.)
350 _get_data_file()
352 if ! echo "$IMGOPTS" | grep -q 'data_file='; then
353 return 1
356 echo "$IMGOPTS" | sed -e 's/.*data_file=\([^,]*\).*/\1/' \
357 | sed -e "s#\\\$TEST_IMG#$1#"
360 # Translate a $TEST_IMG to its corresponding $TEST_IMG_FILE for
361 # different protocols
362 _test_img_to_test_img_file()
364 case "$IMGPROTO" in
365 file)
366 echo "$1"
369 fuse)
370 echo "$1" | sed -e "s#$SOCK_DIR/fuse-#$TEST_DIR/#"
373 nfs)
374 echo "$1" | sed -e "s#nfs://127.0.0.1##"
377 ssh)
378 echo "$1" | \
379 sed -e "s#ssh://\\($USER@\\)\\?127.0.0.1\\(:[0-9]\\+\\)\\?##"
383 return 1
385 esac
388 _make_test_img()
390 # extra qemu-img options can be added by tests
391 # at least one argument (the image size) needs to be added
392 local extra_img_options=""
393 local optstr=""
394 local img_name=""
395 local use_backing=0
396 local backing_file=""
397 local object_options=""
398 local opts_param=false
399 local misc_params=()
401 if [[ $IMGPROTO == fuse && $TEST_IMG == $SOCK_DIR/fuse-* ]]; then
402 # The caller may be trying to overwrite an existing image
403 _rm_test_img "$TEST_IMG"
406 if [ -z "$TEST_IMG_FILE" ]; then
407 img_name=$TEST_IMG
408 elif [ "$IMGOPTSSYNTAX" != "true" -a \
409 "$TEST_IMG_FILE" = "$ORIG_TEST_IMG_FILE" ]; then
410 # Handle cases of tests only updating TEST_IMG, but not TEST_IMG_FILE
411 img_name=$(_test_img_to_test_img_file "$TEST_IMG")
412 if [ "$?" != 0 ]; then
413 img_name=$TEST_IMG_FILE
415 else
416 # $TEST_IMG_FILE is not the default value, so it definitely has been
417 # modified by the test
418 img_name=$TEST_IMG_FILE
421 if [ -n "$IMGOPTS" ]; then
422 imgopts_expanded=$(echo "$IMGOPTS" | sed -e "s#\\\$TEST_IMG#$img_name#")
423 optstr=$(_optstr_add "$optstr" "$imgopts_expanded")
425 if [ -n "$IMGKEYSECRET" ]; then
426 object_options="--object secret,id=keysec0,data=$IMGKEYSECRET"
427 optstr=$(_optstr_add "$optstr" "key-secret=keysec0")
430 for param; do
431 if [ "$use_backing" = "1" -a -z "$backing_file" ]; then
432 backing_file=$param
433 continue
434 elif $opts_param; then
435 optstr=$(_optstr_add "$optstr" "$param")
436 opts_param=false
437 continue
440 case "$param" in
442 use_backing=1
446 opts_param=true
449 --no-opts)
450 optstr=""
454 misc_params=("${misc_params[@]}" "$param")
456 esac
457 done
459 if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then
460 optstr=$(_optstr_add "$optstr" "cluster_size=$CLUSTER_SIZE")
463 if [ -n "$optstr" ]; then
464 extra_img_options="-o $optstr $extra_img_options"
467 if [ $IMGPROTO = "nbd" ]; then
468 _stop_nbd_server
471 # XXX(hch): have global image options?
473 if [ $use_backing = 1 ]; then
474 $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options -b "$backing_file" "$img_name" "${misc_params[@]}" 2>&1
475 else
476 $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options "$img_name" "${misc_params[@]}" 2>&1
478 ) | _filter_img_create
480 # Start an NBD server on the image file, which is what we'll be talking to.
481 # Once NBD gains resize support, we may also want to use -f raw at the
482 # server and interpret format over NBD, but for now, the format is
483 # interpreted at the server and raw data sent over NBD.
484 if [ $IMGPROTO = "nbd" ]; then
485 # Pass a sufficiently high number to -e that should be enough for all
486 # tests
487 eval "$QEMU_NBD -v -t -k '$SOCK_DIR/nbd' -f $IMGFMT -e 42 -x '' $TEST_IMG_FILE >/dev/null &"
488 sleep 1 # FIXME: qemu-nbd needs to be listening before we continue
491 if [ $IMGPROTO = "fuse" -a -f "$img_name" ]; then
492 local export_mp
493 local pid
494 local pidfile
495 local timeout
497 export_mp=$(echo "$img_name" | sed -e "s#$TEST_DIR/#$SOCK_DIR/fuse-#")
498 if ! echo "$export_mp" | grep -q "^$SOCK_DIR"; then
499 echo 'Cannot use FUSE exports with images outside of TEST_DIR' >&2
500 return 1
503 touch "$export_mp"
504 rm -f "$SOCK_DIR/fuse-output"
506 # Usually, users would export formatted nodes. But we present fuse as a
507 # protocol-level driver here, so we have to leave the format to the
508 # client.
509 QSD_NEED_PID=y $QSD \
510 --blockdev file,node-name=export-node,filename=$img_name,discard=unmap \
511 --export fuse,id=fuse-export,node-name=export-node,mountpoint="$export_mp",writable=on,growable=on \
514 pidfile="$QEMU_TEST_DIR/qemu-storage-daemon.pid"
516 # Wait for the PID file
517 while [ ! -f "$pidfile" ]; do
518 sleep 0.5
519 done
521 pid=$(cat "$pidfile")
522 rm -f "$pidfile"
524 FUSE_PIDS+=($pid)
525 FUSE_EXPORTS+=("$export_mp")
529 _rm_test_img()
531 local img=$1
533 if [[ $IMGPROTO == fuse && $img == $SOCK_DIR/fuse-* ]]; then
534 # Drop a FUSE export
535 local df_output
536 local i
537 local image_file
538 local index=''
539 local timeout
541 for i in "${!FUSE_EXPORTS[@]}"; do
542 if [ "${FUSE_EXPORTS[i]}" = "$img" ]; then
543 index=$i
544 break
546 done
548 if [ -z "$index" ]; then
549 # Probably gone already
550 return 0
553 kill "${FUSE_PIDS[index]}"
555 # Wait until the mount is gone
556 timeout=10 # *0.5 s
557 while true; do
558 # Will show the mount point; if the mount is still there,
559 # it will be $img.
560 df_output=$(df "$img" 2>/dev/null)
562 # But df may also show an error ("Transpoint endpoint not
563 # connected"), so retry in such cases
564 if [ -n "$df_output" ]; then
565 if ! echo "$df_output" | grep -q "$img"; then
566 break
570 sleep 0.5
572 timeout=$((timeout - 1))
573 if [ "$timeout" = 0 ]; then
574 echo 'Failed to take down FUSE export' >&2
575 return 1
577 done
579 rm -f "$img"
581 unset "FUSE_PIDS[$index]"
582 unset "FUSE_EXPORTS[$index]"
584 image_file=$(echo "$img" | sed -e "s#$SOCK_DIR/fuse-#$TEST_DIR/#")
585 _rm_test_img "$image_file"
586 return
589 if [ "$IMGFMT" = "vmdk" ]; then
590 # Remove all the extents for vmdk
591 "$QEMU_IMG" info "$img" 2>/dev/null | grep 'filename:' | cut -f 2 -d: \
592 | xargs -I {} rm -f "{}"
593 elif [ "$IMGFMT" = "qcow2" ]; then
594 # Remove external data file
595 if data_file=$(_get_data_file "$img"); then
596 rm -f "$data_file"
599 rm -f "$img"
602 _cleanup_test_img()
604 case "$IMGPROTO" in
606 nbd)
607 _stop_nbd_server
608 rm -f "$TEST_IMG_FILE"
611 fuse)
612 local mp
614 for mp in "${FUSE_EXPORTS[@]}"; do
615 _rm_test_img "$mp"
616 done
618 FUSE_PIDS=()
619 FUSE_EXPORTS=()
622 file)
623 _rm_test_img "$TEST_DIR/t.$IMGFMT"
624 _rm_test_img "$TEST_DIR/t.$IMGFMT.orig"
625 _rm_test_img "$TEST_DIR/t.$IMGFMT.base"
626 if [ -n "$SAMPLE_IMG_FILE" ]
627 then
628 rm -f "$TEST_DIR/$SAMPLE_IMG_FILE"
629 SAMPLE_IMG_FILE=
630 TEST_IMG="$ORIG_TEST_IMG"
634 rbd)
635 rbd --no-progress rm "$TEST_DIR/t.$IMGFMT" > /dev/null
638 sheepdog)
639 collie vdi delete "$TEST_DIR/t.$IMGFMT"
642 esac
645 _check_test_img()
648 if [ "$IMGOPTSSYNTAX" = "true" ]; then
649 $QEMU_IMG check $QEMU_IMG_EXTRA_ARGS "$@" "$TEST_IMG" 2>&1
650 else
651 $QEMU_IMG check "$@" -f $IMGFMT "$TEST_IMG" 2>&1
653 ) | _filter_testdir | _filter_qemu_img_check
655 # return real qemu_img check status, to analyze in
656 # _check_test_img_ignore_leaks
657 return ${PIPESTATUS[0]}
660 _check_test_img_ignore_leaks()
662 out=$(_check_test_img "$@")
663 status=$?
664 if [ $status = 3 ]; then
665 # This must correspond to success output in dump_human_image_check()
666 echo "No errors were found on the image."
667 return 0
669 echo "$out"
670 return $status
673 _img_info()
675 if [[ "$1" == "--format-specific" ]]; then
676 local format_specific=1
677 shift
678 else
679 local format_specific=0
682 discard=0
683 regex_json_spec_start='^ *"format-specific": \{'
684 $QEMU_IMG info $QEMU_IMG_EXTRA_ARGS "$@" "$TEST_IMG" 2>&1 | \
685 sed -e "s#$REMOTE_TEST_DIR#TEST_DIR#g" \
686 -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
687 -e "s#$TEST_DIR#TEST_DIR#g" \
688 -e "s#$SOCK_DIR/fuse-#TEST_DIR/#g" \
689 -e "s#$IMGFMT#IMGFMT#g" \
690 -e "/^disk size:/ D" \
691 -e "/actual-size/ D" | \
692 while IFS='' read -r line; do
693 if [[ $format_specific == 1 ]]; then
694 discard=0
695 elif [[ $line == "Format specific information:" ]]; then
696 discard=1
697 elif [[ $line =~ $regex_json_spec_start ]]; then
698 discard=2
699 regex_json_spec_end="^${line%%[^ ]*}\\},? *$"
701 if [[ $discard == 0 ]]; then
702 echo "$line"
703 elif [[ $discard == 1 && ! $line ]]; then
704 echo
705 discard=0
706 elif [[ $discard == 2 && $line =~ $regex_json_spec_end ]]; then
707 discard=0
709 done
712 # bail out, setting up .notrun file
714 _notrun()
716 echo "$*" >"$OUTPUT_DIR/$seq.notrun"
717 echo "$seq not run: $*"
718 status=0
719 exit
722 # bail out, setting up .casenotrun file
723 # The function _casenotrun() is used as a notifier. It is the
724 # caller's responsibility to make skipped a particular test.
726 _casenotrun()
728 echo " [case not run] $*" >>"$OUTPUT_DIR/$seq.casenotrun"
731 # just plain bail out
733 _fail()
735 echo "$*" | tee -a "$OUTPUT_DIR/$seq.full"
736 echo "(see $seq.full for details)"
737 status=1
738 exit 1
741 # tests whether $IMGFMT is one of the supported image formats for a test
743 _supported_fmt()
745 # "generic" is suitable for most image formats. For some formats it doesn't
746 # work, however (most notably read-only formats), so they can opt out by
747 # setting IMGFMT_GENERIC to false.
748 for f; do
749 if [ "$f" = "$IMGFMT" -o "$f" = "generic" -a "$IMGFMT_GENERIC" = "true" ]; then
750 if [ "$IMGFMT" = "luks" ]; then
751 _require_working_luks
753 return
755 done
757 _notrun "not suitable for this image format: $IMGFMT"
760 # tests whether $IMGFMT is one of the unsupported image format for a test
762 _unsupported_fmt()
764 for f; do
765 if [ "$f" = "$IMGFMT" ]; then
766 _notrun "not suitable for this image format: $IMGFMT"
768 done
771 # tests whether $IMGPROTO is one of the supported image protocols for a test
773 _supported_proto()
775 for f; do
776 if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then
777 return
779 done
781 _notrun "not suitable for this image protocol: $IMGPROTO"
784 # tests whether $IMGPROTO is specified as an unsupported image protocol for a test
786 _unsupported_proto()
788 for f; do
789 if [ "$f" = "$IMGPROTO" ]; then
790 _notrun "not suitable for this image protocol: $IMGPROTO"
791 return
793 done
796 # tests whether the host OS is one of the supported OSes for a test
798 _supported_os()
800 for h
802 if [ "$h" = "$HOSTOS" ]
803 then
804 return
806 done
808 _notrun "not suitable for this OS: $HOSTOS"
811 _supported_cache_modes()
813 for mode; do
814 if [ "$mode" = "$CACHEMODE" ]; then
815 return
817 done
818 _notrun "not suitable for cache mode: $CACHEMODE"
821 # Check whether the filesystem supports O_DIRECT
822 _check_o_direct()
824 $QEMU_IMG create -f raw "$TEST_IMG".test_o_direct 1M > /dev/null
825 out=$($QEMU_IO -f raw -t none -c quit "$TEST_IMG".test_o_direct 2>&1)
826 rm -f "$TEST_IMG".test_o_direct
828 [[ "$out" != *"O_DIRECT"* ]]
831 _require_o_direct()
833 if ! _check_o_direct; then
834 _notrun "file system on $TEST_DIR does not support O_DIRECT"
838 _check_cache_mode()
840 if [ $CACHEMODE == "none" ] || [ $CACHEMODE == "directsync" ]; then
841 _require_o_direct
845 _check_cache_mode
847 # $1 - cache mode to use by default
848 # $2 - (optional) cache mode to use by default if O_DIRECT is not supported
849 _default_cache_mode()
851 if $CACHEMODE_IS_DEFAULT; then
852 if [ -z "$2" ] || _check_o_direct; then
853 CACHEMODE="$1"
854 else
855 CACHEMODE="$2"
857 QEMU_IO="$QEMU_IO --cache $CACHEMODE"
858 _check_cache_mode
859 return
862 _supported_aio_modes()
864 for mode; do
865 if [ "$mode" = "$AIOMODE" ]; then
866 return
868 done
869 _notrun "not suitable for aio mode: $AIOMODE"
871 _default_aio_mode()
873 AIOMODE="$1"
874 QEMU_IO="$QEMU_IO --aio $1"
877 _unsupported_imgopts()
879 for bad_opt
881 if echo "$IMGOPTS" | grep -q 2>/dev/null "$bad_opt"
882 then
883 _notrun "not suitable for image option: $bad_opt"
885 done
888 # Caution: Overwrites $TEST_DIR/t.luks
889 _require_working_luks()
891 file="$TEST_DIR/t.luks"
893 output=$(
894 $QEMU_IMG create -f luks \
895 --object secret,id=sec0,data=hunter0 \
896 -o key-secret=sec0 \
897 -o iter-time=10 \
898 "$file" \
899 1M \
900 2>&1
902 status=$?
904 IMGFMT='luks' _rm_test_img "$file"
906 if [ $status != 0 ]; then
907 reason=$(echo "$output" | grep "$file:" | $SED -e "s#.*$file: *##")
908 if [ -z "$reason" ]; then
909 reason="Failed to create a LUKS image"
911 _notrun "$reason"
915 # this test requires that a specified command (executable) exists
917 _require_command()
919 if [ "$1" = "QEMU" ]; then
920 c=$QEMU_PROG
921 elif [ "$1" = "QEMU_IMG" ]; then
922 c=$QEMU_IMG_PROG
923 elif [ "$1" = "QEMU_IO" ]; then
924 c=$QEMU_IO_PROG
925 elif [ "$1" = "QEMU_NBD" ]; then
926 c=$QEMU_NBD_PROG
927 else
928 eval c=\$$1
930 [ -x "$c" ] || _notrun "$1 utility required, skipped this test"
933 # Check that a set of drivers has been whitelisted in the QEMU binary
935 _require_drivers()
937 available=$($QEMU -drive format=help | \
938 sed -e '/Supported formats:/!d' -e 's/Supported formats://')
939 for driver
941 if ! echo "$available" | grep -q " $driver\( \|$\)"; then
942 _notrun "$driver not available"
944 done
947 # Check that we have a file system that allows huge (but very sparse) files
949 _require_large_file()
951 if ! truncate --size="$1" "$TEST_IMG"; then
952 _notrun "file system on $TEST_DIR does not support large enough files"
954 rm "$TEST_IMG"
957 # Check that a set of devices is available in the QEMU binary
959 _require_devices()
961 available=$($QEMU -M none -device help | \
962 grep ^name | sed -e 's/^name "//' -e 's/".*$//')
963 for device
965 if ! echo "$available" | grep -q "$device" ; then
966 _notrun "$device not available"
968 done
971 # make sure this script returns success
972 true