| 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Functional test that hotplugs a virtio blk disk and checks it on a Linux |
| 4 | # guest |
| 5 | # |
| 6 | # Copyright (c) 2021 Red Hat, Inc. |
| 7 | # Copyright (c) Yandex |
| 8 | # |
| 9 | # This work is licensed under the terms of the GNU GPL, version 2 or |
| 10 | # later. See the COPYING file in the top-level directory. |
| 11 | |
| 12 | from qemu_test import LinuxKernelTest, Asset, exec_command_and_wait_for_pattern |
| 13 | |
| 14 | |
| 15 | class HotPlugBlk(LinuxKernelTest): |
| 16 | |
| 17 | ASSET_KERNEL = Asset( |
| 18 | ('https://archives.fedoraproject.org/pub/archive/fedora/linux/releases' |
| 19 | '/31/Server/x86_64/os/images/pxeboot/vmlinuz'), |
| 20 | 'd4738d03dbbe083ca610d0821d0a8f1488bebbdccef54ce33e3adb35fda00129') |
| 21 | |
| 22 | ASSET_INITRD = Asset( |
| 23 | ('https://archives.fedoraproject.org/pub/archive/fedora/linux/releases' |
| 24 | '/31/Server/x86_64/os/images/pxeboot/initrd.img'), |
| 25 | '277cd6c7adf77c7e63d73bbb2cded8ef9e2d3a2f100000e92ff1f8396513cd8b') |
| 26 | |
| 27 | def blockdev_add(self) -> None: |
| 28 | self.vm.cmd('blockdev-add', **{ |
| 29 | 'driver': 'null-co', |
| 30 | 'size': 1073741824, |
| 31 | 'node-name': 'disk' |
| 32 | }) |
| 33 | |
| 34 | def assert_vda(self) -> None: |
| 35 | exec_command_and_wait_for_pattern(self, 'while ! test -e /sys/block/vda ;' |
| 36 | ' do sleep 0.2 ; done', '# ') |
| 37 | |
| 38 | def assert_no_vda(self) -> None: |
| 39 | exec_command_and_wait_for_pattern(self, 'while test -e /sys/block/vda ;' |
| 40 | ' do sleep 0.2 ; done', '# ') |
| 41 | |
| 42 | def plug(self) -> None: |
| 43 | args = { |
| 44 | 'driver': 'virtio-blk-pci', |
| 45 | 'drive': 'disk', |
| 46 | 'id': 'virtio-disk0', |
| 47 | 'bus': 'pci.1', |
| 48 | 'addr': '1', |
| 49 | } |
| 50 | |
| 51 | self.assert_no_vda() |
| 52 | self.vm.cmd('device_add', args) |
| 53 | self.wait_for_console_pattern('virtio_blk virtio0: [vda]') |
| 54 | self.assert_vda() |
| 55 | |
| 56 | def unplug(self) -> None: |
| 57 | self.vm.cmd('device_del', id='virtio-disk0') |
| 58 | |
| 59 | self.vm.event_wait('DEVICE_DELETED', 1.0, |
| 60 | match={'data': {'device': 'virtio-disk0'}}) |
| 61 | |
| 62 | self.assert_no_vda() |
| 63 | |
| 64 | def test(self) -> None: |
| 65 | self.require_accelerator('kvm') |
| 66 | self.set_machine('q35') |
| 67 | |
| 68 | self.vm.add_args('-accel', 'kvm') |
| 69 | self.vm.add_args("-nic", "none") # avoid creating default nic |
| 70 | self.vm.add_args('-device', 'pcie-pci-bridge,id=pci.1,bus=pcie.0') |
| 71 | self.vm.add_args('-m', '1G') |
| 72 | self.vm.add_args('-append', 'console=ttyS0 rd.rescue') |
| 73 | |
| 74 | self.launch_kernel(self.ASSET_KERNEL.fetch(), |
| 75 | self.ASSET_INITRD.fetch(), |
| 76 | wait_for='Entering emergency mode.') |
| 77 | self.wait_for_console_pattern('# ') |
| 78 | |
| 79 | self.blockdev_add() |
| 80 | |
| 81 | self.plug() |
| 82 | self.unplug() |
| 83 | |
| 84 | |
| 85 | if __name__ == '__main__': |
| 86 | LinuxKernelTest.main() |