[libvirt PATCH 05/20] qemu_snapshot: introduce qemuSnapshotDomainDefUpdateDisk

Pavel Hrdina posted 20 patches 1 year, 4 months ago
There is a newer version of this series
[libvirt PATCH 05/20] qemu_snapshot: introduce qemuSnapshotDomainDefUpdateDisk
Posted by Pavel Hrdina 1 year, 4 months ago
Extract the code that updates disks in domain definition while creating
external snapshots. We will use it later in the external snapshot revert
code.

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
---
 src/qemu/qemu_snapshot.c | 63 ++++++++++++++++++++++++----------------
 1 file changed, 38 insertions(+), 25 deletions(-)

diff --git a/src/qemu/qemu_snapshot.c b/src/qemu/qemu_snapshot.c
index 7aa4195f04..924731fdf1 100644
--- a/src/qemu/qemu_snapshot.c
+++ b/src/qemu/qemu_snapshot.c
@@ -142,6 +142,42 @@ qemuSnapshotFSThaw(virDomainObj *vm,
 }
 
 
+static int
+qemuSnapshotDomainDefUpdateDisk(virDomainDef *domdef,
+                                virDomainSnapshotDef *snapdef,
+                                bool reuse)
+{
+    size_t i;
+
+    for (i = 0; i < snapdef->ndisks; i++) {
+        g_autoptr(virStorageSource) newsrc = NULL;
+        virDomainSnapshotDiskDef *snapdisk = &(snapdef->disks[i]);
+        virDomainDiskDef *defdisk = domdef->disks[i];
+
+        if (snapdisk->snapshot != VIR_DOMAIN_SNAPSHOT_LOCATION_EXTERNAL)
+            continue;
+
+        if (!(newsrc = virStorageSourceCopy(snapdisk->src, false)))
+            return -1;
+
+        if (virStorageSourceInitChainElement(newsrc, defdisk->src, false) < 0)
+            return -1;
+
+        if (!reuse &&
+            virStorageSourceHasBacking(defdisk->src)) {
+            defdisk->src->readonly = true;
+            newsrc->backingStore = g_steal_pointer(&defdisk->src);
+        } else {
+            virObjectUnref(defdisk->src);
+        }
+
+        defdisk->src = g_steal_pointer(&newsrc);
+    }
+
+    return 0;
+}
+
+
 /* The domain is expected to be locked and inactive. */
 static int
 qemuSnapshotCreateInactiveInternal(virQEMUDriver *driver,
@@ -216,31 +252,8 @@ qemuSnapshotCreateInactiveExternal(virQEMUDriver *driver,
     }
 
     /* update disk definitions */
-    for (i = 0; i < snapdef->ndisks; i++) {
-        g_autoptr(virStorageSource) newsrc = NULL;
-
-        snapdisk = &(snapdef->disks[i]);
-        defdisk = vm->def->disks[i];
-
-        if (snapdisk->snapshot != VIR_DOMAIN_SNAPSHOT_LOCATION_EXTERNAL)
-            continue;
-
-        if (!(newsrc = virStorageSourceCopy(snapdisk->src, false)))
-            goto cleanup;
-
-        if (virStorageSourceInitChainElement(newsrc, defdisk->src, false) < 0)
-            goto cleanup;
-
-        if (!reuse &&
-            virStorageSourceHasBacking(defdisk->src)) {
-            defdisk->src->readonly = true;
-            newsrc->backingStore = g_steal_pointer(&defdisk->src);
-        } else {
-            virObjectUnref(defdisk->src);
-        }
-
-        defdisk->src = g_steal_pointer(&newsrc);
-    }
+    if (qemuSnapshotDomainDefUpdateDisk(vm->def, snapdef, reuse) < 0)
+        goto cleanup;
 
     if (virDomainDefSave(vm->def, driver->xmlopt, cfg->configDir) < 0)
         goto cleanup;
-- 
2.39.2
Re: [libvirt PATCH 05/20] qemu_snapshot: introduce qemuSnapshotDomainDefUpdateDisk
Posted by Peter Krempa 1 year, 3 months ago
On Mon, Mar 13, 2023 at 16:42:06 +0100, Pavel Hrdina wrote:
> Extract the code that updates disks in domain definition while creating
> external snapshots. We will use it later in the external snapshot revert
> code.
> 
> Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
> ---
>  src/qemu/qemu_snapshot.c | 63 ++++++++++++++++++++++++----------------
>  1 file changed, 38 insertions(+), 25 deletions(-)

Reviewed-by: Peter Krempa <pkrempa@redhat.com>