xref: /openbmc/qemu/tests/qemu-iotests/303 (revision 6fdc5bc1)
1#!/usr/bin/env python3
2# group: rw quick
3#
4# Test for dumping of qcow2 image metadata
5#
6# Copyright (c) 2020 Virtuozzo International GmbH
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
22import iotests
23import subprocess
24from iotests import qemu_img_create, qemu_io_log, file_path, log, \
25        verify_qcow2_zstd_compression
26
27iotests.script_initialize(supported_fmts=['qcow2'],
28                          unsupported_imgopts=['refcount_bits', 'compat'])
29verify_qcow2_zstd_compression()
30
31disk = file_path('disk')
32chunk = 1024 * 1024
33
34
35def create_bitmap(bitmap_number, disabled):
36    granularity = 1 << (14 + bitmap_number)
37    bitmap_name = 'bitmap-' + str(bitmap_number)
38    args = ['bitmap', '--add', '-g', f'{granularity}', '-f', iotests.imgfmt,
39            disk, bitmap_name]
40    if disabled:
41        args.append('--disable')
42
43    iotests.qemu_img(*args)
44
45
46def write_to_disk(offset, size):
47    write = f'write {offset} {size}'
48    qemu_io_log('-c', write, disk)
49
50
51def add_bitmap(num, begin, end, disabled):
52    log(f'Add bitmap {num}')
53    create_bitmap(num, disabled)
54    for i in range(begin, end):
55        write_to_disk((i) * chunk, chunk)
56    log('')
57
58
59def test(compression_type: str, json_output: bool) -> None:
60    qemu_img_create('-f', iotests.imgfmt,
61                    '-o', f'compression_type={compression_type}',
62                    disk, '10M')
63    add_bitmap(1, 0, 6, False)
64    add_bitmap(2, 6, 8, True)
65
66    cmd = ['./qcow2.py', disk, 'dump-header']
67    if json_output:
68        cmd.append('-j')
69
70    subprocess.run(cmd)
71
72
73test('zlib', False)
74test('zstd', True)
75