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