Unify the two distinct disk definition validators. Signed-off-by: Peter Krempa <pkrempa@xxxxxxxxxx> --- src/conf/domain_conf.c | 143 +------------------------------------ src/conf/domain_validate.c | 125 ++++++++++++++++++++++++++++++++ src/conf/domain_validate.h | 2 + 3 files changed, 128 insertions(+), 142 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 3f32e046f3..c8c0476a42 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -9044,144 +9044,6 @@ virDomainDiskDefGeometryParse(virDomainDiskDef *def, } -static int -virDomainDiskDefParseValidateSourceChainOne(const virStorageSource *src) -{ - if (src->type == VIR_STORAGE_TYPE_NETWORK && src->auth) { - virStorageAuthDef *authdef = src->auth; - int actUsage; - - if ((actUsage = virSecretUsageTypeFromString(authdef->secrettype)) < 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unknown secret type '%s'"), - NULLSTR(authdef->secrettype)); - return -1; - } - - if ((src->protocol == VIR_STORAGE_NET_PROTOCOL_ISCSI && - actUsage != VIR_SECRET_USAGE_TYPE_ISCSI) || - (src->protocol == VIR_STORAGE_NET_PROTOCOL_RBD && - actUsage != VIR_SECRET_USAGE_TYPE_CEPH)) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("invalid secret type '%s'"), - virSecretUsageTypeToString(actUsage)); - return -1; - } - } - - if (src->encryption) { - virStorageEncryption *encryption = src->encryption; - - if (encryption->format == VIR_STORAGE_ENCRYPTION_FORMAT_LUKS && - encryption->encinfo.cipher_name) { - - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", - _("supplying <cipher> for domain disk definition " - "is unnecessary")); - return -1; - } - } - - return 0; -} - - -static int -virDomainDiskDefParseValidateSource(const virStorageSource *src) -{ - const virStorageSource *next; - - for (next = src; next; next = next->backingStore) { - if (virDomainDiskDefParseValidateSourceChainOne(next) < 0) - return -1; - } - - return 0; -} - - -static int -virDomainDiskDefParseValidate(const virDomainDiskDef *def) - -{ - if (virDomainDiskDefParseValidateSource(def->src) < 0) - return -1; - - if (def->bus != VIR_DOMAIN_DISK_BUS_VIRTIO) { - if (def->event_idx != VIR_TRISTATE_SWITCH_ABSENT) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", - _("disk event_idx mode supported only for virtio bus")); - return -1; - } - - if (def->ioeventfd != VIR_TRISTATE_SWITCH_ABSENT) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", - _("disk ioeventfd mode supported only for virtio bus")); - return -1; - } - } - - if (def->device != VIR_DOMAIN_DISK_DEVICE_LUN) { - if (def->rawio != VIR_TRISTATE_BOOL_ABSENT) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", - _("rawio can be used only with device='lun'")); - return -1; - } - - if (def->sgio != VIR_DOMAIN_DEVICE_SGIO_DEFAULT) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", - _("sgio can be used only with device='lun'")); - return -1; - } - } - - if (def->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY && - def->bus != VIR_DOMAIN_DISK_BUS_FDC) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Invalid bus type '%s' for floppy disk"), - virDomainDiskBusTypeToString(def->bus)); - return -1; - } - - if (def->device != VIR_DOMAIN_DISK_DEVICE_FLOPPY && - def->bus == VIR_DOMAIN_DISK_BUS_FDC) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Invalid bus type '%s' for disk"), - virDomainDiskBusTypeToString(def->bus)); - return -1; - } - - if (def->removable != VIR_TRISTATE_SWITCH_ABSENT && - def->bus != VIR_DOMAIN_DISK_BUS_USB) { - virReportError(VIR_ERR_XML_ERROR, "%s", - _("removable is only valid for usb disks")); - return -1; - } - - if (def->startupPolicy != VIR_DOMAIN_STARTUP_POLICY_DEFAULT) { - if (def->src->type == VIR_STORAGE_TYPE_NETWORK) { - virReportError(VIR_ERR_XML_ERROR, - _("Setting disk %s is not allowed for " - "disk of network type"), - virDomainStartupPolicyTypeToString(def->startupPolicy)); - return -1; - } - - if (def->device != VIR_DOMAIN_DISK_DEVICE_CDROM && - def->device != VIR_DOMAIN_DISK_DEVICE_FLOPPY && - def->startupPolicy == VIR_DOMAIN_STARTUP_POLICY_REQUISITE) { - virReportError(VIR_ERR_XML_ERROR, "%s", - _("Setting disk 'requisite' is allowed only for " - "cdrom or floppy")); - return -1; - } - } - - - return 0; -} - - static int virDomainDiskDefDriverParseXML(virDomainDiskDef *def, xmlNodePtr cur, @@ -9730,9 +9592,6 @@ virDomainDiskDefParseXML(virDomainXMLOption *xmlopt, virDomainDiskDefParsePrivateData(ctxt, def, xmlopt) < 0) return NULL; - if (virDomainDiskDefParseValidate(def) < 0) - return NULL; - return g_steal_pointer(&def); } @@ -16497,7 +16356,7 @@ virDomainDiskDefParseSource(const char *xmlStr, return NULL; } - if (virDomainDiskDefParseValidateSource(src) < 0) + if (virDomainDiskDefValidateSource(src) < 0) return NULL; return g_steal_pointer(&src); diff --git a/src/conf/domain_validate.c b/src/conf/domain_validate.c index 4d253599af..97fa218252 100644 --- a/src/conf/domain_validate.c +++ b/src/conf/domain_validate.c @@ -430,6 +430,62 @@ virDomainDiskVhostUserValidate(const virDomainDiskDef *disk) } +static int +virDomainDiskDefValidateSourceChainOne(const virStorageSource *src) +{ + if (src->type == VIR_STORAGE_TYPE_NETWORK && src->auth) { + virStorageAuthDef *authdef = src->auth; + int actUsage; + + if ((actUsage = virSecretUsageTypeFromString(authdef->secrettype)) < 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("unknown secret type '%s'"), + NULLSTR(authdef->secrettype)); + return -1; + } + + if ((src->protocol == VIR_STORAGE_NET_PROTOCOL_ISCSI && + actUsage != VIR_SECRET_USAGE_TYPE_ISCSI) || + (src->protocol == VIR_STORAGE_NET_PROTOCOL_RBD && + actUsage != VIR_SECRET_USAGE_TYPE_CEPH)) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("invalid secret type '%s'"), + virSecretUsageTypeToString(actUsage)); + return -1; + } + } + + if (src->encryption) { + virStorageEncryption *encryption = src->encryption; + + if (encryption->format == VIR_STORAGE_ENCRYPTION_FORMAT_LUKS && + encryption->encinfo.cipher_name) { + + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("supplying <cipher> for domain disk definition " + "is unnecessary")); + return -1; + } + } + + return 0; +} + + +int +virDomainDiskDefValidateSource(const virStorageSource *src) +{ + const virStorageSource *next; + + for (next = src; next; next = next->backingStore) { + if (virDomainDiskDefValidateSourceChainOne(next) < 0) + return -1; + } + + return 0; +} + + #define VENDOR_LEN 8 #define PRODUCT_LEN 16 @@ -439,6 +495,9 @@ virDomainDiskDefValidate(const virDomainDef *def, { virStorageSource *next; + if (virDomainDiskDefValidateSource(disk->src) < 0) + return -1; + /* Validate LUN configuration */ if (disk->device == VIR_DOMAIN_DISK_DEVICE_LUN) { /* volumes haven't been translated at this point, so accept them */ @@ -457,6 +516,18 @@ virDomainDiskDefValidate(const virDomainDef *def, _("<reservations/> allowed only for lun devices")); return -1; } + + if (disk->rawio != VIR_TRISTATE_BOOL_ABSENT) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("rawio can be used only with device='lun'")); + return -1; + } + + if (disk->sgio != VIR_DOMAIN_DEVICE_SGIO_DEFAULT) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("sgio can be used only with device='lun'")); + return -1; + } } /* Reject disks with a bus type that is not compatible with the @@ -490,6 +561,18 @@ virDomainDiskDefValidate(const virDomainDef *def, return -1; } + if (disk->event_idx != VIR_TRISTATE_SWITCH_ABSENT) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("disk event_idx mode supported only for virtio bus")); + return -1; + } + + if (disk->ioeventfd != VIR_TRISTATE_SWITCH_ABSENT) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("disk ioeventfd mode supported only for virtio bus")); + return -1; + } + if (virDomainCheckVirtioOptionsAreAbsent(disk->virtio) < 0) return -1; } @@ -538,6 +621,48 @@ virDomainDiskDefValidate(const virDomainDef *def, return -1; } + if (disk->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY && + disk->bus != VIR_DOMAIN_DISK_BUS_FDC) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Invalid bus type '%s' for floppy disk"), + virDomainDiskBusTypeToString(disk->bus)); + return -1; + } + + if (disk->device != VIR_DOMAIN_DISK_DEVICE_FLOPPY && + disk->bus == VIR_DOMAIN_DISK_BUS_FDC) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Invalid bus type '%s' for disk"), + virDomainDiskBusTypeToString(disk->bus)); + return -1; + } + + if (disk->removable != VIR_TRISTATE_SWITCH_ABSENT && + disk->bus != VIR_DOMAIN_DISK_BUS_USB) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("removable is only valid for usb disks")); + return -1; + } + + if (disk->startupPolicy != VIR_DOMAIN_STARTUP_POLICY_DEFAULT) { + if (disk->src->type == VIR_STORAGE_TYPE_NETWORK) { + virReportError(VIR_ERR_XML_ERROR, + _("Setting disk %s is not allowed for " + "disk of network type"), + virDomainStartupPolicyTypeToString(disk->startupPolicy)); + return -1; + } + + if (disk->device != VIR_DOMAIN_DISK_DEVICE_CDROM && + disk->device != VIR_DOMAIN_DISK_DEVICE_FLOPPY && + disk->startupPolicy == VIR_DOMAIN_STARTUP_POLICY_REQUISITE) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("Setting disk 'requisite' is allowed only for " + "cdrom or floppy")); + return -1; + } + } + return 0; } diff --git a/src/conf/domain_validate.h b/src/conf/domain_validate.h index 5f31616c85..38a1abcc8f 100644 --- a/src/conf/domain_validate.h +++ b/src/conf/domain_validate.h @@ -38,3 +38,5 @@ int virDomainDeviceDefValidate(const virDomainDeviceDef *dev, unsigned int parseFlags, virDomainXMLOption *xmlopt, void *parseOpaque); + +int virDomainDiskDefValidateSource(const virStorageSource *src); -- 2.30.2