xref: /openbmc/qemu/tests/qemu-iotests/common.rc (revision b306e26c)
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
112config=common.config
113test -f $config || config=../common.config
114if ! test -f $config
115then
116    echo "$0: failed to find common.config"
117    exit 1
118fi
119if ! . $config
120    then
121    echo "$0: failed to source common.config"
122    exit 1
123fi
124
125# Set the variables to the empty string to turn Valgrind off
126# for specific processes, e.g.
127# $ VALGRIND_QEMU_IO= ./check -qcow2 -valgrind 015
128
129: ${VALGRIND_QEMU_VM=$VALGRIND_QEMU}
130: ${VALGRIND_QEMU_IMG=$VALGRIND_QEMU}
131: ${VALGRIND_QEMU_IO=$VALGRIND_QEMU}
132: ${VALGRIND_QEMU_NBD=$VALGRIND_QEMU}
133: ${VALGRIND_QSD=$VALGRIND_QEMU}
134
135# The Valgrind own parameters may be set with
136# its environment variable VALGRIND_OPTS, e.g.
137# $ VALGRIND_OPTS="--leak-check=yes" ./check -qcow2 -valgrind 015
138
139_qemu_proc_exec()
140{
141    local VALGRIND_LOGFILE="$1"
142    shift
143    if [[ "${VALGRIND_QEMU}" == "y" && "${NO_VALGRIND}" != "y" ]]; then
144        exec valgrind --log-file="${VALGRIND_LOGFILE}" --error-exitcode=99 "$@"
145    else
146        exec "$@"
147    fi
148}
149
150_qemu_proc_valgrind_log()
151{
152    local VALGRIND_LOGFILE="$1"
153    local RETVAL="$2"
154    if [[ "${VALGRIND_QEMU}" == "y" && "${NO_VALGRIND}" != "y" ]]; then
155        if [ $RETVAL == 99 ]; then
156            cat "${VALGRIND_LOGFILE}"
157        fi
158        rm -f "${VALGRIND_LOGFILE}"
159    fi
160}
161
162_qemu_wrapper()
163{
164    local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
165    (
166        if [ -n "${QEMU_NEED_PID}" ]; then
167            echo $BASHPID > "${QEMU_TEST_DIR}/qemu-${_QEMU_HANDLE}.pid"
168        fi
169        VALGRIND_QEMU="${VALGRIND_QEMU_VM}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
170            "$QEMU_PROG" $QEMU_OPTIONS "$@"
171    )
172    RETVAL=$?
173    _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
174    return $RETVAL
175}
176
177_qemu_img_wrapper()
178{
179    local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
180    (
181        VALGRIND_QEMU="${VALGRIND_QEMU_IMG}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
182            "$QEMU_IMG_PROG" $QEMU_IMG_OPTIONS "$@"
183    )
184    RETVAL=$?
185    _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
186    return $RETVAL
187}
188
189_qemu_io_wrapper()
190{
191    local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
192    local QEMU_IO_ARGS="$QEMU_IO_OPTIONS"
193    if [ "$IMGOPTSSYNTAX" = "true" ]; then
194        QEMU_IO_ARGS="--image-opts $QEMU_IO_ARGS"
195        if [ -n "$IMGKEYSECRET" ]; then
196            QEMU_IO_ARGS="--object secret,id=keysec0,data=$IMGKEYSECRET $QEMU_IO_ARGS"
197        fi
198    fi
199    (
200        VALGRIND_QEMU="${VALGRIND_QEMU_IO}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
201            "$QEMU_IO_PROG" $QEMU_IO_ARGS "$@"
202    )
203    RETVAL=$?
204    _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
205    return $RETVAL
206}
207
208_qemu_nbd_wrapper()
209{
210    local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
211    (
212        VALGRIND_QEMU="${VALGRIND_QEMU_NBD}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
213            "$QEMU_NBD_PROG" --pid-file="${QEMU_TEST_DIR}/qemu-nbd.pid" \
214             $QEMU_NBD_OPTIONS "$@"
215    )
216    RETVAL=$?
217    _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
218    return $RETVAL
219}
220
221_qemu_storage_daemon_wrapper()
222{
223    local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
224    (
225        if [ -n "${QSD_NEED_PID}" ]; then
226            echo $BASHPID > "${QEMU_TEST_DIR}/qemu-storage-daemon.pid"
227        fi
228        VALGRIND_QEMU="${VALGRIND_QSD}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
229            "$QSD_PROG" $QSD_OPTIONS "$@"
230    )
231    RETVAL=$?
232    _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
233    return $RETVAL
234}
235
236# Valgrind bug #409141 https://bugs.kde.org/show_bug.cgi?id=409141
237# Until valgrind 3.16+ is ubiquitous, we must work around a hang in
238# valgrind when issuing sigkill. Disable valgrind for this invocation.
239_NO_VALGRIND()
240{
241    NO_VALGRIND="y" "$@"
242}
243
244export QEMU=_qemu_wrapper
245export QEMU_IMG=_qemu_img_wrapper
246export QEMU_IO=_qemu_io_wrapper
247export QEMU_NBD=_qemu_nbd_wrapper
248export QSD=_qemu_storage_daemon_wrapper
249
250if [ "$IMGOPTSSYNTAX" = "true" ]; then
251    DRIVER="driver=$IMGFMT"
252    QEMU_IMG_EXTRA_ARGS="--image-opts $QEMU_IMG_EXTRA_ARGS"
253    if [ -n "$IMGKEYSECRET" ]; then
254        QEMU_IMG_EXTRA_ARGS="--object secret,id=keysec0,data=$IMGKEYSECRET $QEMU_IMG_EXTRA_ARGS"
255    fi
256    if [ "$IMGFMT" = "luks" ]; then
257        DRIVER="$DRIVER,key-secret=keysec0"
258    fi
259    if [ "$IMGPROTO" = "file" ]; then
260        TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
261        TEST_IMG="$DRIVER,file.filename=$TEST_DIR/t.$IMGFMT"
262    elif [ "$IMGPROTO" = "nbd" ]; then
263        TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
264        TEST_IMG="$DRIVER,file.driver=nbd,file.type=unix"
265        TEST_IMG="$TEST_IMG,file.path=$SOCK_DIR/nbd"
266    elif [ "$IMGPROTO" = "fuse" ]; then
267        TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
268        TEST_IMG="$DRIVER,file.filename=$SOCK_DIR/fuse-t.$IMGFMT"
269    elif [ "$IMGPROTO" = "ssh" ]; then
270        TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
271        TEST_IMG="$DRIVER,file.driver=ssh,file.host=127.0.0.1,file.path=$TEST_IMG_FILE"
272    elif [ "$IMGPROTO" = "nfs" ]; then
273        TEST_DIR="$DRIVER,file.driver=nfs,file.filename=nfs://127.0.0.1/$TEST_DIR"
274        TEST_IMG=$TEST_DIR/t.$IMGFMT
275    else
276        TEST_IMG="$DRIVER,file.driver=$IMGPROTO,file.filename=$TEST_DIR/t.$IMGFMT"
277    fi
278else
279    QEMU_IMG_EXTRA_ARGS=
280    if [ "$IMGPROTO" = "file" ]; then
281        TEST_IMG=$TEST_DIR/t.$IMGFMT
282    elif [ "$IMGPROTO" = "nbd" ]; then
283        TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
284        TEST_IMG="nbd+unix:///?socket=$SOCK_DIR/nbd"
285    elif [ "$IMGPROTO" = "fuse" ]; then
286        TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
287        TEST_IMG="$SOCK_DIR/fuse-t.$IMGFMT"
288    elif [ "$IMGPROTO" = "ssh" ]; then
289        TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
290        REMOTE_TEST_DIR="ssh://\\($USER@\\)\\?127.0.0.1\\(:[0-9]\\+\\)\\?$TEST_DIR"
291        TEST_IMG="ssh://127.0.0.1$TEST_IMG_FILE"
292    elif [ "$IMGPROTO" = "nfs" ]; then
293        TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
294        REMOTE_TEST_DIR="nfs://127.0.0.1$TEST_DIR"
295        TEST_IMG="nfs://127.0.0.1$TEST_IMG_FILE"
296    else
297        TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT
298    fi
299fi
300ORIG_TEST_IMG_FILE=$TEST_IMG_FILE
301ORIG_TEST_IMG="$TEST_IMG"
302
303FUSE_PIDS=()
304FUSE_EXPORTS=()
305
306if [ -z "$TEST_DIR" ]; then
307        TEST_DIR=$PWD/scratch
308fi
309
310QEMU_TEST_DIR="${TEST_DIR}"
311
312if [ ! -e "$TEST_DIR" ]; then
313        mkdir "$TEST_DIR"
314fi
315
316if [ ! -d "$TEST_DIR" ]; then
317    echo "common.rc: Error: \$TEST_DIR ($TEST_DIR) is not a directory"
318    exit 1
319fi
320
321if [ -z "$REMOTE_TEST_DIR" ]; then
322    REMOTE_TEST_DIR="$TEST_DIR"
323fi
324
325if [ ! -d "$SAMPLE_IMG_DIR" ]; then
326    echo "common.rc: Error: \$SAMPLE_IMG_DIR ($SAMPLE_IMG_DIR) is not a directory"
327    exit 1
328fi
329
330_use_sample_img()
331{
332    SAMPLE_IMG_FILE="${1%\.bz2}"
333    TEST_IMG="$TEST_DIR/$SAMPLE_IMG_FILE"
334    bzcat "$SAMPLE_IMG_DIR/$1" > "$TEST_IMG"
335    if [ $? -ne 0 ]
336    then
337        echo "_use_sample_img error, cannot extract '$SAMPLE_IMG_DIR/$1'"
338        exit 1
339    fi
340}
341
342_stop_nbd_server()
343{
344    if [ -f "${QEMU_TEST_DIR}/qemu-nbd.pid" ]; then
345        local QEMU_NBD_PID
346        read QEMU_NBD_PID < "${QEMU_TEST_DIR}/qemu-nbd.pid"
347        kill ${QEMU_NBD_PID}
348        rm -f "${QEMU_TEST_DIR}/qemu-nbd.pid" "$SOCK_DIR/nbd"
349    fi
350}
351
352# Gets the data_file value from IMGOPTS and replaces the '$TEST_IMG'
353# pattern by '$1'
354# Caution: The replacement is done with sed, so $1 must be escaped
355#          properly.  (The delimiter is '#'.)
356_get_data_file()
357{
358    if ! echo "$IMGOPTS" | grep -q 'data_file='; then
359        return 1
360    fi
361
362    echo "$IMGOPTS" | sed -e 's/.*data_file=\([^,]*\).*/\1/' \
363                    | sed -e "s#\\\$TEST_IMG#$1#"
364}
365
366# Translate a $TEST_IMG to its corresponding $TEST_IMG_FILE for
367# different protocols
368_test_img_to_test_img_file()
369{
370    case "$IMGPROTO" in
371        file)
372            echo "$1"
373            ;;
374
375        fuse)
376            echo "$1" | sed -e "s#$SOCK_DIR/fuse-#$TEST_DIR/#"
377            ;;
378
379        nfs)
380            echo "$1" | sed -e "s#nfs://127.0.0.1##"
381            ;;
382
383        ssh)
384            echo "$1" | \
385                sed -e "s#ssh://\\($USER@\\)\\?127.0.0.1\\(:[0-9]\\+\\)\\?##"
386            ;;
387
388        *)
389            return 1
390            ;;
391    esac
392}
393
394_make_test_img()
395{
396    # extra qemu-img options can be added by tests
397    # at least one argument (the image size) needs to be added
398    local extra_img_options=""
399    local optstr=""
400    local img_name=""
401    local use_backing=0
402    local backing_file=""
403    local object_options=""
404    local opts_param=false
405    local misc_params=()
406
407    if [[ $IMGPROTO == fuse && $TEST_IMG == $SOCK_DIR/fuse-* ]]; then
408        # The caller may be trying to overwrite an existing image
409        _rm_test_img "$TEST_IMG"
410    fi
411
412    if [ -z "$TEST_IMG_FILE" ]; then
413        img_name=$TEST_IMG
414    elif [ "$IMGOPTSSYNTAX" != "true" -a \
415           "$TEST_IMG_FILE" = "$ORIG_TEST_IMG_FILE" ]; then
416        # Handle cases of tests only updating TEST_IMG, but not TEST_IMG_FILE
417        img_name=$(_test_img_to_test_img_file "$TEST_IMG")
418        if [ "$?" != 0 ]; then
419            img_name=$TEST_IMG_FILE
420        fi
421    else
422        # $TEST_IMG_FILE is not the default value, so it definitely has been
423        # modified by the test
424        img_name=$TEST_IMG_FILE
425    fi
426
427    if [ -n "$IMGOPTS" ]; then
428        imgopts_expanded=$(echo "$IMGOPTS" | sed -e "s#\\\$TEST_IMG#$img_name#")
429        optstr=$(_optstr_add "$optstr" "$imgopts_expanded")
430    fi
431    if [ -n "$IMGKEYSECRET" ]; then
432        object_options="--object secret,id=keysec0,data=$IMGKEYSECRET"
433        optstr=$(_optstr_add "$optstr" "key-secret=keysec0")
434    fi
435
436    for param; do
437        if [ "$use_backing" = "1" -a -z "$backing_file" ]; then
438            backing_file=$param
439            continue
440        elif $opts_param; then
441            optstr=$(_optstr_add "$optstr" "$param")
442            opts_param=false
443            continue
444        fi
445
446        case "$param" in
447            -b)
448                use_backing=1
449                ;;
450
451            -o)
452                opts_param=true
453                ;;
454
455            --no-opts)
456                optstr=""
457                ;;
458
459            *)
460                misc_params=("${misc_params[@]}" "$param")
461                ;;
462        esac
463    done
464
465    if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then
466        optstr=$(_optstr_add "$optstr" "cluster_size=$CLUSTER_SIZE")
467    fi
468
469    if [ -n "$optstr" ]; then
470        extra_img_options="-o $optstr $extra_img_options"
471    fi
472
473    if [ $IMGPROTO = "nbd" ]; then
474        _stop_nbd_server
475    fi
476
477    # XXX(hch): have global image options?
478    (
479     if [ $use_backing = 1 ]; then
480        $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options -b "$backing_file" "$img_name" "${misc_params[@]}" 2>&1
481     else
482        $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options "$img_name" "${misc_params[@]}" 2>&1
483     fi
484    ) | _filter_img_create
485
486    # Start an NBD server on the image file, which is what we'll be talking to.
487    # Once NBD gains resize support, we may also want to use -f raw at the
488    # server and interpret format over NBD, but for now, the format is
489    # interpreted at the server and raw data sent over NBD.
490    if [ $IMGPROTO = "nbd" ]; then
491        # Pass a sufficiently high number to -e that should be enough for all
492        # tests
493        eval "$QEMU_NBD -v -t -k '$SOCK_DIR/nbd' -f $IMGFMT -e 42 -x '' $TEST_IMG_FILE >/dev/null &"
494        sleep 1 # FIXME: qemu-nbd needs to be listening before we continue
495    fi
496
497    if [ $IMGPROTO = "fuse" -a -f "$img_name" ]; then
498        local export_mp
499        local pid
500        local pidfile
501        local timeout
502
503        export_mp=$(echo "$img_name" | sed -e "s#$TEST_DIR/#$SOCK_DIR/fuse-#")
504        if ! echo "$export_mp" | grep -q "^$SOCK_DIR"; then
505            echo 'Cannot use FUSE exports with images outside of TEST_DIR' >&2
506            return 1
507        fi
508
509        touch "$export_mp"
510        rm -f "$SOCK_DIR/fuse-output"
511
512        # Usually, users would export formatted nodes.  But we present fuse as a
513        # protocol-level driver here, so we have to leave the format to the
514        # client.
515        # Switch off allow-other, because in general we do not need it for
516        # iotests.  The default allow-other=auto has the downside of printing a
517        # fusermount error on its first attempt if allow_other is not
518        # permissible, which we would need to filter.
519        QSD_NEED_PID=y $QSD \
520              --blockdev file,node-name=export-node,filename=$img_name,discard=unmap \
521              --export fuse,id=fuse-export,node-name=export-node,mountpoint="$export_mp",writable=on,growable=on,allow-other=off \
522              &
523
524        pidfile="$QEMU_TEST_DIR/qemu-storage-daemon.pid"
525
526        # Wait for the PID file
527        while [ ! -f "$pidfile" ]; do
528            sleep 0.5
529        done
530
531        pid=$(cat "$pidfile")
532        rm -f "$pidfile"
533
534        FUSE_PIDS+=($pid)
535        FUSE_EXPORTS+=("$export_mp")
536    fi
537}
538
539_rm_test_img()
540{
541    local img=$1
542
543    if [[ $IMGPROTO == fuse && $img == $SOCK_DIR/fuse-* ]]; then
544        # Drop a FUSE export
545        local df_output
546        local i
547        local image_file
548        local index=''
549        local timeout
550
551        for i in "${!FUSE_EXPORTS[@]}"; do
552            if [ "${FUSE_EXPORTS[i]}" = "$img" ]; then
553                index=$i
554                break
555            fi
556        done
557
558        if [ -z "$index" ]; then
559            # Probably gone already
560            return 0
561        fi
562
563        kill "${FUSE_PIDS[index]}"
564
565        # Wait until the mount is gone
566        timeout=10 # *0.5 s
567        while true; do
568            # Will show the mount point; if the mount is still there,
569            # it will be $img.
570            df_output=$(df "$img" 2>/dev/null)
571
572            # But df may also show an error ("Transpoint endpoint not
573            # connected"), so retry in such cases
574            if [ -n "$df_output" ]; then
575                if ! echo "$df_output" | grep -q "$img"; then
576                    break
577                fi
578            fi
579
580            sleep 0.5
581
582            timeout=$((timeout - 1))
583            if [ "$timeout" = 0 ]; then
584                echo 'Failed to take down FUSE export' >&2
585                return 1
586            fi
587        done
588
589        rm -f "$img"
590
591        unset "FUSE_PIDS[$index]"
592        unset "FUSE_EXPORTS[$index]"
593
594        image_file=$(echo "$img" | sed -e "s#$SOCK_DIR/fuse-#$TEST_DIR/#")
595        _rm_test_img "$image_file"
596        return
597    fi
598
599    if [ "$IMGFMT" = "vmdk" ]; then
600        # Remove all the extents for vmdk
601        "$QEMU_IMG" info "$img" 2>/dev/null | grep 'filename:' | cut -f 2 -d: \
602            | xargs -I {} rm -f "{}"
603    elif [ "$IMGFMT" = "qcow2" ]; then
604        # Remove external data file
605        if data_file=$(_get_data_file "$img"); then
606            rm -f "$data_file"
607        fi
608    fi
609    rm -f "$img"
610}
611
612_cleanup_test_img()
613{
614    case "$IMGPROTO" in
615
616        nbd)
617            _stop_nbd_server
618            rm -f "$TEST_IMG_FILE"
619            ;;
620
621        fuse)
622            local mp
623
624            for mp in "${FUSE_EXPORTS[@]}"; do
625                _rm_test_img "$mp"
626            done
627
628            FUSE_PIDS=()
629            FUSE_EXPORTS=()
630            ;;
631
632        file)
633            _rm_test_img "$TEST_DIR/t.$IMGFMT"
634            _rm_test_img "$TEST_DIR/t.$IMGFMT.orig"
635            _rm_test_img "$TEST_DIR/t.$IMGFMT.base"
636            if [ -n "$SAMPLE_IMG_FILE" ]
637            then
638                rm -f "$TEST_DIR/$SAMPLE_IMG_FILE"
639                SAMPLE_IMG_FILE=
640                TEST_IMG="$ORIG_TEST_IMG"
641            fi
642            ;;
643
644        rbd)
645            rbd --no-progress rm "$TEST_DIR/t.$IMGFMT" > /dev/null
646            ;;
647
648    esac
649}
650
651_check_test_img()
652{
653    (
654        if [ "$IMGOPTSSYNTAX" = "true" ]; then
655            $QEMU_IMG check $QEMU_IMG_EXTRA_ARGS "$@" "$TEST_IMG" 2>&1
656        else
657            $QEMU_IMG check "$@" -f $IMGFMT "$TEST_IMG" 2>&1
658        fi
659    ) | _filter_testdir | _filter_qemu_img_check
660
661    # return real qemu_img check status, to analyze in
662    # _check_test_img_ignore_leaks
663    return ${PIPESTATUS[0]}
664}
665
666_check_test_img_ignore_leaks()
667{
668    out=$(_check_test_img "$@")
669    status=$?
670    if [ $status = 3 ]; then
671        # This must correspond to success output in dump_human_image_check()
672        echo "No errors were found on the image."
673        return 0
674    fi
675    echo "$out"
676    return $status
677}
678
679_img_info()
680{
681    if [[ "$1" == "--format-specific" ]]; then
682        local format_specific=1
683        shift
684    else
685        local format_specific=0
686    fi
687
688    discard=0
689    regex_json_spec_start='^ *"format-specific": \{'
690    $QEMU_IMG info $QEMU_IMG_EXTRA_ARGS "$@" "$TEST_IMG" 2>&1 | \
691        sed -e "s#$REMOTE_TEST_DIR#TEST_DIR#g" \
692            -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
693            -e "s#$TEST_DIR#TEST_DIR#g" \
694            -e "s#$SOCK_DIR/fuse-#TEST_DIR/#g" \
695            -e "s#$IMGFMT#IMGFMT#g" \
696            -e "/^disk size:/ D" \
697            -e "/actual-size/ D" | \
698        while IFS='' read -r line; do
699            if [[ $format_specific == 1 ]]; then
700                discard=0
701            elif [[ $line == "Format specific information:" ]]; then
702                discard=1
703            elif [[ $line =~ $regex_json_spec_start ]]; then
704                discard=2
705                regex_json_spec_end="^${line%%[^ ]*}\\},? *$"
706            fi
707            if [[ $discard == 0 ]]; then
708                echo "$line"
709            elif [[ $discard == 1 && ! $line ]]; then
710                echo
711                discard=0
712            elif [[ $discard == 2 && $line =~ $regex_json_spec_end ]]; then
713                discard=0
714            fi
715        done
716}
717
718# bail out, setting up .notrun file
719#
720_notrun()
721{
722    echo "$*" >"$OUTPUT_DIR/$seq.notrun"
723    echo "$seq not run: $*"
724    status=0
725    exit
726}
727
728# bail out, setting up .casenotrun file
729# The function _casenotrun() is used as a notifier. It is the
730# caller's responsibility to make skipped a particular test.
731#
732_casenotrun()
733{
734    echo "    [case not run] $*" >>"$OUTPUT_DIR/$seq.casenotrun"
735}
736
737# just plain bail out
738#
739_fail()
740{
741    echo "$*" | tee -a "$OUTPUT_DIR/$seq.full"
742    echo "(see $seq.full for details)"
743    status=1
744    exit 1
745}
746
747# tests whether $IMGFMT is one of the supported image formats for a test
748#
749_supported_fmt()
750{
751    # "generic" is suitable for most image formats. For some formats it doesn't
752    # work, however (most notably read-only formats), so they can opt out by
753    # setting IMGFMT_GENERIC to false.
754    for f; do
755        if [ "$f" = "$IMGFMT" -o "$f" = "generic" -a "$IMGFMT_GENERIC" = "true" ]; then
756            if [ "$IMGFMT" = "luks" ]; then
757                _require_working_luks
758            fi
759            return
760        fi
761    done
762
763    _notrun "not suitable for this image format: $IMGFMT"
764}
765
766# tests whether $IMGFMT is one of the unsupported image format for a test
767#
768_unsupported_fmt()
769{
770    for f; do
771        if [ "$f" = "$IMGFMT" ]; then
772            _notrun "not suitable for this image format: $IMGFMT"
773        fi
774    done
775}
776
777# tests whether $IMGPROTO is one of the supported image protocols for a test
778#
779_supported_proto()
780{
781    for f; do
782        if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then
783            return
784        fi
785    done
786
787    _notrun "not suitable for this image protocol: $IMGPROTO"
788}
789
790# tests whether $IMGPROTO is specified as an unsupported image protocol for a test
791#
792_unsupported_proto()
793{
794    for f; do
795        if [ "$f" = "$IMGPROTO" ]; then
796            _notrun "not suitable for this image protocol: $IMGPROTO"
797            return
798        fi
799    done
800}
801
802# tests whether the host OS is one of the supported OSes for a test
803#
804_supported_os()
805{
806    for h
807    do
808        if [ "$h" = "$HOSTOS" ]
809        then
810            return
811        fi
812    done
813
814    _notrun "not suitable for this OS: $HOSTOS"
815}
816
817_supported_cache_modes()
818{
819    for mode; do
820        if [ "$mode" = "$CACHEMODE" ]; then
821            return
822        fi
823    done
824    _notrun "not suitable for cache mode: $CACHEMODE"
825}
826
827# Check whether the filesystem supports O_DIRECT
828_check_o_direct()
829{
830    testfile="$TEST_DIR"/_check_o_direct
831    $QEMU_IMG create -f raw "$testfile" 1M > /dev/null
832    out=$($QEMU_IO -f raw -t none -c quit "$testfile" 2>&1)
833    rm -f "$testfile"
834
835    [[ "$out" != *"O_DIRECT"* ]]
836}
837
838_require_o_direct()
839{
840    if ! _check_o_direct; then
841        _notrun "file system on $TEST_DIR does not support O_DIRECT"
842    fi
843}
844
845_check_cache_mode()
846{
847    if [ $CACHEMODE == "none" ] || [ $CACHEMODE == "directsync" ]; then
848        _require_o_direct
849    fi
850}
851
852_check_cache_mode
853
854# $1 - cache mode to use by default
855# $2 - (optional) cache mode to use by default if O_DIRECT is not supported
856_default_cache_mode()
857{
858    if $CACHEMODE_IS_DEFAULT; then
859        if [ -z "$2" ] || _check_o_direct; then
860            CACHEMODE="$1"
861        else
862            CACHEMODE="$2"
863        fi
864        QEMU_IO="$QEMU_IO --cache $CACHEMODE"
865        _check_cache_mode
866        return
867    fi
868}
869_supported_aio_modes()
870{
871    for mode; do
872        if [ "$mode" = "$AIOMODE" ]; then
873            return
874        fi
875    done
876    _notrun "not suitable for aio mode: $AIOMODE"
877}
878_default_aio_mode()
879{
880    AIOMODE="$1"
881    QEMU_IO="$QEMU_IO --aio $1"
882}
883
884_unsupported_imgopts()
885{
886    for bad_opt
887    do
888        # Add a space so tests can match for whitespace that marks the
889        # end of an option (\b or \> are not portable)
890        if echo "$IMGOPTS " | grep -q 2>/dev/null "$bad_opt"
891        then
892            _notrun "not suitable for image option: $bad_opt"
893        fi
894    done
895}
896
897# Caution: Overwrites $TEST_DIR/t.luks
898_require_working_luks()
899{
900    file="$TEST_DIR/t.luks"
901
902    output=$(
903        $QEMU_IMG create -f luks \
904            --object secret,id=sec0,data=hunter0 \
905            -o key-secret=sec0 \
906            -o iter-time=10 \
907            "$file" \
908            1M \
909            2>&1
910    )
911    status=$?
912
913    IMGFMT='luks' _rm_test_img "$file"
914
915    if [ $status != 0 ]; then
916        reason=$(echo "$output" | grep "$file:" | $SED -e "s#.*$file: *##")
917        if [ -z "$reason" ]; then
918            reason="Failed to create a LUKS image"
919        fi
920        _notrun "$reason"
921    fi
922}
923
924# this test requires that a specified command (executable) exists
925#
926_require_command()
927{
928    if [ "$1" = "QEMU" ]; then
929        c=$QEMU_PROG
930    elif [ "$1" = "QEMU_IMG" ]; then
931        c=$QEMU_IMG_PROG
932    elif [ "$1" = "QEMU_IO" ]; then
933        c=$QEMU_IO_PROG
934    elif [ "$1" = "QEMU_NBD" ]; then
935        c=$QEMU_NBD_PROG
936    else
937        eval c=\$$1
938    fi
939    [ -x "$c" ] || _notrun "$1 utility required, skipped this test"
940}
941
942# Check that a set of drivers has been whitelisted in the QEMU binary
943#
944_require_drivers()
945{
946    available=$($QEMU -drive format=help | \
947                sed -e '/Supported formats:/!d' -e 's/Supported formats://')
948    for driver
949    do
950        if ! echo "$available" | grep -q " $driver\( \|$\)"; then
951            _notrun "$driver not available"
952        fi
953    done
954}
955
956# Check that we have a file system that allows huge (but very sparse) files
957#
958_require_large_file()
959{
960    if ! truncate --size="$1" "$TEST_IMG"; then
961        _notrun "file system on $TEST_DIR does not support large enough files"
962    fi
963    rm "$TEST_IMG"
964}
965
966# Check that a set of devices is available in the QEMU binary
967#
968_require_devices()
969{
970    available=$($QEMU -M none -device help | \
971                grep ^name | sed -e 's/^name "//' -e 's/".*$//')
972    for device
973    do
974        if ! echo "$available" | grep -q "$device" ; then
975            _notrun "$device not available"
976        fi
977    done
978}
979
980_require_one_device_of()
981{
982    available=$($QEMU -M none -device help | \
983                grep ^name | sed -e 's/^name "//' -e 's/".*$//')
984    for device
985    do
986        if echo "$available" | grep -q "$device" ; then
987            return
988        fi
989    done
990    _notrun "$* not available"
991}
992
993# make sure this script returns success
994true
995