[PATCH v3 REBASE 2 03/12] qemu: support host usb device unplug

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Handle host usb device unplug in DEVICE_DELETED handle execution
path.

Signed-off-by: Nikolay Shirokovskiy <nshirokovskiy@xxxxxxxxxxxxx>
Reviewed-by: Daniel Henrique Barboza <danielhb413@xxxxxxxxx>
---
 src/qemu/qemu_hotplug.c | 38 +++++++++++++++++++++++++++++++-------
 1 file changed, 31 insertions(+), 7 deletions(-)

diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
index 5770ce3482..1a5b9aa0c2 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -4397,7 +4397,8 @@ qemuDomainRemoveUSBHostDevice(virQEMUDriverPtr driver,
                               virDomainHostdevDefPtr hostdev)
 {
     qemuHostdevReAttachUSBDevices(driver, vm->def->name, &hostdev, 1);
-    qemuDomainReleaseDeviceAddress(vm, hostdev->info);
+    if (hostdev->deleteAction != VIR_DOMAIN_HOSTDEV_DELETE_ACTION_UNPLUG)
+        qemuDomainReleaseDeviceAddress(vm, hostdev->info);
 }
 
 static void
@@ -4437,6 +4438,7 @@ qemuDomainRemoveHostDevice(virQEMUDriverPtr driver,
     qemuDomainObjPrivatePtr priv = vm->privateData;
     g_autofree char *drivealias = NULL;
     g_autofree char *objAlias = NULL;
+    bool unplug = hostdev->deleteAction == VIR_DOMAIN_HOSTDEV_DELETE_ACTION_UNPLUG;
 
     VIR_DEBUG("Removing host device %s from domain %p %s",
               hostdev->info->alias, vm, vm->def->name);
@@ -4479,16 +4481,24 @@ qemuDomainRemoveHostDevice(virQEMUDriverPtr driver,
         }
     }
 
-    for (i = 0; i < vm->def->nhostdevs; i++) {
-        if (vm->def->hostdevs[i] == hostdev) {
-            virDomainHostdevRemove(vm->def, i);
-            break;
+    if (!unplug) {
+        for (i = 0; i < vm->def->nhostdevs; i++) {
+            if (vm->def->hostdevs[i] == hostdev) {
+                virDomainHostdevRemove(vm->def, i);
+                break;
+            }
         }
     }
 
     virDomainAuditHostdev(vm, hostdev, "detach", true);
 
-    if (!virHostdevIsVFIODevice(hostdev) &&
+    /*
+     * 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 &&
         qemuSecurityRestoreHostdevLabel(driver, vm, hostdev) < 0)
         VIR_WARN("Failed to restore host device labelling");
 
@@ -4522,7 +4532,13 @@ qemuDomainRemoveHostDevice(virQEMUDriverPtr driver,
         break;
     }
 
-    virDomainHostdevDefFree(hostdev);
+    if (unplug) {
+        virDomainHostdevSubsysUSBPtr usbsrc = &hostdev->source.subsys.u.usb;
+        usbsrc->bus = 0;
+        usbsrc->device = 0;
+    } else {
+        virDomainHostdevDefFree(hostdev);
+    }
 
     if (net) {
         if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK) {
@@ -4537,6 +4553,8 @@ qemuDomainRemoveHostDevice(virQEMUDriverPtr driver,
         virDomainNetDefFree(net);
     }
 
+    hostdev->deleteAction = VIR_DOMAIN_HOSTDEV_DELETE_ACTION_NONE;
+
     return 0;
 }
 
@@ -4975,6 +4993,7 @@ qemuDomainRemoveDevice(virQEMUDriverPtr driver,
     virDomainDeviceInfoPtr info;
     virObjectEventPtr event;
     g_autofree char *alias = NULL;
+    bool unplug;
 
     /*
      * save the alias to use when sending a DEVICE_REMOVED event after
@@ -5013,8 +5032,13 @@ qemuDomainRemoveDevice(virQEMUDriverPtr driver,
             return -1;
         break;
     case VIR_DOMAIN_DEVICE_HOSTDEV:
+        unplug = dev->data.hostdev->deleteAction == VIR_DOMAIN_HOSTDEV_DELETE_ACTION_UNPLUG;
+
         if (qemuDomainRemoveHostDevice(driver, vm, dev->data.hostdev) < 0)
             return -1;
+
+        if (unplug)
+            return 0;
         break;
     case VIR_DOMAIN_DEVICE_RNG:
         if (qemuDomainRemoveRNGDevice(driver, vm, dev->data.rng) < 0)
-- 
2.23.0

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





[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux