mirror of https://gitee.com/openkylin/libvirt.git
domain_conf: move virDomainDiskDefValidate() to domain_validate.c
Next patch will add more validations to the function. Let's move it beforehand to domain_validate.c. virSecurityDeviceLabelDefValidateXML() is still used inside domain_conf.c, so make it public for now until its current caller (virDomainChrSourceDefValidate()) is also moved to domain_validate.c. Reviewed-by: Michal Privoznik <mprivozn@redhat.com> Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
This commit is contained in:
parent
b628416399
commit
654e106397
|
@ -6043,158 +6043,6 @@ virDomainDefPostParse(virDomainDefPtr def,
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* virDomainDiskAddressDiskBusCompatibility:
|
||||
* @bus: disk bus type
|
||||
* @addressType: disk address type
|
||||
*
|
||||
* Check if the specified disk address type @addressType is compatible
|
||||
* with the specified disk bus type @bus. This function checks
|
||||
* compatibility with the bus types SATA, SCSI, FDC, and IDE only,
|
||||
* because only these are handled in common code.
|
||||
*
|
||||
* Returns true if compatible or can't be decided in common code,
|
||||
* false if known to be not compatible.
|
||||
*/
|
||||
static bool
|
||||
virDomainDiskAddressDiskBusCompatibility(virDomainDiskBus bus,
|
||||
virDomainDeviceAddressType addressType)
|
||||
{
|
||||
if (addressType == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE)
|
||||
return true;
|
||||
|
||||
switch (bus) {
|
||||
case VIR_DOMAIN_DISK_BUS_IDE:
|
||||
case VIR_DOMAIN_DISK_BUS_FDC:
|
||||
case VIR_DOMAIN_DISK_BUS_SCSI:
|
||||
case VIR_DOMAIN_DISK_BUS_SATA:
|
||||
return addressType == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_DRIVE;
|
||||
case VIR_DOMAIN_DISK_BUS_VIRTIO:
|
||||
case VIR_DOMAIN_DISK_BUS_XEN:
|
||||
case VIR_DOMAIN_DISK_BUS_USB:
|
||||
case VIR_DOMAIN_DISK_BUS_UML:
|
||||
case VIR_DOMAIN_DISK_BUS_SD:
|
||||
case VIR_DOMAIN_DISK_BUS_LAST:
|
||||
return true;
|
||||
}
|
||||
|
||||
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
||||
_("unexpected bus type '%d'"),
|
||||
bus);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
virSecurityDeviceLabelDefValidateXML(virSecurityDeviceLabelDefPtr *seclabels,
|
||||
size_t nseclabels,
|
||||
virSecurityLabelDefPtr *vmSeclabels,
|
||||
size_t nvmSeclabels)
|
||||
{
|
||||
virSecurityDeviceLabelDefPtr seclabel;
|
||||
size_t i;
|
||||
size_t j;
|
||||
|
||||
for (i = 0; i < nseclabels; i++) {
|
||||
seclabel = seclabels[i];
|
||||
|
||||
/* find the security label that it's being overridden */
|
||||
for (j = 0; j < nvmSeclabels; j++) {
|
||||
if (STRNEQ_NULLABLE(vmSeclabels[j]->model, seclabel->model))
|
||||
continue;
|
||||
|
||||
if (!vmSeclabels[j]->relabel) {
|
||||
virReportError(VIR_ERR_XML_ERROR, "%s",
|
||||
_("label overrides require relabeling to be "
|
||||
"enabled at the domain level"));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
virDomainDiskDefValidate(const virDomainDef *def,
|
||||
const virDomainDiskDef *disk)
|
||||
{
|
||||
virStorageSourcePtr next;
|
||||
|
||||
/* Validate LUN configuration */
|
||||
if (disk->device == VIR_DOMAIN_DISK_DEVICE_LUN) {
|
||||
/* volumes haven't been translated at this point, so accept them */
|
||||
if (!(disk->src->type == VIR_STORAGE_TYPE_BLOCK ||
|
||||
disk->src->type == VIR_STORAGE_TYPE_VOLUME ||
|
||||
(disk->src->type == VIR_STORAGE_TYPE_NETWORK &&
|
||||
disk->src->protocol == VIR_STORAGE_NET_PROTOCOL_ISCSI))) {
|
||||
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
||||
_("disk '%s' improperly configured for a "
|
||||
"device='lun'"), disk->dst);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (disk->src->pr &&
|
||||
disk->device != VIR_DOMAIN_DISK_DEVICE_LUN) {
|
||||
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
|
||||
_("<reservations/> allowed only for lun devices"));
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Reject disks with a bus type that is not compatible with the
|
||||
* given address type. The function considers only buses that are
|
||||
* handled in common code. For other bus types it's not possible
|
||||
* to decide compatibility in common code.
|
||||
*/
|
||||
if (!virDomainDiskAddressDiskBusCompatibility(disk->bus, disk->info.type)) {
|
||||
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
||||
_("Invalid address type '%s' for the disk '%s' with the bus type '%s'"),
|
||||
virDomainDeviceAddressTypeToString(disk->info.type),
|
||||
disk->dst,
|
||||
virDomainDiskBusTypeToString(disk->bus));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (disk->queues && disk->bus != VIR_DOMAIN_DISK_BUS_VIRTIO) {
|
||||
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
|
||||
_("queues attribute in disk driver element is only "
|
||||
"supported by virtio-blk"));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (disk->bus != VIR_DOMAIN_DISK_BUS_VIRTIO &&
|
||||
(disk->model == VIR_DOMAIN_DISK_MODEL_VIRTIO ||
|
||||
disk->model == VIR_DOMAIN_DISK_MODEL_VIRTIO_TRANSITIONAL ||
|
||||
disk->model == VIR_DOMAIN_DISK_MODEL_VIRTIO_NON_TRANSITIONAL)) {
|
||||
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
||||
_("disk model '%s' not supported for bus '%s'"),
|
||||
virDomainDiskModelTypeToString(disk->model),
|
||||
virDomainDiskBusTypeToString(disk->bus));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (disk->src->type == VIR_STORAGE_TYPE_NVME) {
|
||||
/* NVMe namespaces start from 1 */
|
||||
if (disk->src->nvme->namespc == 0) {
|
||||
virReportError(VIR_ERR_XML_ERROR, "%s",
|
||||
_("NVMe namespace can't be zero"));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
for (next = disk->src; next; next = next->backingStore) {
|
||||
if (virSecurityDeviceLabelDefValidateXML(next->seclabels,
|
||||
next->nseclabels,
|
||||
def->seclabels,
|
||||
def->nseclabels) < 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool
|
||||
virDomainDefHasUSB(const virDomainDef *def)
|
||||
{
|
||||
|
|
|
@ -150,3 +150,156 @@ virDomainVideoDefValidate(const virDomainVideoDef *video,
|
|||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* virDomainDiskAddressDiskBusCompatibility:
|
||||
* @bus: disk bus type
|
||||
* @addressType: disk address type
|
||||
*
|
||||
* Check if the specified disk address type @addressType is compatible
|
||||
* with the specified disk bus type @bus. This function checks
|
||||
* compatibility with the bus types SATA, SCSI, FDC, and IDE only,
|
||||
* because only these are handled in common code.
|
||||
*
|
||||
* Returns true if compatible or can't be decided in common code,
|
||||
* false if known to be not compatible.
|
||||
*/
|
||||
static bool
|
||||
virDomainDiskAddressDiskBusCompatibility(virDomainDiskBus bus,
|
||||
virDomainDeviceAddressType addressType)
|
||||
{
|
||||
if (addressType == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE)
|
||||
return true;
|
||||
|
||||
switch (bus) {
|
||||
case VIR_DOMAIN_DISK_BUS_IDE:
|
||||
case VIR_DOMAIN_DISK_BUS_FDC:
|
||||
case VIR_DOMAIN_DISK_BUS_SCSI:
|
||||
case VIR_DOMAIN_DISK_BUS_SATA:
|
||||
return addressType == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_DRIVE;
|
||||
case VIR_DOMAIN_DISK_BUS_VIRTIO:
|
||||
case VIR_DOMAIN_DISK_BUS_XEN:
|
||||
case VIR_DOMAIN_DISK_BUS_USB:
|
||||
case VIR_DOMAIN_DISK_BUS_UML:
|
||||
case VIR_DOMAIN_DISK_BUS_SD:
|
||||
case VIR_DOMAIN_DISK_BUS_LAST:
|
||||
return true;
|
||||
}
|
||||
|
||||
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
||||
_("unexpected bus type '%d'"),
|
||||
bus);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
virSecurityDeviceLabelDefValidateXML(virSecurityDeviceLabelDefPtr *seclabels,
|
||||
size_t nseclabels,
|
||||
virSecurityLabelDefPtr *vmSeclabels,
|
||||
size_t nvmSeclabels)
|
||||
{
|
||||
virSecurityDeviceLabelDefPtr seclabel;
|
||||
size_t i;
|
||||
size_t j;
|
||||
|
||||
for (i = 0; i < nseclabels; i++) {
|
||||
seclabel = seclabels[i];
|
||||
|
||||
/* find the security label that it's being overridden */
|
||||
for (j = 0; j < nvmSeclabels; j++) {
|
||||
if (STRNEQ_NULLABLE(vmSeclabels[j]->model, seclabel->model))
|
||||
continue;
|
||||
|
||||
if (!vmSeclabels[j]->relabel) {
|
||||
virReportError(VIR_ERR_XML_ERROR, "%s",
|
||||
_("label overrides require relabeling to be "
|
||||
"enabled at the domain level"));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
virDomainDiskDefValidate(const virDomainDef *def,
|
||||
const virDomainDiskDef *disk)
|
||||
{
|
||||
virStorageSourcePtr next;
|
||||
|
||||
/* Validate LUN configuration */
|
||||
if (disk->device == VIR_DOMAIN_DISK_DEVICE_LUN) {
|
||||
/* volumes haven't been translated at this point, so accept them */
|
||||
if (!(disk->src->type == VIR_STORAGE_TYPE_BLOCK ||
|
||||
disk->src->type == VIR_STORAGE_TYPE_VOLUME ||
|
||||
(disk->src->type == VIR_STORAGE_TYPE_NETWORK &&
|
||||
disk->src->protocol == VIR_STORAGE_NET_PROTOCOL_ISCSI))) {
|
||||
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
||||
_("disk '%s' improperly configured for a "
|
||||
"device='lun'"), disk->dst);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (disk->src->pr &&
|
||||
disk->device != VIR_DOMAIN_DISK_DEVICE_LUN) {
|
||||
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
|
||||
_("<reservations/> allowed only for lun devices"));
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Reject disks with a bus type that is not compatible with the
|
||||
* given address type. The function considers only buses that are
|
||||
* handled in common code. For other bus types it's not possible
|
||||
* to decide compatibility in common code.
|
||||
*/
|
||||
if (!virDomainDiskAddressDiskBusCompatibility(disk->bus, disk->info.type)) {
|
||||
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
||||
_("Invalid address type '%s' for the disk '%s' with the bus type '%s'"),
|
||||
virDomainDeviceAddressTypeToString(disk->info.type),
|
||||
disk->dst,
|
||||
virDomainDiskBusTypeToString(disk->bus));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (disk->queues && disk->bus != VIR_DOMAIN_DISK_BUS_VIRTIO) {
|
||||
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
|
||||
_("queues attribute in disk driver element is only "
|
||||
"supported by virtio-blk"));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (disk->bus != VIR_DOMAIN_DISK_BUS_VIRTIO &&
|
||||
(disk->model == VIR_DOMAIN_DISK_MODEL_VIRTIO ||
|
||||
disk->model == VIR_DOMAIN_DISK_MODEL_VIRTIO_TRANSITIONAL ||
|
||||
disk->model == VIR_DOMAIN_DISK_MODEL_VIRTIO_NON_TRANSITIONAL)) {
|
||||
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
||||
_("disk model '%s' not supported for bus '%s'"),
|
||||
virDomainDiskModelTypeToString(disk->model),
|
||||
virDomainDiskBusTypeToString(disk->bus));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (disk->src->type == VIR_STORAGE_TYPE_NVME) {
|
||||
/* NVMe namespaces start from 1 */
|
||||
if (disk->src->nvme->namespc == 0) {
|
||||
virReportError(VIR_ERR_XML_ERROR, "%s",
|
||||
_("NVMe namespace can't be zero"));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
for (next = disk->src; next; next = next->backingStore) {
|
||||
if (virSecurityDeviceLabelDefValidateXML(next->seclabels,
|
||||
next->nseclabels,
|
||||
def->seclabels,
|
||||
def->nseclabels) < 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -28,3 +28,9 @@ int virDomainDefBootValidate(const virDomainDef *def);
|
|||
int virDomainDefVideoValidate(const virDomainDef *def);
|
||||
int virDomainVideoDefValidate(const virDomainVideoDef *video,
|
||||
const virDomainDef *def);
|
||||
int virSecurityDeviceLabelDefValidateXML(virSecurityDeviceLabelDefPtr *seclabels,
|
||||
size_t nseclabels,
|
||||
virSecurityLabelDefPtr *vmSeclabels,
|
||||
size_t nvmSeclabels);
|
||||
int virDomainDiskDefValidate(const virDomainDef *def,
|
||||
const virDomainDiskDef *disk);
|
||||
|
|
Loading…
Reference in New Issue