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