xref: /openbmc/qemu/tests/qemu-iotests/303 (revision 31e4c354)
1#!/usr/bin/env python3
2#
3# Test for dumping of qcow2 image metadata
4#
5# Copyright (c) 2020 Virtuozzo International GmbH
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
21import iotests
22import subprocess
23from iotests import qemu_img_create, qemu_io, file_path, log, filter_qemu_io
24
25iotests.script_initialize(supported_fmts=['qcow2'])
26
27disk = file_path('disk')
28chunk = 1024 * 1024
29
30
31def create_bitmap(bitmap_number, disabled):
32    granularity = 1 << (14 + bitmap_number)
33    bitmap_name = 'bitmap-' + str(bitmap_number)
34    args = ['bitmap', '--add', '-g', f'{granularity}', '-f', iotests.imgfmt,
35            disk, bitmap_name]
36    if disabled:
37        args.append('--disable')
38
39    iotests.qemu_img_pipe(*args)
40
41
42def write_to_disk(offset, size):
43    write = f'write {offset} {size}'
44    log(qemu_io('-c', write, disk), filters=[filter_qemu_io])
45
46
47def add_bitmap(num, begin, end, disabled):
48    log(f'Add bitmap {num}')
49    create_bitmap(num, disabled)
50    for i in range(begin, end):
51        write_to_disk((i) * chunk, chunk)
52    log('')
53
54
55qemu_img_create('-f', iotests.imgfmt, disk, '10M')
56
57add_bitmap(1, 0, 6, False)
58add_bitmap(2, 6, 8, True)
59dump = ['qcow2.py', disk, 'dump-header']
60subprocess.run(dump)
61# Dump the metadata in JSON format
62dump.append('-j')
63subprocess.run(dump)
64