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