1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "tools/rbd/ArgumentTypes.h"
5 #include "tools/rbd/Shell.h"
6 #include "tools/rbd/Utils.h"
7 #include "common/errno.h"
9 #include <boost/program_options.hpp>
15 namespace at
= argument_types
;
16 namespace po
= boost::program_options
;
18 int do_clone(librbd::RBD
&rbd
, librados::IoCtx
&p_ioctx
,
19 const char *p_name
, const char *p_snapname
,
20 librados::IoCtx
&c_ioctx
, const char *c_name
,
21 librbd::ImageOptions
& opts
) {
22 return rbd
.clone3(p_ioctx
, p_name
, p_snapname
, c_ioctx
, c_name
, opts
);
25 void get_arguments(po::options_description
*positional
,
26 po::options_description
*options
) {
27 at::add_snap_spec_options(positional
, options
, at::ARGUMENT_MODIFIER_SOURCE
);
28 at::add_image_spec_options(positional
, options
, at::ARGUMENT_MODIFIER_DEST
);
29 at::add_create_image_options(options
, false);
32 int execute(const po::variables_map
&vm
,
33 const std::vector
<std::string
> &ceph_global_init_args
) {
35 std::string pool_name
;
36 std::string namespace_name
;
37 std::string image_name
;
38 std::string snap_name
;
39 int r
= utils::get_pool_image_snapshot_names(
40 vm
, at::ARGUMENT_MODIFIER_SOURCE
, &arg_index
, &pool_name
, &namespace_name
,
41 &image_name
, &snap_name
, true, utils::SNAPSHOT_PRESENCE_REQUIRED
,
42 utils::SPEC_VALIDATION_NONE
);
47 std::string dst_pool_name
;
48 std::string dst_namespace_name
;
49 std::string dst_image_name
;
50 std::string dst_snap_name
;
51 r
= utils::get_pool_image_snapshot_names(
52 vm
, at::ARGUMENT_MODIFIER_DEST
, &arg_index
, &dst_pool_name
,
53 &dst_namespace_name
, &dst_image_name
, &dst_snap_name
, true,
54 utils::SNAPSHOT_PRESENCE_NONE
, utils::SPEC_VALIDATION_FULL
);
59 librbd::ImageOptions opts
;
60 r
= utils::get_image_options(vm
, false, &opts
);
64 opts
.set(RBD_IMAGE_OPTION_FORMAT
, static_cast<uint64_t>(2));
66 librados::Rados rados
;
67 librados::IoCtx io_ctx
;
68 r
= utils::init(pool_name
, namespace_name
, &rados
, &io_ctx
);
73 librados::IoCtx dst_io_ctx
;
74 r
= utils::init_io_ctx(rados
, dst_pool_name
, dst_namespace_name
, &dst_io_ctx
);
80 r
= do_clone(rbd
, io_ctx
, image_name
.c_str(), snap_name
.c_str(), dst_io_ctx
,
81 dst_image_name
.c_str(), opts
);
83 std::cerr
<< "rbd: clone v2 required for cross-namespace clones."
87 std::cerr
<< "rbd: clone error: " << cpp_strerror(r
) << std::endl
;
94 {"clone"}, {}, "Clone a snapshot into a CoW child image.",
95 at::get_long_features_help(), &get_arguments
, &execute
);