Introduce a set of helpers to call blockdev-reopen in certain scenarios
Libvirt will use the QMP command to turn certain members of the backing
chain read-write for bitmap manipulation and we'll also want to use it
to replace/install the backing chain of a qcow2 format node.
Signed-off-by: Peter Krempa <pkrempa(a)redhat.com>
---
src/qemu/qemu_block.c | 121 ++++++++++++++++++++++++++++++++++++++++++
src/qemu/qemu_block.h | 14 +++++
2 files changed, 135 insertions(+)
diff --git a/src/qemu/qemu_block.c b/src/qemu/qemu_block.c
index 152c73f1bf..441b8ec07b 100644
--- a/src/qemu/qemu_block.c
+++ b/src/qemu/qemu_block.c
@@ -2960,3 +2960,124 @@ qemuBlockBitmapsHandleBlockcopy(virStorageSourcePtr src,
return 0;
}
+
+
+/**
+ * qemuBlockReopen:
+ * @vm: domain object
+ * @props: JSON object used as argument of 'blockdev-reopen'
+ * @asyncJob: qemu async job type
+ *
+ * Monitor interaction to call 'blockdev-reopen' @props is consumed on
successful
+ * call and set to NULL if consumed.
+ */
+static int
+qemuBlockReopen(virDomainObjPtr vm,
+ virJSONValuePtr *props,
+ qemuDomainAsyncJob asyncJob)
+{
+ qemuDomainObjPrivatePtr priv = vm->privateData;
+ virQEMUDriverPtr driver = priv->driver;
+ int rc;
+
+ if (qemuDomainObjEnterMonitorAsync(driver, vm, asyncJob) < 0)
+ return -1;
+
+ rc = qemuMonitorBlockdevReopen(priv->mon, props);
+
+ if (qemuDomainObjExitMonitor(driver, vm) < 0 || rc < 0)
+ return -1;
+
+ return 0;
+}
+
+
+/**
+ * qemuBlockReopenFormat:
+ * @vm: domain object
+ * @src: storage source to reopen
+ * @asyncJob: qemu async job type
+ *
+ * Invokes the 'blockdev-reopen' command on the format layer of @src. This means
+ * that @src must be already properly configured for the desired outcome. The
+ * nodenames of @src are used to identify the specific image in qemu. This
+ * function enters the monitor.
+ */
+int
+qemuBlockReopenFormat(virDomainObjPtr vm,
+ virStorageSourcePtr src,
+ qemuDomainAsyncJob asyncJob)
+{
+ g_autoptr(virJSONValue) reopenprops = NULL;
+
+ /* If we are lacking the object here, qemu might have opened an image with
+ * a node name unknown to us */
+ if (!src->backingStore) {
+ virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
+ _("can't reopen image with unknown presence of backing
store"));
+ return -1;
+ }
+
+ if (!(reopenprops = qemuBlockStorageSourceGetBlockdevProps(src,
src->backingStore)))
+ return -1;
+
+ return qemuBlockReopen(vm, &reopenprops, asyncJob);
+}
+
+
+/**
+ * qemuBlockReopenReadWrite:
+ * @vm: domain object
+ * @src: storage source to reopen
+ * @asyncJob: qemu async job type
+ *
+ * Wrapper that reopens @src read-write. We currently depend on qemu
+ * reopening the storage with 'auto-read-only' enabled for us.
+ * After successful reopen @src's 'readonly' flag is modified. Does nothing
+ * if @src is already read-write.
+ */
+int
+qemuBlockReopenReadWrite(virDomainObjPtr vm,
+ virStorageSourcePtr src,
+ qemuDomainAsyncJob asyncJob)
+{
+ if (!src->readonly)
+ return 0;
+
+ src->readonly = false;
+ if (qemuBlockReopenFormat(vm, src, asyncJob) < 0) {
+ src->readonly = true;
+ return -1;
+ }
+
+ return 0;
+}
+
+
+/**
+ * qemuBlockReopenReadOnly:
+ * @vm: domain object
+ * @src: storage source to reopen
+ * @asyncJob: qemu async job type
+ *
+ * Wrapper that reopens @src read-only. We currently depend on qemu
+ * reopening the storage with 'auto-read-only' enabled for us.
+ * After successful reopen @src's 'readonly' flag is modified. Does nothing
+ * if @src is already read-only.
+ */
+int
+qemuBlockReopenReadOnly(virDomainObjPtr vm,
+ virStorageSourcePtr src,
+ qemuDomainAsyncJob asyncJob)
+{
+ if (src->readonly)
+ return 0;
+
+ src->readonly = true;
+ if (qemuBlockReopenFormat(vm, src, asyncJob) < 0) {
+ src->readonly = false;
+ return -1;
+ }
+
+ return 0;
+}
diff --git a/src/qemu/qemu_block.h b/src/qemu/qemu_block.h
index eab0128d5d..b3d7d0f876 100644
--- a/src/qemu/qemu_block.h
+++ b/src/qemu/qemu_block.h
@@ -228,3 +228,17 @@ qemuBlockBitmapsHandleBlockcopy(virStorageSourcePtr src,
virHashTablePtr blockNamedNodeData,
bool shallow,
virJSONValuePtr *actions);
+
+int
+qemuBlockReopenFormat(virDomainObjPtr vm,
+ virStorageSourcePtr src,
+ qemuDomainAsyncJob asyncJob);
+
+int
+qemuBlockReopenReadWrite(virDomainObjPtr vm,
+ virStorageSourcePtr src,
+ qemuDomainAsyncJob asyncJob);
+int
+qemuBlockReopenReadOnly(virDomainObjPtr vm,
+ virStorageSourcePtr src,
+ qemuDomainAsyncJob asyncJob);
--
2.24.1