xref: /openbmc/qemu/tests/qemu-iotests/common.rc (revision 3f53bc61)
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/t.$IMGFMT
73    else
74        TEST_IMG="$DRIVER,file.driver=$IMGPROTO,file.filename=$TEST_DIR/t.$IMGFMT"
75    fi
76else
77    if [ "$IMGPROTO" = "file" ]; then
78        TEST_IMG=$TEST_DIR/t.$IMGFMT
79    elif [ "$IMGPROTO" = "nbd" ]; then
80        TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
81        TEST_IMG="nbd:127.0.0.1:10810"
82    elif [ "$IMGPROTO" = "ssh" ]; then
83        TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
84        TEST_IMG="ssh://127.0.0.1$TEST_IMG_FILE"
85    elif [ "$IMGPROTO" = "nfs" ]; then
86        TEST_DIR="nfs://127.0.0.1/$TEST_DIR"
87        TEST_IMG=$TEST_DIR/t.$IMGFMT
88    else
89        TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT
90    fi
91fi
92
93_optstr_add()
94{
95    if [ -n "$1" ]; then
96        echo "$1,$2"
97    else
98        echo "$2"
99    fi
100}
101
102_set_default_imgopts()
103{
104    if [ "$IMGFMT" == "qcow2" ] && ! (echo "$IMGOPTS" | grep "compat=" > /dev/null); then
105        IMGOPTS=$(_optstr_add "$IMGOPTS" "compat=1.1")
106    fi
107}
108
109_use_sample_img()
110{
111    SAMPLE_IMG_FILE="${1%\.bz2}"
112    TEST_IMG="$TEST_DIR/$SAMPLE_IMG_FILE"
113    bzcat "$SAMPLE_IMG_DIR/$1" > "$TEST_IMG"
114    if [ $? -ne 0 ]
115    then
116        echo "_use_sample_img error, cannot extract '$SAMPLE_IMG_DIR/$1'"
117        exit 1
118    fi
119}
120
121_make_test_img()
122{
123    # extra qemu-img options can be added by tests
124    # at least one argument (the image size) needs to be added
125    local extra_img_options=""
126    local image_size=$*
127    local optstr=""
128    local img_name=""
129    local use_backing=0
130    local backing_file=""
131    local object_options=""
132
133    if [ -n "$TEST_IMG_FILE" ]; then
134        img_name=$TEST_IMG_FILE
135    else
136        img_name=$TEST_IMG
137    fi
138
139    if [ -n "$IMGOPTS" ]; then
140        optstr=$(_optstr_add "$optstr" "$IMGOPTS")
141    fi
142    if [ -n "$IMGKEYSECRET" ]; then
143        object_options="--object secret,id=keysec0,data=$IMGKEYSECRET"
144        optstr=$(_optstr_add "$optstr" "key-secret=keysec0")
145    fi
146
147    if [ "$1" = "-b" ]; then
148        use_backing=1
149        backing_file=$2
150        image_size=$3
151    fi
152    if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then
153        optstr=$(_optstr_add "$optstr" "cluster_size=$CLUSTER_SIZE")
154    fi
155
156    if [ -n "$optstr" ]; then
157        extra_img_options="-o $optstr $extra_img_options"
158    fi
159
160    # XXX(hch): have global image options?
161    (
162     if [ $use_backing = 1 ]; then
163        $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options -b "$backing_file" "$img_name" $image_size 2>&1
164     else
165        $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options "$img_name" $image_size 2>&1
166     fi
167    ) | _filter_img_create
168
169    # Start an NBD server on the image file, which is what we'll be talking to
170    if [ $IMGPROTO = "nbd" ]; then
171        eval "$QEMU_NBD -v -t -b 127.0.0.1 -p 10810 -f $IMGFMT  $TEST_IMG_FILE >/dev/null &"
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            if [ -f "${QEMU_TEST_DIR}/qemu-nbd.pid" ]; then
193                local QEMU_NBD_PID
194                read QEMU_NBD_PID < "${QEMU_TEST_DIR}/qemu-nbd.pid"
195                kill ${QEMU_NBD_PID}
196                rm -f "${QEMU_TEST_DIR}/qemu-nbd.pid"
197            fi
198            rm -f "$TEST_IMG_FILE"
199            ;;
200        file)
201            _rm_test_img "$TEST_DIR/t.$IMGFMT"
202            _rm_test_img "$TEST_DIR/t.$IMGFMT.orig"
203            _rm_test_img "$TEST_DIR/t.$IMGFMT.base"
204            if [ -n "$SAMPLE_IMG_FILE" ]
205            then
206                rm -f "$TEST_DIR/$SAMPLE_IMG_FILE"
207            fi
208            ;;
209
210        rbd)
211            rbd --no-progress rm "$TEST_DIR/t.$IMGFMT" > /dev/null
212            ;;
213
214        sheepdog)
215            collie vdi delete "$TEST_DIR/t.$IMGFMT"
216            ;;
217
218    esac
219}
220
221_check_test_img()
222{
223    (
224        if [ "$IMGOPTSSYNTAX" = "true" ]; then
225            $QEMU_IMG check $QEMU_IMG_EXTRA_ARGS "$@" "$TEST_IMG" 2>&1
226        else
227            $QEMU_IMG check "$@" -f $IMGFMT "$TEST_IMG" 2>&1
228        fi
229    ) | _filter_testdir | _filter_qemu_img_check
230}
231
232_img_info()
233{
234    if [[ "$1" == "--format-specific" ]]; then
235        local format_specific=1
236        shift
237    else
238        local format_specific=0
239    fi
240
241    discard=0
242    regex_json_spec_start='^ *"format-specific": \{'
243    $QEMU_IMG info "$@" "$TEST_IMG" 2>&1 | \
244        sed -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \
245            -e "s#$TEST_DIR#TEST_DIR#g" \
246            -e "s#$IMGFMT#IMGFMT#g" \
247            -e "/^disk size:/ D" \
248            -e "/actual-size/ D" | \
249        while IFS='' read line; do
250            if [[ $format_specific == 1 ]]; then
251                discard=0
252            elif [[ $line == "Format specific information:" ]]; then
253                discard=1
254            elif [[ $line =~ $regex_json_spec_start ]]; then
255                discard=2
256                regex_json_spec_end="^${line%%[^ ]*}\\},? *$"
257            fi
258            if [[ $discard == 0 ]]; then
259                echo "$line"
260            elif [[ $discard == 1 && ! $line ]]; then
261                echo
262                discard=0
263            elif [[ $discard == 2 && $line =~ $regex_json_spec_end ]]; then
264                discard=0
265            fi
266        done
267}
268
269_get_pids_by_name()
270{
271    if [ $# -ne 1 ]
272    then
273        echo "Usage: _get_pids_by_name process-name" 1>&2
274        exit 1
275    fi
276
277    # Algorithm ... all ps(1) variants have a time of the form MM:SS or
278    # HH:MM:SS before the psargs field, use this as the search anchor.
279    #
280    # Matches with $1 (process-name) occur if the first psarg is $1
281    # or ends in /$1 ... the matching uses sed's regular expressions,
282    # so passing a regex into $1 will work.
283
284    ps $PS_ALL_FLAGS \
285    | sed -n \
286        -e 's/$/ /' \
287        -e 's/[         ][         ]*/ /g' \
288        -e 's/^ //' \
289        -e 's/^[^ ]* //' \
290        -e "/[0-9]:[0-9][0-9]  *[^ ]*\/$1 /s/ .*//p" \
291        -e "/[0-9]:[0-9][0-9]  *$1 /s/ .*//p"
292}
293
294# fqdn for localhost
295#
296_get_fqdn()
297{
298    host=`hostname`
299    $NSLOOKUP_PROG $host | $AWK_PROG '{ if ($1 == "Name:") print $2 }'
300}
301
302# check if run as root
303#
304_need_to_be_root()
305{
306    id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'`
307    if [ "$id" -ne 0 ]
308    then
309        echo "Arrgh ... you need to be root (not uid=$id) to run this test"
310        exit 1
311    fi
312}
313
314# bail out, setting up .notrun file
315#
316_notrun()
317{
318    echo "$*" >"$OUTPUT_DIR/$seq.notrun"
319    echo "$seq not run: $*"
320    status=0
321    exit
322}
323
324# just plain bail out
325#
326_fail()
327{
328    echo "$*" | tee -a "$OUTPUT_DIR/$seq.full"
329    echo "(see $seq.full for details)"
330    status=1
331    exit 1
332}
333
334# tests whether $IMGFMT is one of the supported image formats for a test
335#
336_supported_fmt()
337{
338    # "generic" is suitable for most image formats. For some formats it doesn't
339    # work, however (most notably read-only formats), so they can opt out by
340    # setting IMGFMT_GENERIC to false.
341    for f; do
342        if [ "$f" = "$IMGFMT" -o "$f" = "generic" -a "$IMGFMT_GENERIC" = "true" ]; then
343            return
344        fi
345    done
346
347    _notrun "not suitable for this image format: $IMGFMT"
348}
349
350# tests whether $IMGFMT is one of the unsupported image format for a test
351#
352_unsupported_fmt()
353{
354    for f; do
355        if [ "$f" = "$IMGFMT" ]; then
356            _notrun "not suitable for this image format: $IMGFMT"
357        fi
358    done
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 $IMGPROTO is specified as an unsupported image protocol for a test
375#
376_unsupported_proto()
377{
378    for f; do
379        if [ "$f" = "$IMGPROTO" ]; then
380            _notrun "not suitable for this image protocol: $IMGPROTO"
381            return
382        fi
383    done
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    if [ "$1" = "QEMU" ]; then
436        c=$QEMU_PROG
437    elif [ "$1" = "QEMU_IMG" ]; then
438        c=$QEMU_IMG_PROG
439    elif [ "$1" = "QEMU_IO" ]; then
440        c=$QEMU_IO_PROG
441    elif [ "$1" = "QEMU_NBD" ]; then
442        c=$QEMU_NBD_PROG
443    else
444        eval c=\$$1
445    fi
446    [ -x "$c" ] || _notrun "$1 utility required, skipped this test"
447}
448
449_full_imgfmt_details()
450{
451    if [ -n "$IMGOPTS" ]; then
452        echo "$IMGFMT ($IMGOPTS)"
453    else
454        echo "$IMGFMT"
455    fi
456}
457
458_full_imgproto_details()
459{
460    echo "$IMGPROTO"
461}
462
463_full_platform_details()
464{
465    os=`uname -s`
466    host=`hostname -s`
467    kernel=`uname -r`
468    platform=`uname -m`
469    echo "$os/$platform $host $kernel"
470}
471
472_link_out_file()
473{
474   if [ -z "$1" ]; then
475      echo Error must pass \$seq.
476      exit
477   fi
478   rm -f $1
479   if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then
480      ln -s $1.irix $1
481   elif [ "`uname`" == "Linux" ]; then
482      ln -s $1.linux $1
483   else
484      echo Error test $seq does not run on the operating system: `uname`
485      exit
486   fi
487}
488
489_die()
490{
491        echo $@
492        exit 1
493}
494
495# make sure this script returns success
496true
497