blob: 3e61fa25bb6047b59fc2e8d3ed5596fc7317b02d [file] [log] [blame]
Thomas Huthf3d07ce2019-04-23 13:46:49 +02001#!/usr/bin/env bash
Vladimir Sementsov-Ogievskiy9dd003a2021-01-16 16:44:19 +03002# group: rw auto quick
Kevin Wolf76b90e22019-02-22 11:45:50 +01003#
4# Test qcow2 with external data files
5#
6# Copyright (C) 2019 Red Hat, Inc.
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program. If not, see <http://www.gnu.org/licenses/>.
20#
21
22# creator
23owner=kwolf@redhat.com
24
25seq=$(basename $0)
26echo "QA output created by $seq"
27
28status=1 # failure is the default!
29
30_cleanup()
31{
32 _cleanup_test_img
Max Reitzf91ecbd2019-11-07 17:37:01 +010033 _rm_test_img "$TEST_IMG.data"
34 _rm_test_img "$TEST_IMG.src"
Kevin Wolf76b90e22019-02-22 11:45:50 +010035}
36trap "_cleanup; exit \$status" 0 1 2 3 15
37
38# get standard environment, filters and checks
39. ./common.rc
40. ./common.filter
Max Reitz2ec7e8a2021-03-26 15:55:09 +010041. ./common.qemu
Kevin Wolf76b90e22019-02-22 11:45:50 +010042
43_supported_fmt qcow2
44_supported_proto file
45_supported_os Linux
Max Reitz3be20242019-11-07 17:37:07 +010046# External data files do not work with compat=0.10, and because we use
47# our own external data file, we cannot let the user specify one
48_unsupported_imgopts 'compat=0.10' data_file
Kevin Wolf76b90e22019-02-22 11:45:50 +010049
50echo
51echo "=== Create and open image with external data file ==="
52echo
53
54echo "With data file name in the image:"
Max Reitz407fb562019-11-07 17:36:57 +010055_make_test_img -o "data_file=$TEST_IMG.data" 64M
Kevin Wolf76b90e22019-02-22 11:45:50 +010056_check_test_img
57
58$QEMU_IO -c "open $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir
59$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
60$QEMU_IO -c "open -odata-file.filename=inexistent $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir
61
62echo
63echo "Data file required, but without data file name in the image:"
64$QEMU_IMG amend -odata_file= $TEST_IMG
65
66$QEMU_IO -c "open $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir
67$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
68$QEMU_IO -c "open -odata-file.filename=inexistent $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir
69
70echo
71echo "Setting data-file for an image with internal data:"
72_make_test_img 64M
73
74$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
75$QEMU_IO -c "open -odata-file.filename=inexistent $TEST_IMG" -c "read -P 0 0 64k" 2>&1 | _filter_qemu_io | _filter_testdir
76
77echo
78echo "=== Conflicting features ==="
79echo
80
81echo "Convert to compressed target with data file:"
82TEST_IMG="$TEST_IMG.src" _make_test_img 64M
83
84$QEMU_IO -c 'write -P 0x11 0 1M' \
85 -f $IMGFMT "$TEST_IMG.src" |
86 _filter_qemu_io
87
88$QEMU_IMG convert -f $IMGFMT -O $IMGFMT -c -odata_file="$TEST_IMG.data" \
89 "$TEST_IMG.src" "$TEST_IMG"
90
91echo
92echo "Convert uncompressed, then write compressed data manually:"
93$QEMU_IMG convert -f $IMGFMT -O $IMGFMT -odata_file="$TEST_IMG.data" \
94 "$TEST_IMG.src" "$TEST_IMG"
95$QEMU_IMG compare "$TEST_IMG.src" "$TEST_IMG"
96
97$QEMU_IO -c 'write -c -P 0x22 0 1M' \
98 -f $IMGFMT "$TEST_IMG" |
99 _filter_qemu_io
100_check_test_img
101
102echo
103echo "Take an internal snapshot:"
104
105$QEMU_IMG snapshot -c test "$TEST_IMG"
106_check_test_img
107
108echo
109echo "=== Standalone image with external data file (efficient) ==="
110echo
111
Max Reitz407fb562019-11-07 17:36:57 +0100112_make_test_img -o "data_file=$TEST_IMG.data" 64M
Kevin Wolf76b90e22019-02-22 11:45:50 +0100113
114echo -n "qcow2 file size before I/O: "
115du -b $TEST_IMG | cut -f1
116
117# Create image with the following layout
118# 0-1 MB: Unallocated
119# 1-2 MB: Written (pattern 0x11)
120# 2-3 MB: Discarded
121# 3-4 MB: Zero write over discarded space
122# 4-5 MB: Zero write over written space
123# 5-6 MB: Zero write over unallocated space
124
125echo
126$QEMU_IO -c 'write -P 0x11 1M 4M' \
127 -c 'discard 2M 2M' \
128 -c 'write -z 3M 3M' \
129 -f $IMGFMT "$TEST_IMG" |
130 _filter_qemu_io
131_check_test_img
132
133echo
134$QEMU_IMG map --output=json "$TEST_IMG"
135
136echo
137$QEMU_IO -c 'read -P 0 0 1M' \
138 -c 'read -P 0x11 1M 1M' \
139 -c 'read -P 0 2M 4M' \
140 -f $IMGFMT "$TEST_IMG" |
141 _filter_qemu_io
142
143# Zero clusters are only marked as such in the qcow2 metadata, but contain
144# stale data in the external data file
145echo
146$QEMU_IO -c 'read -P 0 0 1M' \
147 -c 'read -P 0x11 1M 1M' \
Kevin Wolf76b90e22019-02-22 11:45:50 +0100148 -c 'read -P 0x11 4M 1M' \
149 -c 'read -P 0 5M 1M' \
150 -f raw "$TEST_IMG.data" |
151 _filter_qemu_io
152
153
154echo -n "qcow2 file size after I/O: "
155du -b $TEST_IMG | cut -f1
156
157echo
158echo "=== Standalone image with external data file (valid raw) ==="
159echo
160
Max Reitz407fb562019-11-07 17:36:57 +0100161_make_test_img -o "data_file=$TEST_IMG.data,data_file_raw=on" 64M
Kevin Wolf76b90e22019-02-22 11:45:50 +0100162
163echo -n "qcow2 file size before I/O: "
164du -b $TEST_IMG | cut -f1
165
166echo
167$QEMU_IO -c 'write -P 0x11 1M 4M' \
168 -c 'discard 2M 2M' \
169 -c 'write -z 3M 3M' \
170 -f $IMGFMT "$TEST_IMG" |
171 _filter_qemu_io
172_check_test_img
173
174echo
175$QEMU_IMG map --output=json "$TEST_IMG"
176
177echo
178$QEMU_IO -c 'read -P 0 0 1M' \
179 -c 'read -P 0x11 1M 1M' \
180 -c 'read -P 0 2M 4M' \
181 -f $IMGFMT "$TEST_IMG" |
182 _filter_qemu_io
183
Paolo Bonzini3fb61082020-04-09 15:10:06 -0400184# Discarded clusters are only marked as such in the qcow2 metadata, but
185# they can contain stale data in the external data file. Instead, zero
186# clusters must be zeroed in the external data file too.
Kevin Wolf76b90e22019-02-22 11:45:50 +0100187echo
Paolo Bonzini3fb61082020-04-09 15:10:06 -0400188$QEMU_IO -c 'read -P 0 0 1M' \
189 -c 'read -P 0x11 1M 1M' \
190 -c 'read -P 0 3M 3M' \
191 -f raw "$TEST_IMG".data |
192 _filter_qemu_io
Kevin Wolf76b90e22019-02-22 11:45:50 +0100193
194echo -n "qcow2 file size after I/O: "
195du -b $TEST_IMG | cut -f1
196
197echo
198echo "=== bdrv_co_block_status test for file and offset=0 ==="
199echo
200
Max Reitz407fb562019-11-07 17:36:57 +0100201_make_test_img -o "data_file=$TEST_IMG.data" 64M
Kevin Wolf76b90e22019-02-22 11:45:50 +0100202
203$QEMU_IO -c 'write -P 0x11 0 1M' -f $IMGFMT "$TEST_IMG" | _filter_qemu_io
204$QEMU_IO -c 'read -P 0x11 0 1M' -f $IMGFMT "$TEST_IMG" | _filter_qemu_io
205$QEMU_IMG map --output=human "$TEST_IMG" | _filter_testdir
206$QEMU_IMG map --output=json "$TEST_IMG"
207
Kevin Wolfa0cf8da2020-02-11 10:49:00 +0100208echo
209echo "=== Copy offloading ==="
210echo
211
212# Make use of copy offloading if the test host can provide it
213_make_test_img -o "data_file=$TEST_IMG.data" 64M
214$QEMU_IMG convert -f $IMGFMT -O $IMGFMT -n -C "$TEST_IMG.src" "$TEST_IMG"
215$QEMU_IMG compare -f $IMGFMT -F $IMGFMT "$TEST_IMG.src" "$TEST_IMG"
216
217# blkdebug doesn't support copy offloading, so this tests the error path
218$QEMU_IMG amend -f $IMGFMT -o "data_file=blkdebug::$TEST_IMG.data" "$TEST_IMG"
219$QEMU_IMG convert -f $IMGFMT -O $IMGFMT -n -C "$TEST_IMG.src" "$TEST_IMG"
220$QEMU_IMG compare -f $IMGFMT -F $IMGFMT "$TEST_IMG.src" "$TEST_IMG"
221
Max Reitzc3656252019-11-13 17:26:07 +0100222echo
223echo "=== Flushing should flush the data file ==="
224echo
225
226# We are going to flush a qcow2 file with a blkdebug node inserted
227# between the qcow2 node and its data file node. The blkdebug node
228# will return an error for all flushes and so we if the data file is
229# flushed, we will see qemu-io return an error.
230
231# We need to write something or the flush will not do anything; we
232# also need -t writeback so the write is not done as a FUA write
233# (which would then fail thanks to the implicit flush)
234$QEMU_IO -c 'write 0 512' -c flush \
235 -t writeback \
236 "json:{
237 'driver': 'qcow2',
238 'file': {
239 'driver': 'file',
240 'filename': '$TEST_IMG'
241 },
242 'data-file': {
243 'driver': 'blkdebug',
244 'inject-error': [{
245 'event': 'none',
246 'iotype': 'flush'
247 }],
248 'image': {
249 'driver': 'file',
250 'filename': '$TEST_IMG.data'
251 }
252 }
253 }" \
254 | _filter_qemu_io
255
256result=${PIPESTATUS[0]}
257echo
258
259case $result in
260 0)
261 echo "ERROR: qemu-io succeeded, so the data file was not flushed"
262 ;;
263 1)
264 echo "Success: qemu-io failed, so the data file was flushed"
265 ;;
266 *)
267 echo "ERROR: qemu-io returned unknown exit code $result"
268 ;;
269esac
270
Max Reitz2ec7e8a2021-03-26 15:55:09 +0100271echo
272echo '=== Preallocation with data-file-raw ==='
273
274echo
275echo '--- Using a non-zeroed data file ---'
276
277# Using data-file-raw must enforce at least metadata preallocation so
278# that it does not matter whether one reads the raw file or the qcow2
279# file
280
281# Pre-create the data file, write some data. Real-world use cases for
282# this are adding a qcow2 metadata file to a block device (i.e., using
283# the device as the data file) or adding qcow2 features to pre-existing
284# raw images (e.g. because the user now wants persistent dirty bitmaps).
285truncate -s 1M "$TEST_IMG.data"
286$QEMU_IO -f raw -c 'write -P 42 0 1M' "$TEST_IMG.data" | _filter_qemu_io
287
288# We cannot use qemu-img to create the qcow2 image, because it would
289# clear the data file. Use the blockdev-create job instead, which will
290# only format the qcow2 image file.
291touch "$TEST_IMG"
292_launch_qemu \
293 -blockdev file,node-name=data,filename="$TEST_IMG.data" \
294 -blockdev file,node-name=meta,filename="$TEST_IMG"
295
296_send_qemu_cmd $QEMU_HANDLE '{ "execute": "qmp_capabilities" }' 'return'
297
298_send_qemu_cmd $QEMU_HANDLE \
299 '{ "execute": "blockdev-create",
300 "arguments": {
301 "job-id": "create",
302 "options": {
303 "driver": "qcow2",
304 "size": '"$((1 * 1024 * 1024))"',
305 "file": "meta",
306 "data-file": "data",
307 "data-file-raw": true
308 } } }' \
309 '"status": "concluded"'
310
311_send_qemu_cmd $QEMU_HANDLE \
312 '{ "execute": "job-dismiss", "arguments": { "id": "create" } }' \
313 'return'
314
315_cleanup_qemu
316
317echo
318echo 'Comparing pattern:'
319
320# Reading from either the qcow2 file or the data file should return
321# the same result:
322$QEMU_IO -f raw -c 'read -P 42 0 1M' "$TEST_IMG.data" | _filter_qemu_io
323$QEMU_IO -f $IMGFMT -c 'read -P 42 0 1M' "$TEST_IMG" | _filter_qemu_io
324
325# For good measure
326$QEMU_IMG compare -f raw "$TEST_IMG.data" "$TEST_IMG"
327
328echo
329echo '--- Truncation (growing) ---'
330
331# Append some new data to the raw file, then resize the qcow2 image
332# accordingly and see whether the new data is visible. Technically
333# that is not allowed, but it is reasonable behavior, so test it.
334truncate -s 2M "$TEST_IMG.data"
335$QEMU_IO -f raw -c 'write -P 84 1M 1M' "$TEST_IMG.data" | _filter_qemu_io
336
337$QEMU_IMG resize "$TEST_IMG" 2M
338
339echo
340echo 'Comparing pattern:'
341
342$QEMU_IO -f raw -c 'read -P 42 0 1M' -c 'read -P 84 1M 1M' "$TEST_IMG.data" \
343 | _filter_qemu_io
344$QEMU_IO -f $IMGFMT -c 'read -P 42 0 1M' -c 'read -P 84 1M 1M' "$TEST_IMG" \
345 | _filter_qemu_io
346
347$QEMU_IMG compare -f raw "$TEST_IMG.data" "$TEST_IMG"
348
349echo
350echo '--- Giving a backing file at runtime ---'
351
352# qcow2 files with data-file-raw cannot have backing files given by
353# their image header, but qemu will allow you to set a backing node at
354# runtime -- it should not have any effect, though (because reading
355# from the qcow2 node should return the same data as reading from the
356# raw node).
357
358_make_test_img -o "data_file=$TEST_IMG.data,data_file_raw=on" 1M
359TEST_IMG="$TEST_IMG.base" _make_test_img 1M
360
361# Write something that is not zero into the base image
362$QEMU_IO -c 'write -P 42 0 1M' "$TEST_IMG.base" | _filter_qemu_io
363
364echo
365echo 'Comparing qcow2 image and raw data file:'
366
367# $TEST_IMG and $TEST_IMG.data must show the same data at all times;
368# that is, the qcow2 node must not fall through to the backing image
369# at any point
370$QEMU_IMG compare --image-opts \
371 "driver=raw,file.filename=$TEST_IMG.data" \
372 "file.filename=$TEST_IMG,backing.file.filename=$TEST_IMG.base"
373
Kevin Wolf76b90e22019-02-22 11:45:50 +0100374# success, all done
375echo "*** done"
376rm -f $seq.full
377status=0