[libvirt PATCH v3 08/25] qemu_snapshot: introduce qemuSnapshotCreateQcow2Files

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

 



Extract creation of qcow2 files for external snapshots to separate
function as we will need it for external snapshot revert code.

Signed-off-by: Pavel Hrdina <phrdina@xxxxxxxxxx>
---
 src/qemu/qemu_snapshot.c | 85 ++++++++++++++++++++++++++--------------
 1 file changed, 56 insertions(+), 29 deletions(-)

diff --git a/src/qemu/qemu_snapshot.c b/src/qemu/qemu_snapshot.c
index 8e1eb21b5d..f43548e29f 100644
--- a/src/qemu/qemu_snapshot.c
+++ b/src/qemu/qemu_snapshot.c
@@ -181,43 +181,37 @@ qemuSnapshotDomainDefUpdateDisk(virDomainDef *domdef,
 }
 
 
-/* The domain is expected to be locked and inactive. */
+/**
+ * qemuSnapshotCreateQcow2Files:
+ * @vm: domain object
+ * @snapdef: snapshot definition
+ * @created: bitmap to store which disks were created
+ *
+ * Create new qcow2 images based on snapshot definition @snapdef and use
+ * domain object @vm as source for backing images.
+ *
+ * Returns 0 on success, -1 on error.
+ */
 static int
-qemuSnapshotCreateInactiveInternal(virQEMUDriver *driver,
-                                   virDomainObj *vm,
-                                   virDomainMomentObj *snap)
-{
-    return qemuDomainSnapshotForEachQcow2(driver, vm->def, snap, "-c", false);
-}
-
-
-/* The domain is expected to be locked and inactive. */
-static int
-qemuSnapshotCreateInactiveExternal(virQEMUDriver *driver,
-                                   virDomainObj *vm,
-                                   virDomainMomentObj *snap,
-                                   bool reuse)
+qemuSnapshotCreateQcow2Files(virDomainObj *vm,
+                             virDomainSnapshotDef *snapdef,
+                             virBitmap *created)
 {
     size_t i;
-    virDomainSnapshotDiskDef *snapdisk;
-    virDomainDiskDef *defdisk;
     const char *qemuImgPath;
-    g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
-    int ret = -1;
     g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
-    virDomainSnapshotDef *snapdef = virDomainSnapshotObjGetDef(snap);
-    g_autoptr(virBitmap) created = virBitmapNew(snapdef->ndisks);
+    virQEMUDriver *driver = ((qemuDomainObjPrivate *) vm->privateData)->driver;
+    virDomainSnapshotDiskDef *snapdisk = NULL;
+    virDomainDiskDef *defdisk = NULL;
 
     if (!(qemuImgPath = qemuFindQemuImgBinary(driver)))
-        goto cleanup;
+        return -1;
 
-    /* If reuse is true, then qemuSnapshotPrepare already
-     * ensured that the new files exist, and it was up to the user to
-     * create them correctly.  */
-    for (i = 0; i < snapdef->ndisks && !reuse; i++) {
+    for (i = 0; i < snapdef->ndisks; i++) {
         g_autoptr(virCommand) cmd = NULL;
         snapdisk = &(snapdef->disks[i]);
         defdisk = vm->def->disks[i];
+
         if (snapdisk->snapshot != VIR_DOMAIN_SNAPSHOT_LOCATION_EXTERNAL)
             continue;
 
@@ -225,7 +219,7 @@ qemuSnapshotCreateInactiveExternal(virQEMUDriver *driver,
             snapdisk->src->format = VIR_STORAGE_FILE_QCOW2;
 
         if (qemuDomainStorageSourceValidateDepth(defdisk->src, 1, defdisk->dst) < 0)
-            goto cleanup;
+            return -1;
 
         /* creates cmd line args: qemu-img create -f qcow2 -o */
         if (!(cmd = virCommandNewArgList(qemuImgPath,
@@ -234,7 +228,7 @@ qemuSnapshotCreateInactiveExternal(virQEMUDriver *driver,
                                          virStorageFileFormatTypeToString(snapdisk->src->format),
                                          "-o",
                                          NULL)))
-            goto cleanup;
+            return -1;
 
         /* adds cmd line arg: backing_fmt=format,backing_file=/path/to/backing/file */
         virBufferAsprintf(&buf, "backing_fmt=%s,backing_file=",
@@ -251,9 +245,42 @@ qemuSnapshotCreateInactiveExternal(virQEMUDriver *driver,
             ignore_value(virBitmapSetBit(created, i));
 
         if (virCommandRun(cmd, NULL) < 0)
-            goto cleanup;
+            return -1;
     }
 
+    return 0;
+}
+
+
+/* The domain is expected to be locked and inactive. */
+static int
+qemuSnapshotCreateInactiveInternal(virQEMUDriver *driver,
+                                   virDomainObj *vm,
+                                   virDomainMomentObj *snap)
+{
+    return qemuDomainSnapshotForEachQcow2(driver, vm->def, snap, "-c", false);
+}
+
+
+/* The domain is expected to be locked and inactive. */
+static int
+qemuSnapshotCreateInactiveExternal(virQEMUDriver *driver,
+                                   virDomainObj *vm,
+                                   virDomainMomentObj *snap,
+                                   bool reuse)
+{
+    virDomainSnapshotDiskDef *snapdisk;
+    g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
+    int ret = -1;
+    virDomainSnapshotDef *snapdef = virDomainSnapshotObjGetDef(snap);
+    g_autoptr(virBitmap) created = virBitmapNew(snapdef->ndisks);
+
+    /* If reuse is true, then qemuSnapshotPrepare already
+     * ensured that the new files exist, and it was up to the user to
+     * create them correctly.  */
+    if (!reuse && qemuSnapshotCreateQcow2Files(vm, snapdef, created) < 0)
+        goto cleanup;
+
     /* update disk definitions */
     if (qemuSnapshotDomainDefUpdateDisk(vm->def, snapdef, reuse) < 0)
         goto cleanup;
-- 
2.41.0




[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