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