Add a command that can replace bs in following BdrvChild structures:

 - qdev blk root child
 - block-export blk root child
 - any child of BlockDriverState selected by child-name

Signed-off-by: Vladimir Sementsov-Ogievskiy <[email protected]>
---
 blockdev.c             | 68 ++++++++++++++++++++++++++++++++++++++++++
 qapi/block-core.json   | 24 +++++++++++++++
 stubs/blk-by-qdev-id.c | 13 ++++++++
 stubs/meson.build      |  1 +
 4 files changed, 106 insertions(+)
 create mode 100644 stubs/blk-by-qdev-id.c

diff --git a/blockdev.c b/blockdev.c
index 2540f991b3..3082a5763c 100644
--- a/blockdev.c
+++ b/blockdev.c
@@ -3681,6 +3681,74 @@ out:
     bdrv_drain_all_end();
 }
 
+void qmp_blockdev_replace(const char *parent, const char *child,
+                          const char *new_child, Error **errp)
+{
+    BdrvChild *child_to_replace = NULL;
+    BlockDriverState *new_child_bs;
+    Error *dev_err = NULL, *exp_err = NULL;
+    BlockDriverState *parent_bs = bdrv_find_node(parent);
+    BlockBackend *dev_blk = blk_by_qdev_id(parent, &dev_err);
+    BlockBackend *exp_blk = blk_by_export_id(parent, &exp_err);
+    unsigned found = !!parent_bs + !!dev_blk + !!exp_blk;
+
+    if (found == 0) {
+        error_setg(errp, "Neither device, nor export, nor block-node exist"
+                   " with name '%s'. block-node: not found,"
+                   " device block-backend: %s, export block-backend: %s",
+                   parent, error_get_pretty(dev_err),
+                   error_get_pretty(exp_err));
+    }
+    error_free(dev_err);
+    error_free(exp_err);
+
+    if (found == 0) {
+        return;
+    }
+
+    if (found > 1) {
+        error_setg(errp, "Parent name '%s' is ambigous: block-node %s,"
+                   " device block-backend %s, export block-backend %s",
+                   parent, parent_bs ? "found" : "not found",
+                   dev_blk ? "found" : "not found",
+                   exp_blk ? "found" : "not found");
+        return;
+    }
+
+    if (parent_bs) {
+        child_to_replace = bdrv_find_child(parent_bs, child);
+        if (!child_to_replace) {
+            error_setg(errp, "Block driver node '%s' doesn't have child "
+                       "named '%s'", parent, child);
+            return;
+        }
+    } else {
+        BlockBackend *blk = dev_blk ?: exp_blk;
+
+        if (strcmp(child, "root")) {
+            error_setg(errp, "Devices and exports may have only 'root' child");
+        }
+
+        child_to_replace = blk_root(blk);
+        if (!child_to_replace) {
+            error_setg(errp, "%s '%s' is empty, nothing to replace",
+                       dev_blk ? "Device" : "Export", parent);
+            return;
+        }
+    }
+
+    assert(child_to_replace);
+    assert(child_to_replace->bs);
+
+    new_child_bs = bdrv_find_node(new_child);
+    if (!new_child_bs) {
+        error_setg(errp, "Node '%s' not found", new_child);
+        return;
+    }
+
+    bdrv_replace_child_bs(child_to_replace, new_child_bs, errp);
+}
+
 QemuOptsList qemu_common_drive_opts = {
     .name = "drive",
     .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
diff --git a/qapi/block-core.json b/qapi/block-core.json
index b82af74256..9cc7c3d140 100644
--- a/qapi/block-core.json
+++ b/qapi/block-core.json
@@ -6334,3 +6334,27 @@
 ##
 { 'struct': 'DummyBlockCoreForceArrays',
   'data': { 'unused-block-graph-info': ['BlockGraphInfo'] } }
+
+##
+# @blockdev-replace:
+#
+# Replace a block-node associated with device (@parent should be
+# QOM path, and @child should be "root") or with block-export (@parent
+# should be export name, and @child should be "root") or any child
+# of block-node (@parent should be node-name, and @child should be any
+# if its children names) with @new-child block-node.
+#
+# @parent: QOM path or block-export or node-name, which @child should
+#     be repalced.  If several matching parents exist, the command
+#     will fail
+#
+# @child: child to replace.  Must be "root" when parent is QOM path or
+#     block-export
+#
+# @new-child: node-name of the block-node, which should become a
+#    replacement for @child's current block-node
+#
+# Since 11.0
+##
+{ 'command': 'blockdev-replace',
+  'data': { 'parent': 'str', 'child': 'str', 'new-child': 'str' } }
diff --git a/stubs/blk-by-qdev-id.c b/stubs/blk-by-qdev-id.c
new file mode 100644
index 0000000000..66ead77f4d
--- /dev/null
+++ b/stubs/blk-by-qdev-id.c
@@ -0,0 +1,13 @@
+#include "qemu/osdep.h"
+#include "qapi/error.h"
+#include "system/block-backend.h"
+
+BlockBackend *blk_by_qdev_id(const char *id, Error **errp)
+{
+    /*
+     * We expect this when blockdev-change is called with parent-type=qdev,
+     * but qdev-monitor is not linked in. So no blk_ API is not available.
+     */
+    error_setg(errp, "Parameter 'parent-type' does not accept value 'qdev'");
+    return NULL;
+}
diff --git a/stubs/meson.build b/stubs/meson.build
index d3b551f4de..93a52d273e 100644
--- a/stubs/meson.build
+++ b/stubs/meson.build
@@ -15,6 +15,7 @@ if have_block
   stub_ss.add(files('bdrv-next-monitor-owned.c'))
   stub_ss.add(files('blk-commit-all.c'))
   stub_ss.add(files('blk-exp-close-all.c'))
+  stub_ss.add(files('blk-by-qdev-id.c'))
   stub_ss.add(files('blockdev-close-all-bdrv-states.c'))
   stub_ss.add(files('change-state-handler.c'))
   stub_ss.add(files('get-vm-name.c'))
-- 
2.52.0

Reply via email to