From 4231485c1ccd2e2f001ca97f35128f467e95041d Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Thu, 16 Apr 2015 19:18:32 -0400 Subject: [PATCH] caps: Use an enum internally for ostype value But the internal API stays the same, and we just convert the value as needed. Not useful yet, but this is the beginning step of using an enum for ostype throughout the code. --- src/conf/capabilities.c | 74 +++++++++++++++++++++++++++++++--------- src/conf/capabilities.h | 2 +- src/conf/domain_conf.c | 8 +++++ src/conf/domain_conf.h | 13 +++++++ src/libvirt_private.syms | 2 ++ src/xenconfig/xen_xl.c | 2 +- 6 files changed, 83 insertions(+), 18 deletions(-) diff --git a/src/conf/capabilities.c b/src/conf/capabilities.c index b66c6ddbf..acae41a8d 100644 --- a/src/conf/capabilities.c +++ b/src/conf/capabilities.c @@ -32,6 +32,7 @@ #include "cpu_conf.h" #include "virerror.h" #include "virstring.h" +#include "domain_conf.h" #define VIR_FROM_THIS VIR_FROM_CAPABILITIES @@ -155,8 +156,6 @@ virCapabilitiesFreeGuest(virCapsGuestPtr guest) if (guest == NULL) return; - VIR_FREE(guest->ostype); - VIR_FREE(guest->arch.defaultInfo.emulator); VIR_FREE(guest->arch.defaultInfo.loader); for (i = 0; i < guest->arch.defaultInfo.nmachines; i++) @@ -408,7 +407,7 @@ virCapabilitiesFreeMachines(virCapsGuestMachinePtr *machines, */ virCapsGuestPtr virCapabilitiesAddGuest(virCapsPtr caps, - const char *ostype, + const char *ostypestr, virArch arch, const char *emulator, const char *loader, @@ -416,13 +415,18 @@ virCapabilitiesAddGuest(virCapsPtr caps, virCapsGuestMachinePtr *machines) { virCapsGuestPtr guest; + int ostype; if (VIR_ALLOC(guest) < 0) goto error; - if (VIR_STRDUP(guest->ostype, ostype) < 0) + if ((ostype = virDomainOSTypeFromString(ostypestr)) < 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("unknown OS type '%s'"), ostypestr); goto error; + } + guest->ostype = ostype; guest->arch.id = arch; guest->arch.wordsize = virArchGetWordSize(arch); @@ -603,11 +607,19 @@ virCapabilitiesSupportsGuestArch(virCapsPtr caps, */ extern int virCapabilitiesSupportsGuestOSType(virCapsPtr caps, - const char *ostype) + const char *ostypestr) { size_t i; + int ostype; + + if ((ostype = virDomainOSTypeFromString(ostypestr)) < 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("unknown OS type '%s'"), ostypestr); + return 0; + } + for (i = 0; i < caps->nguests; i++) { - if (STREQ(caps->guests[i]->ostype, ostype)) + if (caps->guests[i]->ostype == ostype) return 1; } return 0; @@ -625,12 +637,20 @@ virCapabilitiesSupportsGuestOSType(virCapsPtr caps, */ extern int virCapabilitiesSupportsGuestOSTypeArch(virCapsPtr caps, - const char *ostype, + const char *ostypestr, virArch arch) { size_t i; + int ostype; + + if ((ostype = virDomainOSTypeFromString(ostypestr)) < 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("unknown OS type '%s'"), ostypestr); + return 0; + } + for (i = 0; i < caps->nguests; i++) { - if (STREQ(caps->guests[i]->ostype, ostype) && + if (caps->guests[i]->ostype == ostype && caps->guests[i]->arch.id == arch) return 1; } @@ -648,14 +668,21 @@ virCapabilitiesSupportsGuestOSTypeArch(virCapsPtr caps, */ extern virArch virCapabilitiesDefaultGuestArch(virCapsPtr caps, - const char *ostype, + const char *ostypestr, const char *domain) { size_t i, j; + int ostype; + + if ((ostype = virDomainOSTypeFromString(ostypestr)) < 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("unknown OS type '%s'"), ostypestr); + return VIR_ARCH_NONE; + } /* First try to find one matching host arch */ for (i = 0; i < caps->nguests; i++) { - if (STREQ(caps->guests[i]->ostype, ostype)) { + if (caps->guests[i]->ostype == ostype) { for (j = 0; j < caps->guests[i]->arch.ndomains; j++) { if (STREQ(caps->guests[i]->arch.domains[j]->type, domain) && caps->guests[i]->arch.id == caps->host.arch) @@ -666,7 +693,7 @@ virCapabilitiesDefaultGuestArch(virCapsPtr caps, /* Otherwise find the first match */ for (i = 0; i < caps->nguests; i++) { - if (STREQ(caps->guests[i]->ostype, ostype)) { + if (caps->guests[i]->ostype == ostype) { for (j = 0; j < caps->guests[i]->arch.ndomains; j++) { if (STREQ(caps->guests[i]->arch.domains[j]->type, domain)) return caps->guests[i]->arch.id; @@ -690,17 +717,24 @@ virCapabilitiesDefaultGuestArch(virCapsPtr caps, */ extern const char * virCapabilitiesDefaultGuestMachine(virCapsPtr caps, - const char *ostype, + const char *ostypestr, virArch arch, const char *domain) { size_t i; + int ostype; + + if ((ostype = virDomainOSTypeFromString(ostypestr)) < 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("unknown OS type '%s'"), ostypestr); + return NULL; + } for (i = 0; i < caps->nguests; i++) { virCapsGuestPtr guest = caps->guests[i]; size_t j; - if (!STREQ(guest->ostype, ostype) || + if (guest->ostype != ostype || guest->arch.id != arch) continue; @@ -736,14 +770,22 @@ virCapabilitiesDefaultGuestMachine(virCapsPtr caps, */ extern const char * virCapabilitiesDefaultGuestEmulator(virCapsPtr caps, - const char *ostype, + const char *ostypestr, virArch arch, const char *domain) { size_t i, j; + int ostype; + + if ((ostype = virDomainOSTypeFromString(ostypestr)) < 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("unknown OS type '%s'"), ostypestr); + return NULL; + } + for (i = 0; i < caps->nguests; i++) { char *emulator; - if (STREQ(caps->guests[i]->ostype, ostype) && + if (caps->guests[i]->ostype == ostype && caps->guests[i]->arch.id == arch) { emulator = caps->guests[i]->arch.defaultInfo.emulator; for (j = 0; j < caps->guests[i]->arch.ndomains; j++) { @@ -944,7 +986,7 @@ virCapabilitiesFormatXML(virCapsPtr caps) virBufferAddLit(&buf, "\n"); virBufferAdjustIndent(&buf, 2); virBufferAsprintf(&buf, "%s\n", - caps->guests[i]->ostype); + virDomainOSTypeToString(caps->guests[i]->ostype)); if (caps->guests[i]->arch.id) virBufferAsprintf(&buf, "\n", virArchToString(caps->guests[i]->arch.id)); diff --git a/src/conf/capabilities.h b/src/conf/capabilities.h index 476debaa0..fc852ac9b 100644 --- a/src/conf/capabilities.h +++ b/src/conf/capabilities.h @@ -79,7 +79,7 @@ struct _virCapsGuestArch { typedef struct _virCapsGuest virCapsGuest; typedef virCapsGuest *virCapsGuestPtr; struct _virCapsGuest { - char *ostype; + int ostype; virCapsGuestArch arch; size_t nfeatures; size_t nfeatures_max; diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 860c9501c..8731fc1f9 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -122,6 +122,14 @@ VIR_ENUM_IMPL(virDomainVirt, VIR_DOMAIN_VIRT_LAST, "parallels", "bhyve") +VIR_ENUM_IMPL(virDomainOS, VIR_DOMAIN_OSTYPE_LAST, + "hvm", + "xen", + "linux", + "exe", + "uml", + "aix") + VIR_ENUM_IMPL(virDomainBoot, VIR_DOMAIN_BOOT_LAST, "fd", "cdrom", diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index 30456524b..6dc9918f9 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -227,6 +227,19 @@ typedef enum { VIR_DOMAIN_VIRT_LAST } virDomainVirtType; +typedef enum { + VIR_DOMAIN_OSTYPE_HVM, + VIR_DOMAIN_OSTYPE_XEN, + VIR_DOMAIN_OSTYPE_LINUX, + VIR_DOMAIN_OSTYPE_EXE, + VIR_DOMAIN_OSTYPE_UML, + VIR_DOMAIN_OSTYPE_AIX, + + VIR_DOMAIN_OSTYPE_LAST +} virDomainOSType; +VIR_ENUM_DECL(virDomainOS) + + typedef enum { VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE, VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI, diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 8c3730399..a587597fb 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -400,6 +400,8 @@ virDomainObjSetDefTransient; virDomainObjSetMetadata; virDomainObjSetState; virDomainObjTaint; +virDomainOSTypeFromString; +virDomainOSTypeToString; virDomainParseMemory; virDomainPausedReasonTypeFromString; virDomainPausedReasonTypeToString; diff --git a/src/xenconfig/xen_xl.c b/src/xenconfig/xen_xl.c index b5725875c..62284f792 100644 --- a/src/xenconfig/xen_xl.c +++ b/src/xenconfig/xen_xl.c @@ -68,7 +68,7 @@ xenParseXLOS(virConfPtr conf, virDomainDefPtr def, virCapsPtr caps) const char *boot; for (i = 0; i < caps->nguests; i++) { - if (STREQ(caps->guests[i]->ostype, "hvm") && + if (caps->guests[i]->ostype == VIR_DOMAIN_OSTYPE_HVM && caps->guests[i]->arch.id == def->os.arch) { if (VIR_ALLOC(def->os.loader) < 0 || VIR_STRDUP(def->os.loader->path, -- 2.39.5