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