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