1#!/usr/bin/env python3
2# group: rw quick
3#
4# Copyright (C) 2024 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 time
22import qemu
23import iotests
24
25iotests.script_initialize(supported_fmts=['qcow2'],
26                          supported_platforms=['linux'])
27
28with iotests.FilePath('disk1.img') as path, \
29     iotests.FilePath('nbd.sock', base_dir=iotests.sock_dir) as nbd_sock, \
30     qemu.machine.QEMUMachine(iotests.qemu_prog) as vm:
31
32    img_size = '10M'
33
34    iotests.log('Preparing disk...')
35    iotests.qemu_img_create('-f', iotests.imgfmt, path, img_size)
36    vm.add_args('-blockdev', f'file,node-name=disk-file,filename={path}')
37    vm.add_args('-blockdev', 'qcow2,node-name=disk,file=disk-file')
38    vm.add_args('-object', 'iothread,id=iothread0')
39    vm.add_args('-device',
40                'virtio-blk,drive=disk,iothread=iothread0,share-rw=on')
41
42    iotests.log('Launching VM...')
43    vm.add_args('-accel', 'kvm', '-accel', 'tcg')
44    #vm.add_args('-accel', 'qtest')
45    vm.launch()
46
47    iotests.log('Exporting to NBD...')
48    iotests.log(vm.qmp('nbd-server-start',
49                       addr={'type': 'unix', 'data': {'path': nbd_sock}}))
50    iotests.log(vm.qmp('block-export-add', type='nbd', id='exp0',
51                       node_name='disk', writable=True))
52
53    iotests.log('Connecting qemu-img...')
54    qemu_io = iotests.QemuIoInteractive('-f', 'raw',
55                                        f'nbd+unix:///disk?socket={nbd_sock}')
56
57    iotests.log('Moving the NBD export to a different iothread...')
58    for i in range(0, 10):
59        iotests.log(vm.qmp('system_reset'))
60        time.sleep(0.1)
61
62    iotests.log('Checking that it is still alive...')
63    iotests.log(vm.qmp('query-status'))
64
65    qemu_io.close()
66    vm.shutdown()
67