aboutsummaryrefslogtreecommitdiff
path: root/tests/qemu-iotests/244
blob: 3e61fa25bb6047b59fc2e8d3ed5596fc7317b02d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#!/usr/bin/env bash
# group: rw auto quick
#
# Test qcow2 with external data files
#
# Copyright (C) 2019 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# creator
owner=kwolf@redhat.com

seq=$(basename $0)
echo "QA output created by $seq"

status=1	# failure is the default!

_cleanup()
{
    _cleanup_test_img
    _rm_test_img "$TEST_IMG.data"
    _rm_test_img "$TEST_IMG.src"
}
trap "_cleanup; exit \$status" 0 1 2 3 15

# get standard environment, filters and checks
. ./common.rc
. ./common.filter
. ./common.qemu

_supported_fmt qcow2
_supported_proto file
_supported_os Linux
# External data files do not work with compat=0.10, and because we use
# our own external data file, we cannot let the user specify one
_unsupported_imgopts 'compat=0.10' data_file

echo
echo "=== Create and open image with external data file ==="
echo

echo "With data file name in the image:"
_make_test_img -o "data_file=$TEST_IMG.data" 64M
_check_test_img

$QEMU_IO -c "open $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir
$QEMU_IO -c "open -odata-file.filename=$TEST_IMG.data $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir
$QEMU_IO -c "open -odata-file.filename=inexistent $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir

echo
echo "Data file required, but without data file name in the image:"
$QEMU_IMG amend -odata_file= $TEST_IMG

$QEMU_IO -c "open $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir
$QEMU_IO -c "open -odata-file.filename=$TEST_IMG.data $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir
$QEMU_IO -c "open -odata-file.filename=inexistent $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir

echo
echo "Setting data-file for an image with internal data:"
_make_test_img 64M

$QEMU_IO -c "open -odata-file.filename=$TEST_IMG.data $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir
$QEMU_IO -c "open -odata-file.filename=inexistent $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir

echo
echo "=== Conflicting features ==="
echo

echo "Convert to compressed target with data file:"
TEST_IMG="$TEST_IMG.src" _make_test_img 64M

$QEMU_IO -c 'write -P 0x11 0 1M' \
         -f $IMGFMT "$TEST_IMG.src" |
         _filter_qemu_io

$QEMU_IMG convert -f $IMGFMT -O $IMGFMT -c -odata_file="$TEST_IMG.data" \
    "$TEST_IMG.src" "$TEST_IMG"

echo
echo "Convert uncompressed, then write compressed data manually:"
$QEMU_IMG convert -f $IMGFMT -O $IMGFMT -odata_file="$TEST_IMG.data" \
    "$TEST_IMG.src" "$TEST_IMG"
$QEMU_IMG compare "$TEST_IMG.src" "$TEST_IMG"

$QEMU_IO -c 'write -c -P 0x22 0 1M' \
         -f $IMGFMT "$TEST_IMG" |
         _filter_qemu_io
_check_test_img

echo
echo "Take an internal snapshot:"

$QEMU_IMG snapshot -c test "$TEST_IMG"
_check_test_img

echo
echo "=== Standalone image with external data file (efficient) ==="
echo

_make_test_img -o "data_file=$TEST_IMG.data" 64M

echo -n "qcow2 file size before I/O: "
du -b $TEST_IMG | cut -f1

# Create image with the following layout
# 0-1 MB: Unallocated
# 1-2 MB: Written (pattern 0x11)
# 2-3 MB: Discarded
# 3-4 MB: Zero write over discarded space
# 4-5 MB: Zero write over written space
# 5-6 MB: Zero write over unallocated space

echo
$QEMU_IO -c 'write -P 0x11 1M 4M' \
         -c 'discard 2M 2M' \
         -c 'write -z 3M 3M' \
         -f $IMGFMT "$TEST_IMG" |
         _filter_qemu_io
_check_test_img

echo
$QEMU_IMG map --output=json "$TEST_IMG"

echo
$QEMU_IO -c 'read -P 0 0 1M' \
         -c 'read -P 0x11 1M 1M' \
         -c 'read -P 0 2M 4M' \
         -f $IMGFMT "$TEST_IMG" |
         _filter_qemu_io

