1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "tools/rbd/ArgumentTypes.h"
#include "tools/rbd/Shell.h"
#include "tools/rbd/Utils.h"
#include "common/errno.h"
#include "common/Formatter.h"
#include "common/TextTable.h"
#include <iostream>
#include <boost/program_options.hpp>
namespace rbd {
namespace action {
namespace image_meta {
namespace at = argument_types;
namespace po = boost::program_options;
namespace {
void add_key_option(po::options_description *positional) {
positional->add_options()
("key", "image meta key");
}
int get_key(const po::variables_map &vm, size_t *arg_index,
std::string *key) {
*key = utils::get_positional_argument(vm, *arg_index);
if (key->empty()) {
std::cerr << "rbd: metadata key was not specified" << std::endl;
return -EINVAL;
} else {
++(*arg_index);
}
return 0;
}
const uint32_t MAX_KEYS = 64;
} // anonymous namespace
static int do_metadata_list(librbd::Image& image, Formatter *f)
{
int r;
TextTable tbl;
size_t count = 0;
std::string last_key;
bool more_results = true;
while (more_results) {
std::map<std::string, bufferlist> pairs;
r = image.metadata_list(last_key, MAX_KEYS, &pairs);
if (r < 0) {
std::cerr << "failed to list metadata of image : " << cpp_strerror(r)
<< std::endl;
return r;
}
more_results = (pairs.size() == MAX_KEYS);
if (!pairs.empty()) {
if (count == 0) {
if (f) {
f->open_object_section("metadatas");
} else {
tbl.define_column("Key", TextTable::LEFT, TextTable::LEFT);
tbl.define_column("Value", TextTable::LEFT, TextTable::LEFT);
}
}
last_key = pairs.rbegin()->first;
count += pairs.size();
for (auto kv : pairs) {
std::string val(kv.second.c_str(), kv.second.length());
if (f) {
f->dump_string(kv.first.c_str(), val.c_str());
} else {
tbl << kv.first << val << TextTable::endrow;
}
}
}
}
if (f == nullptr) {
bool single = (count == 1);
std::cout << "There " << (single ? "is" : "are") << " " << count << " "
<< (single ? "metadatum" : "metadata") << " on this image"
<< (count == 0 ? "." : ":") << std::endl;
}
if (count > 0) {
if (f) {
f->close_section();
f->flush(std::cout);
} else {
std::cout << std::endl << tbl;
}
}
return 0;
}
static int do_metadata_set(librbd::Image& image, std::string &key,
std::string &value)
{
int r = image.metadata_set(key, value);
if (r < 0) {
std::cerr << "failed to set metadata " << key << " of image : "
<< cpp_strerror(r) << std::endl;
}
return r;
}
static int do_metadata_remove(librbd::Image& image, std::string &key)
{
int r = image.metadata_remove(key);
if (r == -ENOENT) {
std::cerr << "rbd: no existing metadata key " << key << " of image : "
<< cpp_strerror(r) << std::endl;
} else if(r < 0) {
std::cerr << "failed to remove metadata " << key << " of image : "
<< cpp_strerror(r) << std::endl;
}
return r;
}
static int do_metadata_get(librbd::Image& image, std::string &key)
{
std::string s;
int r = image.metadata_get(key, &s);
if (r < 0) {
std::cerr << "failed to get metadata " << key << " of image : "
<< cpp_strerror(r) << std::endl;
return r;
}
std::cout << s << std::endl;
return r;
}
void get_list_arguments(po::options_description *positional,
po::options_description *options) {
at::add_image_spec_options(positional, options, at::ARGUMENT_MODIFIER_NONE);
at::add_format_options(options);
}
int execute_list(const po::variables_map &vm,
const std::vector<std::string> &ceph_global_init_args) {
size_t arg_index = 0;
std::string pool_name;
std::string namespace_name;
std::string image_name;
std::string snap_name;
int r = utils::get_pool_image_snapshot_names(
vm, at::ARGUMENT_MODIFIER_NONE, &arg_index, &pool_name, &namespace_name,
&image_name, &snap_name, true, utils::SNAPSHOT_PRESENCE_NONE,
utils::SPEC_VALIDATION_NONE);
if (r < 0) {
return r;
}
at::Format::Formatter formatter;
r = utils::get_formatter(vm, &formatter);
if (r < 0) {
return r;
}
librados::Rados rados;
librados::IoCtx io_ctx;
librbd::Image image;
r = utils::init_and_open_image(pool_name, namespace_name, image_name, "", "",
false, &rados, &io_ctx, &image);
if (r < 0) {
return r;
}
r = do_metadata_list(image, formatter.get());
if (r < 0) {
std::cerr << "rbd: listing metadata failed: " << cpp_strerror(r)
<< std::endl;
return r;
}
return 0;
}
void get_get_arguments(po::options_description *positional,
po::options_description *options) {
at::add_image_spec_options(positional, options, at::ARGUMENT_MODIFIER_NONE);
add_key_option(positional);
}
int execute_get(const po::variables_map &vm,
const std::vector<std::string> &ceph_global_init_args) {
size_t arg_index = 0;
std::string pool_name;
std::string namespace_name;
std::string image_name;
std::string snap_name;
int r = utils::get_pool_image_snapshot_names(
vm, at::ARGUMENT_MODIFIER_NONE, &arg_index, &pool_name, &namespace_name,
&image_name, &snap_name, true, utils::SNAPSHOT_PRESENCE_NONE,
utils::SPEC_VALIDATION_NONE);
if (r < 0) {
return r;
}
std::string key;
r = get_key(vm, &arg_index, &key);
if (r < 0) {
return r;
}
librados::Rados rados;
librados::IoCtx io_ctx;
librbd::Image image;
r = utils::init_and_open_image(pool_name, namespace_name, image_name, "", "",
false, &rados, &io_ctx, &image);
if (r < 0) {
return r;
}
r = do_metadata_get(image, key);
if (r < 0) {
std::cerr << "rbd: getting metadata failed: " << cpp_strerror(r)
<< std::endl;
return r;
}
return 0;
}
void get_set_arguments(po::options_description *positional,
po::options_description *options) {
at::add_image_spec_options(positional, options, at::ARGUMENT_MODIFIER_NONE);
add_key_option(positional);
positional->add_options()
("value", "image meta value");
}
int execute_set(const po::variables_map &vm,
const std::vector<std::string> &ceph_global_init_args) {
size_t arg_index = 0;
std::string pool_name;
std::string namespace_name;
std::string image_name;
std::string snap_name;
int r = utils::get_pool_image_snapshot_names(
vm, at::ARGUMENT_MODIFIER_NONE, &arg_index, &pool_name, &namespace_name,
&image_name, &snap_name, true, utils::SNAPSHOT_PRESENCE_NONE,
utils::SPEC_VALIDATION_NONE);
if (r < 0) {
return r;
}
std::string key;
r = get_key(vm, &arg_index, &key);
if (r < 0) {
return r;
}
std::string value = utils::get_positional_argument(vm, arg_index);
if (value.empty()) {
std::cerr << "rbd: metadata value was not specified" << std::endl;
return -EINVAL;
}
librados::Rados rados;
librados::IoCtx io_ctx;
librbd::Image image;
r = utils::init_and_open_image(pool_name, namespace_name, image_name, "", "",
false, &rados, &io_ctx, &image);
if (r < 0) {
return r;
}
r = do_metadata_set(image, key, value);
if (r < 0) {
std::cerr << "rbd: setting metadata failed: " << cpp_strerror(r)
<< std::endl;
return r;
}
return 0;
}
void get_remove_arguments(po::options_description *positional,
po::options_description *options) {
at::add_image_spec_options(positional, options, at::ARGUMENT_MODIFIER_NONE);
add_key_option(positional);
}
int execute_remove(const po::variables_map &vm,
const std::vector<std::string> &ceph_global_init_args) {
size_t arg_index = 0;
std::string pool_name;
std::string namespace_name;
std::string image_name;
std::string snap_name;
int r = utils::get_pool_image_snapshot_names(
vm, at::ARGUMENT_MODIFIER_NONE, &arg_index, &pool_name, &namespace_name,
&image_name, &snap_name, true, utils::SNAPSHOT_PRESENCE_NONE,
utils::SPEC_VALIDATION_NONE);
if (r < 0) {
return r;
}
std::string key;
r = get_key(vm, &arg_index, &key);
if (r < 0) {
return r;
}
librados::Rados rados;
librados::IoCtx io_ctx;
librbd::Image image;
r = utils::init_and_open_image(pool_name, namespace_name, image_name, "", "",
false, &rados, &io_ctx, &image);
if (r < 0) {
return r;
}
r = do_metadata_remove(image, key);
if (r < 0) {
std::cerr << "rbd: removing metadata failed: " << cpp_strerror(r)
<< std::endl;
return r;
}
return 0;
}
Shell::Action action_list(
{"image-meta", "list"}, {"image-meta", "ls"}, "Image metadata list keys with values.", "",
&get_list_arguments, &execute_list);
Shell::Action action_get(
{"image-meta", "get"}, {},
"Image metadata get the value associated with the key.", "",
&get_get_arguments, &execute_get);
Shell::Action action_set(
{"image-meta", "set"}, {}, "Image metadata set key with value.", "",
&get_set_arguments, &execute_set);
Shell::Action action_remove(
{"image-meta", "remove"}, {"image-meta", "rm"},
"Image metadata remove the key and value associated.", "",
&get_remove_arguments, &execute_remove);
} // namespace image_meta
} // namespace action
} // namespace rbd
|