Skip to content

Commit

Permalink
virsh_detach_device_alias: enhancement for pci
Browse files Browse the repository at this point in the history
Currently the cases are skipped in most time because below error:

error: Failed to attach device from pci.xml
error: internal error: Non-endpoint PCI devices cannot be assigned to guests

This is because the incorrect pci device is used to attach to the vm.
This fix is an enhancement to increase possiblities of finding the suitable pci device.

Signed-off-by: Dan Zheng <[email protected]>
  • Loading branch information
dzhengfy committed Jan 26, 2025
1 parent 5552930 commit 39d1851
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions libvirt/tests/src/virsh_cmd/domain/virsh_detach_device_alias.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging as log
import os
import platform
import uuid
import logging as log

from virttest import data_dir
from virttest import virsh
Expand All @@ -22,6 +23,34 @@
logging = log.getLogger('avocado.' + __name__)


def get_suitable_pci_device(test):
"""
Get a suitable pci device ID for attaching to the vm
The host PCI devices will vary on different hardwares, so it is hard
to always get an usable pci device to do this.
:param test: test object
:return: str, the pci devce full id or None
"""
pci_ids = utils_sys.get_host_bridge_id()
if not pci_ids:
test.error("Not Found any pci devices")

if platform.machine() != "aarch64":
good_pci = utils_misc.get_full_pci_id(pci_ids[-1]).split("\n")[0]
return good_pci
else:
suitable_pci_ids = [id for id in pci_ids if id != "0000:00"]
for pci_id in suitable_pci_ids:
full_ids = utils_misc.get_full_pci_id(pci_id)
good_pci = "%s:00.0" % pci_id
if full_ids.count(good_pci):
test.log.debug("PCI ID '%s' is chosen", good_pci)
return good_pci
test.log.warning("No suitable PCI ID is chosen")
return None


def run(test, params, env):
"""
Test detach-device-alias command with
Expand Down Expand Up @@ -159,12 +188,7 @@ def start_usbredirserver():
dev_type='controller',
dev_dict=controller_dict,
index=int(params.get("index")))

pci_ids = utils_sys.get_host_bridge_id()
if not pci_ids:
test.error("Not Found any pci devices")
pci_id = utils_misc.get_full_pci_id(pci_ids[-1])

pci_id = get_suitable_pci_device(test)
if not vm.is_alive():
vm.start()
vm.wait_for_login()
Expand Down

0 comments on commit 39d1851

Please sign in to comment.