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