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