]>
Commit | Line | Data |
---|---|---|
11a82d14 | 1 | #!/usr/bin/env bash |
6bf19c94 CH |
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 | |
e8c212d6 | 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
6bf19c94 CH |
18 | # |
19 | ||
bde36af1 PMD |
20 | SED= |
21 | for sed in sed gsed; do | |
22 | ($sed --version | grep 'GNU sed') > /dev/null 2>&1 | |
23 | if [ "$?" -eq 0 ]; then | |
24 | SED=$sed | |
25 | break | |
26 | fi | |
27 | done | |
28 | if [ -z "$SED" ]; then | |
29 | echo "$0: GNU sed not found" | |
30 | exit 1 | |
31 | fi | |
32 | ||
6bf19c94 CH |
33 | dd() |
34 | { | |
35 | if [ "$HOSTOS" == "Linux" ] | |
79e40ab1 KW |
36 | then |
37 | command dd --help | grep noxfer > /dev/null 2>&1 | |
38 | ||
39 | if [ "$?" -eq 0 ] | |
40 | then | |
41 | command dd status=noxfer $@ | |
42 | else | |
43 | command dd $@ | |
44 | fi | |
6bf19c94 | 45 | else |
79e40ab1 | 46 | command dd $@ |
6bf19c94 CH |
47 | fi |
48 | } | |
49 | ||
23ea2ecc SH |
50 | # poke_file 'test.img' 512 '\xff\xfe' |
51 | poke_file() | |
52 | { | |
53 | printf "$3" | dd "of=$1" bs=1 "seek=$2" conv=notrunc &>/dev/null | |
54 | } | |
2f8bb28f HR |
55 | |
56 | # poke_file_le $img_filename $offset $byte_width $value | |
57 | # Example: poke_file_le "$TEST_IMG" 512 2 65534 | |
58 | poke_file_le() | |
59 | { | |
60 | local img=$1 ofs=$2 len=$3 val=$4 str='' | |
61 | ||
62 | while ((len--)); do | |
63 | str+=$(printf '\\x%02x' $((val & 0xff))) | |
64 | val=$((val >> 8)) | |
65 | done | |
66 | ||
67 | poke_file "$img" "$ofs" "$str" | |
68 | } | |
69 | ||
70 | # poke_file_be $img_filename $offset $byte_width $value | |
71 | # Example: poke_file_be "$TEST_IMG" 512 2 65279 | |
72 | poke_file_be() | |
73 | { | |
74 | local img=$1 ofs=$2 len=$3 val=$4 | |
75 | local str=$(printf "%0$((len * 2))x\n" $val | sed 's/\(..\)/\\x\1/g') | |
76 | ||
77 | poke_file "$img" "$ofs" "$str" | |
78 | } | |
23ea2ecc | 79 | |
fc8ba423 HR |
80 | # peek_file_le 'test.img' 512 2 => 65534 |
81 | peek_file_le() | |
82 | { | |
69135eb3 EB |
83 | local val=0 shift=0 byte |
84 | ||
85 | # coreutils' od --endian is not portable, so manually assemble bytes. | |
86 | for byte in $(od -j"$2" -N"$3" -An -v -tu1 "$1"); do | |
87 | val=$(( val | (byte << shift) )) | |
88 | shift=$((shift + 8)) | |
89 | done | |
90 | printf %llu $val | |
fc8ba423 HR |
91 | } |
92 | ||
93 | # peek_file_be 'test.img' 512 2 => 65279 | |
94 | peek_file_be() | |
95 | { | |
69135eb3 EB |
96 | local val=0 byte |
97 | ||
98 | # coreutils' od --endian is not portable, so manually assemble bytes. | |
99 | for byte in $(od -j"$2" -N"$3" -An -v -tu1 "$1"); do | |
100 | val=$(( (val << 8) | byte )) | |
101 | done | |
102 | printf %llu $val | |
fc8ba423 HR |
103 | } |
104 | ||
69135eb3 EB |
105 | # peek_file_raw 'test.img' 512 2 => '\xff\xfe'. Do not use if the raw data |
106 | # is likely to contain \0 or trailing \n. | |
fc8ba423 HR |
107 | peek_file_raw() |
108 | { | |
109 | dd if="$1" bs=1 skip="$2" count="$3" status=none | |
110 | } | |
111 | ||
3817ce03 PB |
112 | |
113 | if ! . ./common.config | |
114 | then | |
4e670492 | 115 | echo "$0: failed to source common.config" |
3817ce03 | 116 | exit 1 |
6bf19c94 CH |
117 | fi |
118 | ||
036d8cbf AS |
119 | # Set the variables to the empty string to turn Valgrind off |
120 | # for specific processes, e.g. | |
121 | # $ VALGRIND_QEMU_IO= ./check -qcow2 -valgrind 015 | |
122 | ||
123 | : ${VALGRIND_QEMU_VM=$VALGRIND_QEMU} | |
124 | : ${VALGRIND_QEMU_IMG=$VALGRIND_QEMU} | |
125 | : ${VALGRIND_QEMU_IO=$VALGRIND_QEMU} | |
126 | : ${VALGRIND_QEMU_NBD=$VALGRIND_QEMU} | |
036d8cbf AS |
127 | |
128 | # The Valgrind own parameters may be set with | |
129 | # its environment variable VALGRIND_OPTS, e.g. | |
130 | # $ VALGRIND_OPTS="--leak-check=yes" ./check -qcow2 -valgrind 015 | |
131 | ||
132 | _qemu_proc_exec() | |
133 | { | |
134 | local VALGRIND_LOGFILE="$1" | |
135 | shift | |
8af224d6 | 136 | if [[ "${VALGRIND_QEMU}" == "y" && "${NO_VALGRIND}" != "y" ]]; then |
036d8cbf AS |
137 | exec valgrind --log-file="${VALGRIND_LOGFILE}" --error-exitcode=99 "$@" |
138 | else | |
139 | exec "$@" | |
140 | fi | |
141 | } | |
142 | ||
143 | _qemu_proc_valgrind_log() | |
144 | { | |
145 | local VALGRIND_LOGFILE="$1" | |
146 | local RETVAL="$2" | |
8af224d6 | 147 | if [[ "${VALGRIND_QEMU}" == "y" && "${NO_VALGRIND}" != "y" ]]; then |
036d8cbf AS |
148 | if [ $RETVAL == 99 ]; then |
149 | cat "${VALGRIND_LOGFILE}" | |
150 | fi | |
151 | rm -f "${VALGRIND_LOGFILE}" | |
152 | fi | |
153 | } | |
154 | ||
d1f2447a PB |
155 | _qemu_wrapper() |
156 | { | |
036d8cbf | 157 | local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind |
d1f2447a PB |
158 | ( |
159 | if [ -n "${QEMU_NEED_PID}" ]; then | |
160 | echo $BASHPID > "${QEMU_TEST_DIR}/qemu-${_QEMU_HANDLE}.pid" | |
161 | fi | |
036d8cbf AS |
162 | VALGRIND_QEMU="${VALGRIND_QEMU_VM}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \ |
163 | "$QEMU_PROG" $QEMU_OPTIONS "$@" | |
d1f2447a | 164 | ) |
036d8cbf AS |
165 | RETVAL=$? |
166 | _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL | |
167 | return $RETVAL | |
d1f2447a PB |
168 | } |
169 | ||
170 | _qemu_img_wrapper() | |
171 | { | |
036d8cbf AS |
172 | local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind |
173 | ( | |
174 | VALGRIND_QEMU="${VALGRIND_QEMU_IMG}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \ | |
175 | "$QEMU_IMG_PROG" $QEMU_IMG_OPTIONS "$@" | |
176 | ) | |
177 | RETVAL=$? | |
178 | _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL | |
179 | return $RETVAL | |
d1f2447a PB |
180 | } |
181 | ||
182 | _qemu_io_wrapper() | |
183 | { | |
184 | local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind | |
185 | local QEMU_IO_ARGS="$QEMU_IO_OPTIONS" | |
186 | if [ "$IMGOPTSSYNTAX" = "true" ]; then | |
187 | QEMU_IO_ARGS="--image-opts $QEMU_IO_ARGS" | |
188 | if [ -n "$IMGKEYSECRET" ]; then | |
189 | QEMU_IO_ARGS="--object secret,id=keysec0,data=$IMGKEYSECRET $QEMU_IO_ARGS" | |
190 | fi | |
191 | fi | |
d1f2447a | 192 | ( |
036d8cbf AS |
193 | VALGRIND_QEMU="${VALGRIND_QEMU_IO}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \ |
194 | "$QEMU_IO_PROG" $QEMU_IO_ARGS "$@" | |
d1f2447a PB |
195 | ) |
196 | RETVAL=$? | |
036d8cbf AS |
197 | _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL |
198 | return $RETVAL | |
d1f2447a PB |
199 | } |
200 | ||
201 | _qemu_nbd_wrapper() | |
202 | { | |
036d8cbf AS |
203 | local VALGRIND_LOGFILE="${TEST_DIR}"/$$.valgrind |
204 | ( | |
205 | VALGRIND_QEMU="${VALGRIND_QEMU_NBD}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \ | |
206 | "$QEMU_NBD_PROG" --pid-file="${QEMU_TEST_DIR}/qemu-nbd.pid" \ | |
207 | $QEMU_NBD_OPTIONS "$@" | |
208 | ) | |
209 | RETVAL=$? | |
210 | _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL | |
211 | return $RETVAL | |
d1f2447a PB |
212 | } |
213 | ||
8af224d6 AS |
214 | # Valgrind bug #409141 https://bugs.kde.org/show_bug.cgi?id=409141 |
215 | # Until valgrind 3.16+ is ubiquitous, we must work around a hang in | |
216 | # valgrind when issuing sigkill. Disable valgrind for this invocation. | |
217 | _NO_VALGRIND() | |
218 | { | |
219 | NO_VALGRIND="y" "$@" | |
220 | } | |
221 | ||
d1f2447a PB |
222 | export QEMU=_qemu_wrapper |
223 | export QEMU_IMG=_qemu_img_wrapper | |
224 | export QEMU_IO=_qemu_io_wrapper | |
225 | export QEMU_NBD=_qemu_nbd_wrapper | |
d1f2447a | 226 | |
076003f5 DB |
227 | if [ "$IMGOPTSSYNTAX" = "true" ]; then |
228 | DRIVER="driver=$IMGFMT" | |
cce293a2 PB |
229 | QEMU_IMG_EXTRA_ARGS="--image-opts $QEMU_IMG_EXTRA_ARGS" |
230 | if [ -n "$IMGKEYSECRET" ]; then | |
231 | QEMU_IMG_EXTRA_ARGS="--object secret,id=keysec0,data=$IMGKEYSECRET $QEMU_IMG_EXTRA_ARGS" | |
232 | fi | |
4e9b25fb DB |
233 | if [ "$IMGFMT" = "luks" ]; then |
234 | DRIVER="$DRIVER,key-secret=keysec0" | |
235 | fi | |
076003f5 DB |
236 | if [ "$IMGPROTO" = "file" ]; then |
237 | TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT | |
238 | TEST_IMG="$DRIVER,file.filename=$TEST_DIR/t.$IMGFMT" | |
239 | elif [ "$IMGPROTO" = "nbd" ]; then | |
240 | TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT | |
eb4ea9aa HR |
241 | TEST_IMG="$DRIVER,file.driver=nbd,file.type=unix" |
242 | TEST_IMG="$TEST_IMG,file.path=$SOCK_DIR/nbd" | |
076003f5 DB |
243 | elif [ "$IMGPROTO" = "ssh" ]; then |
244 | TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT | |
245 | TEST_IMG="$DRIVER,file.driver=ssh,file.host=127.0.0.1,file.path=$TEST_IMG_FILE" | |
246 | elif [ "$IMGPROTO" = "nfs" ]; then | |
247 | TEST_DIR="$DRIVER,file.driver=nfs,file.filename=nfs://127.0.0.1/$TEST_DIR" | |
e5b77eec | 248 | TEST_IMG=$TEST_DIR/t.$IMGFMT |
076003f5 DB |
249 | else |
250 | TEST_IMG="$DRIVER,file.driver=$IMGPROTO,file.filename=$TEST_DIR/t.$IMGFMT" | |
251 | fi | |
9cdfa1b3 | 252 | else |
cce293a2 | 253 | QEMU_IMG_EXTRA_ARGS= |
076003f5 DB |
254 | if [ "$IMGPROTO" = "file" ]; then |
255 | TEST_IMG=$TEST_DIR/t.$IMGFMT | |
256 | elif [ "$IMGPROTO" = "nbd" ]; then | |
257 | TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT | |
f3923a72 | 258 | TEST_IMG="nbd+unix:///?socket=$SOCK_DIR/nbd" |
076003f5 DB |
259 | elif [ "$IMGPROTO" = "ssh" ]; then |
260 | TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT | |
b8c1f901 | 261 | REMOTE_TEST_DIR="ssh://\\($USER@\\)\\?127.0.0.1\\(:[0-9]\\+\\)\\?$TEST_DIR" |
076003f5 DB |
262 | TEST_IMG="ssh://127.0.0.1$TEST_IMG_FILE" |
263 | elif [ "$IMGPROTO" = "nfs" ]; then | |
655ae6bb | 264 | TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT |
8908b253 | 265 | REMOTE_TEST_DIR="nfs://127.0.0.1$TEST_DIR" |
655ae6bb | 266 | TEST_IMG="nfs://127.0.0.1$TEST_IMG_FILE" |
076003f5 DB |
267 | else |
268 | TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT | |
269 | fi | |
9cdfa1b3 | 270 | fi |
59fa68f3 | 271 | ORIG_TEST_IMG="$TEST_IMG" |
6bf19c94 | 272 | |
cce293a2 | 273 | if [ -z "$TEST_DIR" ]; then |
e8d81a61 | 274 | TEST_DIR=$PWD/scratch |
cce293a2 PB |
275 | fi |
276 | ||
277 | QEMU_TEST_DIR="${TEST_DIR}" | |
278 | ||
279 | if [ ! -e "$TEST_DIR" ]; then | |
280 | mkdir "$TEST_DIR" | |
281 | fi | |
282 | ||
283 | if [ ! -d "$TEST_DIR" ]; then | |
49cedf17 | 284 | echo "common.rc: Error: \$TEST_DIR ($TEST_DIR) is not a directory" |
cce293a2 PB |
285 | exit 1 |
286 | fi | |
287 | ||
8908b253 KW |
288 | if [ -z "$REMOTE_TEST_DIR" ]; then |
289 | REMOTE_TEST_DIR="$TEST_DIR" | |
290 | fi | |
291 | ||
cce293a2 | 292 | if [ ! -d "$SAMPLE_IMG_DIR" ]; then |
49cedf17 | 293 | echo "common.rc: Error: \$SAMPLE_IMG_DIR ($SAMPLE_IMG_DIR) is not a directory" |
cce293a2 PB |
294 | exit 1 |
295 | fi | |
296 | ||
85edbd37 JC |
297 | _use_sample_img() |
298 | { | |
299 | SAMPLE_IMG_FILE="${1%\.bz2}" | |
300 | TEST_IMG="$TEST_DIR/$SAMPLE_IMG_FILE" | |
301 | bzcat "$SAMPLE_IMG_DIR/$1" > "$TEST_IMG" | |
302 | if [ $? -ne 0 ] | |
303 | then | |
304 | echo "_use_sample_img error, cannot extract '$SAMPLE_IMG_DIR/$1'" | |
305 | exit 1 | |
306 | fi | |
307 | } | |
308 | ||
2f9d4083 FZ |
309 | _stop_nbd_server() |
310 | { | |
311 | if [ -f "${QEMU_TEST_DIR}/qemu-nbd.pid" ]; then | |
312 | local QEMU_NBD_PID | |
313 | read QEMU_NBD_PID < "${QEMU_TEST_DIR}/qemu-nbd.pid" | |
314 | kill ${QEMU_NBD_PID} | |
f3923a72 | 315 | rm -f "${QEMU_TEST_DIR}/qemu-nbd.pid" "$SOCK_DIR/nbd" |
2f9d4083 FZ |
316 | fi |
317 | } | |
318 | ||
1b35b85a HR |
319 | # Gets the data_file value from IMGOPTS and replaces the '$TEST_IMG' |
320 | # pattern by '$1' | |
321 | # Caution: The replacement is done with sed, so $1 must be escaped | |
322 | # properly. (The delimiter is '#'.) | |
323 | _get_data_file() | |
324 | { | |
325 | if ! echo "$IMGOPTS" | grep -q 'data_file='; then | |
326 | return 1 | |
327 | fi | |
328 | ||
329 | echo "$IMGOPTS" | sed -e 's/.*data_file=\([^,]*\).*/\1/' \ | |
330 | | sed -e "s#\\\$TEST_IMG#$1#" | |
331 | } | |
332 | ||
6bf19c94 CH |
333 | _make_test_img() |
334 | { | |
335 | # extra qemu-img options can be added by tests | |
336 | # at least one argument (the image size) needs to be added | |
21af8148 | 337 | local extra_img_options="" |
89004368 | 338 | local optstr="" |
a9660664 | 339 | local img_name="" |
0018c03f JC |
340 | local use_backing=0 |
341 | local backing_file="" | |
b7e875b2 | 342 | local object_options="" |
8b6d7be6 | 343 | local opts_param=false |
eea871d0 | 344 | local misc_params=() |
a9660664 NT |
345 | |
346 | if [ -n "$TEST_IMG_FILE" ]; then | |
347 | img_name=$TEST_IMG_FILE | |
348 | else | |
349 | img_name=$TEST_IMG | |
350 | fi | |
89004368 KW |
351 | |
352 | if [ -n "$IMGOPTS" ]; then | |
1b35b85a HR |
353 | imgopts_expanded=$(echo "$IMGOPTS" | sed -e "s#\\\$TEST_IMG#$img_name#") |
354 | optstr=$(_optstr_add "$optstr" "$imgopts_expanded") | |
89004368 | 355 | fi |
b7e875b2 DB |
356 | if [ -n "$IMGKEYSECRET" ]; then |
357 | object_options="--object secret,id=keysec0,data=$IMGKEYSECRET" | |
358 | optstr=$(_optstr_add "$optstr" "key-secret=keysec0") | |
359 | fi | |
6bf19c94 | 360 | |
eea871d0 HR |
361 | for param; do |
362 | if [ "$use_backing" = "1" -a -z "$backing_file" ]; then | |
363 | backing_file=$param | |
364 | continue | |
8b6d7be6 HR |
365 | elif $opts_param; then |
366 | optstr=$(_optstr_add "$optstr" "$param") | |
367 | opts_param=false | |
368 | continue | |
eea871d0 HR |
369 | fi |
370 | ||
371 | case "$param" in | |
372 | -b) | |
373 | use_backing=1 | |
374 | ;; | |
375 | ||
8b6d7be6 HR |
376 | -o) |
377 | opts_param=true | |
378 | ;; | |
379 | ||
380 | --no-opts) | |
381 | optstr="" | |
382 | ;; | |
383 | ||
eea871d0 HR |
384 | *) |
385 | misc_params=("${misc_params[@]}" "$param") | |
386 | ;; | |
387 | esac | |
388 | done | |
389 | ||
f5a4bbd9 | 390 | if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then |
89004368 KW |
391 | optstr=$(_optstr_add "$optstr" "cluster_size=$CLUSTER_SIZE") |
392 | fi | |
393 | ||
394 | if [ -n "$optstr" ]; then | |
395 | extra_img_options="-o $optstr $extra_img_options" | |
8fc1024c KW |
396 | fi |
397 | ||
2f9d4083 FZ |
398 | if [ $IMGPROTO = "nbd" ]; then |
399 | _stop_nbd_server | |
400 | fi | |
401 | ||
6bf19c94 | 402 | # XXX(hch): have global image options? |
0018c03f JC |
403 | ( |
404 | if [ $use_backing = 1 ]; then | |
eea871d0 | 405 | $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options -b "$backing_file" "$img_name" "${misc_params[@]}" 2>&1 |
0018c03f | 406 | else |
eea871d0 | 407 | $QEMU_IMG create $object_options -f $IMGFMT $extra_img_options "$img_name" "${misc_params[@]}" 2>&1 |
0018c03f | 408 | fi |
6ffb4cb6 | 409 | ) | _filter_img_create |
a9660664 | 410 | |
f3923a72 EB |
411 | # Start an NBD server on the image file, which is what we'll be talking to. |
412 | # Once NBD gains resize support, we may also want to use -f raw at the | |
413 | # server and interpret format over NBD, but for now, the format is | |
414 | # interpreted at the server and raw data sent over NBD. | |
a9660664 | 415 | if [ $IMGPROTO = "nbd" ]; then |
03a0aa37 HR |
416 | # Pass a sufficiently high number to -e that should be enough for all |
417 | # tests | |
f3923a72 | 418 | eval "$QEMU_NBD -v -t -k '$SOCK_DIR/nbd' -f $IMGFMT -e 42 -x '' $TEST_IMG_FILE >/dev/null &" |
a9660664 NT |
419 | sleep 1 # FIXME: qemu-nbd needs to be listening before we continue |
420 | fi | |
6bf19c94 CH |
421 | } |
422 | ||
487c1910 FZ |
423 | _rm_test_img() |
424 | { | |
425 | local img=$1 | |
426 | if [ "$IMGFMT" = "vmdk" ]; then | |
427 | # Remove all the extents for vmdk | |
c5575274 | 428 | "$QEMU_IMG" info "$img" 2>/dev/null | grep 'filename:' | cut -f 2 -d: \ |
487c1910 | 429 | | xargs -I {} rm -f "{}" |
1b35b85a HR |
430 | elif [ "$IMGFMT" = "qcow2" ]; then |
431 | # Remove external data file | |
432 | if data_file=$(_get_data_file "$img"); then | |
433 | rm -f "$data_file" | |
434 | fi | |
487c1910 | 435 | fi |
c5575274 | 436 | rm -f "$img" |
487c1910 FZ |
437 | } |
438 | ||
6bf19c94 CH |
439 | _cleanup_test_img() |
440 | { | |
9cdfa1b3 MK |
441 | case "$IMGPROTO" in |
442 | ||
a9660664 | 443 | nbd) |
2f9d4083 | 444 | _stop_nbd_server |
fef9c191 | 445 | rm -f "$TEST_IMG_FILE" |
a9660664 | 446 | ;; |
ae0c0a3d | 447 | |
9cdfa1b3 | 448 | file) |
487c1910 FZ |
449 | _rm_test_img "$TEST_DIR/t.$IMGFMT" |
450 | _rm_test_img "$TEST_DIR/t.$IMGFMT.orig" | |
451 | _rm_test_img "$TEST_DIR/t.$IMGFMT.base" | |
85edbd37 JC |
452 | if [ -n "$SAMPLE_IMG_FILE" ] |
453 | then | |
454 | rm -f "$TEST_DIR/$SAMPLE_IMG_FILE" | |
59fa68f3 KW |
455 | SAMPLE_IMG_FILE= |
456 | TEST_IMG="$ORIG_TEST_IMG" | |
85edbd37 | 457 | fi |
9cdfa1b3 MK |
458 | ;; |
459 | ||
460 | rbd) | |
9147d019 | 461 | rbd --no-progress rm "$TEST_DIR/t.$IMGFMT" > /dev/null |
9cdfa1b3 MK |
462 | ;; |
463 | ||
464 | sheepdog) | |
fef9c191 | 465 | collie vdi delete "$TEST_DIR/t.$IMGFMT" |
9cdfa1b3 MK |
466 | ;; |
467 | ||
468 | esac | |
6bf19c94 CH |
469 | } |
470 | ||
471 | _check_test_img() | |
472 | { | |
076003f5 DB |
473 | ( |
474 | if [ "$IMGOPTSSYNTAX" = "true" ]; then | |
475 | $QEMU_IMG check $QEMU_IMG_EXTRA_ARGS "$@" "$TEST_IMG" 2>&1 | |
476 | else | |
477 | $QEMU_IMG check "$@" -f $IMGFMT "$TEST_IMG" 2>&1 | |
478 | fi | |
86ce1f6e | 479 | ) | _filter_testdir | _filter_qemu_img_check |
ee1e66d9 VSO |
480 | |
481 | # return real qemu_img check status, to analyze in | |
482 | # _check_test_img_ignore_leaks | |
483 | return ${PIPESTATUS[0]} | |
484 | } | |
485 | ||
486 | _check_test_img_ignore_leaks() | |
487 | { | |
488 | out=$(_check_test_img "$@") | |
489 | status=$? | |
490 | if [ $status = 3 ]; then | |
491 | # This must correspond to success output in dump_human_image_check() | |
492 | echo "No errors were found on the image." | |
493 | return 0 | |
494 | fi | |
495 | echo "$out" | |
496 | return $status | |
6bf19c94 CH |
497 | } |
498 | ||
514d9da5 SH |
499 | _img_info() |
500 | { | |
e800e5d4 KW |
501 | if [[ "$1" == "--format-specific" ]]; then |
502 | local format_specific=1 | |
503 | shift | |
504 | else | |
505 | local format_specific=0 | |
506 | fi | |
507 | ||
4c2e9465 HR |
508 | discard=0 |
509 | regex_json_spec_start='^ *"format-specific": \{' | |
d06195e6 | 510 | $QEMU_IMG info $QEMU_IMG_EXTRA_ARGS "$@" "$TEST_IMG" 2>&1 | \ |
8908b253 KW |
511 | sed -e "s#$REMOTE_TEST_DIR#TEST_DIR#g" \ |
512 | -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \ | |
514d9da5 SH |
513 | -e "s#$TEST_DIR#TEST_DIR#g" \ |
514 | -e "s#$IMGFMT#IMGFMT#g" \ | |
515 | -e "/^disk size:/ D" \ | |
4c2e9465 | 516 | -e "/actual-size/ D" | \ |
6dd6d7ab | 517 | while IFS='' read -r line; do |
e800e5d4 KW |
518 | if [[ $format_specific == 1 ]]; then |
519 | discard=0 | |
520 | elif [[ $line == "Format specific information:" ]]; then | |
4c2e9465 HR |
521 | discard=1 |
522 | elif [[ $line =~ $regex_json_spec_start ]]; then | |
523 | discard=2 | |
524 | regex_json_spec_end="^${line%%[^ ]*}\\},? *$" | |
525 | fi | |
526 | if [[ $discard == 0 ]]; then | |
527 | echo "$line" | |
528 | elif [[ $discard == 1 && ! $line ]]; then | |
529 | echo | |
530 | discard=0 | |
531 | elif [[ $discard == 2 && $line =~ $regex_json_spec_end ]]; then | |
532 | discard=0 | |
533 | fi | |
534 | done | |
514d9da5 SH |
535 | } |
536 | ||
6bf19c94 CH |
537 | # bail out, setting up .notrun file |
538 | # | |
539 | _notrun() | |
540 | { | |
e8f8624d | 541 | echo "$*" >"$OUTPUT_DIR/$seq.notrun" |
6bf19c94 CH |
542 | echo "$seq not run: $*" |
543 | status=0 | |
544 | exit | |
545 | } | |
546 | ||
5ff1c2c8 AS |
547 | # bail out, setting up .casenotrun file |
548 | # The function _casenotrun() is used as a notifier. It is the | |
549 | # caller's responsibility to make skipped a particular test. | |
550 | # | |
551 | _casenotrun() | |
552 | { | |
553 | echo " [case not run] $*" >>"$OUTPUT_DIR/$seq.casenotrun" | |
554 | } | |
555 | ||
6bf19c94 CH |
556 | # just plain bail out |
557 | # | |
558 | _fail() | |
559 | { | |
e8f8624d | 560 | echo "$*" | tee -a "$OUTPUT_DIR/$seq.full" |
6bf19c94 CH |
561 | echo "(see $seq.full for details)" |
562 | status=1 | |
563 | exit 1 | |
564 | } | |
565 | ||
566 | # tests whether $IMGFMT is one of the supported image formats for a test | |
567 | # | |
568 | _supported_fmt() | |
569 | { | |
47f73da0 SH |
570 | # "generic" is suitable for most image formats. For some formats it doesn't |
571 | # work, however (most notably read-only formats), so they can opt out by | |
572 | # setting IMGFMT_GENERIC to false. | |
6bf19c94 | 573 | for f; do |
89e91181 | 574 | if [ "$f" = "$IMGFMT" -o "$f" = "generic" -a "$IMGFMT_GENERIC" = "true" ]; then |
d2a839ed HR |
575 | if [ "$IMGFMT" = "luks" ]; then |
576 | _require_working_luks | |
577 | fi | |
79e40ab1 KW |
578 | return |
579 | fi | |
6bf19c94 CH |
580 | done |
581 | ||
582 | _notrun "not suitable for this image format: $IMGFMT" | |
583 | } | |
584 | ||
b4a2caa4 NS |
585 | # tests whether $IMGFMT is one of the unsupported image format for a test |
586 | # | |
587 | _unsupported_fmt() | |
588 | { | |
589 | for f; do | |
590 | if [ "$f" = "$IMGFMT" ]; then | |
591 | _notrun "not suitable for this image format: $IMGFMT" | |
592 | fi | |
593 | done | |
594 | } | |
595 | ||
9cdfa1b3 MK |
596 | # tests whether $IMGPROTO is one of the supported image protocols for a test |
597 | # | |
598 | _supported_proto() | |
599 | { | |
600 | for f; do | |
79e40ab1 KW |
601 | if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then |
602 | return | |
603 | fi | |
9cdfa1b3 MK |
604 | done |
605 | ||
606 | _notrun "not suitable for this image protocol: $IMGPROTO" | |
607 | } | |
608 | ||
dfac03dc JC |
609 | # tests whether $IMGPROTO is specified as an unsupported image protocol for a test |
610 | # | |
611 | _unsupported_proto() | |
612 | { | |
613 | for f; do | |
614 | if [ "$f" = "$IMGPROTO" ]; then | |
615 | _notrun "not suitable for this image protocol: $IMGPROTO" | |
616 | return | |
617 | fi | |
618 | done | |
619 | } | |
620 | ||
6bf19c94 CH |
621 | # tests whether the host OS is one of the supported OSes for a test |
622 | # | |
623 | _supported_os() | |
624 | { | |
625 | for h | |
626 | do | |
79e40ab1 KW |
627 | if [ "$h" = "$HOSTOS" ] |
628 | then | |
629 | return | |
630 | fi | |
6bf19c94 CH |
631 | done |
632 | ||
633 | _notrun "not suitable for this OS: $HOSTOS" | |
634 | } | |
635 | ||
f210a83c | 636 | _supported_cache_modes() |
166f3c7b | 637 | { |
f210a83c FZ |
638 | for mode; do |
639 | if [ "$mode" = "$CACHEMODE" ]; then | |
640 | return | |
641 | fi | |
166f3c7b | 642 | done |
f210a83c FZ |
643 | _notrun "not suitable for cache mode: $CACHEMODE" |
644 | } | |
645 | ||
cfdca2b9 VSO |
646 | # Check whether the filesystem supports O_DIRECT |
647 | _check_o_direct() | |
648 | { | |
649 | $QEMU_IMG create -f raw "$TEST_IMG".test_o_direct 1M > /dev/null | |
650 | out=$($QEMU_IO -f raw -t none -c quit "$TEST_IMG".test_o_direct 2>&1) | |
651 | rm -f "$TEST_IMG".test_o_direct | |
652 | ||
653 | [[ "$out" != *"O_DIRECT"* ]] | |
654 | } | |
655 | ||
656 | _require_o_direct() | |
657 | { | |
658 | if ! _check_o_direct; then | |
659 | _notrun "file system on $TEST_DIR does not support O_DIRECT" | |
660 | fi | |
661 | } | |
662 | ||
663 | _check_cache_mode() | |
664 | { | |
665 | if [ $CACHEMODE == "none" ] || [ $CACHEMODE == "directsync" ]; then | |
666 | _require_o_direct | |
667 | fi | |
668 | } | |
669 | ||
670 | _check_cache_mode | |
671 | ||
672 | # $1 - cache mode to use by default | |
673 | # $2 - (optional) cache mode to use by default if O_DIRECT is not supported | |
f210a83c FZ |
674 | _default_cache_mode() |
675 | { | |
676 | if $CACHEMODE_IS_DEFAULT; then | |
cfdca2b9 VSO |
677 | if [ -z "$2" ] || _check_o_direct; then |
678 | CACHEMODE="$1" | |
679 | else | |
680 | CACHEMODE="$2" | |
681 | fi | |
682 | QEMU_IO="$QEMU_IO --cache $CACHEMODE" | |
683 | _check_cache_mode | |
f210a83c FZ |
684 | return |
685 | fi | |
166f3c7b | 686 | } |
7156ca48 AM |
687 | _supported_aio_modes() |
688 | { | |
689 | for mode; do | |
690 | if [ "$mode" = "$AIOMODE" ]; then | |
691 | return | |
692 | fi | |
693 | done | |
694 | _notrun "not suitable for aio mode: $AIOMODE" | |
695 | } | |
696 | _default_aio_mode() | |
697 | { | |
698 | AIOMODE="$1" | |
699 | QEMU_IO="$QEMU_IO --aio $1" | |
700 | } | |
166f3c7b | 701 | |
2c77f52e FZ |
702 | _unsupported_imgopts() |
703 | { | |
704 | for bad_opt | |
705 | do | |
706 | if echo "$IMGOPTS" | grep -q 2>/dev/null "$bad_opt" | |
707 | then | |
708 | _notrun "not suitable for image option: $bad_opt" | |
709 | fi | |
710 | done | |
711 | } | |
712 | ||
dc4ab029 HR |
713 | # Caution: Overwrites $TEST_DIR/t.luks |
714 | _require_working_luks() | |
715 | { | |
716 | file="$TEST_DIR/t.luks" | |
717 | ||
718 | output=$( | |
719 | $QEMU_IMG create -f luks \ | |
720 | --object secret,id=sec0,data=hunter0 \ | |
721 | -o key-secret=sec0 \ | |
722 | -o iter-time=10 \ | |
723 | "$file" \ | |
724 | 1M \ | |
725 | 2>&1 | |
726 | ) | |
727 | status=$? | |
728 | ||
729 | IMGFMT='luks' _rm_test_img "$file" | |
730 | ||
731 | if [ $status != 0 ]; then | |
732 | reason=$(echo "$output" | grep "$file:" | $SED -e "s#.*$file: *##") | |
733 | if [ -z "$reason" ]; then | |
734 | reason="Failed to create a LUKS image" | |
735 | fi | |
736 | _notrun "$reason" | |
737 | fi | |
738 | } | |
739 | ||
6bf19c94 CH |
740 | # this test requires that a specified command (executable) exists |
741 | # | |
742 | _require_command() | |
743 | { | |
934659c4 HR |
744 | if [ "$1" = "QEMU" ]; then |
745 | c=$QEMU_PROG | |
746 | elif [ "$1" = "QEMU_IMG" ]; then | |
747 | c=$QEMU_IMG_PROG | |
748 | elif [ "$1" = "QEMU_IO" ]; then | |
749 | c=$QEMU_IO_PROG | |
750 | elif [ "$1" = "QEMU_NBD" ]; then | |
751 | c=$QEMU_NBD_PROG | |
752 | else | |
753 | eval c=\$$1 | |
754 | fi | |
9c468a01 | 755 | [ -x "$c" ] || _notrun "$1 utility required, skipped this test" |
6bf19c94 CH |
756 | } |
757 | ||
21b43d00 TH |
758 | # Check that a set of drivers has been whitelisted in the QEMU binary |
759 | # | |
760 | _require_drivers() | |
761 | { | |
762 | available=$($QEMU -drive format=help | \ | |
763 | sed -e '/Supported formats:/!d' -e 's/Supported formats://') | |
764 | for driver | |
765 | do | |
766 | if ! echo "$available" | grep -q " $driver\( \|$\)"; then | |
767 | _notrun "$driver not available" | |
768 | fi | |
769 | done | |
770 | } | |
771 | ||
059f708d TH |
772 | # Check that we have a file system that allows huge (but very sparse) files |
773 | # | |
774 | _require_large_file() | |
775 | { | |
776 | if ! truncate --size="$1" "$TEST_IMG"; then | |
777 | _notrun "file system on $TEST_DIR does not support large enough files" | |
778 | fi | |
779 | rm "$TEST_IMG" | |
780 | } | |
781 | ||
9bdabfbe TH |
782 | # Check that a set of devices is available in the QEMU binary |
783 | # | |
784 | _require_devices() | |
785 | { | |
786 | available=$($QEMU -M none -device help | \ | |
787 | grep ^name | sed -e 's/^name "//' -e 's/".*$//') | |
788 | for device | |
789 | do | |
790 | if ! echo "$available" | grep -q "$device" ; then | |
791 | _notrun "$device not available" | |
792 | fi | |
793 | done | |
794 | } | |
795 | ||
6bf19c94 | 796 | # make sure this script returns success |
a2d9c0c4 | 797 | true |