]> git.proxmox.com Git - ceph.git/blob - ceph/src/tools/rbd/action/Copy.cc
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / tools / rbd / action / Copy.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 copy {
14
15 namespace at = argument_types;
16 namespace po = boost::program_options;
17
18 static int do_copy(librbd::Image &src, librados::IoCtx& dest_pp,
19 const char *destname, librbd::ImageOptions& opts,
20 bool no_progress,
21 size_t sparse_size)
22 {
23 utils::ProgressContext pc("Image copy", no_progress);
24 int r = src.copy_with_progress4(dest_pp, destname, opts, pc, sparse_size);
25 if (r < 0){
26 pc.fail();
27 return r;
28 }
29 pc.finish();
30 return 0;
31 }
32
33 void get_arguments(po::options_description *positional,
34 po::options_description *options) {
35 at::add_image_or_snap_spec_options(positional, options,
36 at::ARGUMENT_MODIFIER_SOURCE);
37 at::add_image_spec_options(positional, options, at::ARGUMENT_MODIFIER_DEST);
38 at::add_create_image_options(options, false);
39 at::add_sparse_size_option(options);
40 at::add_no_progress_option(options);
41 }
42
43 int execute(const po::variables_map &vm) {
44 size_t arg_index = 0;
45 std::string pool_name;
46 std::string image_name;
47 std::string snap_name;
48 int r = utils::get_pool_image_snapshot_names(
49 vm, at::ARGUMENT_MODIFIER_SOURCE, &arg_index, &pool_name, &image_name,
50 &snap_name, utils::SNAPSHOT_PRESENCE_PERMITTED,
51 utils::SPEC_VALIDATION_NONE);
52 if (r < 0) {
53 return r;
54 }
55
56 std::string dst_pool_name;
57 std::string dst_image_name;
58 std::string dst_snap_name;
59 r = utils::get_pool_image_snapshot_names(
60 vm, at::ARGUMENT_MODIFIER_DEST, &arg_index, &dst_pool_name, &dst_image_name,
61 &dst_snap_name, utils::SNAPSHOT_PRESENCE_NONE, utils::SPEC_VALIDATION_FULL);
62 if (r < 0) {
63 return r;
64 }
65
66 librbd::ImageOptions opts;
67 r = utils::get_image_options(vm, false, &opts);
68 if (r < 0) {
69 return r;
70 }
71
72 librados::Rados rados;
73 librados::IoCtx io_ctx;
74 librbd::Image image;
75 r = utils::init_and_open_image(pool_name, image_name, "", snap_name, true,
76 &rados, &io_ctx, &image);
77 if (r < 0) {
78 return r;
79 }
80
81 librados::IoCtx dst_io_ctx;
82 r = utils::init_io_ctx(rados, dst_pool_name, &dst_io_ctx);
83 if (r < 0) {
84 return r;
85 }
86
87 size_t sparse_size = utils::RBD_DEFAULT_SPARSE_SIZE;
88 if (vm.count(at::IMAGE_SPARSE_SIZE)) {
89 sparse_size = vm[at::IMAGE_SPARSE_SIZE].as<size_t>();
90 }
91 r = do_copy(image, dst_io_ctx, dst_image_name.c_str(), opts,
92 vm[at::NO_PROGRESS].as<bool>(), sparse_size);
93 if (r < 0) {
94 std::cerr << "rbd: copy failed: " << cpp_strerror(r) << std::endl;
95 return r;
96 }
97 return 0;
98 }
99
100 Shell::Action action(
101 {"copy"}, {"cp"}, "Copy src image to dest.", at::get_long_features_help(),
102 &get_arguments, &execute);
103
104 } // namespace copy
105 } // namespace action
106 } // namespace rbd