[libvirt] [PATCH v3 REBASE 2 11/12] qemu: handle detaching of unplugged hostdev

Nikolay Shirokovskiy nshirokovskiy at virtuozzo.com
Tue Oct 29 08:18:02 UTC 2019


If hostdev is unplugged we don't need to delete it's correspondent device
from qemu etc. Just remove it from config immediately.

Signed-off-by: Nikolay Shirokovskiy <nshirokovskiy at virtuozzo.com>
---
 src/qemu/qemu_hotplug.c | 16 +++++++++++++++-
 src/util/virhostdev.c   |  2 ++
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
index a2dd37f630..1e623f8763 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -4458,6 +4458,7 @@ qemuDomainRemoveHostDevice(virQEMUDriverPtr driver,
     g_autofree char *drivealias = NULL;
     g_autofree char *objAlias = NULL;
     bool unplug = hostdev->deleteAction == VIR_DOMAIN_HOSTDEV_DELETE_ACTION_UNPLUG;
+    bool unplugged = false;
 
     VIR_DEBUG("Removing host device %s from domain %p %s",
               hostdev->info->alias, vm, vm->def->name);
@@ -4511,13 +4512,17 @@ qemuDomainRemoveHostDevice(virQEMUDriverPtr driver,
 
     virDomainAuditHostdev(vm, hostdev, "detach", true);
 
+    if (hostdev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB) {
+        virDomainHostdevSubsysUSBPtr usbsrc = &hostdev->source.subsys.u.usb;
+        unplugged = !usbsrc->bus && !usbsrc->device;
+    }
     /*
      * In case of unplug the attempt to restore label will fail. But we don't
      * need to restore the label! In case of separate mount namespace for the
      * domain we remove device file later in this function. In case of global
      * mount namespace the device file is deleted or being deleted by systemd.
      */
-    if (!virHostdevIsVFIODevice(hostdev) && !unplug &&
+    if (!virHostdevIsVFIODevice(hostdev) && !unplug && !unplugged &&
         qemuSecurityRestoreHostdevLabel(driver, vm, hostdev) < 0)
         VIR_WARN("Failed to restore host device labelling");
 
@@ -5821,6 +5826,15 @@ qemuDomainDetachDeviceLive(virDomainObjPtr vm,
                                         unplug) < 0) {
             return -1;
         }
+        if (!unplug) {
+            virDomainHostdevDefPtr hostdev = detach.data.hostdev;
+            virDomainHostdevSubsysUSBPtr usbsrc = &hostdev->source.subsys.u.usb;
+
+            if (hostdev->startupPolicy != VIR_DOMAIN_STARTUP_POLICY_OPTIONAL &&
+                hostdev->startupPolicy != VIR_DOMAIN_STARTUP_POLICY_REQUISITE &&
+                usbsrc->device == 0 && usbsrc->bus == 0)
+                return qemuDomainRemoveDevice(driver, vm, &detach);
+        }
         break;
     case VIR_DOMAIN_DEVICE_RNG:
         if (qemuDomainDetachPrepRNG(vm, match->data.rng,
diff --git a/src/util/virhostdev.c b/src/util/virhostdev.c
index 010eb551a9..c28633921b 100644
--- a/src/util/virhostdev.c
+++ b/src/util/virhostdev.c
@@ -1807,6 +1807,8 @@ virHostdevReAttachUSBDevices(virHostdevManagerPtr mgr,
             continue;
         if (hostdev->missing)
             continue;
+        if (!usbsrc->bus && !usbsrc->device)
+            continue;
 
         if (!(usb = virUSBDeviceNew(usbsrc->bus, usbsrc->device, NULL))) {
             VIR_WARN("Unable to reattach USB device %03d.%03d on domain %s",
-- 
2.23.0




More information about the libvir-list mailing list