d76c62
From 416b12099e379ec4c19e55a0414a448d140f3dc1 Mon Sep 17 00:00:00 2001
d76c62
Message-Id: <416b12099e379ec4c19e55a0414a448d140f3dc1@dist-git>
d76c62
From: Peter Krempa <pkrempa@redhat.com>
d76c62
Date: Mon, 16 Mar 2020 22:12:16 +0100
d76c62
Subject: [PATCH] qemu: block: implement helpers for blockdev-reopen
d76c62
MIME-Version: 1.0
d76c62
Content-Type: text/plain; charset=UTF-8
d76c62
Content-Transfer-Encoding: 8bit
d76c62
d76c62
Introduce a set of helpers to call blockdev-reopen in certain scenarios
d76c62
d76c62
Libvirt will use the QMP command to turn certain members of the backing
d76c62
chain read-write for bitmap manipulation and we'll also want to use it
d76c62
to replace/install the backing chain of a qcow2 format node.
d76c62
d76c62
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
d76c62
Reviewed-by: Eric Blake <eblake@redhat.com>
d76c62
(cherry picked from commit 96063ce280a398493d7a78a35f674ed25078d849)
d76c62
https://bugzilla.redhat.com/show_bug.cgi?id=1799013
d76c62
Message-Id: <f7b3a9cc523a22b9f1678483b6b8bf302ae7e1cc.1584391727.git.pkrempa@redhat.com>
d76c62
Reviewed-by: Ján Tomko <jtomko@redhat.com>
d76c62
---
d76c62
 src/qemu/qemu_block.c | 101 ++++++++++++++++++++++++++++++++++++++++++
d76c62
 src/qemu/qemu_block.h |   9 ++++
d76c62
 2 files changed, 110 insertions(+)
d76c62
d76c62
diff --git a/src/qemu/qemu_block.c b/src/qemu/qemu_block.c
d76c62
index 141059ae81..b4da323610 100644
d76c62
--- a/src/qemu/qemu_block.c
d76c62
+++ b/src/qemu/qemu_block.c
d76c62
@@ -2986,3 +2986,104 @@ qemuBlockBitmapsHandleBlockcopy(virStorageSourcePtr src,
d76c62
 
d76c62
     return 0;
d76c62
 }
