xref: /openbmc/qemu/tests/qemu-iotests/common.rc (revision a1857ad1)
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# we need common.config
44if [ "$iam" != "check" ]
45then
46    if ! . ./common.config
47        then
48        echo "$iam: failed to source common.config"
49        exit 1
50    fi
51fi
52
53# make sure we have a standard umask
54umask 022
55
56if [ "$IMGPROTO" = "file" ]; then
57    TEST_IMG=$TEST_DIR/t.$IMGFMT
58elif [ "$IMGPROTO" = "nbd" ]; then
59    TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
60    TEST_IMG="nbd:127.0.0.1:10810"
61elif [ "$IMGPROTO" = "ssh" ]; then
62    TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
63    TEST_IMG="ssh://127.0.0.1$TEST_IMG_FILE"
64elif [ "$IMGPROTO" = "nfs" ]; then
65    TEST_DIR="nfs://127.0.0.1/$TEST_DIR"
66    TEST_IMG=$TEST_DIR/t.$IMGFMT
67elif [ "$IMGPROTO" = "archipelago" ]; then
68    TEST_IMG="archipelago:at.$IMGFMT"
69else
70    TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT
71fi
72
73function valgrind_qemu_io()
74{
75    valgrind --log-file=/tmp/$$.valgrind --error-exitcode=99 $REAL_QEMU_IO "$@"
76    if [ $? != 0 ]; then
77        cat /tmp/$$.valgrind
78    fi
79    rm -f /tmp/$$.valgrind
80}
81
82
83_optstr_add()
84{
85    if [ -n "$1" ]; then
86        echo "$1,$2"
87    else
88        echo "$2"
89    fi
90}
91
92_set_default_imgopts()
93{
94    if [ "$IMGFMT" == "qcow2" ] && ! (echo "$IMGOPTS" | grep "compat=" > /dev/null); then
95        IMGOPTS=$(_optstr_add "$IMGOPTS" "compat=1.1")
96    fi
97}
98
99_use_sample_img()
100{
101    SAMPLE_IMG_FILE="${1%\.bz2}"
102    TEST_IMG="$TEST_DIR/$SAMPLE_IMG_FILE"
103    bzcat "$SAMPLE_IMG_DIR/$1" > "$TEST_IMG"
104    if [ $? -ne 0 ]
105    then
106        echo "_use_sample_img error, cannot extract '$SAMPLE_IMG_DIR/$1'"
107        exit 1
108    fi
109}
110
111_make_test_img()
112{
113    # extra qemu-img options can be added by tests
114    # at least one argument (the image size) needs to be added
115    local extra_img_options=""
116    local image_size=$*
117    local optstr=""
118    local img_name=""
119    local use_backing=0
120    local backing_file=""
121
122    if [ -n "$TEST_IMG_FILE" ]; then
123        img_name=$TEST_IMG_FILE
124    else
125        img_name=$TEST_IMG
126    fi
127
128    if [ -n "$IMGOPTS" ]; then
129        optstr=$(_optstr_add "$optstr" "$IMGOPTS")
130    fi
131
132    if [ "$1" = "-b" ]; then
133        use_backing=1
134        backing_file=$2
135        image_size=$3
136    fi
137    if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then
138        optstr=$(_optstr_add "$optstr" "cluster_size=$CLUSTER_SIZE")
139    fi
140
141    if [ -n "$optstr" ]; then
142        extra_img_options="-o $optstr $extra_img_options"
143    fi
144
145    # XXX(hch): have global image options?
146    (
147     if [ $use_backing = 1 ]; then
148        $QEMU_IMG create -f $IMGFMT $extra_img_options -b "$backing_file" "$img_name" $image_size 2>&1
149     else
150        $QEMU_IMG create -f $IMGFMT $extra_img_options "$img_name" $image_size 2>&1
151     fi
152    ) | \
153        sed -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
154            -e "s#$TEST_DIR#TEST_DIR#g" \
155            -e "s#$IMGFMT#IMGFMT#g" \
156            -e "s# encryption=off##g" \
157            -e "s# cluster_size=[0-9]\\+##g" \
158            -e "s# table_size=[0-9]\\+##g" \
159            -e "s# compat='[^']*'##g" \
160            -e "s# compat6=\\(on\\|off\\)##g" \
161            -e "s# static=\\(on\\|off\\)##g" \
162            -e "s# zeroed_grain=\\(on\\|off\\)##g" \
163            -e "s# subformat='[^']*'##g" \
164            -e "s# adapter_type='[^']*'##g" \
165            -e "s# lazy_refcounts=\\(on\\|off\\)##g" \
166            -e "s# block_size=[0-9]\\+##g" \
167            -e "s# block_state_zero=\\(on\\|off\\)##g" \
168            -e "s# log_size=[0-9]\\+##g" \
169            -e "s/archipelago:a/TEST_DIR\//g"
170
171    # Start an NBD server on the image file, which is what we'll be talking to
172    if [ $IMGPROTO = "nbd" ]; then
173        eval "$QEMU_NBD -v -t -b 127.0.0.1 -p 10810  $TEST_IMG_FILE &"
174        QEMU_NBD_PID=$!
175        sleep 1 # FIXME: qemu-nbd needs to be listening before we continue
176    fi
177}
178
179_rm_test_img()
180{
181    local img=$1
182    if [ "$IMGFMT" = "vmdk" ]; then
183        # Remove all the extents for vmdk
184        "$QEMU_IMG" info "$img" 2>/dev/null | grep 'filename:' | cut -f 2 -d: \
185            | xargs -I {} rm -f "{}"
186    fi
187    rm -f "$img"
188}
189
190_cleanup_test_img()
191{
192    case "$IMGPROTO" in
193
194        nbd)
195            kill $QEMU_NBD_PID
196            rm -f "$TEST_IMG_FILE"
197            ;;
198        file)
199            _rm_test_img "$TEST_DIR/t.$IMGFMT"
200            _rm_test_img "$TEST_DIR/t.$IMGFMT.orig"
201            _rm_test_img "$TEST_DIR/t.$IMGFMT.base"
202            if [ -n "$SAMPLE_IMG_FILE" ]
203            then
204                rm -f "$TEST_DIR/$SAMPLE_IMG_FILE"
205            fi
206            ;;
207
208        rbd)
209            rbd --no-progress rm "$TEST_DIR/t.$IMGFMT" > /dev/null
210            ;;
211
212        archipelago)
213            vlmc remove "at.$IMGFMT" > /dev/null
214            ;;
215
216        sheepdog)
217            collie vdi delete "$TEST_DIR/t.$IMGFMT"
218            ;;
219
220    esac
221}
222
223_check_test_img()
224{
225    $QEMU_IMG check "$@" -f $IMGFMT "$TEST_IMG" 2>&1 | _filter_testdir | \
226        sed -e '/allocated.*fragmented.*compressed clusters/d' \
227            -e 's/qemu-img: This image format does not support checks/No errors were found on the image./' \
228            -e '/Image end offset: [0-9]\+/d'
229}
230
231_img_info()
232{
233    discard=0
234    regex_json_spec_start='^ *"format-specific": \{'
235    $QEMU_IMG info "$@" "$TEST_IMG" 2>&1 | \
236        sed -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
237            -e "s#$TEST_DIR#TEST_DIR#g" \
238            -e "s#$IMGFMT#IMGFMT#g" \
239            -e "/^disk size:/ D" \
240            -e "/actual-size/ D" | \
241        while IFS='' read line; do
242            if [[ $line == "Format specific information:" ]]; then
243                discard=1
244            elif [[ $line =~ $regex_json_spec_start ]]; then
245                discard=2
246                regex_json_spec_end="^${line%%[^ ]*}\\},? *$"
247            fi
248            if [[ $discard == 0 ]]; then
249                echo "$line"
250            elif [[ $discard == 1 && ! $line ]]; then
251                echo
252                discard=0
253            elif [[ $discard == 2 && $line =~ $regex_json_spec_end ]]; then
254                discard=0
255            fi
256        done
257}
258
259_get_pids_by_name()
260{
261    if [ $# -ne 1 ]
262    then
263        echo "Usage: _get_pids_by_name process-name" 1>&2
264        exit 1
265    fi
266
267    # Algorithm ... all ps(1) variants have a time of the form MM:SS or
268    # HH:MM:SS before the psargs field, use this as the search anchor.
269    #
270    # Matches with $1 (process-name) occur if the first psarg is $1
271    # or ends in /$1 ... the matching uses sed's regular expressions,
272    # so passing a regex into $1 will work.
273
274    ps $PS_ALL_FLAGS \
275    | sed -n \
276        -e 's/$/ /' \
277        -e 's/[         ][         ]*/ /g' \
278        -e 's/^ //' \
279        -e 's/^[^ ]* //' \
280        -e "/[0-9]:[0-9][0-9]  *[^ ]*\/$1 /s/ .*//p" \
281        -e "/[0-9]:[0-9][0-9]  *$1 /s/ .*//p"
282}
283
284# fqdn for localhost
285#
286_get_fqdn()
287{
288    host=`hostname`
289    $NSLOOKUP_PROG $host | $AWK_PROG '{ if ($1 == "Name:") print $2 }'
290}
291
292# check if run as root
293#
294_need_to_be_root()
295{
296    id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'`
297    if [ "$id" -ne 0 ]
298    then
299        echo "Arrgh ... you need to be root (not uid=$id) to run this test"
300        exit 1
301    fi
302}
303
304
305# Do a command, log it to $seq.full, optionally test return status
306# and die if command fails. If called with one argument _do executes the
307# command, logs it, and returns its exit status. With two arguments _do
308# first prints the message passed in the first argument, and then "done"
309# or "fail" depending on the return status of the command passed in the
310# second argument. If the command fails and the variable _do_die_on_error
311# is set to "always" or the two argument form is used and _do_die_on_error
312# is set to "message_only" _do will print an error message to
313# $seq.out and exit.
314
315_do()
316{
317    if [ $# -eq 1 ]; then
318        _cmd=$1
319    elif [ $# -eq 2 ]; then
320        _note=$1
321        _cmd=$2
322        echo -n "$_note... "
323    else
324        echo "Usage: _do [note] cmd" 1>&2
325        status=1; exit
326    fi
327
328    (eval "echo '---' \"$_cmd\"") >>"$OUTPUT_DIR/$seq.full"
329    (eval "$_cmd") >$tmp._out 2>&1; ret=$?
330    cat $tmp._out >>"$OUTPUT_DIR/$seq.full"
331    if [ $# -eq 2 ]; then
332        if [ $ret -eq 0 ]; then
333            echo "done"
334        else
335            echo "fail"
336        fi
337    fi
338    if [ $ret -ne 0  ] \
339        && [ "$_do_die_on_error" = "always" \
340            -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
341    then
342        [ $# -ne 2 ] && echo
343        eval "echo \"$_cmd\" failed \(returned $ret\): see $seq.full"
344        status=1; exit
345    fi
346
347    return $ret
348}
349
350# bail out, setting up .notrun file
351#
352_notrun()
353{
354    echo "$*" >"$OUTPUT_DIR/$seq.notrun"
355    echo "$seq not run: $*"
356    status=0
357    exit
358}
359
360# just plain bail out
361#
362_fail()
363{
364    echo "$*" | tee -a "$OUTPUT_DIR/$seq.full"
365    echo "(see $seq.full for details)"
366    status=1
367    exit 1
368}
369
370# tests whether $IMGFMT is one of the supported image formats for a test
371#
372_supported_fmt()
373{
374    # "generic" is suitable for most image formats. For some formats it doesn't
375    # work, however (most notably read-only formats), so they can opt out by
376    # setting IMGFMT_GENERIC to false.
377    for f; do
378        if [ "$f" = "$IMGFMT" -o "$f" = "generic" -a "$IMGFMT_GENERIC" = "true" ]; then
379            return
380        fi
381    done
382
383    _notrun "not suitable for this image format: $IMGFMT"
384}
385
386# tests whether $IMGPROTO is one of the supported image protocols for a test
387#
388_supported_proto()
389{
390    for f; do
391        if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then
392            return
393        fi
394    done
395
396    _notrun "not suitable for this image protocol: $IMGPROTO"
397}
398
399# tests whether the host OS is one of the supported OSes for a test
400#
401_supported_os()
402{
403    for h
404    do
405        if [ "$h" = "$HOSTOS" ]
406        then
407            return
408        fi
409    done
410
411    _notrun "not suitable for this OS: $HOSTOS"
412}
413
414_supported_cache_modes()
415{
416    for mode; do
417        if [ "$mode" = "$CACHEMODE" ]; then
418            return
419        fi
420    done
421    _notrun "not suitable for cache mode: $CACHEMODE"
422}
423
424_default_cache_mode()
425{
426    if $CACHEMODE_IS_DEFAULT; then
427        CACHEMODE="$1"
428        QEMU_IO="$QEMU_IO --cache $1"
429        return
430    fi
431}
432
433_unsupported_imgopts()
434{
435    for bad_opt
436    do
437        if echo "$IMGOPTS" | grep -q 2>/dev/null "$bad_opt"
438        then
439            _notrun "not suitable for image option: $bad_opt"
440        fi
441    done
442}
443
444# this test requires that a specified command (executable) exists
445#
446_require_command()
447{
448    eval c=\$$1
449    [ -x "$c" ] || _notrun "$1 utility required, skipped this test"
450}
451
452_full_imgfmt_details()
453{
454    if [ -n "$IMGOPTS" ]; then
455        echo "$IMGFMT ($IMGOPTS)"
456    else
457        echo "$IMGFMT"
458    fi
459}
460
461_full_imgproto_details()
462{
463    echo "$IMGPROTO"
464}
465
466_full_platform_details()
467{
468    os=`uname -s`
469    host=`hostname -s`
470    kernel=`uname -r`
471    platform=`uname -m`
472    echo "$os/$platform $host $kernel"
473}
474
475_link_out_file()
476{
477   if [ -z "$1" ]; then
478      echo Error must pass \$seq.
479      exit
480   fi
481   rm -f $1
482   if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then
483      ln -s $1.irix $1
484   elif [ "`uname`" == "Linux" ]; then
485      ln -s $1.linux $1
486   else
487      echo Error test $seq does not run on the operating system: `uname`
488      exit
489   fi
490}
491
492_die()
493{
494        echo $@
495        exit 1
496}
497
498# make sure this script returns success
499/bin/true
500