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