Merge tag 'v9.0.0-rc3'
[qemu/ar7.git] / tests / qemu-iotests / 302
bloba6d79e727b55dd2d0d0e6fb286507f5f87bfd3ef
1 #!/usr/bin/env python3
2 # group: quick
4 # Tests converting qcow2 compressed to NBD
6 # Copyright (c) 2020 Nir Soffer <nirsof@gmail.com>
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.
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.
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/>.
21 # owner=nirsof@gmail.com
23 import io
24 import tarfile
26 import iotests
28 from iotests import (
29     file_path,
30     qemu_img,
31     qemu_img_check,
32     qemu_img_create,
33     qemu_img_log,
34     qemu_img_measure,
35     qemu_io,
36     qemu_nbd_popen,
37     img_info_log,
40 iotests.script_initialize(supported_fmts=["qcow2"])
42 # Create source disk. Using qcow2 to enable strict comparing later, and
43 # avoid issues with random filesystem on CI environment.
44 src_disk = file_path("disk.qcow2")
45 qemu_img_create("-f", iotests.imgfmt, src_disk, "1g")
46 qemu_io("-f", iotests.imgfmt, "-c", "write 1m 64k", src_disk)
48 # The use case is writing qcow2 image directly into an ova file, which
49 # is a tar file with specific layout. This is tricky since we don't know the
50 # size of the image before compressing, so we have to do:
51 # 1. Add an ovf file.
52 # 2. Find the offset of the next member data.
53 # 3. Make room for image data, allocating for the worst case.
54 # 4. Write compressed image data into the tar.
55 # 5. Add a tar entry with the actual image size.
56 # 6. Shrink the tar to the actual size, aligned to 512 bytes.
58 tar_file = file_path("test.ova")
60 with tarfile.open(tar_file, "w") as tar:
62     # 1. Add an ovf file.
64     ovf_data = b"<xml/>"
65     ovf = tarfile.TarInfo("vm.ovf")
66     ovf.size = len(ovf_data)
67     tar.addfile(ovf, io.BytesIO(ovf_data))
69     # 2. Find the offset of the next member data.
71     offset = tar.fileobj.tell() + 512
73     # 3. Make room for image data, allocating for the worst case.
75     measure = qemu_img_measure("-O", "qcow2", src_disk)
76     tar.fileobj.truncate(offset + measure["required"])
78     # 4. Write compressed image data into the tar.
80     nbd_sock = file_path("nbd-sock", base_dir=iotests.sock_dir)
81     nbd_uri = "nbd+unix:///exp?socket=" + nbd_sock
83     # Use raw format to allow creating qcow2 directly into tar file.
84     with qemu_nbd_popen(
85             "--socket", nbd_sock,
86             "--export-name", "exp",
87             "--format", "raw",
88             "--offset", str(offset),
89             tar_file):
91         iotests.log("=== Target image info ===")
92         # Not img_info_log as it enforces imgfmt, but now we print info on raw
93         qemu_img_log("info", nbd_uri)
95         qemu_img(
96             "convert",
97             "-f", iotests.imgfmt,
98             "-O", "qcow2",
99             "-c",
100             src_disk,
101             nbd_uri)
103         iotests.log("=== Converted image info ===")
104         img_info_log(nbd_uri)
106         iotests.log("=== Converted image check ===")
107         qemu_img_log("check", nbd_uri)
109         iotests.log("=== Comparing to source disk ===")
110         qemu_img_log("compare", src_disk, nbd_uri)
112         actual_size = qemu_img_check(nbd_uri)["image-end-offset"]
114     # 5. Add a tar entry with the actual image size.
116     disk = tarfile.TarInfo("disk")
117     disk.size = actual_size
118     tar.addfile(disk)
120     # 6. Shrink the tar to the actual size, aligned to 512 bytes.
122     tar_size = offset + (disk.size + 511) & ~511
123     tar.fileobj.seek(tar_size)
124     tar.fileobj.truncate(tar_size)
126 with tarfile.open(tar_file) as tar:
127     members = [{"name": m.name, "size": m.size, "offset": m.offset_data}
128                for m in tar]
129     iotests.log("=== OVA file contents ===")
130     iotests.log(members)