From 668cbff880808a5eb2c09e0426ba95cf832f7163 Mon Sep 17 00:00:00 2001 From: Jason Dillaman Date: Tue, 1 Dec 2020 20:01:23 -0500 Subject: [PATCH] librbd/api: re-use migration open source image state machine Let the open source image state machine parse the provided source-spec JSON and instantiate the source image ctx for the prepare migration step. Signed-off-by: Jason Dillaman --- src/librbd/api/Migration.cc | 50 ++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 28 deletions(-) diff --git a/src/librbd/api/Migration.cc b/src/librbd/api/Migration.cc index 9f7d4d79149..d9d2518992d 100644 --- a/src/librbd/api/Migration.cc +++ b/src/librbd/api/Migration.cc @@ -35,8 +35,8 @@ #include "librbd/image/Types.h" #include "librbd/internal.h" #include "librbd/migration/FormatInterface.h" +#include "librbd/migration/OpenSourceImageRequest.h" #include "librbd/migration/NativeFormat.h" -#include "librbd/migration/SourceSpecBuilder.h" #include "librbd/mirror/DisableRequest.h" #include "librbd/mirror/EnableRequest.h" @@ -532,38 +532,20 @@ int Migration::prepare_import( << dest_io_ctx.get_pool_name() << "/" << dest_image_name << ", opts=" << opts << dendl; - auto src_image_ctx = I::create("", "", nullptr, dest_io_ctx, true); - auto asio_engine = src_image_ctx->asio_engine; - - migration::SourceSpecBuilder source_spec_builder(src_image_ctx); - json_spirit::mObject source_spec_object; - int r = source_spec_builder.parse_source_spec(source_spec, - &source_spec_object); - if (r < 0) { - lderr(cct) << "failed to parse source spec: " << cpp_strerror(r) - << dendl; - src_image_ctx->state->close(); - return r; - } - - std::unique_ptr format; - r = source_spec_builder.build_format(source_spec_object, true, &format); - if (r < 0) { - lderr(cct) << "failed to build migration format handler: " - << cpp_strerror(r) << dendl; - src_image_ctx->state->close(); - return r; - } - + I* src_image_ctx = nullptr; C_SaferCond open_ctx; - format->open(&open_ctx); - r = open_ctx.wait(); + auto req = migration::OpenSourceImageRequest::create( + dest_io_ctx, nullptr, CEPH_NOSNAP, + {-1, "", "", "", source_spec, {}, 0, false}, &src_image_ctx, &open_ctx); + req->send(); + + int r = open_ctx.wait(); if (r < 0) { - lderr(cct) << "failed to open migration source: " << cpp_strerror(r) - << dendl; + lderr(cct) << "failed to open source image: " << cpp_strerror(r) << dendl; return r; } + auto asio_engine = src_image_ctx->asio_engine; BOOST_SCOPE_EXIT_TPL(src_image_ctx) { src_image_ctx->state->close(); } BOOST_SCOPE_EXIT_END; @@ -584,6 +566,18 @@ int Migration::prepare_import( ldout(cct, 20) << "updated opts=" << opts << dendl; + // use json-spirit to clean-up json formatting + json_spirit::mObject source_spec_object; + json_spirit::mValue json_root; + if(json_spirit::read(source_spec, json_root)) { + try { + source_spec_object = json_root.get_obj(); + } catch (std::runtime_error&) { + lderr(cct) << "failed to clean source spec" << dendl; + return -EINVAL; + } + } + auto dst_image_ctx = I::create( dest_image_name, util::generate_image_id(dest_io_ctx), nullptr, dest_io_ctx, false); -- 2.39.5