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