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