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