xen: Prevent updating device when attaching a device

When attaching a device that already exists, xend driver updates
the device with "device_configure", it causes problems (e.g. for
disk device, 'device_configure' only can be used to update device
like CDROM), on the other hand, we provide additional API
(virDomainUpdateDevice) to update device, this fix is to raise up
errors instead of updating the existed device which is not CDROM
device.

Changes from v1 to v2:
  - allow to update CDROM

* src/xen/xend_internal.c
This commit is contained in:
Osier Yang 2011-02-12 14:29:09 +08:00
parent 699a5888b7
commit 0e629db3bf

View File

@ -3965,6 +3965,7 @@ xenDaemonAttachDeviceFlags(virDomainPtr domain, const char *xml,
virDomainDefPtr def = NULL; virDomainDefPtr def = NULL;
virBuffer buf = VIR_BUFFER_INITIALIZER; virBuffer buf = VIR_BUFFER_INITIALIZER;
char class[8], ref[80]; char class[8], ref[80];
char *target = NULL;
if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL)) { if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL)) {
virXendError(VIR_ERR_INVALID_ARG, __FUNCTION__); virXendError(VIR_ERR_INVALID_ARG, __FUNCTION__);
@ -4029,6 +4030,13 @@ xenDaemonAttachDeviceFlags(virDomainPtr domain, const char *xml,
STREQ(def->os.type, "hvm") ? 1 : 0, STREQ(def->os.type, "hvm") ? 1 : 0,
priv->xendConfigVersion, 1) < 0) priv->xendConfigVersion, 1) < 0)
goto cleanup; goto cleanup;
if (dev->data.disk->device != VIR_DOMAIN_DISK_DEVICE_CDROM) {
if (!(target = strdup(dev->data.disk->dst))) {
virReportOOMError();
goto cleanup;
}
}
break; break;
case VIR_DOMAIN_DEVICE_NET: case VIR_DOMAIN_DEVICE_NET:
@ -4038,6 +4046,14 @@ xenDaemonAttachDeviceFlags(virDomainPtr domain, const char *xml,
STREQ(def->os.type, "hvm") ? 1 : 0, STREQ(def->os.type, "hvm") ? 1 : 0,
priv->xendConfigVersion, 1) < 0) priv->xendConfigVersion, 1) < 0)
goto cleanup; goto cleanup;
char macStr[VIR_MAC_STRING_BUFLEN];
virFormatMacAddr(dev->data.net->mac, macStr);
if (!(target = strdup(macStr))) {
virReportOOMError();
goto cleanup;
}
break; break;
case VIR_DOMAIN_DEVICE_HOSTDEV: case VIR_DOMAIN_DEVICE_HOSTDEV:
@ -4046,6 +4062,17 @@ xenDaemonAttachDeviceFlags(virDomainPtr domain, const char *xml,
if (xenDaemonFormatSxprOnePCI(dev->data.hostdev, if (xenDaemonFormatSxprOnePCI(dev->data.hostdev,
&buf, 0) < 0) &buf, 0) < 0)
goto cleanup; goto cleanup;
virDomainDevicePCIAddress PCIAddr;
PCIAddr = dev->data.hostdev->source.subsys.u.pci;
virAsprintf(&target, "PCI device: %.4x:%.2x:%.2x", PCIAddr.domain,
PCIAddr.bus, PCIAddr.slot);
if (target == NULL) {
virReportOOMError();
goto cleanup;
}
} else { } else {
virXendError(VIR_ERR_NO_SUPPORT, "%s", virXendError(VIR_ERR_NO_SUPPORT, "%s",
_("unsupported device type")); _("unsupported device type"));
@ -4065,17 +4092,22 @@ xenDaemonAttachDeviceFlags(virDomainPtr domain, const char *xml,
/* device doesn't exist, define it */ /* device doesn't exist, define it */
ret = xend_op(domain->conn, domain->name, "op", "device_create", ret = xend_op(domain->conn, domain->name, "op", "device_create",
"config", sexpr, NULL); "config", sexpr, NULL);
} } else {
else { if (dev->data.disk->device != VIR_DOMAIN_DISK_DEVICE_CDROM) {
/* device exists, attempt to modify it */ virXendError(VIR_ERR_OPERATION_INVALID,
ret = xend_op(domain->conn, domain->name, "op", "device_configure", _("target '%s' already exists"), target);
"config", sexpr, "dev", ref, NULL); } else {
/* device exists, attempt to modify it */
ret = xend_op(domain->conn, domain->name, "op", "device_configure",
"config", sexpr, "dev", ref, NULL);
}
} }
cleanup: cleanup:
VIR_FREE(sexpr); VIR_FREE(sexpr);
virDomainDefFree(def); virDomainDefFree(def);
virDomainDeviceDefFree(dev); virDomainDeviceDefFree(dev);
VIR_FREE(target);
return ret; return ret;
} }