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