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    'node-name': 'base',
45    'driver': 'qcow2',
46    'file': {
47        'driver': 'file',
48        'filename':  base
49    },
50    'read-only': False
51}
52
53# Don't want to bother with filtering qmp_log for reopen command
54result = vm.qmp('x-blockdev-reopen', **new_base_opts)
55if result != {'return': {}}:
56    log('Failed to reopen: ' + str(result))
57
58log('Remove persistent bitmap from base node reopened to RW:')
59vm.qmp_log('block-dirty-bitmap-remove', node='base', name='bitmap0')
60
61new_base_opts['read-only'] = True
62result = vm.qmp('x-blockdev-reopen', **new_base_opts)
63if result != {'return': {}}:
64    log('Failed to reopen: ' + str(result))
65
66vm.shutdown()
67
68if 'bitmaps' in qemu_img_pipe('info', base):
69    log('ERROR: Bitmap is still in the base image')
70