3 # Tests for dirty bitmaps migration.
5 # Copyright (c) 2016-2017 Virtuozzo International GmbH. All rights reserved.
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.
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.
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/>.
27 from iotests import qemu_img, qemu_img_create, Timeout
30 disk_a = os.path.join(iotests.test_dir, 'disk_a')
31 disk_b = os.path.join(iotests.test_dir, 'disk_b')
32 base_a = os.path.join(iotests.test_dir, 'base_a')
34 mig_file = os.path.join(iotests.test_dir, 'mig_file')
35 mig_cmd = 'exec: cat > ' + mig_file
36 incoming_cmd = 'exec: cat ' + mig_file
39 class TestDirtyBitmapMigration(iotests.QMPTestCase):
48 qemu_img('create', '-f', iotests.imgfmt, disk_a, size)
49 qemu_img('create', '-f', iotests.imgfmt, disk_b, size)
51 self.vm_a = iotests.VM(path_suffix='a').add_drive(disk_a)
54 self.vm_b = iotests.VM(path_suffix='b')
56 def add_bitmap(self, vm, granularity, persistent):
57 params = {'node': 'drive0',
59 'granularity': granularity}
61 params['persistent'] = True
63 result = vm.qmp('block-dirty-bitmap-add', **params)
64 self.assert_qmp(result, 'return', {});
66 def get_bitmap_hash(self, vm):
67 result = vm.qmp('x-debug-block-dirty-bitmap-sha256',
68 node='drive0', name='bitmap0')
69 return result['return']['sha256']
71 def check_bitmap(self, vm, sha256):
72 result = vm.qmp('x-debug-block-dirty-bitmap-sha256',
73 node='drive0', name='bitmap0')
75 self.assert_qmp(result, 'return/sha256', sha256);
77 self.assert_qmp(result, 'error/desc',
78 "Dirty bitmap 'bitmap0' not found");
80 def do_test_migration_resume_source(self, persistent, migrate_bitmaps):
83 # regions = ((start, count), ...)
84 regions = ((0, 0x10000),
88 mig_caps = [{'capability': 'events', 'state': True}]
90 mig_caps.append({'capability': 'dirty-bitmaps', 'state': True})
92 result = self.vm_a.qmp('migrate-set-capabilities',
93 capabilities=mig_caps)
94 self.assert_qmp(result, 'return', {})
96 self.add_bitmap(self.vm_a, granularity, persistent)
98 self.vm_a.hmp_qemu_io('drive0', 'write %d %d' % r)
99 sha256 = self.get_bitmap_hash(self.vm_a)
101 result = self.vm_a.qmp('migrate', uri=mig_cmd)
103 event = self.vm_a.event_wait('MIGRATION')
104 if event['data']['status'] == 'completed':
107 result = self.vm_a.qmp('query-status')
108 if (result['return']['status'] == 'postmigrate'):
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)
115 result = self.vm_a.qmp('cont')
116 self.assert_qmp(result, 'return', {})
118 # test that bitmap is still here after invalidation
119 self.check_bitmap(self.vm_a, sha256)
121 # shutdown and check that invalidation didn't fail
124 # catch 'Could not reopen qcow2 layer: Bitmap already exists'
126 log = self.vm_a.get_log()
127 log = re.sub(r'^\[I \d+\.\d+\] OPENED\n', '', log)
128 log = re.sub(r'^(wrote .* bytes at offset .*\n.*KiB.*ops.*sec.*\n){3}',
130 log = re.sub(r'\[I \+\d+\.\d+\] CLOSED\n?$', '', log)
131 self.assertEqual(log, '')
133 # test that bitmap is still persistent
135 self.check_bitmap(self.vm_a, sha256 if persistent else False)
137 def do_test_migration(self, persistent, migrate_bitmaps, online,
138 shared_storage, pre_shutdown):
141 # regions = ((start, count), ...)
142 regions = ((0, 0x10000),
147 (migrate_bitmaps and (persistent or not pre_shutdown)) or \
148 (persistent and shared_storage)
149 mig_caps = [{'capability': 'events', 'state': True}]
151 mig_caps.append({'capability': 'dirty-bitmaps', 'state': True})
153 self.vm_b.add_incoming(incoming_cmd if online else "defer")
154 self.vm_b.add_drive(disk_a if shared_storage else disk_b)
159 result = self.vm_b.qmp('migrate-set-capabilities',
160 capabilities=mig_caps)
161 self.assert_qmp(result, 'return', {})
163 self.add_bitmap(self.vm_a, granularity, persistent)
165 self.vm_a.hmp_qemu_io('drive0', 'write %d %d' % r)
166 sha256 = self.get_bitmap_hash(self.vm_a)
172 result = self.vm_a.qmp('migrate-set-capabilities',
173 capabilities=mig_caps)
174 self.assert_qmp(result, 'return', {})
176 result = self.vm_a.qmp('migrate', uri=mig_cmd)
178 event = self.vm_a.event_wait('MIGRATION')
179 if event['data']['status'] == 'completed':
185 result = self.vm_b.qmp('migrate-set-capabilities',
186 capabilities=mig_caps)
187 self.assert_qmp(result, 'return', {})
188 result = self.vm_b.qmp('migrate-incoming', uri=incoming_cmd)
189 self.assert_qmp(result, 'return', {})
192 event = self.vm_b.event_wait('MIGRATION')
193 if event['data']['status'] == 'completed':
196 self.check_bitmap(self.vm_b, sha256 if should_migrate else False)
201 # catch 'Could not reopen qcow2 layer: Bitmap already exists'
203 log = self.vm_b.get_log()
204 log = re.sub(r'^\[I \d+\.\d+\] OPENED\n', '', log)
205 log = re.sub(r'\[I \+\d+\.\d+\] CLOSED\n?$', '', log)
206 self.assertEqual(log, '')
208 # recreate vm_b, as we don't want -incoming option (this will lead
209 # to "cat" process left alive after test finish)
210 self.vm_b = iotests.VM(path_suffix='b')
211 self.vm_b.add_drive(disk_a if shared_storage else disk_b)
213 self.check_bitmap(self.vm_b, sha256 if persistent else False)
216 def inject_test_case(klass, name, method, *args, **kwargs):
217 mc = operator.methodcaller(method, *args, **kwargs)
218 setattr(klass, 'test_' + method + name, lambda self: mc(self))
220 for cmb in list(itertools.product((True, False), repeat=5)):
221 name = ('_' if cmb[0] else '_not_') + 'persistent_'
222 name += ('_' if cmb[1] else '_not_') + 'migbitmap_'
223 name += '_online' if cmb[2] else '_offline'
224 name += '_shared' if cmb[3] else '_nonshared'
226 name += '__pre_shutdown'
228 inject_test_case(TestDirtyBitmapMigration, name, 'do_test_migration',
231 for cmb in list(itertools.product((True, False), repeat=2)):
232 name = ('_' if cmb[0] else '_not_') + 'persistent_'
233 name += ('_' if cmb[1] else '_not_') + 'migbitmap'
235 inject_test_case(TestDirtyBitmapMigration, name,
236 'do_test_migration_resume_source', *list(cmb))
239 class TestDirtyBitmapBackingMigration(iotests.QMPTestCase):
241 qemu_img_create('-f', iotests.imgfmt, base_a, size)
242 qemu_img_create('-f', iotests.imgfmt, '-F', iotests.imgfmt,
243 '-b', base_a, disk_a, size)
245 for f in (disk_a, base_a):
246 qemu_img('bitmap', '--add', f, 'bmap0')
249 'node-name': 'node0',
250 'driver': iotests.imgfmt,
256 'node-name': 'node0-base',
257 'driver': iotests.imgfmt,
265 self.vm = iotests.VM()
268 result = self.vm.qmp('blockdev-add', **blockdev)
269 self.assert_qmp(result, 'return', {})
271 # Check that the bitmaps are there
272 for node in self.vm.qmp('query-named-block-nodes', flat=True)['return']:
273 if 'node0' in node['node-name']:
274 self.assert_qmp(node, 'dirty-bitmaps[0]/name', 'bmap0')
276 caps = [{'capability': 'events', 'state': True}]
277 result = self.vm.qmp('migrate-set-capabilities', capabilities=caps)
278 self.assert_qmp(result, 'return', {})
282 for f in (disk_a, base_a):
285 def test_cont_on_source(self):
287 Continue the source after migration.
289 result = self.vm.qmp('migrate', uri=f'exec: cat > /dev/null')
290 self.assert_qmp(result, 'return', {})
292 with Timeout(10, 'Migration timeout'):
293 self.vm.wait_migration('postmigrate')
295 result = self.vm.qmp('cont')
296 self.assert_qmp(result, 'return', {})
299 if __name__ == '__main__':
300 iotests.main(supported_fmts=['qcow2'],
301 supported_protocols=['file'])