3 # Tests for active mirroring
5 # Copyright (C) 2018 Red Hat, Inc.
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/>.
23 from iotests import qemu_img
25 source_img = os.path.join(iotests.test_dir, 'source.' + iotests.imgfmt)
26 target_img = os.path.join(iotests.test_dir, 'target.' + iotests.imgfmt)
28 class TestActiveMirror(iotests.QMPTestCase):
29 image_len = 128 * 1024 * 1024 # MB
30 potential_writes_in_flight = True
33 qemu_img('create', '-f', iotests.imgfmt, source_img, '128M')
34 qemu_img('create', '-f', iotests.imgfmt, target_img, '128M')
36 blk_source = {'id': 'source',
38 'node-name': 'source-node',
39 'driver': iotests.imgfmt,
40 'file': {'driver': 'file',
41 'filename': source_img}}
43 blk_target = {'node-name': 'target-node',
44 'driver': iotests.imgfmt,
45 'file': {'driver': 'file',
46 'filename': target_img}}
48 self.vm = iotests.VM()
49 self.vm.add_drive_raw(self.vm.qmp_to_opts(blk_source))
50 self.vm.add_blockdev(self.vm.qmp_to_opts(blk_target))
51 self.vm.add_device('virtio-blk,drive=source')
57 if not self.potential_writes_in_flight:
58 self.assertTrue(iotests.compare_images(source_img, target_img),
59 'mirror target does not match source')
64 def doActiveIO(self, sync_source_and_target):
65 # Fill the source image
66 self.vm.hmp_qemu_io('source',
67 'write -P 1 0 %i' % self.image_len);
69 # Start some background requests
70 for offset in range(1 * self.image_len // 8, 3 * self.image_len // 8, 1024 * 1024):
71 self.vm.hmp_qemu_io('source', 'aio_write -P 2 %i 1M' % offset)
72 for offset in range(2 * self.image_len // 8, 3 * self.image_len // 8, 1024 * 1024):
73 self.vm.hmp_qemu_io('source', 'aio_write -z %i 1M' % offset)
76 result = self.vm.qmp('blockdev-mirror',
78 filter_node_name='mirror-node',
82 copy_mode='write-blocking')
83 self.assert_qmp(result, 'return', {})
85 # Start some more requests
86 for offset in range(3 * self.image_len // 8, 5 * self.image_len // 8, 1024 * 1024):
87 self.vm.hmp_qemu_io('source', 'aio_write -P 3 %i 1M' % offset)
88 for offset in range(4 * self.image_len // 8, 5 * self.image_len // 8, 1024 * 1024):
89 self.vm.hmp_qemu_io('source', 'aio_write -z %i 1M' % offset)
91 # Wait for the READY event
92 self.wait_ready(drive='mirror')
94 # Now start some final requests; all of these (which land on
95 # the source) should be settled using the active mechanism.
96 # The mirror code itself asserts that the source BDS's dirty
97 # bitmap will stay clean between READY and COMPLETED.
98 for offset in range(5 * self.image_len // 8, 7 * self.image_len // 8, 1024 * 1024):
99 self.vm.hmp_qemu_io('source', 'aio_write -P 3 %i 1M' % offset)
100 for offset in range(6 * self.image_len // 8, 7 * self.image_len // 8, 1024 * 1024):
101 self.vm.hmp_qemu_io('source', 'aio_write -z %i 1M' % offset)
103 if sync_source_and_target:
104 # If source and target should be in sync after the mirror,
105 # we have to flush before completion
106 self.vm.hmp_qemu_io('source', 'aio_flush')
107 self.potential_writes_in_flight = False
109 self.complete_and_wait(drive='mirror', wait_ready=False)
111 def testActiveIO(self):
112 self.doActiveIO(False)
114 def testActiveIOFlushed(self):
115 self.doActiveIO(True)
117 def testUnalignedActiveIO(self):
118 # Fill the source image
119 result = self.vm.hmp_qemu_io('source', 'write -P 1 0 2M')
121 # Start the block job (very slowly)
122 result = self.vm.qmp('blockdev-mirror',
124 filter_node_name='mirror-node',
125 device='source-node',
126 target='target-node',
128 copy_mode='write-blocking',
129 buf_size=(1048576 // 4),
131 self.assert_qmp(result, 'return', {})
133 # Start an unaligned request to a dirty area
134 result = self.vm.hmp_qemu_io('source', 'write -P 2 %i 1' % (1048576 + 42))
137 result = self.vm.qmp('block-job-set-speed', device='mirror', speed=0)
138 self.assert_qmp(result, 'return', {})
139 self.complete_and_wait(drive='mirror')
141 self.potential_writes_in_flight = False
144 if __name__ == '__main__':
145 iotests.main(supported_fmts=['qcow2', 'raw'],
146 supported_protocols=['file'])