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