xref: /openbmc/qemu/tests/qemu-iotests/242 (revision 6c3a9247)
1#!/usr/bin/env python3
2# group: rw quick
3#
4# Test for qcow2 bitmap printed information
5#
6# Copyright (c) 2019 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 json
24import struct
25from iotests import qemu_img_create, qemu_io, qemu_img_pipe, \
26    file_path, img_info_log, log, filter_qemu_io
27
28iotests.script_initialize(supported_fmts=['qcow2'],
29                          supported_protocols=['file'],
30                          unsupported_imgopts=['refcount_bits', 'compat'])
31
32disk = file_path('disk')
33chunk = 256 * 1024
34bitmap_flag_unknown = 1 << 2
35# flag_offset = 5*cluster_size + flag_offset_in_bitmap_directory_entry
36flag_offset = 0x5000f
37
38
39def print_bitmap(extra_args):
40    log('qemu-img info dump:\n')
41    img_info_log(disk, extra_args=extra_args)
42    result = json.loads(qemu_img_pipe('info', '--force-share',
43                                      '--output=json', disk))
44    if 'bitmaps' in result['format-specific']['data']:
45        bitmaps = result['format-specific']['data']['bitmaps']
46        log('The same bitmaps in JSON format:')
47        log(bitmaps, indent=2)
48    else:
49        log('No bitmap in JSON format output')
50
51
52def add_bitmap(bitmap_number, persistent, disabled):
53    granularity = 1 << (13 + bitmap_number)
54    bitmap_name = 'bitmap-' + str(bitmap_number-1)
55    vm = iotests.VM().add_drive(disk)
56    vm.launch()
57    vm.qmp_log('block-dirty-bitmap-add', node='drive0', name=bitmap_name,
58               granularity=granularity, persistent=persistent,
59               disabled=disabled)
60    vm.shutdown()
61
62
63def write_to_disk(offset, size):
64    write = 'write {} {}'.format(offset, size)
65    log(qemu_io('-c', write, disk), filters=[filter_qemu_io])
66
67
68def toggle_flag(offset):
69    with open(disk, "r+b") as f:
70        f.seek(offset, 0)
71        # Read one byte in a way compatible with Python 2
72        flags = struct.unpack("B", f.read(1))
73        toggled = flags[0] ^ bitmap_flag_unknown
74        f.seek(-1, 1)
75        f.write(struct.pack("B", toggled))
76
77
78qemu_img_create('-f', iotests.imgfmt, disk, '1M')
79
80for num in range(1, 4):
81    disabled = False
82    if num == 2:
83        disabled = True
84    log('Test {}'.format(num))
85    add_bitmap(num, num > 1, disabled)
86    write_to_disk((num-1) * chunk, chunk)
87    print_bitmap([])
88    log('')
89
90vm = iotests.VM().add_drive(disk)
91vm.launch()
92num += 1
93log('Test {}\nChecking "in-use" flag...'.format(num))
94print_bitmap(['--force-share'])
95vm.shutdown()
96
97num += 1
98log('\nTest {}'.format(num))
99qemu_img_create('-f', iotests.imgfmt, disk, '1M')
100add_bitmap(1, True, False)
101log('Write an unknown bitmap flag \'{}\' into a new QCOW2 image at offset {}'
102    .format(hex(bitmap_flag_unknown), flag_offset))
103toggle_flag(flag_offset)
104img_info_log(disk)
105toggle_flag(flag_offset)
106log('Unset the unknown bitmap flag \'{}\' in the bitmap directory entry:\n'
107    .format(hex(bitmap_flag_unknown)))
108img_info_log(disk)
109log('Test complete')
110