virpci: introduce virPCIDeviceExists()

We're going to add logic to handle the case where a previously
existing PCI device does not longer exist in the host.

The logic was copied from virPCIDeviceNew(), which verifies if a
PCI device exists in the host, returning NULL and throwing an
error if it doesn't. The NULL is used for other errors as well
(product/vendor id read errors, dev id overflow), meaning that we
can't re-use virPCIDeviceNew() for the purpose of detecting
if the device exists.

Reviewed-by: Laine Stump <laine@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
This commit is contained in:
Daniel Henrique Barboza 2021-01-04 09:54:29 -03:00
parent 155151a3d0
commit 3acc65e1b0
3 changed files with 12 additions and 0 deletions

View File

@ -2924,6 +2924,7 @@ virPCIDeviceAddressIsValid;
virPCIDeviceAddressParse;
virPCIDeviceCopy;
virPCIDeviceDetach;
virPCIDeviceExists;
virPCIDeviceFileIterate;
virPCIDeviceFree;
virPCIDeviceGetAddress;

View File

@ -1448,6 +1448,16 @@ virPCIDeviceAddressAsString(const virPCIDeviceAddress *addr)
return str;
}
bool
virPCIDeviceExists(const virPCIDeviceAddress *addr)
{
g_autofree char *devName = virPCIDeviceAddressAsString(addr);
g_autofree char *devPath = g_strdup_printf(PCI_SYSFS "devices/%s/config",
devName);
return virFileExists(devPath);
}
virPCIDevicePtr
virPCIDeviceNew(const virPCIDeviceAddress *address)
{

View File

@ -206,6 +206,7 @@ int virPCIDeviceAddressGetIOMMUGroupAddresses(virPCIDeviceAddressPtr devAddr,
size_t *nIommuGroupDevices);
int virPCIDeviceAddressGetIOMMUGroupNum(virPCIDeviceAddressPtr addr);
char *virPCIDeviceAddressGetIOMMUGroupDev(const virPCIDeviceAddress *devAddr);
bool virPCIDeviceExists(const virPCIDeviceAddress *addr);
char *virPCIDeviceGetIOMMUGroupDev(virPCIDevicePtr dev);
int virPCIDeviceIsAssignable(virPCIDevicePtr dev,