@@ -2008,28 +2008,43 @@ def ssh(machine: Machine, cmd, user="root", password="root", ip="192.168.1.2"):
20082008 return status , out
20092009
20102010
2011- def number_of_devices (machine : Machine ) :
2011+ def number_of_devices (machine : Machine , filter : str = "" ) -> int :
20122012 """
20132013 Returns the number of PCI devices in the VM.
20142014
2015+ :param filter: Optional filter for the PCI device, e.g., vendor ID or device class.
20152016 :param machine: VM host
20162017 :return: number of PCI devices in VM
20172018 """
2018- status , out = ssh (machine , "lspci | wc -l" )
2019+ if filter == "" :
2020+ cmd = "lspci | wc -l"
2021+ else :
2022+ cmd = f"lspci -n | grep { filter } | wc -l"
2023+ status , out = ssh (machine , cmd )
20192024 assert status == 0
20202025 return int (out )
20212026
20222027
2023- def number_of_network_devices (machine : Machine ):
2024- status , out = ssh (machine , "lspci -n | grep 0200 | wc -l" )
2025- assert status == 0
2026- return int (out )
2028+ def number_of_network_devices (machine : Machine ) -> int :
2029+ """
2030+ Returns the number of PCI virtio-net devices in the VM.
20272031
2032+ :param machine: VM host
2033+ :return: number of PCI devices in VM
2034+ """
2035+ PCI_CLASS_GENERIC_ETHERNET_CONTROLLER = "0200"
2036+ return number_of_devices (machine , PCI_CLASS_GENERIC_ETHERNET_CONTROLLER )
20282037
2029- def number_of_storage_devices (machine : Machine ):
2030- status , out = ssh (machine , "lspci -n | grep 0180 | wc -l" )
2031- assert status == 0
2032- return int (out )
2038+
2039+ def number_of_storage_devices (machine : Machine ) -> int :
2040+ """
2041+ Returns the number of PCI virtio-blk devices in the VM.
2042+
2043+ :param machine: VM host
2044+ :return: number of PCI devices in VM
2045+ """
2046+ PCI_CLASS_GENERIC_STORAGE_CONTROLLER = "0180"
2047+ return number_of_devices (machine , PCI_CLASS_GENERIC_STORAGE_CONTROLLER )
20332048
20342049
20352050def reset_system_image (machine : Machine ):
@@ -2085,9 +2100,11 @@ def wait_for_guest_pci_device_enumeration(machine: Machine, new_count: int):
20852100 :param new_count: New device count
20862101 :return:
20872102 """
2088- # retries=10 => max 1s => we expect hotplug events to be relatively quick
2089- if not wait_until_succeed (lambda : number_of_devices (machine ) == new_count , 10 ):
2090- raise RuntimeError ("guest did acknowledge PCI hotplug event" )
2103+ # retries=20 => max 2s => we expect hotplug events to be relatively quick
2104+ if not wait_until_succeed (lambda : number_of_devices (machine ) == new_count , 20 ):
2105+ raise RuntimeError (
2106+ f"guest did acknowledge PCI hotplug event: should be { new_count } but is { number_of_devices (machine )} "
2107+ )
20912108
20922109
20932110runner = unittest .TextTestRunner ()
0 commit comments