xref: /openbmc/qemu/tests/qemu-iotests/303 (revision b30b8077)
1bf654b37SAndrey Shinkevich#!/usr/bin/env python3
29dd003a9SVladimir Sementsov-Ogievskiy# group: rw quick
3bf654b37SAndrey Shinkevich#
4bf654b37SAndrey Shinkevich# Test for dumping of qcow2 image metadata
5bf654b37SAndrey Shinkevich#
6bf654b37SAndrey Shinkevich# Copyright (c) 2020 Virtuozzo International GmbH
7bf654b37SAndrey Shinkevich#
8bf654b37SAndrey Shinkevich# This program is free software; you can redistribute it and/or modify
9bf654b37SAndrey Shinkevich# it under the terms of the GNU General Public License as published by
10bf654b37SAndrey Shinkevich# the Free Software Foundation; either version 2 of the License, or
11bf654b37SAndrey Shinkevich# (at your option) any later version.
12bf654b37SAndrey Shinkevich#
13bf654b37SAndrey Shinkevich# This program is distributed in the hope that it will be useful,
14bf654b37SAndrey Shinkevich# but WITHOUT ANY WARRANTY; without even the implied warranty of
15bf654b37SAndrey Shinkevich# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16bf654b37SAndrey Shinkevich# GNU General Public License for more details.
17bf654b37SAndrey Shinkevich#
18bf654b37SAndrey Shinkevich# You should have received a copy of the GNU General Public License
19bf654b37SAndrey Shinkevich# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20bf654b37SAndrey Shinkevich#
21bf654b37SAndrey Shinkevich
22bf654b37SAndrey Shinkevichimport iotests
23bf654b37SAndrey Shinkevichimport subprocess
24bf654b37SAndrey Shinkevichfrom iotests import qemu_img_create, qemu_io, file_path, log, filter_qemu_io
25bf654b37SAndrey Shinkevich
26*b30b8077SVladimir Sementsov-Ogievskiyiotests.script_initialize(supported_fmts=['qcow2'],
27*b30b8077SVladimir Sementsov-Ogievskiy                          unsupported_imgopts=['refcount_bits', 'compat'])
28bf654b37SAndrey Shinkevich
29bf654b37SAndrey Shinkevichdisk = file_path('disk')
30bf654b37SAndrey Shinkevichchunk = 1024 * 1024
31bf654b37SAndrey Shinkevich
32bf654b37SAndrey Shinkevich
33bf654b37SAndrey Shinkevichdef create_bitmap(bitmap_number, disabled):
34bf654b37SAndrey Shinkevich    granularity = 1 << (14 + bitmap_number)
35bf654b37SAndrey Shinkevich    bitmap_name = 'bitmap-' + str(bitmap_number)
36bf654b37SAndrey Shinkevich    args = ['bitmap', '--add', '-g', f'{granularity}', '-f', iotests.imgfmt,
37bf654b37SAndrey Shinkevich            disk, bitmap_name]
38bf654b37SAndrey Shinkevich    if disabled:
39bf654b37SAndrey Shinkevich        args.append('--disable')
40bf654b37SAndrey Shinkevich
41bf654b37SAndrey Shinkevich    iotests.qemu_img_pipe(*args)
42bf654b37SAndrey Shinkevich
43bf654b37SAndrey Shinkevich
44bf654b37SAndrey Shinkevichdef write_to_disk(offset, size):
45bf654b37SAndrey Shinkevich    write = f'write {offset} {size}'
46bf654b37SAndrey Shinkevich    log(qemu_io('-c', write, disk), filters=[filter_qemu_io])
47bf654b37SAndrey Shinkevich
48bf654b37SAndrey Shinkevich
49bf654b37SAndrey Shinkevichdef add_bitmap(num, begin, end, disabled):
50bf654b37SAndrey Shinkevich    log(f'Add bitmap {num}')
51bf654b37SAndrey Shinkevich    create_bitmap(num, disabled)
52bf654b37SAndrey Shinkevich    for i in range(begin, end):
53bf654b37SAndrey Shinkevich        write_to_disk((i) * chunk, chunk)
54bf654b37SAndrey Shinkevich    log('')
55bf654b37SAndrey Shinkevich
56bf654b37SAndrey Shinkevich
57bf654b37SAndrey Shinkevichqemu_img_create('-f', iotests.imgfmt, disk, '10M')
58bf654b37SAndrey Shinkevich
59bf654b37SAndrey Shinkevichadd_bitmap(1, 0, 6, False)
60bf654b37SAndrey Shinkevichadd_bitmap(2, 6, 8, True)
61fb07b5c7SVladimir Sementsov-Ogievskiydump = ['./qcow2.py', disk, 'dump-header']
62bf654b37SAndrey Shinkevichsubprocess.run(dump)
6376bbbb2dSAndrey Shinkevich# Dump the metadata in JSON format
6476bbbb2dSAndrey Shinkevichdump.append('-j')
6576bbbb2dSAndrey Shinkevichsubprocess.run(dump)
66