[libvirt] [PATCH 6/6] qemu: handle hostdev add/del when libvirtd is down

Nikolay Shirokovskiy posted 6 patches 6 years, 5 months ago
There is a newer version of this series
[libvirt] [PATCH 6/6] qemu: handle hostdev add/del when libvirtd is down
Posted by Nikolay Shirokovskiy 6 years, 5 months ago
Somebody can easily detach hostdev on node while libvirtd is being
stopped. Also hostdev can be attached or detached and attached back and
so forth. Let's handle such cases.

Signed-off-by: Nikolay Shirokovskiy <nshirokovskiy@virtuozzo.com>
---
 src/qemu/qemu_process.c | 49 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)

diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index 5cb97de4f3..f70f0ae251 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -3747,6 +3747,52 @@ qemuProcessUpdateDevices(virQEMUDriverPtr driver,
     return ret;
 }
 
+
+static int
+qemuProcessReattachUSBDevices(virQEMUDriverPtr driver,
+                              virDomainObjPtr vm)
+{
+    size_t i;
+
+    for (i = 0; i < vm->def->nhostdevs; i++) {
+        virDomainDeviceDef dev = { .type = VIR_DOMAIN_DEVICE_HOSTDEV };
+        virDomainHostdevDefPtr hostdev = vm->def->hostdevs[i];
+        virDomainHostdevSubsysUSBPtr usbsrc = &hostdev->source.subsys.u.usb;
+        bool found;
+
+
+        if (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB)
+            continue;
+
+        if (hostdev->missing) {
+            int num;
+
+            if ((num = virUSBDeviceFindByVendor(usbsrc->vendor, usbsrc->product,
+                                                NULL, false, NULL)) < 0)
+                return -1;
+
+            found = num != 0;
+        } else {
+            virUSBDevicePtr usb;
+
+            if (virUSBDeviceFindByBus(usbsrc->bus, usbsrc->device,
+                                      NULL, false, &usb) < 0)
+                return -1;
+
+            found = usb != NULL;
+            virUSBDeviceFree(usb);
+        }
+
+        dev.data.hostdev = hostdev;
+        if ((hostdev->missing ^ !found) &&
+            qemuDomainDetachDeviceLive(vm, &dev, driver, true, true) < 0)
+            return -1;
+    }
+
+    return 0;
+}
+
+
 static int
 qemuDomainPerfRestart(virDomainObjPtr vm)
 {
@@ -8204,6 +8250,9 @@ qemuProcessReconnect(void *opaque)
     if (qemuProcessUpdateDevices(driver, obj) < 0)
         goto error;
 
+    if (qemuProcessReattachUSBDevices(driver, obj) < 0)
+        goto error;
+
     if (qemuRefreshPRManagerState(driver, obj) < 0)
         goto error;
 
-- 
2.23.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list