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