4 # Test for changing mirror copy mode from background to active
6 # Copyright (C) 2023 Proxmox Server Solutions GmbH
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.
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.
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/>.
26 from iotests import qemu_img, QemuStorageDaemon
29 iops_source = iops_target * 2
30 image_size = 1 * 1024 * 1024
31 source_img = os.path.join(iotests.test_dir, 'source.' + iotests.imgfmt)
32 target_img = os.path.join(iotests.test_dir, 'target.' + iotests.imgfmt)
33 nbd_sock = os.path.join(iotests.sock_dir, 'nbd.sock')
35 class TestMirrorChangeCopyMode(iotests.QMPTestCase):
38 qemu_img('create', '-f', iotests.imgfmt, source_img, str(image_size))
39 qemu_img('create', '-f', iotests.imgfmt, target_img, str(image_size))
41 self.qsd = QemuStorageDaemon('--nbd-server',
42 f'addr.type=unix,addr.path={nbd_sock}',
45 self.qsd.cmd('object-add', {
46 'qom-type': 'throttle-group',
49 'iops-write': iops_target,
50 'iops-write-max': iops_target
54 self.qsd.cmd('blockdev-add', {
55 'node-name': 'target',
57 'throttle-group': 'thrgr-target',
59 'driver': iotests.imgfmt,
62 'filename': target_img
67 self.qsd.cmd('block-export-add', {
70 'node-name': 'target',
74 self.vm = iotests.VM()
75 self.vm.add_args('-drive',
76 f'file={source_img},if=none,format={iotests.imgfmt},'
77 f'iops_wr={iops_source},'
78 f'iops_wr_max={iops_source},'
82 self.vm.cmd('blockdev-add', {
83 'node-name': 'target',
96 self.check_qemu_io_errors()
97 self.check_images_identical()
101 # Once the VM is shut down we can parse the log and see if qemu-io ran
103 def check_qemu_io_errors(self):
104 self.assertFalse(self.vm.is_running())
105 log = self.vm.get_log()
106 for line in log.split("\n"):
107 assert not line.startswith("Pattern verification failed")
109 def check_images_identical(self):
110 qemu_img('compare', '-f', iotests.imgfmt, source_img, target_img)
112 def start_mirror(self):
113 self.vm.cmd('blockdev-mirror',
117 filter_node_name='mirror-top',
119 copy_mode='background')
121 def test_background_to_active(self):
122 self.vm.hmp_qemu_io('source', f'write 0 {image_size}')
123 self.vm.hmp_qemu_io('target', f'write 0 {image_size}')
127 result = self.vm.cmd('query-block-jobs')
128 assert not result[0]['actively-synced']
130 self.vm.event_wait('BLOCK_JOB_READY')
132 result = self.vm.cmd('query-block-jobs')
133 assert not result[0]['actively-synced']
135 # Start some background requests.
136 reqs = 4 * iops_source
137 req_size = image_size // reqs
138 for i in range(0, reqs):
139 req = f'aio_write -P 7 {req_size * i} {req_size}'
140 self.vm.hmp_qemu_io('source', req)
142 # Wait for the first few requests.
144 self.vm.qtest(f'clock_step {1 * 1000 * 1000 * 1000}')
146 result = self.vm.cmd('query-block-jobs')
147 # There should've been new requests.
148 assert result[0]['len'] > image_size
149 # To verify later that not all requests were completed at this point.
150 len_before_change = result[0]['len']
152 # Change the copy mode while requests are happening.
153 self.vm.cmd('block-job-change',
156 copy_mode='write-blocking')
158 # Wait until image is actively synced.
161 self.vm.qtest(f'clock_step {100 * 1000 * 1000}')
162 result = self.vm.cmd('query-block-jobs')
163 if result[0]['actively-synced']:
166 # Because of throttling, not all requests should have been completed
168 result = self.vm.cmd('query-block-jobs')
169 assert result[0]['len'] > len_before_change
171 # Issue enough requests for a few seconds only touching the first half
173 reqs = 4 * iops_target
174 req_size = image_size // 2 // reqs
175 for i in range(0, reqs):
176 req = f'aio_write -P 19 {req_size * i} {req_size}'
177 self.vm.hmp_qemu_io('source', req)
179 # Now issue a synchronous write in the second half of the image and
180 # immediately verify that it was written to the target too. This would
181 # fail without switching the copy mode. Note that this only produces a
182 # log line and the actual checking happens during tearDown().
183 req_args = f'-P 37 {3 * (image_size // 4)} {req_size}'
184 self.vm.hmp_qemu_io('source', f'write {req_args}')
185 self.vm.hmp_qemu_io('target', f'read {req_args}')
187 self.vm.cmd('block-job-cancel', device='mirror')
188 while len(self.vm.cmd('query-block-jobs')) > 0:
191 if __name__ == '__main__':
192 iotests.main(supported_fmts=['qcow2', 'raw'],
193 supported_protocols=['file'])