]> git.proxmox.com Git - ceph.git/blob - ceph/src/tools/rbd/action/Flatten.cc
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / tools / rbd / action / Flatten.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 flatten {
14
15 namespace at = argument_types;
16 namespace po = boost::program_options;
17
18 static int do_flatten(librbd::Image& image, bool no_progress)
19 {
20 utils::ProgressContext pc("Image flatten", no_progress);
21 int r = image.flatten_with_progress(pc);
22 if (r < 0) {
23 pc.fail();
24 return r;
25 }
26 pc.finish();
27 return 0;
28 }
29
30 void get_arguments(po::options_description *positional,
31 po::options_description *options) {
32 at::add_image_spec_options(positional, options, at::ARGUMENT_MODIFIER_NONE);
33 at::add_no_progress_option(options);
34 at::add_encryption_options(options);
35 }
36
37 int execute(const po::variables_map &vm,
38 const std::vector<std::string> &ceph_global_init_args) {
39 size_t arg_index = 0;
40 std::string pool_name;
41 std::string namespace_name;
42 std::string image_name;
43 std::string snap_name;
44 int r = utils::get_pool_image_snapshot_names(
45 vm, at::ARGUMENT_MODIFIER_NONE, &arg_index, &pool_name, &namespace_name,
46 &image_name, &snap_name, true, utils::SNAPSHOT_PRESENCE_NONE,
47 utils::SPEC_VALIDATION_NONE);
48 if (r < 0) {
49 return r;
50 }
51
52 utils::EncryptionOptions encryption_options;
53 r = utils::get_encryption_options(vm, &encryption_options);
54 if (r < 0) {
55 return r;
56 }
57
58 librados::Rados rados;
59 librados::IoCtx io_ctx;
60 librbd::Image image;
61 r = utils::init_and_open_image(pool_name, namespace_name, image_name, "", "",
62 false, &rados, &io_ctx, &image);
63 if (r < 0) {
64 return r;
65 }
66
67 auto spec_count = encryption_options.specs.size();
68 if (spec_count > 0) {
69 r = image.encryption_load2(&encryption_options.specs[0], spec_count);
70
71 if (r < 0) {
72 std::cerr << "rbd: encryption load failed: " << cpp_strerror(r)
73 << std::endl;
74 return r;
75 }
76 }
77
78 r = do_flatten(image, vm[at::NO_PROGRESS].as<bool>());
79 if (r < 0) {
80 std::cerr << "rbd: flatten error: " << cpp_strerror(r) << std::endl;
81 return r;
82 }
83 return 0;
84 }
85
86 Shell::Action action(
87 {"flatten"}, {}, "Fill clone with parent data (make it independent).", "",
88 &get_arguments, &execute);
89
90 } // namespace flatten
91 } // namespace action
92 } // namespace rbd