xref: /openbmc/qemu/tests/qemu-iotests/tests/migrate-bitmaps-test (revision f2ec48fefd172a8dd20cb0073087d659aca9578c)
1#!/usr/bin/env python3
2# group: rw migration
3#
4# Tests for dirty bitmaps migration.
5#
6# Copyright (c) 2016-2017 Virtuozzo International GmbH. All rights reserved.
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20#
21
22import itertools
23import operator
24import os
25import re
26
27import iotests
28from iotests import qemu_img, qemu_img_create, Timeout
29
30
31disk_a = os.path.join(iotests.test_dir, 'disk_a')
32disk_b = os.path.join(iotests.test_dir, 'disk_b')
33base_a = os.path.join(iotests.test_dir, 'base_a')
34size = '1M'
35mig_file = os.path.join(iotests.test_dir, 'mig_file')
36mig_cmd = 'exec: cat > ' + mig_file
37incoming_cmd = 'exec: cat ' + mig_file
38
39
40def get_bitmap_hash(vm):
41    result = vm.qmp('x-debug-block-dirty-bitmap-sha256',
42                    node='drive0', name='bitmap0')
43    return result['return']['sha256']
44
45
46class TestDirtyBitmapMigration(iotests.QMPTestCase):
47    def tearDown(self):
48        self.vm_a.shutdown()
49        self.vm_b.shutdown()
50        os.remove(disk_a)
51        os.remove(disk_b)
52        os.remove(mig_file)
53
54    def setUp(self):
55        qemu_img('create', '-f', iotests.imgfmt, disk_a, size)
56        qemu_img('create', '-f', iotests.imgfmt, disk_b, size)
57
58        self.vm_a = iotests.VM(path_suffix='a').add_drive(disk_a)
59        self.vm_a.launch()
60
61        self.vm_b = iotests.VM(path_suffix='b')
62
63    def add_bitmap(self, vm, granularity, persistent):
64        params = {'node': 'drive0',
65                  'name': 'bitmap0',
66                  'granularity': granularity}
67        if persistent:
68            params['persistent'] = True
69
70        vm.cmd('block-dirty-bitmap-add', params)
71
72    def check_bitmap(self, vm, sha256):
73        result = vm.qmp('x-debug-block-dirty-bitmap-sha256',
74                        node='drive0', name='bitmap0')
75        if sha256:
76            self.assert_qmp(result, 'return/sha256', sha256)
77        else:
78            self.assert_qmp(result, 'error/desc',
79                            "Dirty bitmap 'bitmap0' not found")
80
81    def do_test_migration_resume_source(self, persistent, migrate_bitmaps):
82        granularity = 512
83
84        # regions = ((start, count), ...)
85        regions = ((0, 0x10000),
86                   (0xf0000, 0x10000),
87                   (0xa0201, 0x1000))
88
89        mig_caps = [{'capability': 'events', 'state': True}]
90        if migrate_bitmaps:
91            mig_caps.append({'capability': 'dirty-bitmaps', 'state': True})
92
93        self.vm_a.cmd('migrate-set-capabilities',
94                      capabilities=mig_caps)
95
96        self.add_bitmap(self.vm_a, granularity, persistent)
97        for r in regions:
98            self.vm_a.hmp_qemu_io('drive0', 'write %d %d' % r)
99        sha256 = get_bitmap_hash(self.vm_a)
100
101        self.vm_a.cmd('migrate', uri=mig_cmd)
102        while True:
103            event = self.vm_a.event_wait('MIGRATION')
104            if event['data']['status'] == 'completed':
105                break
106        while True:
107            result = self.vm_a.qmp('query-status')
108            if result['return']['status'] == 'postmigrate':
109                break
110
111        # test that bitmap is still here
112        removed = (not migrate_bitmaps) and persistent
113        self.check_bitmap(self.vm_a, False if removed else sha256)
114
115        self.vm_a.cmd('cont')
116
117        # test that bitmap is still here after invalidation
118        self.check_bitmap(self.vm_a, sha256)
119
120        # shutdown and check that invalidation didn't fail
121        self.vm_a.shutdown()
122
123        # catch 'Could not reopen qcow2 layer: Bitmap already exists'
124        # possible error
125        log = iotests.filter_qtest(self.vm_a.get_log())
126        log = re.sub(r'^(wrote .* bytes at offset .*\n'
127                     r'.*KiB.*ops.*sec.*\n?){3}',
128                     '', log)
129        self.assertEqual(log, '')
130
131        # test that bitmap is still persistent
132        self.vm_a.launch()
133        self.check_bitmap(self.vm_a, sha256 if persistent else False)
134
135    def do_test_migration(self, persistent, migrate_bitmaps, online,
136                          shared_storage, pre_shutdown):
137        granularity = 512
138
139        # regions = ((start, count), ...)
140        regions = ((0, 0x10000),
141                   (0xf0000, 0x10000),
142                   (0xa0201, 0x1000))
143
144        should_migrate = \
145            (migrate_bitmaps and (persistent or not pre_shutdown)) or \
146            (persistent and shared_storage)
147        mig_caps = [{'capability': 'events', 'state': True}]
148        if migrate_bitmaps:
149            mig_caps.append({'capability': 'dirty-bitmaps', 'state': True})
150
151        self.vm_b.add_incoming(incoming_cmd if online else "defer")
152        self.vm_b.add_drive(disk_a if shared_storage else disk_b)
153
154        if online:
155            os.mkfifo(mig_file)
156            self.vm_b.launch()
157            self.vm_b.cmd('migrate-set-capabilities',
158                          capabilities=mig_caps)
159
160        self.add_bitmap(self.vm_a, granularity, persistent)
161        for r in regions:
162            self.vm_a.hmp_qemu_io('drive0', 'write %d %d' % r)
163        sha256 = get_bitmap_hash(self.vm_a)
164
165        if pre_shutdown:
166            self.vm_a.shutdown()
167            self.vm_a.launch()
168
169        self.vm_a.cmd('migrate-set-capabilities',
170                      capabilities=mig_caps)
171
172        self.vm_a.cmd('migrate', uri=mig_cmd)
173        while True:
174            event = self.vm_a.event_wait('MIGRATION')
175            if event['data']['status'] == 'completed':
176                break
177
178        if not online:
179            self.vm_a.shutdown()
180            self.vm_b.launch()
181            self.vm_b.cmd('migrate-set-capabilities',
182                          capabilities=mig_caps)
183            self.vm_b.cmd('migrate-incoming', uri=incoming_cmd)
184
185        while True:
186            event = self.vm_b.event_wait('MIGRATION')
187            if event['data']['status'] == 'completed':
188                break
189
190        self.check_bitmap(self.vm_b, sha256 if should_migrate else False)
191
192        if should_migrate:
193            self.vm_b.shutdown()
194
195            # catch 'Could not reopen qcow2 layer: Bitmap already exists'
196            # possible error
197            log = self.vm_b.get_log()
198            log = re.sub(r'^\[I \d+\.\d+\] OPENED\n', '', log)
199            log = re.sub(r'\[I \+\d+\.\d+\] CLOSED\n?$', '', log)
200            self.assertEqual(log, '')
201
202            # recreate vm_b, as we don't want -incoming option (this will lead
203            # to "cat" process left alive after test finish)
204            self.vm_b = iotests.VM(path_suffix='b')
205            self.vm_b.add_drive(disk_a if shared_storage else disk_b)
206            self.vm_b.launch()
207            self.check_bitmap(self.vm_b, sha256 if persistent else False)
208
209
210def inject_test_case(klass, suffix, method, *args, **kwargs):
211    mc = operator.methodcaller(method, *args, **kwargs)
212    # We want to add a function attribute to `klass`, so that it is
213    # correctly converted to a method on instantiation.  The
214    # methodcaller object `mc` is a callable, not a function, so we
215    # need the lambda to turn it into a function.
216    # pylint: disable=unnecessary-lambda
217    setattr(klass, 'test_' + method + suffix, lambda self: mc(self))
218
219
220class TestDirtyBitmapBackingMigration(iotests.QMPTestCase):
221    def setUp(self):
222        qemu_img_create('-f', iotests.imgfmt, base_a, size)
223        qemu_img_create('-f', iotests.imgfmt, '-F', iotests.imgfmt,
224                        '-b', base_a, disk_a, size)
225
226        for f in (disk_a, base_a):
227            qemu_img('bitmap', '--add', f, 'bmap0')
228
229        blockdev = {
230            'node-name': 'node0',
231            'driver': iotests.imgfmt,
232            'file': {
233                'driver': 'file',
234                'filename': disk_a
235            },
236            'backing': {
237                'node-name': 'node0-base',
238                'driver': iotests.imgfmt,
239                'file': {
240                    'driver': 'file',
241                    'filename': base_a
242                }
243            }
244        }
245
246        self.vm = iotests.VM()
247        self.vm.launch()
248
249        self.vm.cmd('blockdev-add', blockdev)
250
251        # Check that the bitmaps are there
252        nodes = self.vm.qmp('query-named-block-nodes', flat=True)['return']
253        for node in nodes:
254            if 'node0' in node['node-name']:
255                self.assert_qmp(node, 'dirty-bitmaps[0]/name', 'bmap0')
256
257        caps = [{'capability': 'events', 'state': True}]
258        self.vm.cmd('migrate-set-capabilities', capabilities=caps)
259
260    def tearDown(self):
261        self.vm.shutdown()
262        for f in (disk_a, base_a):
263            os.remove(f)
264
265    def test_cont_on_source(self):
266        """
267        Continue the source after migration.
268        """
269        self.vm.cmd('migrate', uri='exec: cat > /dev/null')
270
271        with Timeout(10, 'Migration timeout'):
272            self.vm.wait_migration('postmigrate')
273
274        self.vm.cmd('cont')
275
276
277def main() -> None:
278    for cmb in list(itertools.product((True, False), repeat=5)):
279        name = ('_' if cmb[0] else '_not_') + 'persistent_'
280        name += ('_' if cmb[1] else '_not_') + 'migbitmap_'
281        name += '_online' if cmb[2] else '_offline'
282        name += '_shared' if cmb[3] else '_nonshared'
283        if cmb[4]:
284            name += '__pre_shutdown'
285
286        inject_test_case(TestDirtyBitmapMigration, name, 'do_test_migration',
287                         *list(cmb))
288
289    for cmb in list(itertools.product((True, False), repeat=2)):
290        name = ('_' if cmb[0] else '_not_') + 'persistent_'
291        name += ('_' if cmb[1] else '_not_') + 'migbitmap'
292
293        inject_test_case(TestDirtyBitmapMigration, name,
294                         'do_test_migration_resume_source', *list(cmb))
295
296    iotests.main(
297        supported_fmts=['qcow2'],
298        supported_protocols=['file'],
299        unsupported_imgopts=['compat']
300    )
301
302
303if __name__ == '__main__':
304    main()
305