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