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