]>
git.proxmox.com Git - ceph.git/blob - ceph/src/tools/rbd/action/ImageMeta.cc
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"
8 #include "common/Formatter.h"
9 #include "common/TextTable.h"
11 #include <boost/program_options.hpp>
15 namespace image_meta
{
17 namespace at
= argument_types
;
18 namespace po
= boost::program_options
;
22 void add_key_option(po::options_description
*positional
) {
23 positional
->add_options()
24 ("key", "image meta key");
27 int get_key(const po::variables_map
&vm
, std::string
*key
) {
28 *key
= utils::get_positional_argument(vm
, 1);
30 std::cerr
<< "rbd: metadata key was not specified" << std::endl
;
36 } // anonymous namespace
38 static int do_metadata_list(librbd::Image
& image
, Formatter
*f
)
40 std::map
<std::string
, bufferlist
> pairs
;
44 r
= image
.metadata_list("", 0, &pairs
);
46 std::cerr
<< "failed to list metadata of image : " << cpp_strerror(r
)
52 f
->open_object_section("metadatas");
54 tbl
.define_column("Key", TextTable::LEFT
, TextTable::LEFT
);
55 tbl
.define_column("Value", TextTable::LEFT
, TextTable::LEFT
);
59 bool one
= (pairs
.size() == 1);
62 std::cout
<< "There " << (one
? "is " : "are ") << pairs
.size()
63 << " metadata" << (one
? "" : "s") << " on this image.\n";
66 for (std::map
<std::string
, bufferlist
>::iterator it
= pairs
.begin();
67 it
!= pairs
.end(); ++it
) {
68 std::string
val(it
->second
.c_str(), it
->second
.length());
70 f
->dump_string(it
->first
.c_str(), val
.c_str());
72 tbl
<< it
->first
<< val
.c_str() << TextTable::endrow
;
86 static int do_metadata_set(librbd::Image
& image
, const char *key
,
89 int r
= image
.metadata_set(key
, value
);
91 std::cerr
<< "failed to set metadata " << key
<< " of image : "
92 << cpp_strerror(r
) << std::endl
;
97 static int do_metadata_remove(librbd::Image
& image
, const char *key
)
99 int r
= image
.metadata_remove(key
);
101 std::cerr
<< "rbd: no existing metadata key " << key
<< " of image : "
102 << cpp_strerror(r
) << std::endl
;
104 std::cerr
<< "failed to remove metadata " << key
<< " of image : "
105 << cpp_strerror(r
) << std::endl
;
110 static int do_metadata_get(librbd::Image
& image
, const char *key
)
113 int r
= image
.metadata_get(key
, &s
);
115 std::cerr
<< "failed to get metadata " << key
<< " of image : "
116 << cpp_strerror(r
) << std::endl
;
119 std::cout
<< s
<< std::endl
;
123 void get_list_arguments(po::options_description
*positional
,
124 po::options_description
*options
) {
125 at::add_image_spec_options(positional
, options
, at::ARGUMENT_MODIFIER_NONE
);
126 at::add_format_options(options
);
129 int execute_list(const po::variables_map
&vm
) {
130 size_t arg_index
= 0;
131 std::string pool_name
;
132 std::string image_name
;
133 std::string snap_name
;
134 int r
= utils::get_pool_image_snapshot_names(
135 vm
, at::ARGUMENT_MODIFIER_NONE
, &arg_index
, &pool_name
, &image_name
,
136 &snap_name
, utils::SNAPSHOT_PRESENCE_NONE
, utils::SPEC_VALIDATION_NONE
);
141 at::Format::Formatter formatter
;
142 r
= utils::get_formatter(vm
, &formatter
);
147 librados::Rados rados
;
148 librados::IoCtx io_ctx
;
150 r
= utils::init_and_open_image(pool_name
, image_name
, "", "", false,
151 &rados
, &io_ctx
, &image
);
156 r
= do_metadata_list(image
, formatter
.get());
158 std::cerr
<< "rbd: listing metadata failed: " << cpp_strerror(r
)
165 void get_get_arguments(po::options_description
*positional
,
166 po::options_description
*options
) {
167 at::add_image_spec_options(positional
, options
, at::ARGUMENT_MODIFIER_NONE
);
168 add_key_option(positional
);
171 int execute_get(const po::variables_map
&vm
) {
172 size_t arg_index
= 0;
173 std::string pool_name
;
174 std::string image_name
;
175 std::string snap_name
;
176 int r
= utils::get_pool_image_snapshot_names(
177 vm
, at::ARGUMENT_MODIFIER_NONE
, &arg_index
, &pool_name
, &image_name
,
178 &snap_name
, utils::SNAPSHOT_PRESENCE_NONE
, utils::SPEC_VALIDATION_NONE
);
184 r
= get_key(vm
, &key
);
189 librados::Rados rados
;
190 librados::IoCtx io_ctx
;
192 r
= utils::init_and_open_image(pool_name
, image_name
, "", "", false,
193 &rados
, &io_ctx
, &image
);
198 r
= do_metadata_get(image
, key
.c_str());
200 std::cerr
<< "rbd: getting metadata failed: " << cpp_strerror(r
)
207 void get_set_arguments(po::options_description
*positional
,
208 po::options_description
*options
) {
209 at::add_image_spec_options(positional
, options
, at::ARGUMENT_MODIFIER_NONE
);
210 add_key_option(positional
);
211 positional
->add_options()
212 ("value", "image meta value");
215 int execute_set(const po::variables_map
&vm
) {
216 size_t arg_index
= 0;
217 std::string pool_name
;
218 std::string image_name
;
219 std::string snap_name
;
220 int r
= utils::get_pool_image_snapshot_names(
221 vm
, at::ARGUMENT_MODIFIER_NONE
, &arg_index
, &pool_name
, &image_name
,
222 &snap_name
, utils::SNAPSHOT_PRESENCE_NONE
, utils::SPEC_VALIDATION_NONE
);
228 r
= get_key(vm
, &key
);
233 std::string value
= utils::get_positional_argument(vm
, 2);
235 std::cerr
<< "rbd: metadata value was not specified" << std::endl
;
239 librados::Rados rados
;
240 librados::IoCtx io_ctx
;
242 r
= utils::init_and_open_image(pool_name
, image_name
, "", "", false,
243 &rados
, &io_ctx
, &image
);
248 r
= do_metadata_set(image
, key
.c_str(), value
.c_str());
250 std::cerr
<< "rbd: setting metadata failed: " << cpp_strerror(r
)
257 void get_remove_arguments(po::options_description
*positional
,
258 po::options_description
*options
) {
259 at::add_image_spec_options(positional
, options
, at::ARGUMENT_MODIFIER_NONE
);
260 add_key_option(positional
);
263 int execute_remove(const po::variables_map
&vm
) {
264 size_t arg_index
= 0;
265 std::string pool_name
;
266 std::string image_name
;
267 std::string snap_name
;
268 int r
= utils::get_pool_image_snapshot_names(
269 vm
, at::ARGUMENT_MODIFIER_NONE
, &arg_index
, &pool_name
, &image_name
,
270 &snap_name
, utils::SNAPSHOT_PRESENCE_NONE
, utils::SPEC_VALIDATION_NONE
);
276 r
= get_key(vm
, &key
);
281 librados::Rados rados
;
282 librados::IoCtx io_ctx
;
284 r
= utils::init_and_open_image(pool_name
, image_name
, "", "", false,
285 &rados
, &io_ctx
, &image
);
290 r
= do_metadata_remove(image
, key
.c_str());
292 std::cerr
<< "rbd: removing metadata failed: " << cpp_strerror(r
)
299 Shell::Action
action_list(
300 {"image-meta", "list"}, {}, "Image metadata list keys with values.", "",
301 &get_list_arguments
, &execute_list
);
302 Shell::Action
action_get(
303 {"image-meta", "get"}, {},
304 "Image metadata get the value associated with the key.", "",
305 &get_get_arguments
, &execute_get
);
306 Shell::Action
action_set(
307 {"image-meta", "set"}, {}, "Image metadata set key with value.", "",
308 &get_set_arguments
, &execute_set
);
309 Shell::Action
action_remove(
310 {"image-meta", "remove"}, {},
311 "Image metadata remove the key and value associated.", "",
312 &get_remove_arguments
, &execute_remove
);
314 } // namespace image_meta
315 } // namespace action