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