1*e8fce34eSKevin Wolf#!/usr/bin/env python3
2*e8fce34eSKevin Wolf# group: rw quick
3*e8fce34eSKevin Wolf#
4*e8fce34eSKevin Wolf# Copyright (C) 2024 Red Hat, Inc.
5*e8fce34eSKevin Wolf#
6*e8fce34eSKevin Wolf# This program is free software; you can redistribute it and/or modify
7*e8fce34eSKevin Wolf# it under the terms of the GNU General Public License as published by
8*e8fce34eSKevin Wolf# the Free Software Foundation; either version 2 of the License, or
9*e8fce34eSKevin Wolf# (at your option) any later version.
10*e8fce34eSKevin Wolf#
11*e8fce34eSKevin Wolf# This program is distributed in the hope that it will be useful,
12*e8fce34eSKevin Wolf# but WITHOUT ANY WARRANTY; without even the implied warranty of
13*e8fce34eSKevin Wolf# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*e8fce34eSKevin Wolf# GNU General Public License for more details.
15*e8fce34eSKevin Wolf#
16*e8fce34eSKevin Wolf# You should have received a copy of the GNU General Public License
17*e8fce34eSKevin Wolf# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18*e8fce34eSKevin Wolf#
19*e8fce34eSKevin Wolf# Creator/Owner: Kevin Wolf <kwolf@redhat.com>
20*e8fce34eSKevin Wolf
21*e8fce34eSKevin Wolfimport time
22*e8fce34eSKevin Wolfimport qemu
23*e8fce34eSKevin Wolfimport iotests
24*e8fce34eSKevin Wolf
25*e8fce34eSKevin Wolfiotests.script_initialize(supported_fmts=['qcow2'],
26*e8fce34eSKevin Wolf                          supported_platforms=['linux'])
27*e8fce34eSKevin Wolf
28*e8fce34eSKevin Wolfwith iotests.FilePath('disk1.img') as path, \
29*e8fce34eSKevin Wolf     iotests.FilePath('nbd.sock', base_dir=iotests.sock_dir) as nbd_sock, \
30*e8fce34eSKevin Wolf     qemu.machine.QEMUMachine(iotests.qemu_prog) as vm:
31*e8fce34eSKevin Wolf
32*e8fce34eSKevin Wolf    img_size = '10M'
33*e8fce34eSKevin Wolf
34*e8fce34eSKevin Wolf    iotests.log('Preparing disk...')
35*e8fce34eSKevin Wolf    iotests.qemu_img_create('-f', iotests.imgfmt, path, img_size)
36*e8fce34eSKevin Wolf    vm.add_args('-blockdev', f'file,node-name=disk-file,filename={path}')
37*e8fce34eSKevin Wolf    vm.add_args('-blockdev', 'qcow2,node-name=disk,file=disk-file')
38*e8fce34eSKevin Wolf    vm.add_args('-object', 'iothread,id=iothread0')
39*e8fce34eSKevin Wolf    vm.add_args('-device',
40*e8fce34eSKevin Wolf                'virtio-blk,drive=disk,iothread=iothread0,share-rw=on')
41*e8fce34eSKevin Wolf
42*e8fce34eSKevin Wolf    iotests.log('Launching VM...')
43*e8fce34eSKevin Wolf    vm.add_args('-accel', 'kvm', '-accel', 'tcg')
44*e8fce34eSKevin Wolf    #vm.add_args('-accel', 'qtest')
45*e8fce34eSKevin Wolf    vm.launch()
46*e8fce34eSKevin Wolf
47*e8fce34eSKevin Wolf    iotests.log('Exporting to NBD...')
48*e8fce34eSKevin Wolf    iotests.log(vm.qmp('nbd-server-start',
49*e8fce34eSKevin Wolf                       addr={'type': 'unix', 'data': {'path': nbd_sock}}))
50*e8fce34eSKevin Wolf    iotests.log(vm.qmp('block-export-add', type='nbd', id='exp0',
51*e8fce34eSKevin Wolf                       node_name='disk', writable=True))
52*e8fce34eSKevin Wolf
53*e8fce34eSKevin Wolf    iotests.log('Connecting qemu-img...')
54*e8fce34eSKevin Wolf    qemu_io = iotests.QemuIoInteractive('-f', 'raw',
55*e8fce34eSKevin Wolf                                        f'nbd+unix:///disk?socket={nbd_sock}')
56*e8fce34eSKevin Wolf
57*e8fce34eSKevin Wolf    iotests.log('Moving the NBD export to a different iothread...')
58*e8fce34eSKevin Wolf    for i in range(0, 10):
59*e8fce34eSKevin Wolf        iotests.log(vm.qmp('system_reset'))
60*e8fce34eSKevin Wolf        time.sleep(0.1)
61*e8fce34eSKevin Wolf
62*e8fce34eSKevin Wolf    iotests.log('Checking that it is still alive...')
63*e8fce34eSKevin Wolf    iotests.log(vm.qmp('query-status'))
64*e8fce34eSKevin Wolf
65*e8fce34eSKevin Wolf    qemu_io.close()
66*e8fce34eSKevin Wolf    vm.shutdown()
67