Skip to content

Commit

Permalink
librbd/migration: add nbd stream
Browse files Browse the repository at this point in the history
Co-authored-by: Ilya Dryomov <[email protected]>
Signed-off-by: Effi Ofer <[email protected]>
Signed-off-by: Ilya Dryomov <[email protected]>
  • Loading branch information
effi-ofer and idryomov committed Sep 6, 2024
1 parent b0d8273 commit dc8eed6
Show file tree
Hide file tree
Showing 26 changed files with 701 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,12 @@ if(WITH_RBD AND LINUX)
set(HAVE_LIBCRYPTSETUP ${LIBCRYPTSETUP_FOUND})
endif()

# libnbd
if(WITH_RBD AND NOT WIN32)
find_package(libnbd 1.0 REQUIRED)
set(HAVE_LIBNBD ${LIBNBD_FOUND})
endif()

include(CMakeDependentOption)

CMAKE_DEPENDENT_OPTION(WITH_LIBURING "Enable io_uring bluestore backend" ON
Expand Down
1 change: 1 addition & 0 deletions ceph.spec.in
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ BuildRequires: gperftools-devel >= 2.4
BuildRequires: libaio-devel
BuildRequires: libblkid-devel >= 2.17
BuildRequires: cryptsetup-devel
BuildRequires: libnbd-devel
BuildRequires: libcurl-devel
BuildRequires: libcap-devel
BuildRequires: libcap-ng-devel
Expand Down
33 changes: 33 additions & 0 deletions cmake/modules/Findlibnbd.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# - Find libnbd
# Sets the following:
#
# LIBNBD_INCLUDE_DIR
# LIBNBD_LIBRARIES
# LIBNBD_VERSION
# LIBNBD_FOUND

find_package(PkgConfig QUIET REQUIRED)
pkg_search_module(PC_libnbd libnbd)

find_path(LIBNBD_INCLUDE_DIR
NAMES libnbd.h
PATHS ${PC_libnbd_INCLUDE_DIRS})

find_library(LIBNBD_LIBRARIES
NAMES libnbd.so
PATHS ${PC_libnbd_LIBRARY_DIRS})

set(LIBNBD_VERSION ${PC_libnbd_VERSION})

include(FindPackageHandleStandardArgs)

find_package_handle_standard_args(libnbd
REQUIRED_VARS
LIBNBD_INCLUDE_DIR
LIBNBD_LIBRARIES
VERSION_VAR LIBNBD_VERSION)

mark_as_advanced(
LIBNBD_LIBRARIES
LIBNBD_INCLUDE_DIR
LIBNBD_VERSION)
1 change: 1 addition & 0 deletions debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Build-Depends: automake,
liblttng-ust-dev,
liblua5.3-dev,
liblz4-dev (>= 0.0~r131),
libnbd-dev,
libncurses-dev,
libnss3-dev,
liboath-dev,
Expand Down
19 changes: 16 additions & 3 deletions doc/rbd/rbd-live-migration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ parent.
The live-migration process can also be used in an import-only mode where the
source image remains unmodified and the target image can be linked to an image
in another Ceph cluster or to an external data source such as a backing file,
HTTP(s) file, or S3 object.
HTTP(s) file, S3 object, or NBD export.

The live-migration copy process can safely run in the background while the new
target image is in use. There is currently a requirement to temporarily stop
Expand Down Expand Up @@ -145,8 +145,8 @@ The general format for the ``source-spec`` JSON is as follows::
}

The following formats are currently supported: ``native``, ``qcow``, and
``raw``. The following streams are currently supported: ``file``, ``http``, and
``s3``.
``raw``. The following streams are currently supported: ``file``, ``http``,
``s3``, and ``nbd``.

Formats
~~~~~~~
Expand Down Expand Up @@ -306,6 +306,19 @@ as follows::
stored in the config-key store via ``ceph config-key set <key-path> <value>``
(e.g. ``ceph config-key set rbd/s3/access_key NX5QOQKC6BH2IDN8HC7A``).

The ``nbd`` stream can be used to import from a remote NBD export. Its
``source-spec`` JSON is encoded as follows::

{
<format unique parameters>
"stream": {
"type": "nbd",
"server": "<server>",
"port": "<port>"
}
}


Execute Migration
=================

Expand Down
44 changes: 44 additions & 0 deletions qa/workunits/rbd/cli_migration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ IMAGE3=image3
IMAGES="${IMAGE1} ${IMAGE2} ${IMAGE3}"

cleanup() {
kill_nbd_server
cleanup_tempdir
remove_images
}
Expand Down Expand Up @@ -65,6 +66,10 @@ remove_images() {
done
}

kill_nbd_server() {
pkill -9 qemu-nbd || true
}

show_diff()
{
local file1=$1
Expand Down Expand Up @@ -390,6 +395,42 @@ EOF
remove_image "${dest_image}"
}

