xref: /openbmc/qemu/tests/qemu-iotests/common.rc (revision 24496b8d)
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
57if ! . ./common.config
58    then
59    echo "$0: failed to source common.config"
60    exit 1
61fi
62
63_qemu_wrapper()
64{
65    (
66        if [ -n "${QEMU_NEED_PID}" ]; then
67            echo $BASHPID > "${QEMU_TEST_DIR}/qemu-${_QEMU_HANDLE}.pid"
68        fi
69        exec "$QEMU_PROG" $QEMU_OPTIONS "$@"
70    )
71}
72
73_qemu_img_wrapper()
74{
75    (exec "$QEMU_IMG_PROG" $QEMU_IMG_OPTIONS "$@")
76}
77
78_qemu_io_wrapper()
79{
80    local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind
81    local QEMU_IO_ARGS="$QEMU_IO_OPTIONS"
82    if [ "$IMGOPTSSYNTAX" = "true" ]; then
83        QEMU_IO_ARGS="--image-opts $QEMU_IO_ARGS"
84        if [ -n "$IMGKEYSECRET" ]; then
85            QEMU_IO_ARGS="--object secret,id=keysec0,data=$IMGKEYSECRET $QEMU_IO_ARGS"
86        fi
87    fi
88    local RETVAL
89    (
90        if [ "${VALGRIND_QEMU}" == "y" ]; then
91            exec valgrind --log-file="${VALGRIND_LOGFILE}" --error-exitcode=99 "$QEMU_IO_PROG" $QEMU_IO_ARGS "$@"
92        else
93            exec "$QEMU_IO_PROG" $QEMU_IO_ARGS "$@"
94        fi
95    )
96    RETVAL=$?
97    if [ "${VALGRIND_QEMU}" == "y" ]; then
98        if [ $RETVAL == 99 ]; then
99            cat "${VALGRIND_LOGFILE}"
100        fi
101        rm -f "${VALGRIND_LOGFILE}"
102    fi
103    (exit $RETVAL)
104}
105
106_qemu_nbd_wrapper()
107{
108    (
109        echo $BASHPID > "${QEMU_TEST_DIR}/qemu-nbd.pid"
110        exec "$QEMU_NBD_PROG" $QEMU_NBD_OPTIONS "$@"
111    )
112}
113
114_qemu_vxhs_wrapper()
115{
116    (
117        echo $BASHPID > "${TEST_DIR}/qemu-vxhs.pid"
118        exec "$QEMU_VXHS_PROG" $QEMU_VXHS_OPTIONS "$@"
119    )
120}
121
122export QEMU=_qemu_wrapper
123export QEMU_IMG=_qemu_img_wrapper
124export QEMU_IO=_qemu_io_wrapper
125export QEMU_NBD=_qemu_nbd_wrapper
126export QEMU_VXHS=_qemu_vxhs_wrapper
127
128if [ "$IMGOPTSSYNTAX" = "true" ]; then
129    DRIVER="driver=$IMGFMT"
130    QEMU_IMG_EXTRA_ARGS="--image-opts $QEMU_IMG_EXTRA_ARGS"
131    if [ -n "$IMGKEYSECRET" ]; then
132        QEMU_IMG_EXTRA_ARGS="--object secret,id=keysec0,data=$IMGKEYSECRET $QEMU_IMG_EXTRA_ARGS"
133    fi
134    if [ "$IMGFMT" = "luks" ]; then
135        DRIVER="$DRIVER,key-secret=keysec0"
136    fi
137    if [ "$IMGPROTO" = "file" ]; then
138        TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
139        TEST_IMG="$DRIVER,file.filename=$TEST_DIR/t.$IMGFMT"
140    elif [ "$IMGPROTO" = "nbd" ]; then
141        TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
142        TEST_IMG="$DRIVER,file.driver=nbd,file.host=127.0.0.1,file.port=10810"
143    elif [ "$IMGPROTO" = "ssh" ]; then
144        TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
145        TEST_IMG="$DRIVER,file.driver=ssh,file.host=127.0.0.1,file.path=$TEST_IMG_FILE"
146    elif [ "$IMGPROTO" = "nfs" ]; then
147        TEST_DIR="$DRIVER,file.driver=nfs,file.filename=nfs://127.0.0.1/$TEST_DIR"
148        TEST_IMG=$TEST_DIR/t.$IMGFMT
149    else
150        TEST_IMG="$DRIVER,file.driver=$IMGPROTO,file.filename=$TEST_DIR/t.$IMGFMT"
151    fi
152else
153    QEMU_IMG_EXTRA_ARGS=
154    if [ "$IMGPROTO" = "file" ]; then
155        TEST_IMG=$TEST_DIR/t.$IMGFMT
156    elif [ "$IMGPROTO" = "nbd" ]; then
157        TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
158        TEST_IMG="nbd:127.0.0.1:10810"
159    elif [ "$IMGPROTO" = "ssh" ]; then
160        TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
161        REMOTE_TEST_DIR="ssh://127.0.0.1$TEST_DIR"
162        TEST_IMG="ssh://127.0.0.1$TEST_IMG_FILE"
163    elif [ "$IMGPROTO" = "nfs" ]; then
164        TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
165        REMOTE_TEST_DIR="nfs://127.0.0.1$TEST_DIR"
166        TEST_IMG="nfs://127.0.0.1$TEST_IMG_FILE"
167    elif [ "$IMGPROTO" = "vxhs" ]; then
168        TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
169        TEST_IMG="vxhs://127.0.0.1:9999/t.$IMGFMT"
170    else
171        TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT
172    fi
173fi
174ORIG_TEST_IMG="$TEST_IMG"
175
176if [ -z "$TEST_DIR" ]; then
177        TEST_DIR=$PWD/scratch
178fi
179
180QEMU_TEST_DIR="${TEST_DIR}"
181
182if [ ! -e "$TEST_DIR" ]; then
183        mkdir "$TEST_DIR"
184fi
185
186if [ ! -d "$TEST_DIR" ]; then
187    echo "common.rc: Error: \$TEST_DIR ($TEST_DIR) is not a directory"
188    exit 1
189fi
190
191if [ -z "$REMOTE_TEST_DIR" ]; then
192    REMOTE_TEST_DIR="$TEST_DIR"
193fi
194
195if [ ! -d "$SAMPLE_IMG_DIR" ]; then
196    echo "common.rc: Error: \$SAMPLE_IMG_DIR ($SAMPLE_IMG_DIR) is not a directory"
197    exit 1
198fi
199
200_use_sample_img()
201{
202    SAMPLE_IMG_FILE="${1%\.bz2}"
203    TEST_IMG="$TEST_DIR/$SAMPLE_IMG_FILE"
204    bzcat "$SAMPLE_IMG_DIR/$1" > "$TEST_IMG"
205    if [ $? -ne 0 ]
206    then
207        echo "_use_sample_img error, cannot extract '$SAMPLE_IMG_DIR/$1'"
208        exit 1
209    fi
210}
211
212_stop_nbd_server()
213{
214    if [ -f "${QEMU_TEST_DIR}/qemu-nbd.pid" ]; then
215        local QEMU_NBD_PID
216        read QEMU_NBD_PID < "${QEMU_TEST_DIR}/qemu-nbd.pid"
217        kill ${QEMU_NBD_PID}
218        rm -f "${QEMU_TEST_DIR}/qemu-nbd.pid"
219    fi
220}
221
222_make_test_img()
223{
224    # extra qemu-img options can be added by tests
225    # at least one argument (the image size) needs to be added
226    local extra_img_options=""
227    local image_size=$*
228    local optstr=""
229    local img_name=""
230    local use_backing=0
231    local backing_file=""
232    local object_options=""
233
234    if [ -n "$TEST_IMG_FILE" ]; then
235        img_name=$TEST_IMG_FILE
236    else
237        img_name=$TEST_IMG
238    fi
239
240    if [ -n "$IMGOPTS" ]; then
241        optstr=$(_optstr_add "$optstr" "$IMGOPTS")
242    fi
243    if [ -n "$IMGKEYSECRET" ]; then
244        object_options="--object secret,id=keysec0,data=$IMGKEYSECRET"
245        optstr=$(_optstr_add "$optstr" "key-secret=keysec0")
246    fi
247
248    if [ "$1" = "-b" ]; then
249        use_backing=1
250        backing_file=$2
251        image_size=$3
252    fi
253    if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then
254        optstr=$(_optstr_add "$optstr" "cluster_size=$CLUSTER_SIZE")
255    fi
256
257    if [ -n "$optstr" ]; then
258        extra_img_options="-o $optstr $extra_img_options"
259    fi
260
261    if [ $IMGPROTO = "nbd" ]; then
262        _stop_nbd_server
263    fi
264
265    # XXX(hch): have global image options?
266    (
267     if [ $use_backing = 1 ]; then
268        $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options -b "$backing_file" "$img_name" $image_size 2>&1
269     else
270        $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options "$img_name" $image_size 2>&1
271     fi
272    ) | _filter_img_create
273
274    # Start an NBD server on the image file, which is what we'll be talking to
275    if [ $IMGPROTO = "nbd" ]; then
276        # Pass a sufficiently high number to -e that should be enough for all
277        # tests
278        eval "$QEMU_NBD -v -t -b 127.0.0.1 -p 10810 -f $IMGFMT -e 42 -x '' $TEST_IMG_FILE >/dev/null &"
279        sleep 1 # FIXME: qemu-nbd needs to be listening before we continue
280    fi
281
282    # Start QNIO server on image directory for vxhs protocol
283    if [ $IMGPROTO = "vxhs" ]; then
284        eval "$QEMU_VXHS -d  $TEST_DIR > /dev/null &"
285        sleep 1 # Wait for server to come up.
286    fi
287}
288
289_rm_test_img()
290{
291    local img=$1
292    if [ "$IMGFMT" = "vmdk" ]; then
293        # Remove all the extents for vmdk
294        "$QEMU_IMG" info "$img" 2>/dev/null | grep 'filename:' | cut -f 2 -d: \
295            | xargs -I {} rm -f "{}"
296    fi
297    rm -f "$img"
298}
299
300_cleanup_test_img()
301{
302    case "$IMGPROTO" in
303
304        nbd)
305            _stop_nbd_server
306            rm -f "$TEST_IMG_FILE"
307            ;;
308        vxhs)
309            if [ -f "${TEST_DIR}/qemu-vxhs.pid" ]; then
310                local QEMU_VXHS_PID
311                read QEMU_VXHS_PID < "${TEST_DIR}/qemu-vxhs.pid"
312                kill ${QEMU_VXHS_PID} >/dev/null 2>&1
313                rm -f "${TEST_DIR}/qemu-vxhs.pid"
314            fi
315            rm -f "$TEST_IMG_FILE"
316            ;;
317
318        file)
319            _rm_test_img "$TEST_DIR/t.$IMGFMT"
320            _rm_test_img "$TEST_DIR/t.$IMGFMT.orig"
321            _rm_test_img "$TEST_DIR/t.$IMGFMT.base"
322            if [ -n "$SAMPLE_IMG_FILE" ]
323            then
324                rm -f "$TEST_DIR/$SAMPLE_IMG_FILE"
325                SAMPLE_IMG_FILE=
326                TEST_IMG="$ORIG_TEST_IMG"
327            fi
328            ;;
329
330        rbd)
331            rbd --no-progress rm "$TEST_DIR/t.$IMGFMT" > /dev/null
332            ;;
333
334        sheepdog)
335            collie vdi delete "$TEST_DIR/t.$IMGFMT"
336            ;;
337
338    esac
339}
340
341_check_test_img()
342{
343    (
344        if [ "$IMGOPTSSYNTAX" = "true" ]; then
345            $QEMU_IMG check $QEMU_IMG_EXTRA_ARGS "$@" "$TEST_IMG" 2>&1
346        else
347            $QEMU_IMG check "$@" -f $IMGFMT "$TEST_IMG" 2>&1
348        fi
349    ) | _filter_testdir | _filter_qemu_img_check
350}
351
352_img_info()
353{
354    if [[ "$1" == "--format-specific" ]]; then
355        local format_specific=1
356        shift
357    else
358        local format_specific=0
359    fi
360
361    discard=0
362    regex_json_spec_start='^ *"format-specific": \{'
363    $QEMU_IMG info $QEMU_IMG_EXTRA_ARGS "$@" "$TEST_IMG" 2>&1 | \
364        sed -e "s#$REMOTE_TEST_DIR#TEST_DIR#g" \
365            -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
366            -e "s#$TEST_DIR#TEST_DIR#g" \
367            -e "s#$IMGFMT#IMGFMT#g" \
368            -e "/^disk size:/ D" \
369            -e "/actual-size/ D" | \
370        while IFS='' read -r line; do
371            if [[ $format_specific == 1 ]]; then
372                discard=0
373            elif [[ $line == "Format specific information:" ]]; then
374                discard=1
375            elif [[ $line =~ $regex_json_spec_start ]]; then
376                discard=2
377                regex_json_spec_end="^${line%%[^ ]*}\\},? *$"
378            fi
379            if [[ $discard == 0 ]]; then
380                echo "$line"
381            elif [[ $discard == 1 && ! $line ]]; then
382                echo
383                discard=0
384            elif [[ $discard == 2 && $line =~ $regex_json_spec_end ]]; then
385                discard=0
386            fi
387        done
388}
389
390# bail out, setting up .notrun file
391#
392_notrun()
393{
394    echo "$*" >"$OUTPUT_DIR/$seq.notrun"
395    echo "$seq not run: $*"
396    status=0
397    exit
398}
399
400# just plain bail out
401#
402_fail()
403{
404    echo "$*" | tee -a "$OUTPUT_DIR/$seq.full"
405    echo "(see $seq.full for details)"
406    status=1
407    exit 1
408}
409
410# tests whether $IMGFMT is one of the supported image formats for a test
411#
412_supported_fmt()
413{
414    # "generic" is suitable for most image formats. For some formats it doesn't
415    # work, however (most notably read-only formats), so they can opt out by
416    # setting IMGFMT_GENERIC to false.
417    for f; do
418        if [ "$f" = "$IMGFMT" -o "$f" = "generic" -a "$IMGFMT_GENERIC" = "true" ]; then
419            return
420        fi
421    done
422
423    _notrun "not suitable for this image format: $IMGFMT"
424}
425
426# tests whether $IMGFMT is one of the unsupported image format for a test
427#
428_unsupported_fmt()
429{
430    for f; do
431        if [ "$f" = "$IMGFMT" ]; then
432            _notrun "not suitable for this image format: $IMGFMT"
433        fi
434    done
435}
436
437# tests whether $IMGPROTO is one of the supported image protocols for a test
438#
439_supported_proto()
440{
441    for f; do
442        if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then
443            return
444        fi
445    done
446
447    _notrun "not suitable for this image protocol: $IMGPROTO"
448}
449
450# tests whether $IMGPROTO is specified as an unsupported image protocol for a test
451#
452_unsupported_proto()
453{
454    for f; do
455        if [ "$f" = "$IMGPROTO" ]; then
456            _notrun "not suitable for this image protocol: $IMGPROTO"
457            return
458        fi
459    done
460}
461
462# tests whether the host OS is one of the supported OSes for a test
463#
464_supported_os()
465{
466    for h
467    do
468        if [ "$h" = "$HOSTOS" ]
469        then
470            return
471        fi
472    done
473
474    _notrun "not suitable for this OS: $HOSTOS"
475}
476
477_supported_cache_modes()
478{
479    for mode; do
480        if [ "$mode" = "$CACHEMODE" ]; then
481            return
482        fi
483    done
484    _notrun "not suitable for cache mode: $CACHEMODE"
485}
486
487_default_cache_mode()
488{
489    if $CACHEMODE_IS_DEFAULT; then
490        CACHEMODE="$1"
491        QEMU_IO="$QEMU_IO --cache $1"
492        return
493    fi
494}
495
496_unsupported_imgopts()
497{
498    for bad_opt
499    do
500        if echo "$IMGOPTS" | grep -q 2>/dev/null "$bad_opt"
501        then
502            _notrun "not suitable for image option: $bad_opt"
503        fi
504    done
505}
506
507# this test requires that a specified command (executable) exists
508#
509_require_command()
510{
511    if [ "$1" = "QEMU" ]; then
512        c=$QEMU_PROG
513    elif [ "$1" = "QEMU_IMG" ]; then
514        c=$QEMU_IMG_PROG
515    elif [ "$1" = "QEMU_IO" ]; then
516        c=$QEMU_IO_PROG
517    elif [ "$1" = "QEMU_NBD" ]; then
518        c=$QEMU_NBD_PROG
519    else
520        eval c=\$$1
521    fi
522    [ -x "$c" ] || _notrun "$1 utility required, skipped this test"
523}
524
525# make sure this script returns success
526true
527