]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
qemu: hotplug: Extract legacy disk media changing bits
authorPeter Krempa <pkrempa@redhat.com>
Fri, 13 Jul 2018 15:44:32 +0000 (17:44 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Fri, 20 Jul 2018 12:23:09 +0000 (14:23 +0200)
Prepare for the -blockdev implementation of ejectable media changing by
splitting up the old bits.

Additionally since both callers make sure that the device is a cdrom or
floppy the check is no longer necessary.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/qemu/qemu_hotplug.c

index 5337cca2fe5553f750c2796f195a0e8091cf6b0e..d1e16fc9d07dbe3ad2910aa892b67118124c32ce 100644 (file)
@@ -192,7 +192,7 @@ qemuHotplugWaitForTrayEject(virQEMUDriverPtr driver,
 
 
 /**
- * qemuDomainChangeEjectableMedia:
+ * qemuDomainChangeMediaLegacy:
  * @driver: qemu driver structure
  * @vm: domain definition
  * @disk: disk definition to change the source of
@@ -206,12 +206,12 @@ qemuHotplugWaitForTrayEject(virQEMUDriverPtr driver,
  *
  * Returns 0 on success, -1 on error and reports libvirt error
  */
-int
-qemuDomainChangeEjectableMedia(virQEMUDriverPtr driver,
-                               virDomainObjPtr vm,
-                               virDomainDiskDefPtr disk,
-                               virStorageSourcePtr newsrc,
-                               bool force)
+static int
+qemuDomainChangeMediaLegacy(virQEMUDriverPtr driver,
+                            virDomainObjPtr vm,
+                            virDomainDiskDefPtr disk,
+                            virStorageSourcePtr newsrc,
+                            bool force)
 {
     int ret = -1, rc;
     char *driveAlias = NULL;
@@ -231,19 +231,8 @@ qemuDomainChangeEjectableMedia(virQEMUDriverPtr driver,
     if (srcPriv)
         secinfo = srcPriv->secinfo;
 
-    if (disk->device != VIR_DOMAIN_DISK_DEVICE_FLOPPY &&
-        disk->device != VIR_DOMAIN_DISK_DEVICE_CDROM) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("Removable media not supported for %s device"),
-                       virDomainDiskDeviceTypeToString(disk->device));
-        goto cleanup;
-    }
-
-    if (qemuHotplugPrepareDiskAccess(driver, vm, disk, newsrc, false) < 0)
-        goto cleanup;
-
     if (!(driveAlias = qemuAliasDiskDriveFromDisk(disk)))
-        goto error;
+        goto cleanup;
 
     qemuDomainObjEnterMonitor(driver, vm);
     rc = qemuMonitorEjectMedia(priv->mon, driveAlias, force);
@@ -255,16 +244,16 @@ qemuDomainChangeEjectableMedia(virQEMUDriverPtr driver,
         virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE_TRAY_MOVED)) {
         rc = qemuHotplugWaitForTrayEject(driver, vm, disk, driveAlias);
         if (rc < 0)
-            goto error;
+            goto cleanup;
     } else  {
         /* otherwise report possible errors from the attempt to eject the media*/
         if (rc < 0)
-            goto error;
+            goto cleanup;
     }
 
     if (!virStorageSourceIsEmpty(newsrc)) {
         if (qemuGetDriveSourceString(newsrc, secinfo, &sourcestr) < 0)
-            goto error;
+            goto cleanup;
 
         if (virStorageSourceGetActualType(newsrc) != VIR_STORAGE_TYPE_DIR) {
             if (newsrc->format > 0) {
@@ -283,29 +272,15 @@ qemuDomainChangeEjectableMedia(virQEMUDriverPtr driver,
             goto cleanup;
     }
 
-    virDomainAuditDisk(vm, disk->src, newsrc, "update", rc >= 0);
-
     if (rc < 0)
-        goto error;
-
-    /* remove the old source from shared device list */
-    ignore_value(qemuRemoveSharedDisk(driver, disk, vm->def->name));
-    ignore_value(qemuHotplugPrepareDiskAccess(driver, vm, disk, NULL, true));
+        goto cleanup;
 
-    virStorageSourceFree(disk->src);
-    disk->src = newsrc;
-    newsrc = NULL;
     ret = 0;
 
  cleanup:
     VIR_FREE(driveAlias);
     VIR_FREE(sourcestr);
     return ret;
-
- error:
-    virDomainAuditDisk(vm, disk->src, newsrc, "update", false);
-    ignore_value(qemuHotplugPrepareDiskAccess(driver, vm, disk, newsrc, true));
-    goto cleanup;
 }
 
 
@@ -592,6 +567,56 @@ qemuHotplugDiskSourceRemove(qemuMonitorPtr mon,
 }
 
 
+/**
+ * qemuDomainChangeEjectableMedia:
+ * @driver: qemu driver structure
+ * @vm: domain definition
+ * @disk: disk definition to change the source of
+ * @newsrc: new disk source to change to
+ * @force: force the change of media
+ *
+ * Change the media in an ejectable device to the one described by
+ * @newsrc. This function also removes the old source from the
+ * shared device table if appropriate. Note that newsrc is consumed
+ * on success and the old source is freed on success.
+ *
+ * Returns 0 on success, -1 on error and reports libvirt error
+ */
+int
+qemuDomainChangeEjectableMedia(virQEMUDriverPtr driver,
+                               virDomainObjPtr vm,
+                               virDomainDiskDefPtr disk,
+                               virStorageSourcePtr newsrc,
+                               bool force)
+{
+    int ret = -1;
+    int rc;
+
+    if (qemuHotplugPrepareDiskAccess(driver, vm, disk, newsrc, false) < 0)
+        goto cleanup;
+
+    rc = qemuDomainChangeMediaLegacy(driver, vm, disk, newsrc, force);
+
+    virDomainAuditDisk(vm, disk->src, newsrc, "update", rc >= 0);
+
+    if (rc < 0) {
+        ignore_value(qemuHotplugPrepareDiskAccess(driver, vm, disk, newsrc, true));
+        goto cleanup;
+    }
+
+    /* remove the old source from shared device list */
+    ignore_value(qemuRemoveSharedDisk(driver, disk, vm->def->name));
+    ignore_value(qemuHotplugPrepareDiskAccess(driver, vm, disk, NULL, true));
+
+    virStorageSourceFree(disk->src);
+    VIR_STEAL_PTR(disk->src, newsrc);
+    ret = 0;
+
+ cleanup:
+    return ret;
+}
+
+
 /**
  * qemuDomainAttachDiskGeneric:
  *