sync with tizen_2.2
[sdk/emulator/qemu.git] / tests / qemu-iotests / common.rc
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
20 dd()
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
38 if [ "$iam" != "check" ]
39 then
40     if ! . ./common.config
41         then
42         echo "$iam: failed to source common.config"
43         exit 1
44     fi
45 fi
46
47 # make sure we have a standard umask
48 umask 022
49
50 if [ "$IMGPROTO" = "file" ]; then
51     TEST_IMG=$TEST_DIR/t.$IMGFMT
52 else
53     TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT
54 fi
55
56 function 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 _get_pids_by_name()
149 {
150     if [ $# -ne 1 ]
151     then
152         echo "Usage: _get_pids_by_name process-name" 1>&2
153         exit 1
154     fi
155
156     # Algorithm ... all ps(1) variants have a time of the form MM:SS or
157     # HH:MM:SS before the psargs field, use this as the search anchor.
158     #
159     # Matches with $1 (process-name) occur if the first psarg is $1
160     # or ends in /$1 ... the matching uses sed's regular expressions,
161     # so passing a regex into $1 will work.
162
163     ps $PS_ALL_FLAGS \
164     | sed -n \
165         -e 's/$/ /' \
166         -e 's/[         ][      ]*/ /g' \
167         -e 's/^ //' \
168         -e 's/^[^ ]* //' \
169         -e "/[0-9]:[0-9][0-9]  *[^ ]*\/$1 /s/ .*//p" \
170         -e "/[0-9]:[0-9][0-9]  *$1 /s/ .*//p"
171 }
172
173 # fqdn for localhost
174 #
175 _get_fqdn()
176 {
177     host=`hostname`
178     $NSLOOKUP_PROG $host | $AWK_PROG '{ if ($1 == "Name:") print $2 }'
179 }
180
181 # check if run as root
182 #
183 _need_to_be_root()
184 {
185     id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'`
186     if [ "$id" -ne 0 ]
187     then
188         echo "Arrgh ... you need to be root (not uid=$id) to run this test"
189         exit 1
190     fi
191 }
192
193
194 # Do a command, log it to $seq.full, optionally test return status
195 # and die if command fails. If called with one argument _do executes the
196 # command, logs it, and returns its exit status. With two arguments _do
197 # first prints the message passed in the first argument, and then "done"
198 # or "fail" depending on the return status of the command passed in the
199 # second argument. If the command fails and the variable _do_die_on_error
200 # is set to "always" or the two argument form is used and _do_die_on_error
201 # is set to "message_only" _do will print an error message to
202 # $seq.out and exit.
203
204 _do()
205 {
206     if [ $# -eq 1 ]; then
207         _cmd=$1
208     elif [ $# -eq 2 ]; then
209         _note=$1
210         _cmd=$2
211         echo -n "$_note... "
212     else
213         echo "Usage: _do [note] cmd" 1>&2
214         status=1; exit
215     fi
216
217     (eval "echo '---' \"$_cmd\"") >>$here/$seq.full
218     (eval "$_cmd") >$tmp._out 2>&1; ret=$?
219     cat $tmp._out >>$here/$seq.full
220     if [ $# -eq 2 ]; then
221         if [ $ret -eq 0 ]; then
222             echo "done"
223         else
224             echo "fail"
225         fi
226     fi
227     if [ $ret -ne 0  ] \
228         && [ "$_do_die_on_error" = "always" \
229             -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
230     then
231         [ $# -ne 2 ] && echo
232         eval "echo \"$_cmd\" failed \(returned $ret\): see $seq.full"
233         status=1; exit
234     fi
235
236     return $ret
237 }
238
239 # bail out, setting up .notrun file
240 #
241 _notrun()
242 {
243     echo "$*" >$seq.notrun
244     echo "$seq not run: $*"
245     status=0
246     exit
247 }
248
249 # just plain bail out
250 #
251 _fail()
252 {
253     echo "$*" | tee -a $here/$seq.full
254     echo "(see $seq.full for details)"
255     status=1
256     exit 1
257 }
258
259 # tests whether $IMGFMT is one of the supported image formats for a test
260 #
261 _supported_fmt()
262 {
263     for f; do
264         if [ "$f" = "$IMGFMT" -o "$f" = "generic" ]; then
265             return
266         fi
267     done
268
269     _notrun "not suitable for this image format: $IMGFMT"
270 }
271
272 # tests whether $IMGPROTO is one of the supported image protocols for a test
273 #
274 _supported_proto()
275 {
276     for f; do
277         if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then
278             return
279         fi
280     done
281
282     _notrun "not suitable for this image protocol: $IMGPROTO"
283 }
284
285 # tests whether the host OS is one of the supported OSes for a test
286 #
287 _supported_os()
288 {
289     for h
290     do
291         if [ "$h" = "$HOSTOS" ]
292         then
293             return
294         fi
295     done
296
297     _notrun "not suitable for this OS: $HOSTOS"
298 }
299
300 _unsupported_qemu_io_options()
301 {
302     for bad_opt
303     do
304         for opt in $QEMU_IO_OPTIONS
305         do
306             if [ "$bad_opt" = "$opt" ]
307             then
308                 _notrun "not suitable for qemu-io option: $bad_opt"
309             fi
310         done
311     done
312 }
313
314 # this test requires that a specified command (executable) exists
315 #
316 _require_command()
317 {
318     [ -x "$1" ] || _notrun "$1 utility required, skipped this test"
319 }
320
321 _full_imgfmt_details()
322 {
323     if [ -n "$IMGOPTS" ]; then
324         echo "$IMGFMT ($IMGOPTS)"
325     else
326         echo "$IMGFMT"
327     fi
328 }
329
330 _full_imgproto_details()
331 {
332     echo "$IMGPROTO"
333 }
334
335 _full_platform_details()
336 {
337     os=`uname -s`
338     host=`hostname -s`
339     kernel=`uname -r`
340     platform=`uname -m`
341     echo "$os/$platform $host $kernel"
342 }
343
344 _link_out_file()
345 {
346    if [ -z "$1" ]; then
347       echo Error must pass \$seq.
348       exit
349    fi
350    rm -f $1
351    if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then
352       ln -s $1.irix $1
353    elif [ "`uname`" == "Linux" ]; then
354       ln -s $1.linux $1
355    else
356       echo Error test $seq does not run on the operating system: `uname`
357       exit
358    fi
359 }
360
361 _die()
362 {
363         echo $@
364         exit 1
365 }
366
367 # make sure this script returns success
368 /bin/true