d76c62
+
d76c62
+
d76c62
+/**
d76c62
+ * qemuBlockReopenFormat:
d76c62
+ * @vm: domain object
d76c62
+ * @src: storage source to reopen
d76c62
+ * @asyncJob: qemu async job type
d76c62
+ *
d76c62
+ * Invokes the 'blockdev-reopen' command on the format layer of @src. This means
d76c62
+ * that @src must be already properly configured for the desired outcome. The
d76c62
+ * nodenames of @src are used to identify the specific image in qemu.
d76c62
+ */
d76c62
+static int
d76c62
+qemuBlockReopenFormat(virDomainObjPtr vm,
d76c62
+                      virStorageSourcePtr src,
d76c62
+                      qemuDomainAsyncJob asyncJob)
d76c62
+{
d76c62
+    qemuDomainObjPrivatePtr priv = vm->privateData;
d76c62
+    virQEMUDriverPtr driver = priv->driver;
d76c62
+    g_autoptr(virJSONValue) reopenprops = NULL;
d76c62
+    int rc;
d76c62
+
d76c62
+    /* If we are lacking the object here, qemu might have opened an image with
d76c62
+     * a node name unknown to us */
d76c62
+    if (!src->backingStore) {
d76c62
+        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
d76c62
+                       _("can't reopen image with unknown presence of backing store"));
d76c62
+        return -1;
d76c62
+    }
d76c62
+
d76c62
+    if (!(reopenprops = qemuBlockStorageSourceGetBlockdevProps(src, src->backingStore)))
d76c62
+        return -1;
d76c62
+
d76c62
+    if (qemuDomainObjEnterMonitorAsync(driver, vm, asyncJob) < 0)
d76c62
+        return -1;
d76c62
+
d76c62
+    rc = qemuMonitorBlockdevReopen(priv->mon, &reopenprops);
d76c62
+
d76c62
+    if (qemuDomainObjExitMonitor(driver, vm) < 0 || rc < 0)
d76c62
+        return -1;
d76c62
+
d76c62
+    return 0;
d76c62
+}
d76c62
+
d76c62
+
d76c62
+/**
d76c62
+ * qemuBlockReopenReadWrite:
d76c62
+ * @vm: domain object
d76c62
+ * @src: storage source to reopen
d76c62
+ * @asyncJob: qemu async job type
d76c62
+ *
d76c62
+ * Wrapper that reopens @src read-write. We currently depend on qemu
d76c62
+ * reopening the storage with 'auto-read-only' enabled for us.
d76c62
+ * After successful reopen @src's 'readonly' flag is modified. Does nothing
d76c62
+ * if @src is already read-write.
d76c62
+ */
d76c62
+int
d76c62
+qemuBlockReopenReadWrite(virDomainObjPtr vm,
d76c62
+                         virStorageSourcePtr src,
d76c62
+                         qemuDomainAsyncJob asyncJob)
d76c62
+{
d76c62
+    if (!src->readonly)
d76c62
+        return 0;
d76c62
+
d76c62
+    src->readonly = false;
d76c62
+    if (qemuBlockReopenFormat(vm, src, asyncJob) < 0) {
d76c62
+        src->readonly = true;
d76c62
+        return -1;
d76c62
+    }
d76c62
+
d76c62
+    return 0;
d76c62
+}
d76c62
+
d76c62
+
d76c62
+/**
d76c62
+ * qemuBlockReopenReadOnly:
d76c62
+ * @vm: domain object
d76c62
+ * @src: storage source to reopen
d76c62
+ * @asyncJob: qemu async job type
d76c62
+ *
d76c62
+ * Wrapper that reopens @src read-only. We currently depend on qemu
d76c62
+ * reopening the storage with 'auto-read-only' enabled for us.
d76c62
+ * After successful reopen @src's 'readonly' flag is modified. Does nothing
d76c62
+ * if @src is already read-only.
d76c62
+ */
d76c62
+int
d76c62
+qemuBlockReopenReadOnly(virDomainObjPtr vm,
d76c62
+                         virStorageSourcePtr src,
d76c62
+                         qemuDomainAsyncJob asyncJob)
d76c62
+{
d76c62
+    if (src->readonly)
d76c62
+        return 0;
d76c62
+
d76c62
+    src->readonly = true;
d76c62
+    if (qemuBlockReopenFormat(vm, src, asyncJob) < 0) {
d76c62
+        src->readonly = false;
d76c62
+        return -1;
d76c62
+    }
d76c62
+
d76c62
+    return 0;
d76c62
+}
d76c62
diff --git a/src/qemu/qemu_block.h b/src/qemu/qemu_block.h
d76c62
index 197f5dae97..e012052352 100644
d76c62
--- a/src/qemu/qemu_block.h
d76c62
+++ b/src/qemu/qemu_block.h
d76c62
@@ -231,3 +231,12 @@ qemuBlockBitmapsHandleBlockcopy(virStorageSourcePtr src,
d76c62
                                 virHashTablePtr blockNamedNodeData,
d76c62
                                 bool shallow,
d76c62
                                 virJSONValuePtr *actions);
d76c62
+
d76c62
+int
d76c62
+qemuBlockReopenReadWrite(virDomainObjPtr vm,
d76c62
+                         virStorageSourcePtr src,
d76c62
+                         qemuDomainAsyncJob asyncJob);
d76c62
+int
d76c62
+qemuBlockReopenReadOnly(virDomainObjPtr vm,
d76c62
+                        virStorageSourcePtr src,
d76c62
+                        qemuDomainAsyncJob asyncJob);
d76c62
-- 
d76c62
2.25.1
d76c62