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