xref: /openbmc/qemu/tests/qemu-iotests/260 (revision 7f6c3d1a)
1#!/usr/bin/env python3
2#
3# Tests for temporary external snapshot when we have bitmaps.
4#
5# Copyright (c) 2019 Virtuozzo International GmbH. All rights reserved.
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 qemu_img_create, file_path, log, filter_qmp_event
23
24iotests.script_initialize(
25    supported_fmts=['qcow2']
26)
27
28base, top = file_path('base', 'top')
29size = 64 * 1024 * 3
30
31
32def print_bitmap(msg, vm):
33    result = vm.qmp('query-block')['return'][0]
34    if 'dirty-bitmaps' in result:
35        bitmap = result['dirty-bitmaps'][0]
36        log('{}: name={} dirty-clusters={}'.format(msg, bitmap['name'],
37            bitmap['count'] // 64 // 1024))
38    else:
39        log(msg + ': not found')
40
41
42def test(persistent, restart):
43    assert persistent or not restart
44    log("\nTestcase {}persistent {} restart\n".format(
45            '' if persistent else 'non-', 'with' if restart else 'without'))
46
47    qemu_img_create('-f', iotests.imgfmt, base, str(size))
48
49    vm = iotests.VM().add_drive(base)
50    vm.launch()
51
52    vm.qmp_log('block-dirty-bitmap-add', node='drive0', name='bitmap0',
53               persistent=persistent)
54    vm.hmp_qemu_io('drive0', 'write 0 64K')
55    print_bitmap('initial bitmap', vm)
56
57    vm.qmp_log('blockdev-snapshot-sync', device='drive0', snapshot_file=top,
58               format=iotests.imgfmt, filters=[iotests.filter_qmp_testfiles])
59    vm.hmp_qemu_io('drive0', 'write 64K 512')
60    print_bitmap('check that no bitmaps are in snapshot', vm)
61
62    if restart:
63        log("... Restart ...")
64        vm.shutdown()
65        vm = iotests.VM().add_drive(top)
66        vm.launch()
67
68    vm.qmp_log('block-commit', device='drive0', top=top,
69               filters=[iotests.filter_qmp_testfiles])
70    ev = vm.events_wait((('BLOCK_JOB_READY', None),
71                         ('BLOCK_JOB_COMPLETED', None)))
72    log(filter_qmp_event(ev))
73    if (ev['event'] == 'BLOCK_JOB_COMPLETED'):
74        vm.shutdown()
75        log(vm.get_log())
76        exit()
77
78    vm.qmp_log('block-job-complete', device='drive0')
79    ev = vm.event_wait('BLOCK_JOB_COMPLETED')
80    log(filter_qmp_event(ev))
81    print_bitmap('check bitmap after commit', vm)
82
83    vm.hmp_qemu_io('drive0', 'write 128K 64K')
84    print_bitmap('check updated bitmap', vm)
85
86    vm.shutdown()
87
88
89test(persistent=False, restart=False)
90test(persistent=True, restart=False)
91test(persistent=True, restart=True)
92