2 # group: rw quick export
4 # Copyright (C) 2020 Red Hat, Inc.
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.
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.
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/>.
19 # Creator/Owner: Kevin Wolf <kwolf@redhat.com>
21 # Test the block export QAPI interfaces
26 # Need a writable image format (but not vpc, which rounds the image size, nor
27 # luks which requires special command lines)
28 iotests.script_initialize(
29 supported_fmts=['generic'],
30 unsupported_fmts=['luks', 'vpc'],
31 supported_platforms=['linux'],
34 with iotests.FilePath('image') as img, \
35 iotests.FilePath('socket', base_dir=iotests.sock_dir) as socket, \
38 iotests.qemu_img('create', '-f', iotests.imgfmt, img, '64M')
39 iotests.qemu_io_log('-f', iotests.imgfmt, '-c', 'write -P 0x11 0 4k', img)
41 iotests.log('=== Launch VM ===')
43 virtio_scsi_device = iotests.get_virtio_scsi_device()
45 vm.add_object('iothread,id=iothread0')
46 vm.add_blockdev(f'file,filename={img},node-name=file')
47 vm.add_blockdev(f'{iotests.imgfmt},file=file,node-name=fmt')
48 vm.add_blockdev('raw,file=file,node-name=ro,read-only=on')
49 vm.add_device(f'id=scsi0,driver={virtio_scsi_device},iothread=iothread0')
52 vm.qmp_log('nbd-server-start',
53 addr={'type': 'unix', 'data': {'path': socket}},
54 filters=(iotests.filter_qmp_testfiles, ))
55 vm.qmp_log('query-block-exports')
57 iotests.log('\n=== Create a read-only NBD export ===')
59 vm.qmp_log('block-export-add', id='export0', type='nbd', node_name='fmt')
60 vm.qmp_log('query-block-exports')
62 iotests.qemu_nbd_list_log('-k', socket)
64 iotests.log('\n=== Try a few invalid things ===')
66 vm.qmp_log('block-export-add', id='#invalid', type='nbd', node_name='fmt')
67 vm.qmp_log('block-export-add', id='export0', type='nbd', node_name='fmt')
68 vm.qmp_log('block-export-add', id='export1', type='nbd', node_name='ro',
70 vm.qmp_log('block-export-del', id='export1')
71 vm.qmp_log('query-block-exports')
73 iotests.log('\n=== Move export to an iothread ===')
75 vm.qmp_log('device_add', id='sda', driver='scsi-hd', drive='fmt')
76 vm.qmp_log('query-block-exports')
77 iotests.qemu_nbd_list_log('-k', socket)
79 iotests.log('\n=== Add a writable export ===')
81 # This fails because share-rw=off
82 vm.qmp_log('block-export-add', id='export1', type='nbd', node_name='fmt',
83 name='export1', writable=True, writethrough=True,
84 description='This is the writable second export')
86 vm.qmp_log('device_del', id='sda')
87 event = vm.event_wait(name="DEVICE_DELETED",
88 match={'data': {'device': 'sda'}})
89 iotests.log(event, filters=[iotests.filter_qmp_event])
90 vm.qmp_log('device_add', id='sda', driver='scsi-hd', drive='fmt',
94 vm.qmp_log('block-export-add', id='export1', type='nbd', node_name='fmt',
95 name='export1', writable=True, writethrough=True,
96 description='This is the writable second export')
98 vm.qmp_log('query-block-exports')
99 iotests.qemu_nbd_list_log('-k', socket)
101 iotests.log('\n=== Connect qemu-io to export1, try removing exports ===')
103 nbd_url = f'nbd+unix:///export1?socket={socket}'
104 qemu_io = iotests.QemuIoInteractive('-f', 'raw', nbd_url)
106 iotests.log(qemu_io.cmd('read -P 0x11 0 4k'),
107 filters=[iotests.filter_qemu_io])
108 iotests.log(qemu_io.cmd('write -P 0x22 4k 4k'),
109 filters=[iotests.filter_qemu_io])
111 vm.qmp_log('block-export-del', id='export1')
112 vm.qmp_log('block-export-del', id='export0')
113 iotests.log(vm.get_qmp_events_filtered())
116 vm.qmp_log('query-block-exports')
117 iotests.qemu_nbd_list_log('-k', socket)
119 iotests.log('\n=== Connect qemu-io again, try force removing ===')
121 qemu_io = iotests.QemuIoInteractive('-f', 'raw', nbd_url)
122 vm.qmp_log('block-export-del', id='export1')
123 vm.qmp_log('block-export-del', id='export1', mode='hard')
125 # This should fail now
126 iotests.log(qemu_io.cmd('read -P 0x11 0 4k'))
129 vm.qmp_log('query-block-exports')
130 iotests.qemu_nbd_list_log('-k', socket)
132 iotests.log('\n=== Shut down QEMU ===')