xref: /openbmc/qemu/tests/qemu-iotests/307 (revision 1d6f147f)
1#!/usr/bin/env python3
2# group: rw quick export
3#
4# Copyright (C) 2020 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#
21# Test the block export QAPI interfaces
22
23import iotests
24import os
25
26# Need a writable image format (but not vpc, which rounds the image size, nor
27# luks which requires special command lines)
28iotests.script_initialize(
29    supported_fmts=['generic'],
30    unsupported_fmts=['luks', 'vpc'],
31    supported_platforms=['linux'],
32)
33
34with iotests.FilePath('image') as img, \
35     iotests.FilePath('socket', base_dir=iotests.sock_dir) as socket, \
36     iotests.VM() as vm:
37
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)
40
41    iotests.log('=== Launch VM ===')
42
43    vm.add_object('iothread,id=iothread0')
44    vm.add_object('iothread,id=iothread1')
45    vm.add_blockdev(f'file,filename={img},node-name=file')
46    vm.add_blockdev(f'{iotests.imgfmt},file=file,node-name=fmt')
47    vm.add_blockdev('raw,file=file,node-name=ro,read-only=on')
48    vm.add_blockdev('null-co,node-name=null')
49    vm.add_device(f'id=scsi0,driver=virtio-scsi,iothread=iothread0')
50    vm.launch()
51
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')
56
57    iotests.log('\n=== Create a read-only NBD export ===')
58
59    vm.qmp_log('block-export-add', id='export0', type='nbd', node_name='fmt')
60    vm.qmp_log('query-block-exports')
61
62    iotests.qemu_nbd_list_log('-k', socket)
63
64    iotests.log('\n=== Try a few invalid things ===')
65
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',
69               writable=True)
70    vm.qmp_log('block-export-del', id='export1')
71    vm.qmp_log('query-block-exports')
72
73    iotests.log('\n=== Move export to an iothread ===')
74
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)
78
79    iotests.log('\n=== Add export with conflicting iothread ===')
80
81    vm.qmp_log('device_add', id='sdb', driver='scsi-hd', drive='null')
82
83    # Should fail because of fixed-iothread
84    vm.qmp_log('block-export-add', id='export1', type='nbd', node_name='null',
85               iothread='iothread1', fixed_iothread=True, writable=True)
86
87    # Should ignore the iothread conflict, but then fail because of the
88    # permission conflict (and not crash)
89    vm.qmp_log('block-export-add', id='export1', type='nbd', node_name='null',
90               iothread='iothread1', fixed_iothread=False, writable=True)
91
92    iotests.log('\n=== Add a writable export ===')
93
94    # This fails because share-rw=off
95    vm.qmp_log('block-export-add', id='export1', type='nbd', node_name='fmt',
96               name='export1', writable=True, writethrough=True,
97               description='This is the writable second export')
98
99    vm.qmp_log('device_del', id='sda')
100    event = vm.event_wait(name="DEVICE_DELETED",
101                          match={'data': {'device': 'sda'}})
102    iotests.log(event, filters=[iotests.filter_qmp_event])
103    vm.qmp_log('device_add', id='sda', driver='scsi-hd', drive='fmt',
104               share_rw=True)
105
106    # Now it should work
107    vm.qmp_log('block-export-add', id='export1', type='nbd', node_name='fmt',
108               name='export1', writable=True, writethrough=True,
109               description='This is the writable second export')
110
111    vm.qmp_log('query-block-exports')
112    iotests.qemu_nbd_list_log('-k', socket)
113
114    iotests.log('\n=== Connect qemu-io to export1, try removing exports ===')
115
116    nbd_url = f'nbd+unix:///export1?socket={socket}'
117    qemu_io = iotests.QemuIoInteractive('-f', 'raw', nbd_url)
118
119    iotests.log(qemu_io.cmd('read -P 0x11 0 4k'),
120                filters=[iotests.filter_qemu_io])
121    iotests.log(qemu_io.cmd('write -P 0x22 4k 4k'),
122                filters=[iotests.filter_qemu_io])
123
124    vm.qmp_log('block-export-del', id='export1')
125    vm.qmp_log('block-export-del', id='export0')
126    iotests.log(vm.get_qmp_events_filtered())
127    qemu_io.close()
128
129    vm.qmp_log('query-block-exports')
130    iotests.qemu_nbd_list_log('-k', socket)
131
132    iotests.log('\n=== Connect qemu-io again, try force removing ===')
133
134    qemu_io = iotests.QemuIoInteractive('-f', 'raw', nbd_url)
135    vm.qmp_log('block-export-del', id='export1')
136    vm.qmp_log('block-export-del', id='export1', mode='hard')
137
138    # This should fail now
139    iotests.log(qemu_io.cmd('read -P 0x11 0 4k'))
140    qemu_io.close()
141
142    vm.qmp_log('query-block-exports')
143    iotests.qemu_nbd_list_log('-k', socket)
144
145    iotests.log('\n=== Shut down QEMU ===')
146    vm.shutdown()
147