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