USB disks, redirected devices, host devices and serial devices
are supported.
---
src/conf/domain_addr.c | 28 ++++++++++++++++++++++
src/conf/domain_addr.h | 4 ++++
src/libvirt_private.syms | 1 +
src/qemu/qemu_domain_address.c | 5 ++++
src/qemu/qemu_hotplug.c | 27 +++++++++++++++++++++
.../qemuhotplug-hotplug-base-live+disk-usb.xml | 1 +
6 files changed, 66 insertions(+)
diff --git a/src/conf/domain_addr.c b/src/conf/domain_addr.c
index b0cbcfc..b500ddc 100644
--- a/src/conf/domain_addr.c
+++ b/src/conf/domain_addr.c
@@ -1704,3 +1704,31 @@ virDomainUSBAddressEnsure(virDomainUSBAddressSetPtr addrs,
return 0;
}
+
+
+int
+virDomainUSBAddressRelease(virDomainUSBAddressSetPtr addrs,
+ virDomainDeviceInfoPtr info)
+{
+ virDomainUSBAddressHubPtr targetHub = NULL;
+ char *portstr = NULL;
+ int targetPort;
+ int ret = -1;
+
+ if (info->type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_USB)
+ return 0;
+
+ portstr = virDomainUSBAddressPortFormat(info->addr.usb.port);
+ VIR_DEBUG("Releasing USB addr bus=%u port=%s", info->addr.usb.bus,
portstr);
+
+ if (!(targetHub = virDomainUSBAddressFindPort(addrs, info, &targetPort)))
+ goto cleanup;
+
+ ignore_value(virBitmapClearBit(targetHub->ports, targetPort));
+
+ ret = 0;
+
+ cleanup:
+ VIR_FREE(portstr);
+ return ret;
+}
diff --git a/src/conf/domain_addr.h b/src/conf/domain_addr.h
index 331b597..c9ea287 100644
--- a/src/conf/domain_addr.h
+++ b/src/conf/domain_addr.h
@@ -288,4 +288,8 @@ int
virDomainUSBAddressEnsure(virDomainUSBAddressSetPtr addrs,
virDomainDeviceInfoPtr info)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
+int
+virDomainUSBAddressRelease(virDomainUSBAddressSetPtr addrs,
+ virDomainDeviceInfoPtr info)
+ ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
#endif /* __DOMAIN_ADDR_H__ */
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index a58a643..b5dca75 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -114,6 +114,7 @@ virDomainUSBAddressCountAvailablePorts;
virDomainUSBAddressEnsure;
virDomainUSBAddressPortFormat;
virDomainUSBAddressPortFormatBuf;
+virDomainUSBAddressRelease;
virDomainUSBAddressReserve;
virDomainUSBAddressSetAddControllers;
virDomainUSBAddressSetCreate;
diff --git a/src/qemu/qemu_domain_address.c b/src/qemu/qemu_domain_address.c
index ea96a33..9590e29 100644
--- a/src/qemu/qemu_domain_address.c
+++ b/src/qemu/qemu_domain_address.c
@@ -1786,4 +1786,9 @@ qemuDomainReleaseDeviceAddress(virDomainObjPtr vm,
virDomainVirtioSerialAddrRelease(priv->vioserialaddrs, info) < 0)
VIR_WARN("Unable to release virtio-serial address on %s",
NULLSTR(devstr));
+ if (info->type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_USB &&
+ priv->usbaddrs &&
+ virDomainUSBAddressRelease(priv->usbaddrs, info) < 0)
+ VIR_WARN("Unable to release usb address on %s",
+ NULLSTR(devstr));
}
diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
index 8f2795d..593d8e5 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -627,6 +627,13 @@ qemuDomainAttachUSBMassStorageDevice(virQEMUDriverPtr driver,
char *devstr = NULL;
virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
const char *src = virDomainDiskGetSource(disk);
+ bool releaseaddr;
+
+ if (priv->usbaddrs) {
+ if (virDomainUSBAddressEnsure(priv->usbaddrs, &disk->info) < 0)
+ goto cleanup;
+ releaseaddr = true;
+ }
if (qemuDomainPrepareDisk(driver, vm, disk, NULL, false) < 0)
goto cleanup;
@@ -672,6 +679,8 @@ qemuDomainAttachUSBMassStorageDevice(virQEMUDriverPtr driver,
virDomainDiskInsertPreAlloced(vm->def, disk);
cleanup:
+ if (ret < 0 && releaseaddr)
+ virDomainUSBAddressRelease(priv->usbaddrs, &disk->info);
VIR_FREE(devstr);
VIR_FREE(drivestr);
virObjectUnref(cfg);
@@ -1473,6 +1482,12 @@ qemuDomainAttachChrDeviceAssignAddr(qemuDomainObjPrivatePtr priv,
return -1;
return 1;
+ } else if (chr->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL &&
+ chr->targetType == VIR_DOMAIN_CHR_SERIAL_TARGET_TYPE_USB) {
+ if (virDomainUSBAddressEnsure(priv->usbaddrs, &chr->info) < 0)
+ return -1;
+ return 1;
+
} else if (chr->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_CHANNEL &&
chr->targetType == VIR_DOMAIN_CHR_CHANNEL_TARGET_TYPE_VIRTIO) {
if (virDomainVirtioSerialAddrAutoAssign(NULL, priv->vioserialaddrs,
@@ -1791,11 +1806,18 @@ qemuDomainAttachHostUSBDevice(virQEMUDriverPtr driver,
{
qemuDomainObjPrivatePtr priv = vm->privateData;
char *devstr = NULL;
+ bool releaseaddr = false;
bool added = false;
bool teardowncgroup = false;
bool teardownlabel = false;
int ret = -1;
+ if (priv->usbaddrs) {
+ if (virDomainUSBAddressEnsure(priv->usbaddrs, hostdev->info) < 0)
+ goto cleanup;
+ releaseaddr = true;
+ }
+
if (qemuHostdevPrepareUSBDevices(driver, vm->def->name, &hostdev, 1, 0)
< 0)
goto cleanup;
@@ -1841,6 +1863,8 @@ qemuDomainAttachHostUSBDevice(virQEMUDriverPtr driver,
VIR_WARN("Unable to restore host device labelling on hotplug
fail");
if (added)
qemuHostdevReAttachUSBDevices(driver, vm->def->name, &hostdev, 1);
+ if (releaseaddr)
+ virDomainUSBAddressRelease(priv->usbaddrs, hostdev->info);
}
VIR_FREE(devstr);
return ret;
@@ -2834,6 +2858,8 @@ qemuDomainRemoveDiskDevice(virQEMUDriverPtr driver,
dev.type = VIR_DOMAIN_DEVICE_DISK;
dev.data.disk = disk;
ignore_value(qemuRemoveSharedDevice(driver, &dev, vm->def->name));
+ if (priv->usbaddrs)
+ virDomainUSBAddressRelease(priv->usbaddrs, &disk->info);
virDomainDiskDefFree(disk);
return 0;
@@ -2930,6 +2956,7 @@ qemuDomainRemoveUSBHostDevice(virQEMUDriverPtr driver,
virDomainHostdevDefPtr hostdev)
{
qemuHostdevReAttachUSBDevices(driver, vm->def->name, &hostdev, 1);
+ qemuDomainReleaseDeviceAddress(vm, hostdev->info, NULL);
}
static void
diff --git a/tests/qemuhotplugtestdata/qemuhotplug-hotplug-base-live+disk-usb.xml
b/tests/qemuhotplugtestdata/qemuhotplug-hotplug-base-live+disk-usb.xml
index 41039a4..cd686e6 100644
--- a/tests/qemuhotplugtestdata/qemuhotplug-hotplug-base-live+disk-usb.xml
+++ b/tests/qemuhotplugtestdata/qemuhotplug-hotplug-base-live+disk-usb.xml
@@ -27,6 +27,7 @@
<readonly/>
<shareable/>
<alias name='usb-disk16'/>
+ <address type='usb' bus='0' port='1'/>
</disk>
<controller type='usb' index='0'>
<alias name='usb'/>
--
2.7.3