xref: /openbmc/qemu/tests/qemu-iotests/307 (revision 587adaca)
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_blockdev(f'file,filename={img},node-name=file')
45    vm.add_blockdev(f'{iotests.imgfmt},file=file,node-name=fmt')
46    vm.add_blockdev('raw,file=file,node-name=ro,read-only=on')
47    vm.add_device(f'id=scsi0,driver=virtio-scsi,iothread=iothread0')
48    vm.launch()
49
50    vm.qmp_log('nbd-server-start',
51               addr={'type': 'unix', 'data': {'path': socket}},
52               filters=(iotests.filter_qmp_testfiles, ))
53    vm.qmp_log('query-block-exports')
54
55    iotests.log('\n=== Create a read-only NBD export ===')
56
57    vm.qmp_log('block-export-add', id='export0', type='nbd', node_name='fmt')
58    vm.qmp_log('query-block-exports')
59
60    iotests.qemu_nbd_list_log('-k', socket)
61
62    iotests.log('\n=== Try a few invalid things ===')
63
64    vm.qmp_log('block-export-add', id='#invalid', type='nbd', node_name='fmt')
65    vm.qmp_log('block-export-add', id='export0', type='nbd', node_name='fmt')
66    vm.qmp_log('block-export-add', id='export1', type='nbd', node_name='ro',
67               writable=True)
68    vm.qmp_log('block-export-del', id='export1')
69    vm.qmp_log('query-block-exports')
70
71    iotests.log('\n=== Move export to an iothread ===')
72
73    vm.qmp_log('device_add', id='sda', driver='scsi-hd', drive='fmt')
74    vm.qmp_log('query-block-exports')
75    iotests.qemu_nbd_list_log('-k', socket)
76
77    iotests.log('\n=== Add a writable export ===')
78
79    # This fails because share-rw=off
80    vm.qmp_log('block-export-add', id='export1', type='nbd', node_name='fmt',
81               name='export1', writable=True, writethrough=True,
82               description='This is the writable second export')
83
84    vm.qmp_log('device_del', id='sda')
85    event = vm.event_wait(name="DEVICE_DELETED",
86                          match={'data': {'device': 'sda'}})
87    iotests.log(event, filters=[iotests.filter_qmp_event])
88    vm.qmp_log('device_add', id='sda', driver='scsi-hd', drive='fmt',
89               share_rw=True)
90
91    # Now it should work
92    vm.qmp_log('block-export-add', id='export1', type='nbd', node_name='fmt',
93               name='export1', writable=True, writethrough=True,
94               description='This is the writable second export')
95
96    vm.qmp_log('query-block-exports')
97    iotests.qemu_nbd_list_log('-k', socket)
98
99    iotests.log('\n=== Connect qemu-io to export1, try removing exports ===')
100
101    nbd_url = f'nbd+unix:///export1?socket={socket}'
102    qemu_io = iotests.QemuIoInteractive('-f', 'raw', nbd_url)
103
104    iotests.log(qemu_io.cmd('read -P 0x11 0 4k'),
105                filters=[iotests.filter_qemu_io])
106    iotests.log(qemu_io.cmd('write -P 0x22 4k 4k'),
107                filters=[iotests.filter_qemu_io])
108
109    vm.qmp_log('block-export-del', id='export1')
110    vm.qmp_log('block-export-del', id='export0')
111    iotests.log(vm.get_qmp_events_filtered())
112    qemu_io.close()
113
114    vm.qmp_log('query-block-exports')
115    iotests.qemu_nbd_list_log('-k', socket)
116
117    iotests.log('\n=== Connect qemu-io again, try force removing ===')
118
119    qemu_io = iotests.QemuIoInteractive('-f', 'raw', nbd_url)
120    vm.qmp_log('block-export-del', id='export1')
121    vm.qmp_log('block-export-del', id='export1', mode='hard')
122
123    # This should fail now
124    iotests.log(qemu_io.cmd('read -P 0x11 0 4k'))
125    qemu_io.close()
126
127    vm.qmp_log('query-block-exports')
128    iotests.qemu_nbd_list_log('-k', socket)
129
130    iotests.log('\n=== Shut down QEMU ===')
131    vm.shutdown()
132