aboutsummaryrefslogtreecommitdiff
path: root/tests/qemu-iotests/307
blob: b429b5aa50a40b5c0c1273a30b0638622d35c927 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python3
# group: rw quick export
#
# Copyright (C) 2020 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Creator/Owner: Kevin Wolf <kwolf@redhat.com>
#
# Test the block export QAPI interfaces

import iotests
import os

# Need a writable image format (but not vpc, which rounds the image size, nor
# luks which requires special command lines)
iotests.script_initialize(
    supported_fmts=['generic'],
    unsupported_fmts=['luks', 'vpc'],
    supported_platforms=['linux'],
)

with iotests.FilePath('image') as img, \
     iotests.FilePath('socket', base_dir=iotests.sock_dir) as socket, \
     iotests.VM() as vm:

    iotests.qemu_img('create', '-f', iotests.imgfmt, img, '64M')
    iotests.qemu_io_log('-f', iotests.imgfmt, '-c', 'write -P 0x11 0 4k', img)

    iotests.log('=== Launch VM ===')

    vm.add_object('iothread,id=iothread0')
    vm.add_object('iothread,id=iothread1')
    vm.add_blockdev(f'file,filename={img},node-name=file')
    vm.add_blockdev(f'{iotests.imgfmt},file=file,node-name=fmt')
    vm.add_blockdev('raw,file=file,node-name=ro,read-only=on')
    vm.add_blockdev('null-co,node-name=null')
    vm.add_device(f'id=scsi0,driver=virtio-scsi,iothread=iothread0')
    vm.launch()

    vm.qmp_log('nbd-server-start',
               addr={'type': 'unix', 'data': {'path': socket}},
               filters=(iotests.filter_qmp_testfiles, ))
    vm.qmp_log('query-block-exports')

    iotests.log('\n=== Create a read-only NBD export ===')

    vm.qmp_log('block-export-add', id='export0', type='nbd', node_name='fmt')
    vm.qmp_log('query-block-exports')

    iotests.qemu_nbd_list_log('-k', socket)

    iotests.log('\n=== Try a few invalid things ===')

    vm.qmp_log('block-export-add', id='#invalid', type='nbd', node_name='fmt')
    vm.qmp_log('block-export-add', id='export0', type='nbd', node_name='fmt')
    vm.qmp_log('block-export-add', id='export1', type='nbd', node_name='ro',
               writable=True)
    vm.qmp_log('block-export-del', id='export1')
    vm.qmp_log('query-block-exports')

    iotests.log('\n=== Move export to an iothread ===')

    vm.qmp_log('device_add', id='sda', driver='scsi-hd', drive='fmt')
    vm.qmp_log('query-block-exports')
    iotests.qemu_nbd_list_log('-k', socket)

    iotests.log('\n=== Add export with conflicting iothread ===')

    vm.qmp_log('device_add', id='sdb', driver='scsi-hd', drive='null')

    # Should fail because of fixed-iothread
    vm.qmp_log('block-export-add', id='export1', type='nbd', node_name='null',
               iothread='iothread1', fixed_iothread=True, writable=True)

    # Should ignore the iothread conflict, but then fail because of the
    # permission conflict (and not crash)
    vm.qmp_log('block-export-add', id='export1', type='nbd', node_name='null',
               iothread='iothread1', fixed_iothread=False, writable=True)

    iotests.log('\n=== Add a writable export ===')

    # This fails because share-rw=off
    vm.qmp_log('block-export-add', id='export1', type='nbd', node_name='fmt',
               name='export1', writable=True, writethrough=True,
               description='This is the writable second export')

    vm.qmp_log('device_del', id='sda')
    event = vm.event_wait(name="DEVICE_DELETED",
                          match={'data': {'device': 'sda'}})
    iotests.log(event, filters=[iotests.filter_qmp_event])
    vm.qmp_log('device_add', id='sda', driver='scsi-hd', drive='fmt',
               share_rw=True)

    # Now it should work
    vm.qmp_log('block-export-add', id='export1', type='nbd', node_name='fmt',
               name='export1', writable=True, writethrough=True,
               description='This is the writable second export')

    vm.qmp_log('query-block-exports')
    iotests.qemu_nbd_list_log('-k', socket)

    iotests.log('\n=== Connect qemu-io to export1, try removing exports ===')

    nbd_url = f'nbd+unix:///export1?socket={socket}'
    qemu_io = iotests.QemuIoInteractive('-f', 'raw', nbd_url)

    iotests.log(qemu_io.cmd('read -P 0x11 0 4k'),
                filters=[iotests.filter_qemu_io])
    iotests.log(qemu_io.cmd('write -P 0x22 4k 4k'),
                filters=[iotests.filter_qemu_io])

    vm.qmp_log('block-export-del', id='export1')
    vm.qmp_log('block-export-del', id='export0')
    iotests.log(vm.get_qmp_events_filtered())
    qemu_io.close()

    vm.qmp_log('query-block-exports')
    iotests.qemu_nbd_list_log('-k', socket)

    iotests.log('\n=== Connect qemu-io again, try force removing ===')

    qemu_io = iotests.QemuIoInteractive('-f', 'raw', nbd_url)
    vm.qmp_log('block-export-del', id='export1')
    vm.qmp_log('block-export-del', id='export1', mode='hard')

    # This should fail now
    iotests.log(qemu_io.cmd('read -P 0x11 0 4k'))
    qemu_io.close()

    vm.qmp_log('query-block-exports')
    iotests.qemu_nbd_list_log('-k', socket)

    iotests.log('\n=== Shut down QEMU ===')
    vm.shutdown()