Skip to content

Commit 03a970b

Browse files
nirsebblake
authored andcommitted
iotests: Test convert to qcow2 compressed to NBD
Add test for "qemu-img convert -O qcow2 -c" to NBD target. The tests     create a OVA file and write compressed qcow2 disk content directly into the OVA file via qemu-nbd. Signed-off-by: Nir Soffer <[email protected]> Message-Id: <[email protected]> Tested-by: Eric Blake <[email protected]> Reviewed-by: Eric Blake <[email protected]> Signed-off-by: Eric Blake <[email protected]>
1 parent 4b914b0 commit 03a970b

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed

tests/qemu-iotests/302

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Tests converting qcow2 compressed to NBD
4+
#
5+
# Copyright (c) 2020 Nir Soffer <[email protected]>
6+
#
7+
# This program is free software; you can redistribute it and/or modify
8+
# it under the terms of the GNU General Public License as published by
9+
# the Free Software Foundation; either version 2 of the License, or
10+
# (at your option) any later version.
11+
#
12+
# This program is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU General Public License
18+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
#
20+
21+
22+
import io
23+
import tarfile
24+
25+
import iotests
26+
27+
from iotests import (
28+
file_path,
29+
qemu_img,
30+
qemu_img_check,
31+
qemu_img_create,
32+
qemu_img_log,
33+
qemu_img_measure,
34+
qemu_io,
35+
qemu_nbd_popen,
36+
)
37+
38+
iotests.script_initialize(supported_fmts=["qcow2"])
39+
40+
# Create source disk. Using qcow2 to enable strict comparing later, and
41+
# avoid issues with random filesystem on CI environment.
42+
src_disk = file_path("disk.qcow2")
43+
qemu_img_create("-f", iotests.imgfmt, src_disk, "1g")
44+
qemu_io("-f", iotests.imgfmt, "-c", "write 1m 64k", src_disk)
45+
46+
# The use case is writing qcow2 image directly into an ova file, which
47+
# is a tar file with specific layout. This is tricky since we don't know the
48+
# size of the image before compressing, so we have to do:
49+
# 1. Add an ovf file.
50+
# 2. Find the offset of the next member data.
51+
# 3. Make room for image data, allocating for the worst case.
52+
# 4. Write compressed image data into the tar.
53+
# 5. Add a tar entry with the actual image size.
54+
# 6. Shrink the tar to the actual size, aligned to 512 bytes.
55+
56+
tar_file = file_path("test.ova")
57+
58+
with tarfile.open(tar_file, "w") as tar:
59+
60+
# 1. Add an ovf file.
61+
62+
ovf_data = b"<xml/>"
63+
ovf = tarfile.TarInfo("vm.ovf")
64+
ovf.size = len(ovf_data)
65+
tar.addfile(ovf, io.BytesIO(ovf_data))
66+
67+
# 2. Find the offset of the next member data.
68+
69+
offset = tar.fileobj.tell() + 512
70+
71+
# 3. Make room for image data, allocating for the worst case.
72+
73+
measure = qemu_img_measure("-O", "qcow2", src_disk)
74+
tar.fileobj.truncate(offset + measure["required"])
75+
76+
# 4. Write compressed image data into the tar.
77+
78+
nbd_sock = file_path("nbd-sock", base_dir=iotests.sock_dir)
79+
nbd_uri = "nbd+unix:///exp?socket=" + nbd_sock
80+
81+
# Use raw format to allow creating qcow2 directly into tar file.
82+
with qemu_nbd_popen(
83+
"--socket", nbd_sock,
84+
"--export-name", "exp",
85+
"--format", "raw",
86+
"--offset", str(offset),
87+
tar_file):
88+
89+
iotests.log("=== Target image info ===")
90+
qemu_img_log("info", nbd_uri)
91+
92+
qemu_img(
93+
"convert",
94+
"-f", iotests.imgfmt,
95+
"-O", "qcow2",
96+
"-c",
97+
src_disk,
98+
nbd_uri)
99+
100+
iotests.log("=== Converted image info ===")
101+
qemu_img_log("info", nbd_uri)
102+
103+
iotests.log("=== Converted image check ===")
104+
qemu_img_log("check", nbd_uri)
105+
106+
iotests.log("=== Comparing to source disk ===")
107+
qemu_img_log("compare", src_disk, nbd_uri)
108+
109+
actual_size = qemu_img_check(nbd_uri)["image-end-offset"]
110+
111+
# 5. Add a tar entry with the actual image size.
112+
113+
disk = tarfile.TarInfo("disk")
114+
disk.size = actual_size
115+
tar.addfile(disk)
116+
117+
# 6. Shrink the tar to the actual size, aligned to 512 bytes.
118+
119+
tar_size = offset + (disk.size + 511) & ~511
120+
tar.fileobj.seek(tar_size)
121+
tar.fileobj.truncate(tar_size)
122+
123+
with tarfile.open(tar_file) as tar:
124+
members = [{"name": m.name, "size": m.size, "offset": m.offset_data}
125+
for m in tar]
126+
iotests.log("=== OVA file contents ===")
127+
iotests.log(members)

tests/qemu-iotests/302.out

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Start NBD server
2+
=== Target image info ===
3+
image: nbd+unix:///exp?socket=SOCK_DIR/PID-nbd-sock
4+
file format: raw
5+
virtual size: 448 KiB (458752 bytes)
6+
disk size: unavailable
7+
8+
=== Converted image info ===
9+
image: nbd+unix:///exp?socket=SOCK_DIR/PID-nbd-sock
10+
file format: qcow2
11+
virtual size: 1 GiB (1073741824 bytes)
12+
disk size: unavailable
13+
cluster_size: 65536
14+
Format specific information:
15+
compat: 1.1
16+
compression type: zlib
17+
lazy refcounts: false
18+
refcount bits: 16
19+
corrupt: false
20+
21+
=== Converted image check ===
22+
No errors were found on the image.
23+
1/16384 = 0.01% allocated, 100.00% fragmented, 100.00% compressed clusters
24+
Image end offset: 393216
25+
26+
=== Comparing to source disk ===
27+
Images are identical.
28+
29+
Kill NBD server
30+
=== OVA file contents ===
31+
[{"name": "vm.ovf", "offset": 512, "size": 6}, {"name": "disk", "offset": 1536, "size": 393216}]

tests/qemu-iotests/group

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,4 @@
308308
297 meta
309309
299 auto quick
310310
301 backing quick
311+
302 quick

0 commit comments

Comments
 (0)