# Zero clusters are only marked as such in the qcow2 metadata, but contain
# stale data in the external data file
echo
$QEMU_IO -c 'read -P 0 0 1M' \
         -c 'read -P 0x11 1M 1M' \
         -c 'read -P 0x11 4M 1M' \
         -c 'read -P 0 5M 1M' \
         -f raw "$TEST_IMG.data" |
         _filter_qemu_io


echo -n "qcow2 file size after I/O: "
du -b $TEST_IMG | cut -f1

echo
echo "=== Standalone image with external data file (valid raw) ==="
echo

_make_test_img -o "data_file=$TEST_IMG.data,data_file_raw=on" 64M

echo -n "qcow2 file size before I/O: "
du -b $TEST_IMG | cut -f1

echo
$QEMU_IO -c 'write -P 0x11 1M 4M' \
         -c 'discard 2M 2M' \
         -c 'write -z 3M 3M' \
         -f $IMGFMT "$TEST_IMG" |
         _filter_qemu_io
_check_test_img

echo
$QEMU_IMG map --output=json "$TEST_IMG"

echo
$QEMU_IO -c 'read -P 0 0 1M' \
         -c 'read -P 0x11 1M 1M' \
         -c 'read -P 0 2M 4M' \
         -f $IMGFMT "$TEST_IMG" |
         _filter_qemu_io

# Discarded clusters are only marked as such in the qcow2 metadata, but
# they can contain stale data in the external data file.  Instead, zero
# clusters must be zeroed in the external data file too.
echo
$QEMU_IO -c 'read -P 0 0 1M' \
         -c 'read -P 0x11 1M 1M' \
         -c 'read -P 0 3M 3M' \
         -f raw "$TEST_IMG".data |
         _filter_qemu_io

echo -n "qcow2 file size after I/O: "
du -b $TEST_IMG | cut -f1

echo
echo "=== bdrv_co_block_status test for file and offset=0 ==="
echo

_make_test_img -o "data_file=$TEST_IMG.data" 64M

$QEMU_IO -c 'write -P 0x11 0 1M' -f $IMGFMT "$TEST_IMG" | _filter_qemu_io
$QEMU_IO -c 'read -P 0x11 0 1M' -f $IMGFMT "$TEST_IMG" | _filter_qemu_io
$QEMU_IMG map --output=human "$TEST_IMG" | _filter_testdir
$QEMU_IMG map --output=json "$TEST_IMG"

echo
echo "=== Copy offloading ==="
echo

# Make use of copy offloading if the test host can provide it
_make_test_img -o "data_file=$TEST_IMG.data" 64M
$QEMU_IMG convert -f $IMGFMT -O $IMGFMT -n -C "$TEST_IMG.src" "$TEST_IMG"
$QEMU_IMG compare -f $IMGFMT -F $IMGFMT "$TEST_IMG.src" "$TEST_IMG"

# blkdebug doesn't support copy offloading, so this tests the error path
$QEMU_IMG amend -f $IMGFMT -o "data_file=blkdebug::$TEST_IMG.data" "$TEST_IMG"
$QEMU_IMG convert -f $IMGFMT -O $IMGFMT -n -C "$TEST_IMG.src" "$TEST_IMG"
$QEMU_IMG compare -f $IMGFMT -F $IMGFMT "$TEST_IMG.src" "$TEST_IMG"

echo
echo "=== Flushing should flush the data file ==="
echo

# We are going to flush a qcow2 file with a blkdebug node inserted
# between the qcow2 node and its data file node.  The blkdebug node
# will return an error for all flushes and so we if the data file is
# flushed, we will see qemu-io return an error.

# We need to write something or the flush will not do anything; we
# also need -t writeback so the write is not done as a FUA write
# (which would then fail thanks to the implicit flush)
$QEMU_IO -c 'write 0 512' -c flush \
    -t writeback \
    "json:{
         'driver': 'qcow2',
         'file': {
             'driver': 'file',
             'filename': '$TEST_IMG'
         },
         'data-file': {
             'driver': 'blkdebug',
             'inject-error': [{
                 'event': 'none',
                 'iotype': 'flush'
             }],
             'image': {
                 'driver': 'file',
                 'filename': '$TEST_IMG.data'
             }
         }
     }" \
    | _filter_qemu_io

result=${PIPESTATUS[0]}
echo

case $result in
    0)
        echo "ERROR: qemu-io succeeded, so the data file was not flushed"
        ;;
    1)
        echo "Success: qemu-io failed, so the data file was flushed"
        ;;
    *)
        echo "ERROR: qemu-io returned unknown exit code $result"
        ;;
