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