]> git.proxmox.com Git - ceph.git/blob - ceph/src/tools/rbd/action/Feature.cc
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / tools / rbd / action / Feature.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 "include/stringify.h"
8 #include "common/errno.h"
9 #include <iostream>
10 #include <map>
11 #include <boost/program_options.hpp>
12
13 namespace rbd {
14 namespace action {
15 namespace feature {
16
17 namespace at = argument_types;
18 namespace po = boost::program_options;
19
20 void get_arguments(po::options_description *positional,
21 po::options_description *options, bool enabled) {
22 at::add_image_spec_options(positional, options, at::ARGUMENT_MODIFIER_NONE);
23 positional->add_options()
24 ("features", po::value<at::ImageFeatures>()->multitoken(),
25 ("image features\n" + at::get_short_features_help(false)).c_str());
26 if (enabled) {
27 at::add_create_journal_options(options);
28 }
29 }
30
31 void get_arguments_disable(po::options_description *positional,
32 po::options_description *options) {
33 get_arguments(positional, options, false);
34 }
35
36 void get_arguments_enable(po::options_description *positional,
37 po::options_description *options) {
38 get_arguments(positional, options, true);
39 }
40
41 int execute(const po::variables_map &vm, bool enabled) {
42 size_t arg_index = 0;
43 std::string pool_name;
44 std::string namespace_name;
45 std::string image_name;
46 std::string snap_name;
47 int r = utils::get_pool_image_snapshot_names(
48 vm, at::ARGUMENT_MODIFIER_NONE, &arg_index, &pool_name, &namespace_name,
49 &image_name, &snap_name, true, utils::SNAPSHOT_PRESENCE_NONE,
50 utils::SPEC_VALIDATION_NONE);
51 if (r < 0) {
52 return r;
53 }
54
55 librbd::ImageOptions opts;
56 r = utils::get_journal_options(vm, &opts);
57 if (r < 0) {
58 return r;
59 }
60
61 std::vector<std::string> feature_names;
62 if (vm.count(at::POSITIONAL_ARGUMENTS)) {
63 const std::vector<std::string> &args =
64 vm[at::POSITIONAL_ARGUMENTS].as<std::vector<std::string> >();
65 feature_names.insert(feature_names.end(), args.begin() + arg_index,
66 args.end());
67 }
68
69 if (feature_names.empty()) {
70 std::cerr << "rbd: at least one feature name must be specified"
71 << std::endl;
72 return -EINVAL;
73 }
74
75 boost::any features_any(static_cast<uint64_t>(0));
76 at::ImageFeatures image_features;
77 at::validate(features_any, feature_names, &image_features, 0);
78
79 librados::Rados rados;
80 librados::IoCtx io_ctx;
81 librbd::Image image;
82 r = utils::init_and_open_image(pool_name, namespace_name, image_name, "", "",
83 false, &rados, &io_ctx, &image);
84 if (r < 0) {
85 return r;
86 }
87
88 r = image.update_features(boost::any_cast<uint64_t>(features_any), enabled);
89 if (r < 0) {
90 std::cerr << "rbd: failed to update image features: " << cpp_strerror(r)
91 << std::endl;
92 return r;
93 }
94 return 0;
95 }
96
97 int execute_disable(const po::variables_map &vm,
98 const std::vector<std::string> &ceph_global_init_args) {
99 return execute(vm, false);
100 }
101
102 int execute_enable(const po::variables_map &vm,
103 const std::vector<std::string> &ceph_global_init_args) {
104 return execute(vm, true);
105 }
106
107 Shell::Action action_disable(
108 {"feature", "disable"}, {}, "Disable the specified image feature.", "",
109 &get_arguments_disable, &execute_disable);
110 Shell::Action action_enable(
111 {"feature", "enable"}, {}, "Enable the specified image feature.", "",
112 &get_arguments_enable, &execute_enable);
113
114 } // namespace feature
115 } // namespace action
116 } // namespace rbd