esac

echo
echo '=== Preallocation with data-file-raw ==='

echo
echo '--- Using a non-zeroed data file ---'

# Using data-file-raw must enforce at least metadata preallocation so
# that it does not matter whether one reads the raw file or the qcow2
# file

# Pre-create the data file, write some data.  Real-world use cases for
# this are adding a qcow2 metadata file to a block device (i.e., using
# the device as the data file) or adding qcow2 features to pre-existing
# raw images (e.g. because the user now wants persistent dirty bitmaps).
truncate -s 1M "$TEST_IMG.data"
$QEMU_IO -f raw -c 'write -P 42 0 1M' "$TEST_IMG.data" | _filter_qemu_io

# We cannot use qemu-img to create the qcow2 image, because it would
# clear the data file.  Use the blockdev-create job instead, which will
# only format the qcow2 image file.
touch "$TEST_IMG"
_launch_qemu \
    -blockdev file,node-name=data,filename="$TEST_IMG.data" \
    -blockdev file,node-name=meta,filename="$TEST_IMG"

_send_qemu_cmd $QEMU_HANDLE '{ "execute": "qmp_capabilities" }' 'return'

_send_qemu_cmd $QEMU_HANDLE \
    '{ "execute": "blockdev-create",
       "arguments": {
           "job-id": "create",
           "options": {
               "driver": "qcow2",
               "size": '"$((1 * 1024 * 1024))"',
               "file": "meta",
               "data-file": "data",
               "data-file-raw": true
           } } }' \
    '"status": "concluded"'

_send_qemu_cmd $QEMU_HANDLE \
    '{ "execute": "job-dismiss", "arguments": { "id": "create" } }' \
    'return'

_cleanup_qemu

echo
echo 'Comparing pattern:'

# Reading from either the qcow2 file or the data file should return
# the same result:
$QEMU_IO -f raw -c 'read -P 42 0 1M' "$TEST_IMG.data" | _filter_qemu_io
$QEMU_IO -f $IMGFMT -c 'read -P 42 0 1M' "$TEST_IMG" | _filter_qemu_io

# For good measure
$QEMU_IMG compare -f raw "$TEST_IMG.data" "$TEST_IMG"

echo
echo '--- Truncation (growing) ---'

# Append some new data to the raw file, then resize the qcow2 image
# accordingly and see whether the new data is visible.  Technically
# that is not allowed, but it is reasonable behavior, so test it.
truncate -s 2M "$TEST_IMG.data"
$QEMU_IO -f raw -c 'write -P 84 1M 1M' "$TEST_IMG.data" | _filter_qemu_io

$QEMU_IMG resize "$TEST_IMG" 2M

echo
echo 'Comparing pattern:'

$QEMU_IO -f raw -c 'read -P 42 0 1M' -c 'read -P 84 1M 1M' "$TEST_IMG.data" \
    | _filter_qemu_io
$QEMU_IO -f $IMGFMT -c 'read -P 42 0 1M' -c 'read -P 84 1M 1M' "$TEST_IMG" \
    | _filter_qemu_io

$QEMU_IMG compare -f raw "$TEST_IMG.data" "$TEST_IMG"

echo
echo '--- Giving a backing file at runtime ---'

# qcow2 files with data-file-raw cannot have backing files given by
# their image header, but qemu will allow you to set a backing node at
# runtime -- it should not have any effect, though (because reading
# from the qcow2 node should return the same data as reading from the
# raw node).

_make_test_img -o "data_file=$TEST_IMG.data,data_file_raw=on" 1M
TEST_IMG="$TEST_IMG.base" _make_test_img 1M

# Write something that is not zero into the base image
$QEMU_IO -c 'write -P 42 0 1M' "$TEST_IMG.base" | _filter_qemu_io

echo
echo 'Comparing qcow2 image and raw data file:'

# $TEST_IMG and $TEST_IMG.data must show the same data at all times;
# that is, the qcow2 node must not fall through to the backing image
# at any point
$QEMU_IMG compare --image-opts \
    "driver=raw,file.filename=$TEST_IMG.data"  \
    "file.filename=$TEST_IMG,backing.file.filename=$TEST_IMG.base"

# success, all done
echo "*** done"
rm -f $seq.full
status=0