xref: /openbmc/qemu/tests/qemu-iotests/common.rc (revision ed5abf46)
1#!/usr/bin/env bash
2#
3# Copyright (C) 2009 Red Hat, Inc.
4# Copyright (c) 2000-2006 Silicon Graphics, Inc.  All Rights Reserved.
5#
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.
10#
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.
15#
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/>.
18#
19
20SED=
21for 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
26    fi
27done
28if [ -z "$SED" ]; then
29    echo "$0: GNU sed not found"
30    exit 1
31fi
32
33dd()
34{
35   if [ "$HOSTOS" == "Linux" ]
36   then
37        command dd --help | grep noxfer > /dev/null 2>&1
38
39        if [ "$?" -eq 0 ]
40            then
41                command dd status=noxfer $@
42            else
43                command dd $@
44            fi
45   else
46        command dd $@
47   fi
48}
49
50# poke_file 'test.img' 512 '\xff\xfe'
51poke_file()
52{
53    printf "$3" | dd "of=$1" bs=1 "seek=$2" conv=notrunc &>/dev/null
54}
55
56# peek_file_le 'test.img' 512 2 => 65534
57peek_file_le()
58{
59    local val=0 shift=0 byte
60
61    # coreutils' od --endian is not portable, so manually assemble bytes.
62    for byte in $(od -j"$2" -N"$3" -An -v -tu1 "$1"); do
63        val=$(( val | (byte << shift) ))
64        shift=$((shift + 8))
65    done
66    printf %llu $val
67}
68
69# peek_file_be 'test.img' 512 2 => 65279
70peek_file_be()
71{
72    local val=0 byte
73
74    # coreutils' od --endian is not portable, so manually assemble bytes.
75    for byte in $(od -j"$2" -N"$3" -An -v -tu1 "$1"); do
76        val=$(( (val << 8) | byte ))
77    done
78    printf %llu $val
79}
80
81# peek_file_raw 'test.img' 512 2 => '\xff\xfe'. Do not use if the raw data
82# is likely to contain \0 or trailing \n.
83peek_file_raw()
84{
85    dd if="$1" bs=1 skip="$2" count="$3" status=none
86}
87
88
89if ! . ./common.config
90    then
91    echo "$0: failed to source common.config"
92    exit 1
93fi
94
95# Set the variables to the empty string to turn Valgrind off
96# for specific processes, e.g.
97# $ VALGRIND_QEMU_IO= ./check -qcow2 -valgrind 015
98
99: ${VALGRIND_QEMU_VM=$VALGRIND_QEMU}
100: ${VALGRIND_QEMU_IMG=$VALGRIND_QEMU}
101: ${VALGRIND_QEMU_IO=$VALGRIND_QEMU}
102: ${VALGRIND_QEMU_NBD=$VALGRIND_QEMU}
103: ${VALGRIND_QEMU_VXHS=$VALGRIND_QEMU}
104
105# The Valgrind own parameters may be set with
106# its environment variable VALGRIND_OPTS, e.g.
107# $ VALGRIND_OPTS="--leak-check=yes" ./check -qcow2 -valgrind 015
108
109_qemu_proc_exec()
110{
111    local VALGRIND_LOGFILE="$1"
112    shift
113    if [[ "${VALGRIND_QEMU}" == "y" && "${NO_VALGRIND}" != "y" ]]; then
114        exec valgrind --log-file="${VALGRIND_LOGFILE}" --error-exitcode=99 "$@"
115    else
116        exec "$@"
117    fi
118}
119
120_qemu_proc_valgrind_log()
121{
122    local VALGRIND_LOGFILE="$1"
123    local RETVAL="$2"
124    if [[ "${VALGRIND_QEMU}" == "y" && "${NO_VALGRIND}" != "y" ]]; then
125        if [ $RETVAL == 99 ]; then
126            cat "${VALGRIND_LOGFILE}"
127        fi
128        rm -f "${VALGRIND_LOGFILE}"
129    fi
130}
131
132_qemu_wrapper()
133{
134    local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
135    (
136        if [ -n "${QEMU_NEED_PID}" ]; then
137            echo $BASHPID > "${QEMU_TEST_DIR}/qemu-${_QEMU_HANDLE}.pid"
138        fi
139        VALGRIND_QEMU="${VALGRIND_QEMU_VM}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
140            "$QEMU_PROG" $QEMU_OPTIONS "$@"
141    )
142    RETVAL=$?
143    _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
144    return $RETVAL
145}
146
147_qemu_img_wrapper()
148{
149    local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
150    (
151        VALGRIND_QEMU="${VALGRIND_QEMU_IMG}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
152            "$QEMU_IMG_PROG" $QEMU_IMG_OPTIONS "$@"
153    )
154    RETVAL=$?
155    _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
156    return $RETVAL
157}
158
159_qemu_io_wrapper()
160{
161    local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
162    local QEMU_IO_ARGS="$QEMU_IO_OPTIONS"
163    if [ "$IMGOPTSSYNTAX" = "true" ]; then
164        QEMU_IO_ARGS="--image-opts $QEMU_IO_ARGS"
165        if [ -n "$IMGKEYSECRET" ]; then
166            QEMU_IO_ARGS="--object secret,id=keysec0,data=$IMGKEYSECRET $QEMU_IO_ARGS"
167        fi
168    fi
169    (
170        VALGRIND_QEMU="${VALGRIND_QEMU_IO}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
171            "$QEMU_IO_PROG" $QEMU_IO_ARGS "$@"
172    )
173    RETVAL=$?
174    _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
175    return $RETVAL
176}
177
178_qemu_nbd_wrapper()
179{
180    local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
181    (
182        VALGRIND_QEMU="${VALGRIND_QEMU_NBD}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
183            "$QEMU_NBD_PROG" --pid-file="${QEMU_TEST_DIR}/qemu-nbd.pid" \
184             $QEMU_NBD_OPTIONS "$@"
185    )
186    RETVAL=$?
187    _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
188    return $RETVAL
189}
190
191_qemu_vxhs_wrapper()
192{
193    local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
194    (
195        echo $BASHPID > "${TEST_DIR}/qemu-vxhs.pid"
196        VALGRIND_QEMU="${VALGRIND_QEMU_VXHS}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
197            "$QEMU_VXHS_PROG" $QEMU_VXHS_OPTIONS "$@"
198    )
199    RETVAL=$?
200    _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
201    return $RETVAL
202}
203
204# Valgrind bug #409141 https://bugs.kde.org/show_bug.cgi?id=409141
205# Until valgrind 3.16+ is ubiquitous, we must work around a hang in
206# valgrind when issuing sigkill. Disable valgrind for this invocation.
207_NO_VALGRIND()
208{
209    NO_VALGRIND="y" "$@"
210}
211
212export QEMU=_qemu_wrapper
213export QEMU_IMG=_qemu_img_wrapper
214export QEMU_IO=_qemu_io_wrapper
215export QEMU_NBD=_qemu_nbd_wrapper
216export QEMU_VXHS=_qemu_vxhs_wrapper
217
218if [ "$IMGOPTSSYNTAX" = "true" ]; then
219    DRIVER="driver=$IMGFMT"
220    QEMU_IMG_EXTRA_ARGS="--image-opts $QEMU_IMG_EXTRA_ARGS"
221    if [ -n "$IMGKEYSECRET" ]; then
222        QEMU_IMG_EXTRA_ARGS="--object secret,id=keysec0,data=$IMGKEYSECRET $QEMU_IMG_EXTRA_ARGS"
223    fi
224    if [ "$IMGFMT" = "luks" ]; then
225        DRIVER="$DRIVER,key-secret=keysec0"
226    fi
227    if [ "$IMGPROTO" = "file" ]; then
228        TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
229        TEST_IMG="$DRIVER,file.filename=$TEST_DIR/t.$IMGFMT"
230    elif [ "$IMGPROTO" = "nbd" ]; then
231        TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
232        TEST_IMG="$DRIVER,file.driver=nbd,file.type=unix"
233        TEST_IMG="$TEST_IMG,file.path=$SOCK_DIR/nbd"
234    elif [ "$IMGPROTO" = "ssh" ]; then
235        TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
236        TEST_IMG="$DRIVER,file.driver=ssh,file.host=127.0.0.1,file.path=$TEST_IMG_FILE"
237    elif [ "$IMGPROTO" = "nfs" ]; then
238        TEST_DIR="$DRIVER,file.driver=nfs,file.filename=nfs://127.0.0.1/$TEST_DIR"
239        TEST_IMG=$TEST_DIR/t.$IMGFMT
240    else
241        TEST_IMG="$DRIVER,file.driver=$IMGPROTO,file.filename=$TEST_DIR/t.$IMGFMT"
242    fi
243else
244    QEMU_IMG_EXTRA_ARGS=
245    if [ "$IMGPROTO" = "file" ]; then
246        TEST_IMG=$TEST_DIR/t.$IMGFMT
247    elif [ "$IMGPROTO" = "nbd" ]; then
248        TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
249        TEST_IMG="nbd+unix:///?socket=$SOCK_DIR/nbd"
250    elif [ "$IMGPROTO" = "ssh" ]; then
251        TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
252        REMOTE_TEST_DIR="ssh://\\($USER@\\)\\?127.0.0.1\\(:[0-9]\\+\\)\\?$TEST_DIR"
253        TEST_IMG="ssh://127.0.0.1$TEST_IMG_FILE"
254    elif [ "$IMGPROTO" = "nfs" ]; then
255        TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
256        REMOTE_TEST_DIR="nfs://127.0.0.1$TEST_DIR"
257        TEST_IMG="nfs://127.0.0.1$TEST_IMG_FILE"
258    elif [ "$IMGPROTO" = "vxhs" ]; then
259        TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
260        TEST_IMG="vxhs://127.0.0.1:9999/t.$IMGFMT"
261    else
262        TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT
263    fi
264fi
265ORIG_TEST_IMG="$TEST_IMG"
266
267if [ -z "$TEST_DIR" ]; then
268        TEST_DIR=$PWD/scratch
269fi
270
271QEMU_TEST_DIR="${TEST_DIR}"
272
273if [ ! -e "$TEST_DIR" ]; then
274        mkdir "$TEST_DIR"
275fi
276
277if [ ! -d "$TEST_DIR" ]; then
278    echo "common.rc: Error: \$TEST_DIR ($TEST_DIR) is not a directory"
279    exit 1
280fi
281
282if [ -z "$REMOTE_TEST_DIR" ]; then
283    REMOTE_TEST_DIR="$TEST_DIR"
284fi
285
286if [ ! -d "$SAMPLE_IMG_DIR" ]; then
287    echo "common.rc: Error: \$SAMPLE_IMG_DIR ($SAMPLE_IMG_DIR) is not a directory"
288    exit 1
289fi
290
291_use_sample_img()
292{
293    SAMPLE_IMG_FILE="${1%\.bz2}"
294    TEST_IMG="$TEST_DIR/$SAMPLE_IMG_FILE"
295    bzcat "$SAMPLE_IMG_DIR/$1" > "$TEST_IMG"
296    if [ $? -ne 0 ]
297    then
298        echo "_use_sample_img error, cannot extract '$SAMPLE_IMG_DIR/$1'"
299        exit 1
300    fi
301}
302
303_stop_nbd_server()
304{
305    if [ -f "${QEMU_TEST_DIR}/qemu-nbd.pid" ]; then
306        local QEMU_NBD_PID
307        read QEMU_NBD_PID < "${QEMU_TEST_DIR}/qemu-nbd.pid"
308        kill ${QEMU_NBD_PID}
309        rm -f "${QEMU_TEST_DIR}/qemu-nbd.pid" "$SOCK_DIR/nbd"
310    fi
311}
312
313# Gets the data_file value from IMGOPTS and replaces the '$TEST_IMG'
314# pattern by '$1'
315# Caution: The replacement is done with sed, so $1 must be escaped
316#          properly.  (The delimiter is '#'.)
317_get_data_file()
318{
319    if ! echo "$IMGOPTS" | grep -q 'data_file='; then
320        return 1
321    fi
322
323    echo "$IMGOPTS" | sed -e 's/.*data_file=\([^,]*\).*/\1/' \
324                    | sed -e "s#\\\$TEST_IMG#$1#"
325}
326
327_make_test_img()
328{
329    # extra qemu-img options can be added by tests
330    # at least one argument (the image size) needs to be added
331    local extra_img_options=""
332    local optstr=""
333    local img_name=""
334    local use_backing=0
335    local backing_file=""
336    local object_options=""
337    local opts_param=false
338    local misc_params=()
339
340    if [ -n "$TEST_IMG_FILE" ]; then
341        img_name=$TEST_IMG_FILE
342    else
343        img_name=$TEST_IMG
344    fi
345
346    if [ -n "$IMGOPTS" ]; then
347        imgopts_expanded=$(echo "$IMGOPTS" | sed -e "s#\\\$TEST_IMG#$img_name#")
348        optstr=$(_optstr_add "$optstr" "$imgopts_expanded")
349    fi
350    if [ -n "$IMGKEYSECRET" ]; then
351        object_options="--object secret,id=keysec0,data=$IMGKEYSECRET"
352        optstr=$(_optstr_add "$optstr" "key-secret=keysec0")
353    fi
354
355    for param; do
356        if [ "$use_backing" = "1" -a -z "$backing_file" ]; then
357            backing_file=$param
358            continue
359        elif $opts_param; then
360            optstr=$(_optstr_add "$optstr" "$param")
361            opts_param=false
362            continue
363        fi
364
365        case "$param" in
366            -b)
367                use_backing=1
368                ;;
369
370            -o)
371                opts_param=true
372                ;;
373
374            --no-opts)
375                optstr=""
376                ;;
377
378            *)
379                misc_params=("${misc_params[@]}" "$param")
380                ;;
381        esac
382    done
383
384    if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then
385        optstr=$(_optstr_add "$optstr" "cluster_size=$CLUSTER_SIZE")
386    fi
387
388    if [ -n "$optstr" ]; then
389        extra_img_options="-o $optstr $extra_img_options"
390    fi
391
392    if [ $IMGPROTO = "nbd" ]; then
393        _stop_nbd_server
394    fi
395
396    # XXX(hch): have global image options?
397    (
398     if [ $use_backing = 1 ]; then
399        $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options -b "$backing_file" "$img_name" "${misc_params[@]}" 2>&1
400     else
401        $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options "$img_name" "${misc_params[@]}" 2>&1
402     fi
403    ) | _filter_img_create
404
405    # Start an NBD server on the image file, which is what we'll be talking to.
406    # Once NBD gains resize support, we may also want to use -f raw at the
407    # server and interpret format over NBD, but for now, the format is
408    # interpreted at the server and raw data sent over NBD.
409    if [ $IMGPROTO = "nbd" ]; then
410        # Pass a sufficiently high number to -e that should be enough for all
411        # tests
412        eval "$QEMU_NBD -v -t -k '$SOCK_DIR/nbd' -f $IMGFMT -e 42 -x '' $TEST_IMG_FILE >/dev/null &"
413        sleep 1 # FIXME: qemu-nbd needs to be listening before we continue
414    fi
415
416    # Start QNIO server on image directory for vxhs protocol
417    if [ $IMGPROTO = "vxhs" ]; then
418        eval "$QEMU_VXHS -d  $TEST_DIR > /dev/null &"
419        sleep 1 # Wait for server to come up.
420    fi
421}
422
423_rm_test_img()
424{
425    local img=$1
426    if [ "$IMGFMT" = "vmdk" ]; then
427        # Remove all the extents for vmdk
428        "$QEMU_IMG" info "$img" 2>/dev/null | grep 'filename:' | cut -f 2 -d: \
429            | xargs -I {} rm -f "{}"
430    elif [ "$IMGFMT" = "qcow2" ]; then
431        # Remove external data file
432        if data_file=$(_get_data_file "$img"); then
433            rm -f "$data_file"
434        fi
435    fi
436    rm -f "$img"
437}
438
439_cleanup_test_img()
440{
441    case "$IMGPROTO" in
442
443        nbd)
444            _stop_nbd_server
445            rm -f "$TEST_IMG_FILE"
446            ;;
447        vxhs)
448            if [ -f "${TEST_DIR}/qemu-vxhs.pid" ]; then
449                local QEMU_VXHS_PID
450                read QEMU_VXHS_PID < "${TEST_DIR}/qemu-vxhs.pid"
451                kill ${QEMU_VXHS_PID} >/dev/null 2>&1
452                rm -f "${TEST_DIR}/qemu-vxhs.pid"
453            fi
454            rm -f "$TEST_IMG_FILE"
455            ;;
456
457        file)
458            _rm_test_img "$TEST_DIR/t.$IMGFMT"
459            _rm_test_img "$TEST_DIR/t.$IMGFMT.orig"
460            _rm_test_img "$TEST_DIR/t.$IMGFMT.base"
461            if [ -n "$SAMPLE_IMG_FILE" ]
462            then
463                rm -f "$TEST_DIR/$SAMPLE_IMG_FILE"
464                SAMPLE_IMG_FILE=
465                TEST_IMG="$ORIG_TEST_IMG"
466            fi
467            ;;
468
469        rbd)
470            rbd --no-progress rm "$TEST_DIR/t.$IMGFMT" > /dev/null
471            ;;
472
473        sheepdog)
474            collie vdi delete "$TEST_DIR/t.$IMGFMT"
475            ;;
476
477    esac
478}
479
480_check_test_img()
481{
482    (
483        if [ "$IMGOPTSSYNTAX" = "true" ]; then
484            $QEMU_IMG check $QEMU_IMG_EXTRA_ARGS "$@" "$TEST_IMG" 2>&1
485        else
486            $QEMU_IMG check "$@" -f $IMGFMT "$TEST_IMG" 2>&1
487        fi
488    ) | _filter_testdir | _filter_qemu_img_check
489
490    # return real qemu_img check status, to analyze in
491    # _check_test_img_ignore_leaks
492    return ${PIPESTATUS[0]}
493}
494
495_check_test_img_ignore_leaks()
496{
497    out=$(_check_test_img "$@")
498    status=$?
499    if [ $status = 3 ]; then
500        # This must correspond to success output in dump_human_image_check()
501        echo "No errors were found on the image."
502        return 0
503    fi
504    echo "$out"
505    return $status
506}
507
508_img_info()
509{
510    if [[ "$1" == "--format-specific" ]]; then
511        local format_specific=1
512        shift
513    else
514        local format_specific=0
515    fi
516
517    discard=0
518    regex_json_spec_start='^ *"format-specific": \{'
519    $QEMU_IMG info $QEMU_IMG_EXTRA_ARGS "$@" "$TEST_IMG" 2>&1 | \
520        sed -e "s#$REMOTE_TEST_DIR#TEST_DIR#g" \
521            -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
522            -e "s#$TEST_DIR#TEST_DIR#g" \
523            -e "s#$IMGFMT#IMGFMT#g" \
524            -e "/^disk size:/ D" \
525            -e "/actual-size/ D" | \
526        while IFS='' read -r line; do
527            if [[ $format_specific == 1 ]]; then
528                discard=0
529            elif [[ $line == "Format specific information:" ]]; then
530                discard=1
531            elif [[ $line =~ $regex_json_spec_start ]]; then
532                discard=2
533                regex_json_spec_end="^${line%%[^ ]*}\\},? *$"
534            fi
535            if [[ $discard == 0 ]]; then
536                echo "$line"
537            elif [[ $discard == 1 && ! $line ]]; then
538                echo
539                discard=0
540            elif [[ $discard == 2 && $line =~ $regex_json_spec_end ]]; then
541                discard=0
542            fi
543        done
544}
545
546# bail out, setting up .notrun file
547#
548_notrun()
549{
550    echo "$*" >"$OUTPUT_DIR/$seq.notrun"
551    echo "$seq not run: $*"
552    status=0
553    exit
554}
555
556# bail out, setting up .casenotrun file
557# The function _casenotrun() is used as a notifier. It is the
558# caller's responsibility to make skipped a particular test.
559#
560_casenotrun()
561{
562    echo "    [case not run] $*" >>"$OUTPUT_DIR/$seq.casenotrun"
563}
564
565# just plain bail out
566#
567_fail()
568{
569    echo "$*" | tee -a "$OUTPUT_DIR/$seq.full"
570    echo "(see $seq.full for details)"
571    status=1
572    exit 1
573}
574
575# tests whether $IMGFMT is one of the supported image formats for a test
576#
577_supported_fmt()
578{
579    # "generic" is suitable for most image formats. For some formats it doesn't
580    # work, however (most notably read-only formats), so they can opt out by
581    # setting IMGFMT_GENERIC to false.
582    for f; do
583        if [ "$f" = "$IMGFMT" -o "$f" = "generic" -a "$IMGFMT_GENERIC" = "true" ]; then
584            return
585        fi
586    done
587
588    _notrun "not suitable for this image format: $IMGFMT"
589}
590
591# tests whether $IMGFMT is one of the unsupported image format for a test
592#
593_unsupported_fmt()
594{
595    for f; do
596        if [ "$f" = "$IMGFMT" ]; then
597            _notrun "not suitable for this image format: $IMGFMT"
598        fi
599    done
600}
601
602# tests whether $IMGPROTO is one of the supported image protocols for a test
603#
604_supported_proto()
605{
606    for f; do
607        if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then
608            return
609        fi
610    done
611
612    _notrun "not suitable for this image protocol: $IMGPROTO"
613}
614
615# tests whether $IMGPROTO is specified as an unsupported image protocol for a test
616#
617_unsupported_proto()
618{
619    for f; do
620        if [ "$f" = "$IMGPROTO" ]; then
621            _notrun "not suitable for this image protocol: $IMGPROTO"
622            return
623        fi
624    done
625}
626
627# tests whether the host OS is one of the supported OSes for a test
628#
629_supported_os()
630{
631    for h
632    do
633        if [ "$h" = "$HOSTOS" ]
634        then
635            return
636        fi
637    done
638
639    _notrun "not suitable for this OS: $HOSTOS"
640}
641
642_supported_cache_modes()
643{
644    for mode; do
645        if [ "$mode" = "$CACHEMODE" ]; then
646            return
647        fi
648    done
649    _notrun "not suitable for cache mode: $CACHEMODE"
650}
651
652_default_cache_mode()
653{
654    if $CACHEMODE_IS_DEFAULT; then
655        CACHEMODE="$1"
656        QEMU_IO="$QEMU_IO --cache $1"
657        return
658    fi
659}
660_supported_aio_modes()
661{
662    for mode; do
663        if [ "$mode" = "$AIOMODE" ]; then
664            return
665        fi
666    done
667    _notrun "not suitable for aio mode: $AIOMODE"
668}
669_default_aio_mode()
670{
671    AIOMODE="$1"
672    QEMU_IO="$QEMU_IO --aio $1"
673}
674
675_unsupported_imgopts()
676{
677    for bad_opt
678    do
679        if echo "$IMGOPTS" | grep -q 2>/dev/null "$bad_opt"
680        then
681            _notrun "not suitable for image option: $bad_opt"
682        fi
683    done
684}
685
686# this test requires that a specified command (executable) exists
687#
688_require_command()
689{
690    if [ "$1" = "QEMU" ]; then
691        c=$QEMU_PROG
692    elif [ "$1" = "QEMU_IMG" ]; then
693        c=$QEMU_IMG_PROG
694    elif [ "$1" = "QEMU_IO" ]; then
695        c=$QEMU_IO_PROG
696    elif [ "$1" = "QEMU_NBD" ]; then
697        c=$QEMU_NBD_PROG
698    else
699        eval c=\$$1
700    fi
701    [ -x "$c" ] || _notrun "$1 utility required, skipped this test"
702}
703
704# Check that a set of drivers has been whitelisted in the QEMU binary
705#
706_require_drivers()
707{
708    available=$($QEMU -drive format=help | \
709                sed -e '/Supported formats:/!d' -e 's/Supported formats://')
710    for driver
711    do
712        if ! echo "$available" | grep -q " $driver\( \|$\)"; then
713            _notrun "$driver not available"
714        fi
715    done
716}
717
718# Check that we have a file system that allows huge (but very sparse) files
719#
720_require_large_file()
721{
722    if ! truncate --size="$1" "$TEST_IMG"; then
723        _notrun "file system on $TEST_DIR does not support large enough files"
724    fi
725    rm "$TEST_IMG"
726}
727
728# Check that a set of devices is available in the QEMU binary
729#
730_require_devices()
731{
732    available=$($QEMU -M none -device help | \
733                grep ^name | sed -e 's/^name "//' -e 's/".*$//')
734    for device
735    do
736        if ! echo "$available" | grep -q "$device" ; then
737            _notrun "$device not available"
738        fi
739    done
740}
741
742# make sure this script returns success
743true
744