Merge tag 'pull-request-2024-02-23' of https://gitlab.com/thuth/qemu into staging
[qemu/kevin.git] / tests / qemu-iotests / 041
blob98d17b138835a25d42095d094007c00ac01275d5
1 #!/usr/bin/env python3
2 # group: rw auto backing
4 # Tests for image mirroring.
6 # Copyright (C) 2012 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/>.
22 import time
23 import os
24 import re
25 import json
26 import iotests
27 from iotests import qemu_img, qemu_img_map, qemu_io
29 backing_img = os.path.join(iotests.test_dir, 'backing.img')
30 target_backing_img = os.path.join(iotests.test_dir, 'target-backing.img')
31 test_img = os.path.join(iotests.test_dir, 'test.img')
32 target_img = os.path.join(iotests.test_dir, 'target.img')
34 quorum_img1 = os.path.join(iotests.test_dir, 'quorum1.img')
35 quorum_img2 = os.path.join(iotests.test_dir, 'quorum2.img')
36 quorum_img3 = os.path.join(iotests.test_dir, 'quorum3.img')
37 quorum_repair_img = os.path.join(iotests.test_dir, 'quorum_repair.img')
38 quorum_snapshot_file = os.path.join(iotests.test_dir, 'quorum_snapshot.img')
40 nbd_sock_path = os.path.join(iotests.sock_dir, 'nbd.sock')
42 class TestSingleDrive(iotests.QMPTestCase):
43     image_len = 1 * 1024 * 1024 # MB
44     qmp_cmd = 'drive-mirror'
45     qmp_target = target_img
47     def setUp(self):
48         iotests.create_image(backing_img, self.image_len)
49         qemu_img('create', '-f', iotests.imgfmt,
50                  '-o', 'backing_file=%s' % backing_img, '-F', 'raw', test_img)
51         self.vm = iotests.VM().add_drive(test_img, "node-name=top,backing.node-name=base")
52         if iotests.qemu_default_machine == 'pc':
53             self.vm.add_drive(None, 'media=cdrom', 'ide')
54         self.vm.launch()
56     def tearDown(self):
57         self.vm.shutdown()
58         os.remove(test_img)
59         os.remove(backing_img)
60         try:
61             os.remove(target_img)
62         except OSError:
63             pass
65     def test_complete(self):
66         self.assert_no_active_block_jobs()
68         self.vm.cmd(self.qmp_cmd, device='drive0', sync='full',
69                     target=self.qmp_target)
71         self.complete_and_wait()
72         result = self.vm.qmp('query-block')
73         self.assert_qmp(result, 'return[0]/inserted/file', target_img)
74         self.vm.shutdown()
75         self.assertTrue(iotests.compare_images(test_img, target_img),
76                         'target image does not match source after mirroring')
78     def test_cancel(self):
79         self.assert_no_active_block_jobs()
81         self.vm.cmd(self.qmp_cmd, device='drive0', sync='full',
82                     target=self.qmp_target)
84         self.cancel_and_wait(force=True)
85         result = self.vm.qmp('query-block')
86         self.assert_qmp(result, 'return[0]/inserted/file', test_img)
88     def test_cancel_after_ready(self):
89         self.assert_no_active_block_jobs()
91         self.vm.cmd(self.qmp_cmd, device='drive0', sync='full',
92                     target=self.qmp_target)
94         self.wait_ready_and_cancel()
95         result = self.vm.qmp('query-block')
96         self.assert_qmp(result, 'return[0]/inserted/file', test_img)
97         self.vm.shutdown()
98         self.assertTrue(iotests.compare_images(test_img, target_img),
99                         'target image does not match source after mirroring')
101     def test_pause(self):
102         self.assert_no_active_block_jobs()
104         self.vm.cmd(self.qmp_cmd, device='drive0', sync='full',
105                     target=self.qmp_target)
107         self.pause_job('drive0')
109         result = self.vm.qmp('query-block-jobs')
110         offset = self.dictpath(result, 'return[0]/offset')
112         time.sleep(0.5)
113         result = self.vm.qmp('query-block-jobs')
114         self.assert_qmp(result, 'return[0]/offset', offset)
116         self.vm.cmd('block-job-resume', device='drive0')
118         self.complete_and_wait()
119         self.vm.shutdown()
120         self.assertTrue(iotests.compare_images(test_img, target_img),
121                         'target image does not match source after mirroring')
123     def test_small_buffer(self):
124         self.assert_no_active_block_jobs()
126         # A small buffer is rounded up automatically
127         self.vm.cmd(self.qmp_cmd, device='drive0', sync='full',
128                     buf_size=4096, target=self.qmp_target)
130         self.complete_and_wait()
131         result = self.vm.qmp('query-block')
132         self.assert_qmp(result, 'return[0]/inserted/file', target_img)
133         self.vm.shutdown()
134         self.assertTrue(iotests.compare_images(test_img, target_img),
135                         'target image does not match source after mirroring')
137     def test_small_buffer2(self):
138         self.assert_no_active_block_jobs()
140         qemu_img('create', '-f', iotests.imgfmt, '-o', 'cluster_size=%d,size=%d'
141                         % (self.image_len, self.image_len), target_img)
142         self.vm.cmd(self.qmp_cmd, device='drive0', sync='full',
143                     buf_size=65536, mode='existing', target=self.qmp_target)
145         self.complete_and_wait()
146         result = self.vm.qmp('query-block')
147         self.assert_qmp(result, 'return[0]/inserted/file', target_img)
148         self.vm.shutdown()
149         self.assertTrue(iotests.compare_images(test_img, target_img),
150                         'target image does not match source after mirroring')
152     def test_large_cluster(self):
153         self.assert_no_active_block_jobs()
155         qemu_img('create', '-f', iotests.imgfmt, '-o', 'cluster_size=%d,backing_file=%s'
156                         % (self.image_len, backing_img),
157                  '-F', 'raw', target_img)
158         self.vm.cmd(self.qmp_cmd, device='drive0', sync='full',
159                     mode='existing', target=self.qmp_target)
161         self.complete_and_wait()
162         result = self.vm.qmp('query-block')
163         self.assert_qmp(result, 'return[0]/inserted/file', target_img)
164         self.vm.shutdown()
165         self.assertTrue(iotests.compare_images(test_img, target_img),
166                         'target image does not match source after mirroring')
168     # Tests that the insertion of the mirror_top filter node doesn't make a
169     # difference to query-block
170     def test_implicit_node(self):
171         self.assert_no_active_block_jobs()
173         self.vm.cmd(self.qmp_cmd, device='drive0', sync='full',
174                     target=self.qmp_target)
176         result = self.vm.qmp('query-block')
177         self.assert_qmp(result, 'return[0]/inserted/file', test_img)
178         self.assert_qmp(result, 'return[0]/inserted/drv', iotests.imgfmt)
179         self.assert_qmp(result, 'return[0]/inserted/backing_file', backing_img)
180         self.assert_qmp(result, 'return[0]/inserted/backing_file_depth', 1)
181         self.assert_qmp(result, 'return[0]/inserted/image/filename', test_img)
182         self.assert_qmp(result, 'return[0]/inserted/image/backing-image/filename', backing_img)
184         result = self.vm.qmp('query-blockstats')
185         self.assert_qmp(result, 'return[0]/node-name', 'top')
186         self.assert_qmp(result, 'return[0]/backing/node-name', 'base')
188         self.cancel_and_wait(force=True)
189         result = self.vm.qmp('query-block')
190         self.assert_qmp(result, 'return[0]/inserted/file', test_img)
191         self.assert_qmp(result, 'return[0]/inserted/drv', iotests.imgfmt)
192         self.assert_qmp(result, 'return[0]/inserted/backing_file', backing_img)
193         self.assert_qmp(result, 'return[0]/inserted/backing_file_depth', 1)
194         self.assert_qmp(result, 'return[0]/inserted/image/filename', test_img)
195         self.assert_qmp(result, 'return[0]/inserted/image/backing-image/filename', backing_img)
197         result = self.vm.qmp('query-blockstats')
198         self.assert_qmp(result, 'return[0]/node-name', 'top')
199         self.assert_qmp(result, 'return[0]/backing/node-name', 'base')
201     def test_medium_not_found(self):
202         if iotests.qemu_default_machine != 'pc':
203             return
205         result = self.vm.qmp(self.qmp_cmd, device='ide1-cd0', sync='full',
206                              target=self.qmp_target)
207         self.assert_qmp(result, 'error/class', 'GenericError')
209     def test_image_not_found(self):
210         result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full',
211                              mode='existing', target=self.qmp_target)
212         self.assert_qmp(result, 'error/class', 'GenericError')
214     def test_device_not_found(self):
215         result = self.vm.qmp(self.qmp_cmd, device='nonexistent', sync='full',
216                              target=self.qmp_target)
217         self.assert_qmp(result, 'error/class', 'GenericError')
219 class TestSingleBlockdev(TestSingleDrive):
220     qmp_cmd = 'blockdev-mirror'
221     qmp_target = 'node1'
223     def setUp(self):
224         TestSingleDrive.setUp(self)
225         qemu_img('create', '-f', iotests.imgfmt,
226                  '-o', 'backing_file=%s' % backing_img, '-F', 'raw', target_img)
227         args = {'driver': iotests.imgfmt,
228                 'node-name': self.qmp_target,
229                 'file': { 'filename': target_img, 'driver': 'file' } }
230         self.vm.cmd("blockdev-add", args)
232     def test_mirror_to_self(self):
233         result = self.vm.qmp(self.qmp_cmd, job_id='job0',
234                              device=self.qmp_target, sync='full',
235                              target=self.qmp_target)
236         self.assert_qmp(result, 'error/class', 'GenericError')
238     def do_test_resize(self, device, node):
239         def pre_finalize():
240             if device:
241                 result = self.vm.qmp('block_resize', device=device, size=65536)
242                 self.assert_qmp(result, 'error/class', 'GenericError')
244             result = self.vm.qmp('block_resize', node_name=node, size=65536)
245             self.assert_qmp(result, 'error/class', 'GenericError')
247         self.vm.cmd(self.qmp_cmd, job_id='job0', device='drive0',
248                     sync='full', target=self.qmp_target,
249                     auto_finalize=False, auto_dismiss=False)
251         result = self.vm.run_job('job0', auto_finalize=False,
252                                  pre_finalize=pre_finalize)
253         self.assertEqual(result, None)
255     def test_source_resize(self):
256         self.do_test_resize('drive0', 'top')
258     def test_target_resize(self):
259         self.do_test_resize(None, self.qmp_target)
261     def do_test_target_size(self, size):
262         self.vm.cmd('block_resize', node_name=self.qmp_target,
263                     size=size)
265         self.vm.cmd(self.qmp_cmd, job_id='job0',
266                     device='drive0', sync='full', auto_dismiss=False,
267                     target=self.qmp_target)
269         result = self.vm.run_job('job0')
270         self.assertEqual(result, 'Source and target image have different sizes')
272     # qed does not support shrinking
273     @iotests.skip_for_formats(('qed'))
274     def test_small_target(self):
275         self.do_test_target_size(self.image_len // 2)
277     def test_large_target(self):
278         self.do_test_target_size(self.image_len * 2)
280     test_large_cluster = None
281     test_image_not_found = None
282     test_small_buffer2 = None
284 class TestSingleDriveZeroLength(TestSingleDrive):
285     image_len = 0
286     test_small_buffer2 = None
287     test_large_cluster = None
289 class TestSingleBlockdevZeroLength(TestSingleBlockdev):
290     image_len = 0
291     test_small_target = None
292     test_large_target = None
294 class TestSingleDriveUnalignedLength(TestSingleDrive):
295     image_len = 1025 * 1024
296     test_small_buffer2 = None
297     test_large_cluster = None
299 class TestSingleBlockdevUnalignedLength(TestSingleBlockdev):
300     image_len = 1025 * 1024
302 class TestMirrorNoBacking(iotests.QMPTestCase):
303     image_len = 2 * 1024 * 1024 # MB
305     def setUp(self):
306         iotests.create_image(backing_img, TestMirrorNoBacking.image_len)
307         qemu_img('create', '-f', iotests.imgfmt,
308                  '-o', 'backing_file=%s' % backing_img, '-F', 'raw', test_img)
309         self.vm = iotests.VM().add_drive(test_img)
310         self.vm.launch()
312     def tearDown(self):
313         self.vm.shutdown()
314         os.remove(test_img)
315         os.remove(backing_img)
316         try:
317             os.remove(target_backing_img)
318         except:
319             pass
320         os.remove(target_img)
322     def test_complete(self):
323         self.assert_no_active_block_jobs()
325         qemu_img('create', '-f', iotests.imgfmt,
326                  '-o', 'backing_file=%s' % backing_img, '-F', 'raw', target_img)
327         self.vm.cmd('drive-mirror', device='drive0', sync='full',
328                     mode='existing', target=target_img)
330         self.complete_and_wait()
331         result = self.vm.qmp('query-block')
332         self.assert_qmp(result, 'return[0]/inserted/file', target_img)
333         self.vm.shutdown()
334         self.assertTrue(iotests.compare_images(test_img, target_img),
335                         'target image does not match source after mirroring')
337     def test_cancel(self):
338         self.assert_no_active_block_jobs()
340         qemu_img('create', '-f', iotests.imgfmt,
341                  '-o', 'backing_file=%s' % backing_img, '-F', 'raw', target_img)
342         self.vm.cmd('drive-mirror', device='drive0', sync='full',
343                     mode='existing', target=target_img)
345         self.wait_ready_and_cancel()
346         result = self.vm.qmp('query-block')
347         self.assert_qmp(result, 'return[0]/inserted/file', test_img)
348         self.vm.shutdown()
349         self.assertTrue(iotests.compare_images(test_img, target_img),
350                         'target image does not match source after mirroring')
352     def test_large_cluster(self):
353         self.assert_no_active_block_jobs()
355         # qemu-img create fails if the image is not there
356         qemu_img('create', '-f', iotests.imgfmt, '-o', 'size=%d'
357                         %(TestMirrorNoBacking.image_len), target_backing_img)
358         qemu_img('create', '-f', iotests.imgfmt, '-o', 'cluster_size=%d,backing_file=%s'
359                         % (TestMirrorNoBacking.image_len, target_backing_img),
360                  '-F', iotests.imgfmt, target_img)
362         self.vm.cmd('drive-mirror', device='drive0', sync='full',
363                     mode='existing', target=target_img)
365         self.complete_and_wait()
366         result = self.vm.qmp('query-block')
367         self.assert_qmp(result, 'return[0]/inserted/file', target_img)
368         self.vm.shutdown()
369         self.assertTrue(iotests.compare_images(test_img, target_img),
370                         'target image does not match source after mirroring')
372 class TestMirrorResized(iotests.QMPTestCase):
373     backing_len = 1 * 1024 * 1024 # MB
374     image_len = 2 * 1024 * 1024 # MB
376     def setUp(self):
377         iotests.create_image(backing_img, TestMirrorResized.backing_len)
378         qemu_img('create', '-f', iotests.imgfmt,
379                  '-o', 'backing_file=%s' % backing_img, '-F', 'raw', test_img)
380         qemu_img('resize', test_img, '2M')
381         self.vm = iotests.VM().add_drive(test_img)
382         self.vm.launch()
384     def tearDown(self):
385         self.vm.shutdown()
386         os.remove(test_img)
387         os.remove(backing_img)
388         try:
389             os.remove(target_img)
390         except OSError:
391             pass
393     def test_complete_top(self):
394         self.assert_no_active_block_jobs()
396         self.vm.cmd('drive-mirror', device='drive0', sync='top',
397                     target=target_img)
399         self.complete_and_wait()
400         result = self.vm.qmp('query-block')
401         self.assert_qmp(result, 'return[0]/inserted/file', target_img)
402         self.vm.shutdown()
403         self.assertTrue(iotests.compare_images(test_img, target_img),
404                         'target image does not match source after mirroring')
406     def test_complete_full(self):
407         self.assert_no_active_block_jobs()
409         self.vm.cmd('drive-mirror', device='drive0', sync='full',
410                     target=target_img)
412         self.complete_and_wait()
413         result = self.vm.qmp('query-block')
414         self.assert_qmp(result, 'return[0]/inserted/file', target_img)
415         self.vm.shutdown()
416         self.assertTrue(iotests.compare_images(test_img, target_img),
417                         'target image does not match source after mirroring')
419 class TestReadErrors(iotests.QMPTestCase):
420     image_len = 2 * 1024 * 1024 # MB
422     # this should be a multiple of twice the default granularity
423     # so that we hit this offset first in state 1
424     MIRROR_GRANULARITY = 1024 * 1024
426     def create_blkdebug_file(self, name, event, errno):
427         file = open(name, 'w')
428         file.write('''
429 [inject-error]
430 state = "1"
431 event = "%s"
432 errno = "%d"
433 immediately = "off"
434 once = "on"
435 sector = "%d"
437 [set-state]
438 state = "1"
439 event = "%s"
440 new_state = "2"
442 [set-state]
443 state = "2"
444 event = "%s"
445 new_state = "1"
446 ''' % (event, errno, self.MIRROR_GRANULARITY // 512, event, event))
447         file.close()
449     def setUp(self):
450         self.blkdebug_file = backing_img + ".blkdebug"
451         iotests.create_image(backing_img, TestReadErrors.image_len)
452         self.create_blkdebug_file(self.blkdebug_file, "read_aio", 5)
453         qemu_img('create', '-f', iotests.imgfmt,
454                  '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw'
455                        % (self.blkdebug_file, backing_img),
456                  test_img)
457         # Write something for tests that use sync='top'
458         qemu_io('-c', 'write %d 512' % (self.MIRROR_GRANULARITY + 65536),
459                         test_img)
460         self.vm = iotests.VM().add_drive(test_img)
461         self.vm.launch()
463     def tearDown(self):
464         self.vm.shutdown()
465         os.remove(test_img)
466         os.remove(target_img)
467         os.remove(backing_img)
468         os.remove(self.blkdebug_file)
470     def test_report_read(self):
471         self.assert_no_active_block_jobs()
473         self.vm.cmd('drive-mirror', device='drive0', sync='full',
474                     target=target_img)
476         completed = False
477         error = False
478         while not completed:
479             for event in self.vm.get_qmp_events(wait=True):
480                 if event['event'] == 'BLOCK_JOB_ERROR':
481                     self.assert_qmp(event, 'data/device', 'drive0')
482                     self.assert_qmp(event, 'data/operation', 'read')
483                     error = True
484                 elif event['event'] == 'BLOCK_JOB_READY':
485                     self.assertTrue(False, 'job completed unexpectedly')
486                 elif event['event'] == 'BLOCK_JOB_COMPLETED':
487                     self.assertTrue(error, 'job completed unexpectedly')
488                     self.assert_qmp(event, 'data/type', 'mirror')
489                     self.assert_qmp(event, 'data/device', 'drive0')
490                     self.assert_qmp(event, 'data/error', 'Input/output error')
491                     completed = True
492                 elif event['event'] == 'JOB_STATUS_CHANGE':
493                     self.assert_qmp(event, 'data/id', 'drive0')
495         self.assert_no_active_block_jobs()
497     def test_ignore_read(self):
498         self.assert_no_active_block_jobs()
500         self.vm.cmd('drive-mirror', device='drive0', sync='full',
501                     target=target_img, on_source_error='ignore')
503         event = self.vm.get_qmp_event(wait=True)
504         while event['event'] == 'JOB_STATUS_CHANGE':
505             self.assert_qmp(event, 'data/id', 'drive0')
506             event = self.vm.get_qmp_event(wait=True)
508         self.assertEqual(event['event'], 'BLOCK_JOB_ERROR')
509         self.assert_qmp(event, 'data/device', 'drive0')
510         self.assert_qmp(event, 'data/operation', 'read')
511         result = self.vm.qmp('query-block-jobs')
512         self.assertIn(result['return'][0]['status'], ['running', 'ready'])
513         self.complete_and_wait()
515     def test_large_cluster(self):
516         self.assert_no_active_block_jobs()
518         # Test COW into the target image.  The first half of the
519         # cluster at MIRROR_GRANULARITY has to be copied from
520         # backing_img, even though sync='top'.
521         qemu_img('create', '-f', iotests.imgfmt,
522                  '-ocluster_size=131072,backing_file=%s' %(backing_img),
523                  '-F', 'raw', target_img)
524         self.vm.cmd('drive-mirror', device='drive0', sync='top',
525                     on_source_error='ignore',
526                     mode='existing', target=target_img)
528         event = self.vm.get_qmp_event(wait=True)
529         while event['event'] == 'JOB_STATUS_CHANGE':
530             self.assert_qmp(event, 'data/id', 'drive0')
531             event = self.vm.get_qmp_event(wait=True)
533         self.assertEqual(event['event'], 'BLOCK_JOB_ERROR')
534         self.assert_qmp(event, 'data/device', 'drive0')
535         self.assert_qmp(event, 'data/operation', 'read')
536         result = self.vm.qmp('query-block-jobs')
537         self.assertIn(result['return'][0]['status'], ['running', 'ready'])
538         self.complete_and_wait()
539         self.vm.shutdown()
541         # Detach blkdebug to compare images successfully
542         qemu_img('rebase', '-f', iotests.imgfmt, '-u', '-b', backing_img,
543                  '-F', 'raw', test_img)
544         self.assertTrue(iotests.compare_images(test_img, target_img),
545                         'target image does not match source after mirroring')
547     def test_stop_read(self):
548         self.assert_no_active_block_jobs()
550         self.vm.cmd('drive-mirror', device='drive0', sync='full',
551                     target=target_img, on_source_error='stop')
553         error = False
554         ready = False
555         while not ready:
556             for event in self.vm.get_qmp_events(wait=True):
557                 if event['event'] == 'BLOCK_JOB_ERROR':
558                     self.assert_qmp(event, 'data/device', 'drive0')
559                     self.assert_qmp(event, 'data/operation', 'read')
561                     if self.vm.qmp('query-block-jobs')['return'][0]['status'] != 'paused':
562                         self.vm.events_wait([(
563                             'JOB_STATUS_CHANGE',
564                             {'data': {'id': 'drive0', 'status': 'paused'}}
565                         )])
567                     result = self.vm.qmp('query-block-jobs')
568                     self.assert_qmp(result, 'return[0]/status', 'paused')
569                     self.assert_qmp(result, 'return[0]/io-status', 'failed')
571                     self.vm.cmd('block-job-resume', device='drive0')
572                     error = True
573                 elif event['event'] == 'BLOCK_JOB_READY':
574                     self.assertTrue(error, 'job completed unexpectedly')
575                     self.assert_qmp(event, 'data/device', 'drive0')
576                     ready = True
578         result = self.vm.qmp('query-block-jobs')
579         self.assert_qmp(result, 'return[0]/status', 'ready')
580         self.assert_qmp(result, 'return[0]/io-status', 'ok')
582         self.complete_and_wait(wait_ready=False)
583         self.assert_no_active_block_jobs()
585 class TestWriteErrors(iotests.QMPTestCase):
586     image_len = 2 * 1024 * 1024 # MB
588     # this should be a multiple of twice the default granularity
589     # so that we hit this offset first in state 1
590     MIRROR_GRANULARITY = 1024 * 1024
592     def create_blkdebug_file(self, name, event, errno):
593         file = open(name, 'w')
594         file.write('''
595 [inject-error]
596 state = "1"
597 event = "%s"
598 errno = "%d"
599 immediately = "off"
600 once = "on"
601 sector = "%d"
603 [set-state]
604 state = "1"
605 event = "%s"
606 new_state = "2"
608 [set-state]
609 state = "2"
610 event = "%s"
611 new_state = "1"
612 ''' % (event, errno, self.MIRROR_GRANULARITY // 512, event, event))
613         file.close()
615     def setUp(self):
616         self.blkdebug_file = target_img + ".blkdebug"
617         iotests.create_image(backing_img, TestWriteErrors.image_len)
618         self.create_blkdebug_file(self.blkdebug_file, "write_aio", 5)
619         qemu_img('create', '-f', iotests.imgfmt,
620                  '-obacking_file=%s' %(backing_img), '-F', 'raw', test_img)
621         self.vm = iotests.VM().add_drive(test_img)
622         self.target_img = 'blkdebug:%s:%s' % (self.blkdebug_file, target_img)
623         qemu_img('create', '-f', iotests.imgfmt, '-osize=%d' %(TestWriteErrors.image_len), target_img)
624         self.vm.launch()
626     def tearDown(self):
627         self.vm.shutdown()
628         os.remove(test_img)
629         os.remove(target_img)
630         os.remove(backing_img)
631         os.remove(self.blkdebug_file)
633     def test_report_write(self):
634         self.assert_no_active_block_jobs()
636         self.vm.cmd('drive-mirror', device='drive0', sync='full',
637                     mode='existing', target=self.target_img)
639         completed = False
640         error = False
641         while not completed:
642             for event in self.vm.get_qmp_events(wait=True):
643                 if event['event'] == 'BLOCK_JOB_ERROR':
644                     self.assert_qmp(event, 'data/device', 'drive0')
645                     self.assert_qmp(event, 'data/operation', 'write')
646                     error = True
647                 elif event['event'] == 'BLOCK_JOB_READY':
648                     self.assertTrue(False, 'job completed unexpectedly')
649                 elif event['event'] == 'BLOCK_JOB_COMPLETED':
650                     self.assertTrue(error, 'job completed unexpectedly')
651                     self.assert_qmp(event, 'data/type', 'mirror')
652                     self.assert_qmp(event, 'data/device', 'drive0')
653                     self.assert_qmp(event, 'data/error', 'Input/output error')
654                     completed = True
656         self.assert_no_active_block_jobs()
658     def test_ignore_write(self):
659         self.assert_no_active_block_jobs()
661         self.vm.cmd('drive-mirror', device='drive0', sync='full',
662                     mode='existing', target=self.target_img,
663                     on_target_error='ignore')
665         event = self.vm.event_wait(name='BLOCK_JOB_ERROR')
666         self.assertEqual(event['event'], 'BLOCK_JOB_ERROR')
667         self.assert_qmp(event, 'data/device', 'drive0')
668         self.assert_qmp(event, 'data/operation', 'write')
669         result = self.vm.qmp('query-block-jobs')
670         self.assertIn(result['return'][0]['status'], ['running', 'ready'])
671         self.complete_and_wait()
673     def test_stop_write(self):
674         self.assert_no_active_block_jobs()
676         self.vm.cmd('drive-mirror', device='drive0', sync='full',
677                     mode='existing', target=self.target_img,
678                     on_target_error='stop')
680         error = False
681         ready = False
682         while not ready:
683             for event in self.vm.get_qmp_events(wait=True):
684                 if event['event'] == 'BLOCK_JOB_ERROR':
685                     self.assert_qmp(event, 'data/device', 'drive0')
686                     self.assert_qmp(event, 'data/operation', 'write')
688                     if self.vm.qmp('query-block-jobs')['return'][0]['status'] != 'paused':
689                         self.vm.events_wait([(
690                             'JOB_STATUS_CHANGE',
691                             {'data': {'id': 'drive0', 'status': 'paused'}}
692                         )])
694                     result = self.vm.qmp('query-block-jobs')
695                     self.assert_qmp(result, 'return[0]/status', 'paused')
696                     self.assert_qmp(result, 'return[0]/io-status', 'failed')
698                     self.vm.cmd('block-job-resume', device='drive0')
700                     result = self.vm.qmp('query-block-jobs')
701                     self.assertIn(result['return'][0]['status'], ['running', 'ready'])
702                     self.assert_qmp(result, 'return[0]/io-status', 'ok')
703                     error = True
704                 elif event['event'] == 'BLOCK_JOB_READY':
705                     self.assertTrue(error, 'job completed unexpectedly')
706                     self.assert_qmp(event, 'data/device', 'drive0')
707                     ready = True
709         self.complete_and_wait(wait_ready=False)
710         self.assert_no_active_block_jobs()
712 class TestSetSpeed(iotests.QMPTestCase):
713     image_len = 80 * 1024 * 1024 # MB
715     def setUp(self):
716         qemu_img('create', backing_img, str(TestSetSpeed.image_len))
717         qemu_img('create', '-f', iotests.imgfmt,
718                  '-o', 'backing_file=%s' % backing_img, '-F', 'raw', test_img)
719         self.vm = iotests.VM().add_drive(test_img)
720         self.vm.launch()
722     def tearDown(self):
723         self.vm.shutdown()
724         os.remove(test_img)
725         os.remove(backing_img)
726         os.remove(target_img)
728     def test_set_speed(self):
729         self.assert_no_active_block_jobs()
731         self.vm.cmd('drive-mirror', device='drive0', sync='full',
732                     target=target_img)
734         # Default speed is 0
735         result = self.vm.qmp('query-block-jobs')
736         self.assert_qmp(result, 'return[0]/device', 'drive0')
737         self.assert_qmp(result, 'return[0]/speed', 0)
739         self.vm.cmd('block-job-set-speed', device='drive0', speed=8 * 1024 * 1024)
741         # Ensure the speed we set was accepted
742         result = self.vm.qmp('query-block-jobs')
743         self.assert_qmp(result, 'return[0]/device', 'drive0')
744         self.assert_qmp(result, 'return[0]/speed', 8 * 1024 * 1024)
746         self.wait_ready_and_cancel()
748         # Check setting speed in drive-mirror works
749         self.vm.cmd('drive-mirror', device='drive0', sync='full',
750                     target=target_img, speed=4*1024*1024)
752         result = self.vm.qmp('query-block-jobs')
753         self.assert_qmp(result, 'return[0]/device', 'drive0')
754         self.assert_qmp(result, 'return[0]/speed', 4 * 1024 * 1024)
756         self.wait_ready_and_cancel()
758     def test_set_speed_invalid(self):
759         self.assert_no_active_block_jobs()
761         result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
762                              target=target_img, speed=-1)
763         self.assert_qmp(result, 'error/class', 'GenericError')
765         self.assert_no_active_block_jobs()
767         self.vm.cmd('drive-mirror', device='drive0', sync='full',
768                     target=target_img)
770         result = self.vm.qmp('block-job-set-speed', device='drive0', speed=-1)
771         self.assert_qmp(result, 'error/class', 'GenericError')
773         self.wait_ready_and_cancel()
775 class TestUnbackedSource(iotests.QMPTestCase):
776     image_len = 2 * 1024 * 1024 # MB
778     def setUp(self):
779         qemu_img('create', '-f', iotests.imgfmt, test_img,
780                  str(TestUnbackedSource.image_len))
781         self.vm = iotests.VM()
782         self.vm.launch()
783         self.vm.cmd('blockdev-add', node_name='drive0',
784                     driver=iotests.imgfmt,
785                     file={
786                         'driver': 'file',
787                         'filename': test_img,
788                     })
790     def tearDown(self):
791         self.vm.shutdown()
792         os.remove(test_img)
793         os.remove(target_img)
795     def test_absolute_paths_full(self):
796         self.assert_no_active_block_jobs()
797         self.vm.cmd('drive-mirror', job_id='drive0', device='drive0',
798                     sync='full', target=target_img,
799                     mode='absolute-paths')
800         self.complete_and_wait()
801         self.assert_no_active_block_jobs()
803     def test_absolute_paths_top(self):
804         self.assert_no_active_block_jobs()
805         self.vm.cmd('drive-mirror', job_id='drive0', device='drive0',
806                     sync='top', target=target_img,
807                     mode='absolute-paths')
808         self.complete_and_wait()
809         self.assert_no_active_block_jobs()
811     def test_absolute_paths_none(self):
812         self.assert_no_active_block_jobs()
813         self.vm.cmd('drive-mirror', job_id='drive0', device='drive0',
814                     sync='none', target=target_img,
815                     mode='absolute-paths')
816         self.complete_and_wait()
817         self.assert_no_active_block_jobs()
819     def test_existing_full(self):
820         qemu_img('create', '-f', iotests.imgfmt, target_img,
821                  str(self.image_len))
822         qemu_io('-c', 'write -P 42 0 64k', target_img)
824         self.assert_no_active_block_jobs()
825         self.vm.cmd('drive-mirror', job_id='drive0', device='drive0',
826                     sync='full', target=target_img, mode='existing')
827         self.complete_and_wait()
828         self.assert_no_active_block_jobs()
830         self.vm.cmd('blockdev-del', node_name='drive0')
832         self.assertTrue(iotests.compare_images(test_img, target_img),
833                         'target image does not match source after mirroring')
835     def test_blockdev_full(self):
836         qemu_img('create', '-f', iotests.imgfmt, target_img,
837                  str(self.image_len))
838         qemu_io('-c', 'write -P 42 0 64k', target_img)
840         self.vm.cmd('blockdev-add', node_name='target',
841                     driver=iotests.imgfmt,
842                     file={
843                         'driver': 'file',
844                         'filename': target_img,
845                     })
847         self.assert_no_active_block_jobs()
848         self.vm.cmd('blockdev-mirror', job_id='drive0', device='drive0',
849                     sync='full', target='target')
850         self.complete_and_wait()
851         self.assert_no_active_block_jobs()
853         self.vm.cmd('blockdev-del', node_name='drive0')
855         self.vm.cmd('blockdev-del', node_name='target')
857         self.assertTrue(iotests.compare_images(test_img, target_img),
858                         'target image does not match source after mirroring')
860 class TestGranularity(iotests.QMPTestCase):
861     image_len = 10 * 1024 * 1024 # MB
863     def setUp(self):
864         qemu_img('create', '-f', iotests.imgfmt, test_img,
865                  str(TestGranularity.image_len))
866         qemu_io('-c', 'write 0 %d' % (self.image_len),
867                 test_img)
868         self.vm = iotests.VM().add_drive(test_img)
869         self.vm.launch()
871     def tearDown(self):
872         self.vm.shutdown()
873         self.assertTrue(iotests.compare_images(test_img, target_img),
874                         'target image does not match source after mirroring')
875         os.remove(test_img)
876         os.remove(target_img)
878     def test_granularity(self):
879         self.assert_no_active_block_jobs()
880         self.vm.cmd('drive-mirror', device='drive0',
881                     sync='full', target=target_img,
882                     mode='absolute-paths', granularity=8192)
884         event = self.vm.get_qmp_event(wait=60.0)
885         while event['event'] == 'JOB_STATUS_CHANGE':
886             self.assert_qmp(event, 'data/id', 'drive0')
887             event = self.vm.get_qmp_event(wait=60.0)
889         # Failures will manifest as COMPLETED/ERROR.
890         self.assert_qmp(event, 'event', 'BLOCK_JOB_READY')
891         self.complete_and_wait(drive='drive0', wait_ready=False)
892         self.assert_no_active_block_jobs()
894 class TestRepairQuorum(iotests.QMPTestCase):
895     """ This class test quorum file repair using drive-mirror.
896         It's mostly a fork of TestSingleDrive """
897     image_len = 1 * 1024 * 1024 # MB
898     IMAGES = [ quorum_img1, quorum_img2, quorum_img3 ]
900     @iotests.skip_if_unsupported(['quorum'])
901     def setUp(self):
902         self.vm = iotests.VM()
904         if iotests.qemu_default_machine == 'pc':
905             self.vm.add_drive(None, 'media=cdrom', 'ide')
907         # Add each individual quorum images
908         for i in self.IMAGES:
909             qemu_img('create', '-f', iotests.imgfmt, i,
910                      str(self.image_len))
911             # Assign a node name to each quorum image in order to manipulate
912             # them
913             opts = "node-name=img%i" % self.IMAGES.index(i)
914             opts += ',driver=%s' % iotests.imgfmt
915             opts += ',file.driver=file'
916             opts += ',file.filename=%s' % i
917             self.vm = self.vm.add_blockdev(opts)
919         self.vm.launch()
921         #assemble the quorum block device from the individual files
922         args = { "driver": "quorum", "node-name": "quorum0",
923                  "vote-threshold": 2, "children": [ "img0", "img1", "img2" ] }
924         self.vm.cmd("blockdev-add", args)
927     def tearDown(self):
928         self.vm.shutdown()
929         for i in self.IMAGES + [ quorum_repair_img, quorum_snapshot_file,
930                                  nbd_sock_path ]:
931             # Do a try/except because the test may have deleted some images
932             try:
933                 os.remove(i)
934             except OSError:
935                 pass
937     def test_complete(self):
938         self.vm.cmd('drive-mirror', job_id='job0', device='quorum0',
939                     sync='full', node_name="repair0", replaces="img1",
940                     target=quorum_repair_img, format=iotests.imgfmt)
942         self.complete_and_wait(drive="job0")
943         self.assert_has_block_node("repair0", quorum_repair_img)
944         self.vm.assert_block_path('quorum0', '/children.1', 'repair0')
945         self.vm.shutdown()
946         self.assertTrue(iotests.compare_images(quorum_img2, quorum_repair_img),
947                         'target image does not match source after mirroring')
949     def test_cancel(self):
950         self.vm.cmd('drive-mirror', job_id='job0', device='quorum0',
951                     sync='full', node_name="repair0", replaces="img1",
952                     target=quorum_repair_img, format=iotests.imgfmt)
954         self.cancel_and_wait(drive="job0", force=True)
955         # here we check that the last registered quorum file has not been
956         # swapped out and unref
957         self.assert_has_block_node(None, quorum_img3)
959     def test_cancel_after_ready(self):
960         self.vm.cmd('drive-mirror', job_id='job0', device='quorum0',
961                     sync='full', node_name="repair0", replaces="img1",
962                     target=quorum_repair_img, format=iotests.imgfmt)
964         self.wait_ready_and_cancel(drive="job0")
965         # here we check that the last registered quorum file has not been
966         # swapped out and unref
967         self.assert_has_block_node(None, quorum_img3)
968         self.vm.shutdown()
969         self.assertTrue(iotests.compare_images(quorum_img2, quorum_repair_img),
970                         'target image does not match source after mirroring')
972     def test_pause(self):
973         self.vm.cmd('drive-mirror', job_id='job0', device='quorum0',
974                     sync='full', node_name="repair0", replaces="img1",
975                     target=quorum_repair_img, format=iotests.imgfmt)
977         self.pause_job('job0')
979         result = self.vm.qmp('query-block-jobs')
980         offset = self.dictpath(result, 'return[0]/offset')
982         time.sleep(0.5)
983         result = self.vm.qmp('query-block-jobs')
984         self.assert_qmp(result, 'return[0]/offset', offset)
986         self.vm.cmd('block-job-resume', device='job0')
988         self.complete_and_wait(drive="job0")
989         self.vm.shutdown()
990         self.assertTrue(iotests.compare_images(quorum_img2, quorum_repair_img),
991                         'target image does not match source after mirroring')
993     def test_medium_not_found(self):
994         if iotests.qemu_default_machine != 'pc':
995             return
997         result = self.vm.qmp('drive-mirror', job_id='job0', device='drive0', # CD-ROM
998                              sync='full',
999                              node_name='repair0',
1000                              replaces='img1',
1001                              target=quorum_repair_img, format=iotests.imgfmt)
1002         self.assert_qmp(result, 'error/class', 'GenericError')
1004     def test_image_not_found(self):
1005         result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
1006                              sync='full', node_name='repair0', replaces='img1',
1007                              mode='existing', target=quorum_repair_img,
1008                              format=iotests.imgfmt)
1009         self.assert_qmp(result, 'error/class', 'GenericError')
1011     def test_device_not_found(self):
1012         result = self.vm.qmp('drive-mirror', job_id='job0',
1013                              device='nonexistent', sync='full',
1014                              node_name='repair0',
1015                              replaces='img1',
1016                              target=quorum_repair_img, format=iotests.imgfmt)
1017         self.assert_qmp(result, 'error/class', 'GenericError')
1019     def test_wrong_sync_mode(self):
1020         result = self.vm.qmp('drive-mirror', device='quorum0', job_id='job0',
1021                              node_name='repair0',
1022                              replaces='img1',
1023                              target=quorum_repair_img, format=iotests.imgfmt)
1024         self.assert_qmp(result, 'error/class', 'GenericError')
1026     def test_no_node_name(self):
1027         result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
1028                              sync='full', replaces='img1',
1029                              target=quorum_repair_img, format=iotests.imgfmt)
1030         self.assert_qmp(result, 'error/class', 'GenericError')
1032     def test_nonexistent_replaces(self):
1033         result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
1034                              sync='full', node_name='repair0', replaces='img77',
1035                              target=quorum_repair_img, format=iotests.imgfmt)
1036         self.assert_qmp(result, 'error/class', 'GenericError')
1038     def test_after_a_quorum_snapshot(self):
1039         self.vm.cmd('blockdev-snapshot-sync', node_name='img1',
1040                     snapshot_file=quorum_snapshot_file,
1041                     snapshot_node_name="snap1")
1043         result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
1044                              sync='full', node_name='repair0', replaces="img1",
1045                              target=quorum_repair_img, format=iotests.imgfmt)
1046         self.assert_qmp(result, 'error/class', 'GenericError')
1048         self.vm.cmd('drive-mirror', job_id='job0', device='quorum0',
1049                     sync='full', node_name='repair0', replaces="snap1",
1050                     target=quorum_repair_img, format=iotests.imgfmt)
1052         self.complete_and_wait('job0')
1053         self.assert_has_block_node("repair0", quorum_repair_img)
1054         self.vm.assert_block_path('quorum0', '/children.1', 'repair0')
1056     def test_with_other_parent(self):
1057         """
1058         Check that we cannot replace a Quorum child when it has other
1059         parents.
1060         """
1061         self.vm.cmd('nbd-server-start',
1062                     addr={
1063                         'type': 'unix',
1064                         'data': {'path': nbd_sock_path}
1065                     })
1067         self.vm.cmd('nbd-server-add', device='img1')
1069         result = self.vm.qmp('drive-mirror', job_id='mirror', device='quorum0',
1070                              sync='full', node_name='repair0', replaces='img1',
1071                              target=quorum_repair_img, format=iotests.imgfmt)
1072         self.assert_qmp(result, 'error/desc',
1073                         "Cannot replace 'img1' by a node mirrored from "
1074                         "'quorum0', because it cannot be guaranteed that doing "
1075                         "so would not lead to an abrupt change of visible data")
1077     def test_with_other_parents_after_mirror_start(self):
1078         """
1079         The same as test_with_other_parent(), but add the NBD server
1080         only when the mirror job is already running.
1081         """
1082         self.vm.cmd('nbd-server-start',
1083                     addr={
1084                         'type': 'unix',
1085                         'data': {'path': nbd_sock_path}
1086                     })
1088         self.vm.cmd('drive-mirror', job_id='mirror', device='quorum0',
1089                     sync='full', node_name='repair0', replaces='img1',
1090                     target=quorum_repair_img, format=iotests.imgfmt)
1092         self.vm.cmd('nbd-server-add', device='img1')
1094         # The full error message goes to stderr, we will check it later
1095         self.complete_and_wait('mirror',
1096                                completion_error='Operation not permitted')
1098         # Should not have been replaced
1099         self.vm.assert_block_path('quorum0', '/children.1', 'img1')
1101         # Check the full error message now
1102         self.vm.shutdown()
1103         log = self.vm.get_log()
1104         log = re.sub(r'^\[I \d+\.\d+\] OPENED\n', '', log)
1105         log = re.sub(r'^Formatting.*\n', '', log)
1106         log = re.sub(r'\n\[I \+\d+\.\d+\] CLOSED\n?$', '', log)
1107         log = re.sub(r'^%s: ' % os.path.basename(iotests.qemu_prog), '', log)
1109         self.assertEqual(log,
1110                          "Can no longer replace 'img1' by 'repair0', because " +
1111                          "it can no longer be guaranteed that doing so would " +
1112                          "not lead to an abrupt change of visible data")
1115 # Test mirroring with a source that does not have any parents (not even a
1116 # BlockBackend)
1117 class TestOrphanedSource(iotests.QMPTestCase):
1118     def setUp(self):
1119         blk0 = { 'node-name': 'src',
1120                  'driver': 'null-co' }
1122         blk1 = { 'node-name': 'dest',
1123                  'driver': 'null-co' }
1125         blk2 = { 'node-name': 'dest-ro',
1126                  'driver': 'null-co',
1127                  'read-only': 'on' }
1129         self.vm = iotests.VM()
1130         self.vm.add_blockdev(self.vm.qmp_to_opts(blk0))
1131         self.vm.add_blockdev(self.vm.qmp_to_opts(blk1))
1132         self.vm.add_blockdev(self.vm.qmp_to_opts(blk2))
1133         self.vm.launch()
1135     def tearDown(self):
1136         self.vm.shutdown()
1138     def test_no_job_id(self):
1139         self.assert_no_active_block_jobs()
1141         result = self.vm.qmp('blockdev-mirror', device='src', sync='full',
1142                              target='dest')
1143         self.assert_qmp(result, 'error/class', 'GenericError')
1145     def test_success(self):
1146         self.assert_no_active_block_jobs()
1148         self.vm.cmd('blockdev-mirror', job_id='job', device='src',
1149                     sync='full', target='dest')
1151         self.complete_and_wait('job')
1153     def test_failing_permissions(self):
1154         self.assert_no_active_block_jobs()
1156         result = self.vm.qmp('blockdev-mirror', device='src', sync='full',
1157                              target='dest-ro')
1158         self.assert_qmp(result, 'error/class', 'GenericError')
1160     def test_failing_permission_in_complete(self):
1161         self.assert_no_active_block_jobs()
1163         # Unshare consistent-read on the target
1164         # (The mirror job does not care)
1165         self.vm.cmd('blockdev-add',
1166                     driver='blkdebug',
1167                     node_name='dest-perm',
1168                     image='dest',
1169                     unshare_child_perms=['consistent-read'])
1171         self.vm.cmd('blockdev-mirror', job_id='job', device='src',
1172                     sync='full', target='dest',
1173                     filter_node_name='mirror-filter')
1175         # Require consistent-read on the source
1176         # (We can only add this node once the job has started, or it
1177         # will complain that it does not want to run on non-root nodes)
1178         self.vm.cmd('blockdev-add',
1179                     driver='blkdebug',
1180                     node_name='src-perm',
1181                     image='src',
1182                     take_child_perms=['consistent-read'])
1184         # While completing, mirror will attempt to replace src by
1185         # dest, which must fail because src-perm requires
1186         # consistent-read but dest-perm does not share it; thus
1187         # aborting the job when it is supposed to complete
1188         self.complete_and_wait('job',
1189                                completion_error='Operation not permitted')
1191         # Assert that all of our nodes are still there (except for the
1192         # mirror filter, which should be gone despite the failure)
1193         nodes = self.vm.qmp('query-named-block-nodes')['return']
1194         nodes = [node['node-name'] for node in nodes]
1196         for expect in ('src', 'src-perm', 'dest', 'dest-perm'):
1197             self.assertTrue(expect in nodes, '%s disappeared' % expect)
1198         self.assertFalse('mirror-filter' in nodes,
1199                          'Mirror filter node did not disappear')
1201 # Test cases for @replaces that do not necessarily involve Quorum
1202 class TestReplaces(iotests.QMPTestCase):
1203     # Each of these test cases needs their own block graph, so do not
1204     # create any nodes here
1205     def setUp(self):
1206         self.vm = iotests.VM()
1207         self.vm.launch()
1209     def tearDown(self):
1210         self.vm.shutdown()
1211         for img in (test_img, target_img):
1212             try:
1213                 os.remove(img)
1214             except OSError:
1215                 pass
1217     @iotests.skip_if_unsupported(['copy-on-read'])
1218     def test_replace_filter(self):
1219         """
1220         Check that we can replace filter nodes.
1221         """
1222         self.vm.cmd('blockdev-add', {
1223                         'driver': 'copy-on-read',
1224                         'node-name': 'filter0',
1225                         'file': {
1226                             'driver': 'copy-on-read',
1227                             'node-name': 'filter1',
1228                             'file': {
1229                                 'driver': 'null-co'
1230                             }
1231                         }
1232                     })
1234         self.vm.cmd('blockdev-add',
1235                     node_name='target', driver='null-co')
1237         self.vm.cmd('blockdev-mirror', job_id='mirror', device='filter0',
1238                     target='target', sync='full', replaces='filter1')
1240         self.complete_and_wait('mirror')
1242         self.vm.assert_block_path('filter0', '/file', 'target')
1244 # Tests for mirror with filters (and how the mirror filter behaves, as
1245 # an example for an implicit filter)
1246 class TestFilters(iotests.QMPTestCase):
1247     def setUp(self):
1248         qemu_img('create', '-f', iotests.imgfmt, backing_img, '1M')
1249         qemu_img('create', '-f', iotests.imgfmt, '-b', backing_img,
1250                  '-F', iotests.imgfmt, test_img)
1251         qemu_img('create', '-f', iotests.imgfmt, '-b', backing_img,
1252                  '-F', iotests.imgfmt, target_img)
1254         qemu_io('-c', 'write -P 1 0 512k', backing_img)
1255         qemu_io('-c', 'write -P 2 512k 512k', test_img)
1257         self.vm = iotests.VM().add_device('virtio-scsi,id=vio-scsi')
1258         self.vm.launch()
1260         self.vm.cmd('blockdev-add', {
1261                         'node-name': 'target',
1262                         'driver': iotests.imgfmt,
1263                         'file': {
1264                             'driver': 'file',
1265                             'filename': target_img
1266                         },
1267                         'backing': None
1268                     })
1270         self.filterless_chain = {
1271                 'node-name': 'source',
1272                 'driver': iotests.imgfmt,
1273                 'file': {
1274                     'driver': 'file',
1275                     'filename': test_img
1276                 },
1277                 'backing': {
1278                     'node-name': 'backing',
1279                     'driver': iotests.imgfmt,
1280                     'file': {
1281                         'driver': 'file',
1282                         'filename': backing_img
1283                     }
1284                 }
1285             }
1287     def tearDown(self):
1288         self.vm.shutdown()
1290         os.remove(test_img)
1291         os.remove(target_img)
1292         os.remove(backing_img)
1294     def test_cor(self):
1295         self.vm.cmd('blockdev-add', {
1296                         'node-name': 'filter',
1297                         'driver': 'copy-on-read',
1298                         'file': self.filterless_chain
1299                     })
1301         self.vm.cmd('blockdev-mirror',
1302                     job_id='mirror',
1303                     device='filter',
1304                     target='target',
1305                     sync='top')
1307         self.complete_and_wait('mirror')
1309         self.vm.qmp('blockdev-del', node_name='target')
1311         target_map = qemu_img_map(target_img)
1313         assert target_map[0]['start'] == 0
1314         assert target_map[0]['length'] == 512 * 1024
1315         assert target_map[0]['depth'] == 1
1317         assert target_map[1]['start'] == 512 * 1024
1318         assert target_map[1]['length'] == 512 * 1024
1319         assert target_map[1]['depth'] == 0
1321     def test_implicit_mirror_filter(self):
1322         self.vm.cmd('blockdev-add', self.filterless_chain)
1324         # We need this so we can query from above the mirror node
1325         self.vm.cmd('device_add',
1326                     driver='scsi-hd',
1327                     id='virtio',
1328                     bus='vio-scsi.0',
1329                     drive='source')
1331         self.vm.cmd('blockdev-mirror',
1332                     job_id='mirror',
1333                     device='source',
1334                     target='target',
1335                     sync='top')
1337         # The mirror filter is now an implicit node, so it should be
1338         # invisible when querying the backing chain
1339         blockdevs = self.vm.qmp('query-block')['return']
1340         device_info = next(dev for dev in blockdevs if dev['qdev'] == 'virtio')
1342         assert device_info['inserted']['node-name'] == 'source'
1344         image_info = device_info['inserted']['image']
1345         assert image_info['filename'] == test_img
1346         assert image_info['backing-image']['filename'] == backing_img
1348         self.complete_and_wait('mirror')
1350     def test_explicit_mirror_filter(self):
1351         # Same test as above, but this time we give the mirror filter
1352         # a node-name so it will not be invisible
1353         self.vm.cmd('blockdev-add', self.filterless_chain)
1355         # We need this so we can query from above the mirror node
1356         self.vm.cmd('device_add',
1357                     driver='scsi-hd',
1358                     id='virtio',
1359                     bus='vio-scsi.0',
1360                     drive='source')
1362         self.vm.cmd('blockdev-mirror',
1363                     job_id='mirror',
1364                     device='source',
1365                     target='target',
1366                     sync='top',
1367                     filter_node_name='mirror-filter')
1369         # With a node-name given to it, the mirror filter should now
1370         # be visible
1371         blockdevs = self.vm.qmp('query-block')['return']
1372         device_info = next(dev for dev in blockdevs if dev['qdev'] == 'virtio')
1374         assert device_info['inserted']['node-name'] == 'mirror-filter'
1376         self.complete_and_wait('mirror')
1379 if __name__ == '__main__':
1380     iotests.main(supported_fmts=['qcow2', 'qed'],
1381                  supported_protocols=['file'],
1382                  supported_platforms=['linux', 'freebsd', 'netbsd', 'openbsd'])