diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 44be81e2040e00b16e04d3b9048511c8f547f4c8..57cd9b1327f2d55136044ab7ea473aa05e9ba677 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -18314,3 +18314,44 @@ virDomainDiskDefGenSecurityLabelDef(const char *model) return seclabel; } + + +typedef struct { + const char *devAlias; + virDomainDeviceDefPtr dev; +} virDomainDefFindDeviceCallbackData; + +static int +virDomainDefFindDeviceCallback(virDomainDefPtr def ATTRIBUTE_UNUSED, + virDomainDeviceDefPtr dev, + virDomainDeviceInfoPtr info, + void *opaque) +{ + virDomainDefFindDeviceCallbackData *data = opaque; + + if (STREQ_NULLABLE(info->alias, data->devAlias)) { + *data->dev = *dev; + return -1; + } + return 0; +} + +int +virDomainDefFindDevice(virDomainDefPtr def, + const char *devAlias, + virDomainDeviceDefPtr dev) +{ + virDomainDefFindDeviceCallbackData data = { devAlias, dev }; + + dev->type = VIR_DOMAIN_DEVICE_NONE; + virDomainDeviceInfoIterateInternal(def, virDomainDefFindDeviceCallback, + true, &data); + + if (dev->type == VIR_DOMAIN_DEVICE_NONE) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("no device found with alias %s"), devAlias); + return -1; + } + + return 0; +} diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index b14afd9a3c07412cf11d357149af215f877ec175..25dad16bf1a20b60ab7d2503c0b2afe23e1947c6 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -2688,4 +2688,8 @@ virDomainDefMaybeAddController(virDomainDefPtr def, char *virDomainDefGetDefaultEmulator(virDomainDefPtr def, virCapsPtr caps); +int virDomainDefFindDevice(virDomainDefPtr def, + const char *devAlias, + virDomainDeviceDefPtr dev); + #endif /* __DOMAIN_CONF_H */ diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 78a56db029218a0c4a824b86ff1df3973e91d19b..7790ede6d58d9ae03c5c12619a295ad60618f3b8 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -154,6 +154,7 @@ virDomainDefClearDeviceAliases; virDomainDefClearPCIAddresses; virDomainDefCompatibleDevice; virDomainDefCopy; +virDomainDefFindDevice; virDomainDefFormat; virDomainDefFormatInternal; virDomainDefFree;