test_import_nbd_stream() {
local base_image=$1
local dest_image=$2

qemu-nbd -f qcow2 --read-only --shared 10 --persistent --fork \
${TEMPDIR}/${base_image}.qcow2

cat > ${TEMPDIR}/spec.json <<EOF
{
"type": "raw",
"stream": {
"type": "nbd",
"server": "localhost",
"port": "10809"
}
}
EOF
cat ${TEMPDIR}/spec.json

cat ${TEMPDIR}/spec.json | rbd migration prepare --import-only \
--source-spec-path - ${dest_image}
compare_images ${base_image} ${dest_image}
rbd migration abort ${dest_image}

rbd migration prepare --import-only \
--source-spec-path ${TEMPDIR}/spec.json ${dest_image}
compare_images ${base_image} ${dest_image}
rbd migration execute ${dest_image}
compare_images ${base_image} ${dest_image}
rbd migration commit ${dest_image}
compare_images ${base_image} ${dest_image}
remove_image "${dest_image}"

kill_nbd_server
}

# make sure rbd pool is EMPTY.. this is a test script!!
rbd ls 2>&1 | wc -l | grep -v '^0$' && echo "nonempty rbd pool, aborting! run this script on an empty test cluster only." && exit 1

Expand All @@ -401,7 +442,10 @@ export_base_image ${IMAGE1}

test_import_native_format ${IMAGE1} ${IMAGE2}
test_import_qcow_format ${IMAGE1} ${IMAGE2}

test_import_qcow2_format ${IMAGE2} ${IMAGE3}
test_import_nbd_stream ${IMAGE2} ${IMAGE3}

test_import_raw_format ${IMAGE1} ${IMAGE2}

echo OK
3 changes: 3 additions & 0 deletions src/include/config-h.in.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,9 @@
/* Define if libcryptsetup can be used (linux only) */
#cmakedefine HAVE_LIBCRYPTSETUP

/* Define if libnbd can be used */
#cmakedefine HAVE_LIBNBD

/* Shared library extension, such as .so, .dll or .dylib */
#cmakedefine CMAKE_SHARED_LIBRARY_SUFFIX "@CMAKE_SHARED_LIBRARY_SUFFIX@"

Expand Down
9 changes: 9 additions & 0 deletions src/librbd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ if(LINUX AND HAVE_LIBCRYPTSETUP)
crypto/luks/Magic.cc)
endif()

if(HAVE_LIBNBD)
list(APPEND librbd_internal_srcs
migration/NBDStream.cc)
endif()

add_library(rbd_api STATIC librbd.cc)
add_library(rbd_internal STATIC
${librbd_internal_srcs}
Expand All @@ -240,6 +245,10 @@ if(LINUX AND HAVE_LIBCRYPTSETUP)
target_include_directories(rbd_internal PRIVATE ${LIBCRYPTSETUP_INCLUDE_DIR})
target_link_libraries(rbd_internal PRIVATE ${LIBCRYPTSETUP_LIBRARIES})
endif()
if(HAVE_LIBNBD)
target_include_directories(rbd_internal PRIVATE ${LIBNBD_INCLUDE_DIR})
target_link_libraries(rbd_internal PRIVATE ${LIBNBD_LIBRARIES})
endif()

add_custom_target(librbd_plugins)
set(librbd_plugins_dir ${CEPH_INSTALL_PKGLIBDIR}/librbd)
Expand Down
12 changes: 12 additions & 0 deletions src/librbd/migration/FileStream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,18 @@ void FileStream<I>::read(io::Extents&& byte_extents, bufferlist* data,

#endif // BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR

template <typename I>
void FileStream<I>::list_sparse_extents(io::Extents&& byte_extents,
io::SparseExtents* sparse_extents,
Context* on_finish) {
// TODO: list sparse extents based on SEEK_HOLE/SEEK_DATA
for (auto [byte_offset, byte_length] : byte_extents) {
sparse_extents->insert(byte_offset, byte_length,
{io::SPARSE_EXTENT_STATE_DATA, byte_length});
}
on_finish->complete(0);
}

} // namespace migration
} // namespace librbd

Expand Down
4 changes: 4 additions & 0 deletions src/librbd/migration/FileStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class FileStream : public StreamInterface {
void read(io::Extents&& byte_extents, bufferlist* data,
Context* on_finish) override;

void list_sparse_extents(io::Extents&& byte_extents,
io::SparseExtents* sparse_extents,
Context* on_finish) override;

private:
CephContext* m_cct;
std::shared_ptr<AsioEngine> m_asio_engine;
Expand Down
12 changes: 12 additions & 0 deletions src/librbd/migration/HttpStream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ void HttpStream<I>::read(io::Extents&& byte_extents, bufferlist* data,
m_http_client->read(std::move(byte_extents), data, on_finish);
}

template <typename I>
void HttpStream<I>::list_sparse_extents(io::Extents&& byte_extents,
io::SparseExtents* sparse_extents,
Context* on_finish) {
// no sparseness information -- list the full range as DATA
for (auto [byte_offset, byte_length] : byte_extents) {
sparse_extents->insert(byte_offset, byte_length,
{io::SPARSE_EXTENT_STATE_DATA, byte_length});
}
on_finish->complete(0);
}

} // namespace migration
} // namespace librbd

Expand Down
4 changes: 4 additions & 0 deletions src/librbd/migration/HttpStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class HttpStream : public StreamInterface {
void read(io::Extents&& byte_extents, bufferlist* data,
Context* on_finish) override;

void list_sparse_extents(io::Extents&& byte_extents,
io::SparseExtents* sparse_extents,
Context* on_finish) override;

private:
using HttpResponse = boost::beast::http::response<
boost::beast::http::string_body>;
Expand Down
Loading

0 comments on commit dc8eed6

Please sign in to comment.