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