]> git.proxmox.com Git - ceph.git/blob - ceph/src/tools/rbd/action/Clone.cc
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / tools / rbd / action / Clone.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include "tools/rbd/ArgumentTypes.h"
5 #include "tools/rbd/Shell.h"
6 #include "tools/rbd/Utils.h"
7 #include "common/errno.h"
8 #include <iostream>
9 #include <boost/program_options.hpp>
10
11 namespace rbd {
12 namespace action {
13 namespace clone {
14
15 namespace at = argument_types;
16 namespace po = boost::program_options;
17
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);
23 }
24
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);
30 }
31
32 int execute(const po::variables_map &vm,
33 const std::vector<std::string> &ceph_global_init_args) {
34 size_t arg_index = 0;
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);
43 if (r < 0) {
44 return r;
45 }
46
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);
55 if (r < 0) {
56 return r;
57 }
58
59 librbd::ImageOptions opts;
60 r = utils::get_image_options(vm, false, &opts);
61 if (r < 0) {
62 return r;
63 }
64 opts.set(RBD_IMAGE_OPTION_FORMAT, static_cast<uint64_t>(2));
65
66 librados::Rados rados;
67 librados::IoCtx io_ctx;
68 r = utils::init(pool_name, namespace_name, &rados, &io_ctx);
69 if (r < 0) {
70 return r;
71 }
72
73 librados::IoCtx dst_io_ctx;
74 r = utils::init_io_ctx(rados, dst_pool_name, dst_namespace_name, &dst_io_ctx);
75 if (r < 0) {
76 return r;
77 }
78
79 librbd::RBD rbd;
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);
82 if (r == -EXDEV) {
83 std::cerr << "rbd: clone v2 required for cross-namespace clones."
84 << std::endl;
85 return r;
86 } else if (r < 0) {
87 std::cerr << "rbd: clone error: " << cpp_strerror(r) << std::endl;
88 return r;
89 }
90 return 0;
91 }
92
93 Shell::Action action(
94 {"clone"}, {}, "Clone a snapshot into a CoW child image.",
95 at::get_long_features_help(), &get_arguments, &execute);
96
97 } // namespace clone
98 } // namespace action
99 } // namespace rbd