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