xref: /openbmc/qemu/tests/qemu-iotests/307 (revision eb85394f)
1#!/usr/bin/env python3
2#
3# Copyright (C) 2020 Red Hat, Inc.
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17#
18# Creator/Owner: Kevin Wolf <kwolf@redhat.com>
19#
20# Test the block export QAPI interfaces
21
22import iotests
23import os
24
25# Need a writable image format (but not vpc, which rounds the image size, nor
26# luks which requires special command lines)
27iotests.script_initialize(
28    supported_fmts=['generic'],
29    unsupported_fmts=['luks', 'vpc'],
30    supported_platforms=['linux'],
31)
32
33with iotests.FilePath('image') as img, \
34     iotests.FilePath('socket', base_dir=iotests.sock_dir) as socket, \
35     iotests.VM() as vm:
36
37    iotests.qemu_img('create', '-f', iotests.imgfmt, img, '64M')
38    iotests.qemu_io_log('-f', iotests.imgfmt, '-c', 'write -P 0x11 0 4k', img)
39
40    iotests.log('=== Launch VM ===')
41
42    virtio_scsi_device = iotests.get_virtio_scsi_device()
43
44    vm.add_object('iothread,id=iothread0')
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_device(f'id=scsi0,driver={virtio_scsi_device},iothread=iothread0')
49    vm.launch()
50
51    vm.qmp_log('nbd-server-start',
52               addr={'type': 'unix', 'data': {'path': socket}},
53               filters=(iotests.filter_qmp_testfiles, ))
54    vm.qmp_log('query-block-exports')
55
56    iotests.log('\n=== Create a read-only NBD export ===')
57
58    vm.qmp_log('block-export-add', id='export0', type='nbd', node_name='fmt')
59    vm.qmp_log('query-block-exports')
60
61    iotests.qemu_nbd_list_log('-k', socket)
62
63    iotests.log('\n=== Try a few invalid things ===')
64
65    vm.qmp_log('block-export-add', id='#invalid', type='nbd', node_name='fmt')
66    vm.qmp_log('block-export-add', id='export0', type='nbd', node_name='fmt')
67    vm.qmp_log('block-export-add', id='export1', type='nbd', node_name='ro',
68               writable=True)
69    vm.qmp_log('block-export-del', id='export1')
70    vm.qmp_log('query-block-exports')
71
72    iotests.log('\n=== Move export to an iothread ===')
73
74    vm.qmp_log('device_add', id='sda', driver='scsi-hd', drive='fmt')
75    vm.qmp_log('query-block-exports')
76    iotests.qemu_nbd_list_log('-k', socket)
77
78    iotests.log('\n=== Add a writable export ===')
79
80    # This fails because share-rw=off
81    vm.qmp_log('block-export-add', id='export1', type='nbd', node_name='fmt',
82               name='export1', writable=True, writethrough=True,
83               description='This is the writable second export')
84
85    vm.qmp_log('device_del', id='sda')
86    event = vm.event_wait(name="DEVICE_DELETED",
87                          match={'data': {'device': 'sda'}})
88    iotests.log(event, filters=[iotests.filter_qmp_event])
89    vm.qmp_log('device_add', id='sda', driver='scsi-hd', drive='fmt',
90               share_rw=True)
91
92    # Now it should work
93    vm.qmp_log('block-export-add', id='export1', type='nbd', node_name='fmt',
94               name='export1', writable=True, writethrough=True,
95               description='This is the writable second export')
96
97    vm.qmp_log('query-block-exports')
98    iotests.qemu_nbd_list_log('-k', socket)
99
100    iotests.log('\n=== Connect qemu-io to export1, try removing exports ===')
101
102    nbd_url = f'nbd+unix:///export1?socket={socket}'
103    qemu_io = iotests.QemuIoInteractive('-f', 'raw', nbd_url)
104
105    iotests.log(qemu_io.cmd('read -P 0x11 0 4k'),
106                filters=[iotests.filter_qemu_io])
107    iotests.log(qemu_io.cmd('write -P 0x22 4k 4k'),
108                filters=[iotests.filter_qemu_io])
109
110    vm.qmp_log('block-export-del', id='export1')
111    vm.qmp_log('block-export-del', id='export0')
112    iotests.log(vm.get_qmp_events_filtered())
113    qemu_io.close()
114
115    vm.qmp_log('query-block-exports')
116    iotests.qemu_nbd_list_log('-k', socket)
117
118    iotests.log('\n=== Connect qemu-io again, try force removing ===')
119
120    qemu_io = iotests.QemuIoInteractive('-f', 'raw', nbd_url)
121    vm.qmp_log('block-export-del', id='export1')
122    vm.qmp_log('block-export-del', id='export1', mode='hard')
123
124    # This should fail now
125    iotests.log(qemu_io.cmd('read -P 0x11 0 4k'))
126    qemu_io.close()
127
128    vm.qmp_log('query-block-exports')
129    iotests.qemu_nbd_list_log('-k', socket)
130
131    iotests.log('\n=== Shut down QEMU ===')
132    vm.shutdown()
133