xref: /openbmc/qemu/tests/qemu-iotests/common.rc (revision 1da79ecc)
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# poke_file_le $img_filename $offset $byte_width $value
57# Example: poke_file_le "$TEST_IMG" 512 2 65534
58poke_file_le()
59{
60    local img=$1 ofs=$2 len=$3 val=$4 str=''
61
62    while ((len--)); do
63        str+=$(printf '\\x%02x' $((val & 0xff)))
64        val=$((val >> 8))
65    done
66
67    poke_file "$img" "$ofs" "$str"
68}
69
70# poke_file_be $img_filename $offset $byte_width $value
71# Example: poke_file_be "$TEST_IMG" 512 2 65279
72poke_file_be()
73{
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')
76
77    poke_file "$img" "$ofs" "$str"
78}
79
80# peek_file_le 'test.img' 512 2 => 65534
81peek_file_le()
82{
83    local val=0 shift=0 byte
84
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
91}
92
93# peek_file_be 'test.img' 512 2 => 65279
94peek_file_be()
95{
96    local val=0 byte
97
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
103}
104
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.
107peek_file_raw()
108{
109    dd if="$1" bs=1 skip="$2" count="$3" status=none
110}
111
112
113if ! . ./common.config
114    then
115    echo "$0: failed to source common.config"
116    exit 1
117fi
118
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
122
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}
128
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
132
133_qemu_proc_exec()
134{
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 "$@"
141    fi
142}
143
144_qemu_proc_valgrind_log()
145{
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}"
151        fi
152        rm -f "${VALGRIND_LOGFILE}"
153    fi
154}
155
156_qemu_wrapper()
157{
158    local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
159    (
160        if [ -n "${QEMU_NEED_PID}" ]; then
161            echo $BASHPID > "${QEMU_TEST_DIR}/qemu-${_QEMU_HANDLE}.pid"
162        fi
163        VALGRIND_QEMU="${VALGRIND_QEMU_VM}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
164            "$QEMU_PROG" $QEMU_OPTIONS "$@"
165    )
166    RETVAL=$?
167    _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
168    return $RETVAL
169}
170
171_qemu_img_wrapper()
172{
173    local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
174    (
175        VALGRIND_QEMU="${VALGRIND_QEMU_IMG}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
176            "$QEMU_IMG_PROG" $QEMU_IMG_OPTIONS "$@"
177    )
178    RETVAL=$?
179    _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
180    return $RETVAL
181}
182
183_qemu_io_wrapper()
184{
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"
191        fi
192    fi
193    (
194        VALGRIND_QEMU="${VALGRIND_QEMU_IO}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
195            "$QEMU_IO_PROG" $QEMU_IO_ARGS "$@"
196    )
197    RETVAL=$?
198    _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
199    return $RETVAL
200}
201
202_qemu_nbd_wrapper()
203{
204    local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
205    (
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 "$@"
209    )
210    RETVAL=$?
211    _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
212    return $RETVAL
213}
214
215_qemu_storage_daemon_wrapper()
216{
217    local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
218    (
219        if [ -n "${QSD_NEED_PID}" ]; then
220            echo $BASHPID > "${QEMU_TEST_DIR}/qemu-storage-daemon.pid"
221        fi
222        VALGRIND_QEMU="${VALGRIND_QSD}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
223            "$QSD_PROG" $QSD_OPTIONS "$@"
224    )
225    RETVAL=$?
226    _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
227    return $RETVAL
228}
229
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()
234{
235    NO_VALGRIND="y" "$@"
236}
237
238export QEMU=_qemu_wrapper
239export QEMU_IMG=_qemu_img_wrapper
240export QEMU_IO=_qemu_io_wrapper
241export QEMU_NBD=_qemu_nbd_wrapper
242export QSD=_qemu_storage_daemon_wrapper
243
244if [ "$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"
249    fi
250    if [ "$IMGFMT" = "luks" ]; then
251        DRIVER="$DRIVER,key-secret=keysec0"
252    fi
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"
271    fi
272else
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
292    fi
293fi
294ORIG_TEST_IMG_FILE=$TEST_IMG_FILE
295ORIG_TEST_IMG="$TEST_IMG"
296
297FUSE_PIDS=()
298FUSE_EXPORTS=()
299
300if [ -z "$TEST_DIR" ]; then
301        TEST_DIR=$PWD/scratch
302fi
303
304QEMU_TEST_DIR="${TEST_DIR}"
305
306if [ ! -e "$TEST_DIR" ]; then
307        mkdir "$TEST_DIR"
308fi
309
310if [ ! -d "$TEST_DIR" ]; then
311    echo "common.rc: Error: \$TEST_DIR ($TEST_DIR) is not a directory"
312    exit 1
313fi
314
315if [ -z "$REMOTE_TEST_DIR" ]; then
316    REMOTE_TEST_DIR="$TEST_DIR"
317fi
318
319if [ ! -d "$SAMPLE_IMG_DIR" ]; then
320    echo "common.rc: Error: \$SAMPLE_IMG_DIR ($SAMPLE_IMG_DIR) is not a directory"
321    exit 1
322fi
323
324_use_sample_img()
325{
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
333    fi
334}
335
336_stop_nbd_server()
337{
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"
343    fi
344}
345
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()
351{
352    if ! echo "$IMGOPTS" | grep -q 'data_file='; then
353        return 1
354    fi
355
356    echo "$IMGOPTS" | sed -e 's/.*data_file=\([^,]*\).*/\1/' \
357                    | sed -e "s#\\\$TEST_IMG#$1#"
358}
359
360# Translate a $TEST_IMG to its corresponding $TEST_IMG_FILE for
361# different protocols
362_test_img_to_test_img_file()
363{
364    case "$IMGPROTO" in
365        file)
366            echo "$1"
367            ;;
368
369        fuse)
370            echo "$1" | sed -e "s#$SOCK_DIR/fuse-#$TEST_DIR/#"
371            ;;
372
373        nfs)
374            echo "$1" | sed -e "s#nfs://127.0.0.1##"
375            ;;
376
377        ssh)
378            echo "$1" | \
379                sed -e "s#ssh://\\($USER@\\)\\?127.0.0.1\\(:[0-9]\\+\\)\\?##"
380            ;;
381
382        *)
383            return 1
384            ;;
385    esac
386}
387
388_make_test_img()
389{
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=()
400
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"
404    fi
405
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
414        fi
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
419    fi
420
421    if [ -n "$IMGOPTS" ]; then
422        imgopts_expanded=$(echo "$IMGOPTS" | sed -e "s#\\\$TEST_IMG#$img_name#")
423        optstr=$(_optstr_add "$optstr" "$imgopts_expanded")
424    fi
425    if [ -n "$IMGKEYSECRET" ]; then
426        object_options="--object secret,id=keysec0,data=$IMGKEYSECRET"
427        optstr=$(_optstr_add "$optstr" "key-secret=keysec0")
428    fi
429
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
438        fi
439
440        case "$param" in
441            -b)
442                use_backing=1
443                ;;
444
445            -o)
446                opts_param=true
447                ;;
448
449            --no-opts)
450                optstr=""
451                ;;
452
453            *)
454                misc_params=("${misc_params[@]}" "$param")
455                ;;
456        esac
457    done
458
459    if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then
460        optstr=$(_optstr_add "$optstr" "cluster_size=$CLUSTER_SIZE")
461    fi
462
463    if [ -n "$optstr" ]; then
464        extra_img_options="-o $optstr $extra_img_options"
465    fi
466
467    if [ $IMGPROTO = "nbd" ]; then
468        _stop_nbd_server
469    fi
470
471    # XXX(hch): have global image options?
472    (
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
477     fi
478    ) | _filter_img_create
479
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
489    fi
490
491    if [ $IMGPROTO = "fuse" -a -f "$img_name" ]; then
492        local export_mp
493        local pid
494        local pidfile
495        local timeout
496
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
501        fi
502
503        touch "$export_mp"
504        rm -f "$SOCK_DIR/fuse-output"
505
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 \
512              &
513
514        pidfile="$QEMU_TEST_DIR/qemu-storage-daemon.pid"
515
516        # Wait for the PID file
517        while [ ! -f "$pidfile" ]; do
518            sleep 0.5
519        done
520
521        pid=$(cat "$pidfile")
522        rm -f "$pidfile"
523
524        FUSE_PIDS+=($pid)
525        FUSE_EXPORTS+=("$export_mp")
526    fi
527}
528
529_rm_test_img()
530{
531    local img=$1
532
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
540
541        for i in "${!FUSE_EXPORTS[@]}"; do
542            if [ "${FUSE_EXPORTS[i]}" = "$img" ]; then
543                index=$i
544                break
545            fi
546        done
547
548        if [ -z "$index" ]; then
549            # Probably gone already
550            return 0
551        fi
552
553        kill "${FUSE_PIDS[index]}"
554
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)
561
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
567                fi
568            fi
569
570            sleep 0.5
571
572            timeout=$((timeout - 1))
573            if [ "$timeout" = 0 ]; then
574                echo 'Failed to take down FUSE export' >&2
575                return 1
576            fi
577        done
578
579        rm -f "$img"
580
581        unset "FUSE_PIDS[$index]"
582        unset "FUSE_EXPORTS[$index]"
583
584        image_file=$(echo "$img" | sed -e "s#$SOCK_DIR/fuse-#$TEST_DIR/#")
585        _rm_test_img "$image_file"
586        return
587    fi
588
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"
597        fi
598    fi
599    rm -f "$img"
600}
601
602_cleanup_test_img()
603{
604    case "$IMGPROTO" in
605
606        nbd)
607            _stop_nbd_server
608            rm -f "$TEST_IMG_FILE"
609            ;;
610
611        fuse)
612            local mp
613
614            for mp in "${FUSE_EXPORTS[@]}"; do
615                _rm_test_img "$mp"
616            done
617
618            FUSE_PIDS=()
619            FUSE_EXPORTS=()
620            ;;
621
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"
631            fi
632            ;;
633
634        rbd)
635            rbd --no-progress rm "$TEST_DIR/t.$IMGFMT" > /dev/null
636            ;;
637
638        sheepdog)
639            collie vdi delete "$TEST_DIR/t.$IMGFMT"
640            ;;
641
642    esac
643}
644
645_check_test_img()
646{
647    (
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
652        fi
653    ) | _filter_testdir | _filter_qemu_img_check
654
655    # return real qemu_img check status, to analyze in
656    # _check_test_img_ignore_leaks
657    return ${PIPESTATUS[0]}
658}
659
660_check_test_img_ignore_leaks()
661{
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
668    fi
669    echo "$out"
670    return $status
671}
672
673_img_info()
674{
675    if [[ "$1" == "--format-specific" ]]; then
676        local format_specific=1
677        shift
678    else
679        local format_specific=0
680    fi
681
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%%[^ ]*}\\},? *$"
700            fi
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
708            fi
709        done
710}
711
712# bail out, setting up .notrun file
713#
714_notrun()
715{
716    echo "$*" >"$OUTPUT_DIR/$seq.notrun"
717    echo "$seq not run: $*"
718    status=0
719    exit
720}
721
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.
725#
726_casenotrun()
727{
728    echo "    [case not run] $*" >>"$OUTPUT_DIR/$seq.casenotrun"
729}
730
731# just plain bail out
732#
733_fail()
734{
735    echo "$*" | tee -a "$OUTPUT_DIR/$seq.full"
736    echo "(see $seq.full for details)"
737    status=1
738    exit 1
739}
740
741# tests whether $IMGFMT is one of the supported image formats for a test
742#
743_supported_fmt()
744{
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
752            fi
753            return
754        fi
755    done
756
757    _notrun "not suitable for this image format: $IMGFMT"
758}
759
760# tests whether $IMGFMT is one of the unsupported image format for a test
761#
762_unsupported_fmt()
763{
764    for f; do
765        if [ "$f" = "$IMGFMT" ]; then
766            _notrun "not suitable for this image format: $IMGFMT"
767        fi
768    done
769}
770
771# tests whether $IMGPROTO is one of the supported image protocols for a test
772#
773_supported_proto()
774{
775    for f; do
776        if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then
777            return
778        fi
779    done
780
781    _notrun "not suitable for this image protocol: $IMGPROTO"
782}
783
784# tests whether $IMGPROTO is specified as an unsupported image protocol for a test
785#
786_unsupported_proto()
787{
788    for f; do
789        if [ "$f" = "$IMGPROTO" ]; then
790            _notrun "not suitable for this image protocol: $IMGPROTO"
791            return
792        fi
793    done
794}
795
796# tests whether the host OS is one of the supported OSes for a test
797#
798_supported_os()
799{
800    for h
801    do
802        if [ "$h" = "$HOSTOS" ]
803        then
804            return
805        fi
806    done
807
808    _notrun "not suitable for this OS: $HOSTOS"
809}
810
811_supported_cache_modes()
812{
813    for mode; do
814        if [ "$mode" = "$CACHEMODE" ]; then
815            return
816        fi
817    done
818    _notrun "not suitable for cache mode: $CACHEMODE"
819}
820
821# Check whether the filesystem supports O_DIRECT
822_check_o_direct()
823{
824    testfile="$TEST_DIR"/_check_o_direct
825    $QEMU_IMG create -f raw "$testfile" 1M > /dev/null
826    out=$($QEMU_IO -f raw -t none -c quit "$testfile" 2>&1)
827    rm -f "$testfile"
828
829    [[ "$out" != *"O_DIRECT"* ]]
830}
831
832_require_o_direct()
833{
834    if ! _check_o_direct; then
835        _notrun "file system on $TEST_DIR does not support O_DIRECT"
836    fi
837}
838
839_check_cache_mode()
840{
841    if [ $CACHEMODE == "none" ] || [ $CACHEMODE == "directsync" ]; then
842        _require_o_direct
843    fi
844}
845
846_check_cache_mode
847
848# $1 - cache mode to use by default
849# $2 - (optional) cache mode to use by default if O_DIRECT is not supported
850_default_cache_mode()
851{
852    if $CACHEMODE_IS_DEFAULT; then
853        if [ -z "$2" ] || _check_o_direct; then
854            CACHEMODE="$1"
855        else
856            CACHEMODE="$2"
857        fi
858        QEMU_IO="$QEMU_IO --cache $CACHEMODE"
859        _check_cache_mode
860        return
861    fi
862}
863_supported_aio_modes()
864{
865    for mode; do
866        if [ "$mode" = "$AIOMODE" ]; then
867            return
868        fi
869    done
870    _notrun "not suitable for aio mode: $AIOMODE"
871}
872_default_aio_mode()
873{
874    AIOMODE="$1"
875    QEMU_IO="$QEMU_IO --aio $1"
876}
877
878_unsupported_imgopts()
879{
880    for bad_opt
881    do
882        if echo "$IMGOPTS" | grep -q 2>/dev/null "$bad_opt"
883        then
884            _notrun "not suitable for image option: $bad_opt"
885        fi
886    done
887}
888
889# Caution: Overwrites $TEST_DIR/t.luks
890_require_working_luks()
891{
892    file="$TEST_DIR/t.luks"
893
894    output=$(
895        $QEMU_IMG create -f luks \
896            --object secret,id=sec0,data=hunter0 \
897            -o key-secret=sec0 \
898            -o iter-time=10 \
899            "$file" \
900            1M \
901            2>&1
902    )
903    status=$?
904
905    IMGFMT='luks' _rm_test_img "$file"
906
907    if [ $status != 0 ]; then
908        reason=$(echo "$output" | grep "$file:" | $SED -e "s#.*$file: *##")
909        if [ -z "$reason" ]; then
910            reason="Failed to create a LUKS image"
911        fi
912        _notrun "$reason"
913    fi
914}
915
916# this test requires that a specified command (executable) exists
917#
918_require_command()
919{
920    if [ "$1" = "QEMU" ]; then
921        c=$QEMU_PROG
922    elif [ "$1" = "QEMU_IMG" ]; then
923        c=$QEMU_IMG_PROG
924    elif [ "$1" = "QEMU_IO" ]; then
925        c=$QEMU_IO_PROG
926    elif [ "$1" = "QEMU_NBD" ]; then
927        c=$QEMU_NBD_PROG
928    else
929        eval c=\$$1
930    fi
931    [ -x "$c" ] || _notrun "$1 utility required, skipped this test"
932}
933
934# Check that a set of drivers has been whitelisted in the QEMU binary
935#
936_require_drivers()
937{
938    available=$($QEMU -drive format=help | \
939                sed -e '/Supported formats:/!d' -e 's/Supported formats://')
940    for driver
941    do
942        if ! echo "$available" | grep -q " $driver\( \|$\)"; then
943            _notrun "$driver not available"
944        fi
945    done
946}
947
948# Check that we have a file system that allows huge (but very sparse) files
949#
950_require_large_file()
951{
952    if ! truncate --size="$1" "$TEST_IMG"; then
953        _notrun "file system on $TEST_DIR does not support large enough files"
954    fi
955    rm "$TEST_IMG"
956}
957
958# Check that a set of devices is available in the QEMU binary
959#
960_require_devices()
961{
962    available=$($QEMU -M none -device help | \
963                grep ^name | sed -e 's/^name "//' -e 's/".*$//')
964    for device
965    do
966        if ! echo "$available" | grep -q "$device" ; then
967            _notrun "$device not available"
968        fi
969    done
970}
971
972# make sure this script returns success
973true
974