1#!/usr/bin/env python3
2# group: rw quick auto
3#
4# Copyright (C) 2023 Red Hat, Inc.
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18#
19# Creator/Owner: Kevin Wolf <kwolf@redhat.com>
20
21import asyncio
22import iotests
23
24iotests.script_initialize(supported_fmts=['qcow2'],
25                          supported_platforms=['linux'])
26iotests.verify_virtio_scsi_pci_or_ccw()
27
28with iotests.FilePath('disk0.img') as img_path, \
29     iotests.FilePath('disk0-snap.img') as snap_path, \
30     iotests.FilePath('mirror-src.img') as src_path, \
31     iotests.FilePath('mirror-dst.img') as dst_path, \
32     iotests.VM() as vm:
33
34    img_size = '10M'
35    iotests.qemu_img_create('-f', iotests.imgfmt, img_path, img_size)
36    iotests.qemu_img_create('-f', iotests.imgfmt, '-b', img_path,
37                            '-F', iotests.imgfmt, snap_path)
38    iotests.qemu_img_create('-f', iotests.imgfmt, src_path, img_size)
39    iotests.qemu_img_create('-f', iotests.imgfmt, dst_path, img_size)
40
41    iotests.qemu_io_log('-c', 'write 0 64k', img_path)
42    iotests.qemu_io_log('-c', 'write 1M 64k', snap_path)
43    iotests.qemu_io_log('-c', 'write 3M 64k', snap_path)
44
45    iotests.qemu_io_log('-c', f'write 0 {img_size}', src_path)
46
47    iotests.log('Launching VM...')
48    vm.add_object('iothread,id=iothread0')
49    vm.add_object('throttle-group,x-bps-write=1048576,id=tg0')
50    vm.add_blockdev(f'file,node-name=disk0-file,filename={img_path}')
51    vm.add_blockdev('qcow2,node-name=disk0-fmt,file=disk0-file')
52    vm.add_drive(snap_path, 'backing=disk0-fmt,node-name=disk0',
53                 interface='none')
54    vm.add_device('virtio-scsi,iothread=iothread0')
55    vm.add_device('scsi-hd,drive=drive0')
56
57    vm.add_blockdev(f'file,filename={src_path},node-name=mirror-src-file')
58    vm.add_blockdev('qcow2,file=mirror-src-file,node-name=mirror-src')
59    vm.add_blockdev(f'file,filename={dst_path},node-name=mirror-dst-file')
60    vm.add_blockdev('qcow2,file=mirror-dst-file,node-name=mirror-dst-fmt')
61    vm.add_blockdev('throttle,throttle-group=tg0,file=mirror-dst-fmt,'
62                    'node-name=mirror-dst')
63    vm.add_device('scsi-hd,drive=mirror-src')
64
65    vm.launch()
66
67    # The background I/O is created on unrelated nodes (so that they won't be
68    # drained together with the other ones), but on the same iothread
69    iotests.log('Creating some background I/O...')
70    iotests.log(vm.qmp('blockdev-mirror', job_id='job0', sync='full',
71                       device='mirror-src', target='mirror-dst',
72                       auto_dismiss=False))
73
74    iotests.log('Starting active commit...')
75    iotests.log(vm.qmp('block-commit', device='disk0', job_id='job1',
76                       auto_dismiss=False))
77
78    # Should succeed and not time out
79    try:
80        vm.run_job('job1', wait=5.0)
81        vm.shutdown()
82    except asyncio.TimeoutError:
83        # VM may be stuck, kill it
84        vm.kill()
85        raise
86