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