1#!/usr/bin/env python3
2#
3# Test removing persistent bitmap from backing
4#
5# Copyright (c) 2021 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
22from iotests import log, qemu_img_create, qemu_img, qemu_img_info
23
24iotests.script_initialize(supported_fmts=['qcow2'],
25                          unsupported_imgopts=['compat'])
26
27top, base = iotests.file_path('top', 'base')
28size = '1M'
29
30qemu_img_create('-f', iotests.imgfmt, base, size)
31qemu_img_create('-f', iotests.imgfmt, '-b', base,
32                '-F', iotests.imgfmt, top, size)
33
34qemu_img('bitmap', '--add', base, 'bitmap0')
35# Just assert that our method of checking bitmaps in the image works.
36assert 'bitmaps' in qemu_img_info(base)['format-specific']['data']
37
38vm = iotests.VM().add_drive(top, 'backing.node-name=base')
39vm.launch()
40
41log('Trying to remove persistent bitmap from r-o base node, should fail:')
42vm.qmp_log('block-dirty-bitmap-remove', node='base', name='bitmap0')
43
44new_base_opts = {
45    'options': [{
46        'node-name': 'base',
47        'driver': 'qcow2',
48        'file': {
49            'driver': 'file',
50            'filename':  base
51        },
52        'read-only': False
53    }]
54}
55
56# Don't want to bother with filtering qmp_log for reopen command
57result = vm.qmp('blockdev-reopen', **new_base_opts)
58if result != {'return': {}}:
59    log('Failed to reopen: ' + str(result))
60
61log('Remove persistent bitmap from base node reopened to RW:')
62vm.qmp_log('block-dirty-bitmap-remove', node='base', name='bitmap0')
63
64new_base_opts['options'][0]['read-only'] = True
65result = vm.qmp('blockdev-reopen', **new_base_opts)
66if result != {'return': {}}:
67    log('Failed to reopen: ' + str(result))
68
69vm.shutdown()
70
71if 'bitmaps' in qemu_img_info(base)['format-specific']['data']:
72    log('ERROR: Bitmap is still in the base image')
73