]> git.proxmox.com Git - ceph.git/blob - ceph/src/tools/rbd/action/Info.cc
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / tools / rbd / action / Info.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/types.h"
8 #include "include/stringify.h"
9 #include "common/errno.h"
10 #include "common/Formatter.h"
11 #include <iostream>
12 #include <boost/program_options.hpp>
13
14 namespace rbd {
15 namespace action {
16 namespace info {
17
18 namespace at = argument_types;
19 namespace po = boost::program_options;
20
21 static void format_bitmask(Formatter *f, const std::string &name,
22 const std::map<uint64_t, std::string>& mapping,
23 uint64_t bitmask)
24 {
25 int count = 0;
26 std::string group_name(name + "s");
27 if (f == NULL) {
28 std::cout << "\t" << group_name << ": ";
29 } else {
30 f->open_array_section(group_name.c_str());
31 }
32 for (std::map<uint64_t, std::string>::const_iterator it = mapping.begin();
33 it != mapping.end(); ++it) {
34 if ((it->first & bitmask) == 0) {
35 continue;
36 }
37
38 if (f == NULL) {
39 if (count++ > 0) {
40 std::cout << ", ";
41 }
42 std::cout << it->second;
43 } else {
44 f->dump_string(name.c_str(), it->second);
45 }
46 }
47 if (f == NULL) {
48 std::cout << std::endl;
49 } else {
50 f->close_section();
51 }
52 }
53
54 static void format_features(Formatter *f, uint64_t features)
55 {
56 format_bitmask(f, "feature", at::ImageFeatures::FEATURE_MAPPING, features);
57 }
58
59 static void format_flags(Formatter *f, uint64_t flags)
60 {
61 std::map<uint64_t, std::string> mapping = {
62 {RBD_FLAG_OBJECT_MAP_INVALID, "object map invalid"},
63 {RBD_FLAG_FAST_DIFF_INVALID, "fast diff invalid"}};
64 format_bitmask(f, "flag", mapping, flags);
65 }
66
67 static int do_show_info(librados::IoCtx &io_ctx, librbd::Image& image,
68 const std::string &imgname, const std::string &imgid,
69 const std::string &snapname, Formatter *f)
70 {
71 librbd::image_info_t info;
72 uint8_t old_format;
73 uint64_t overlap, features, flags, snap_limit;
74 bool snap_protected = false;
75 librbd::mirror_image_info_t mirror_image;
76 int r;
77
78 r = image.stat(info, sizeof(info));
79 if (r < 0)
80 return r;
81
82 r = image.old_format(&old_format);
83 if (r < 0)
84 return r;
85
86 std::string data_pool;
87 if (!old_format) {
88 int64_t data_pool_id = image.get_data_pool_id();
89 if (data_pool_id != io_ctx.get_id()) {
90 librados::Rados rados(io_ctx);
91 librados::IoCtx data_io_ctx;
92 r = rados.ioctx_create2(data_pool_id, data_io_ctx);
93 if (r < 0) {
94 data_pool = "<missing data pool " + stringify(data_pool_id) + ">";
95 } else {
96 data_pool = data_io_ctx.get_pool_name();
97 }
98 }
99 }
100
101 r = image.overlap(&overlap);
102 if (r < 0)
103 return r;
104
105 r = image.features(&features);
106 if (r < 0)
107 return r;
108
109 r = image.get_flags(&flags);
110 if (r < 0) {
111 return r;
112 }
113
114 if (!snapname.empty()) {
115 r = image.snap_is_protected(snapname.c_str(), &snap_protected);
116 if (r < 0)
117 return r;
118 }
119
120 if (features & RBD_FEATURE_JOURNALING) {
121 r = image.mirror_image_get_info(&mirror_image, sizeof(mirror_image));
122 if (r < 0) {
123 return r;
124 }
125 }
126
127 r = image.snap_get_limit(&snap_limit);
128 if (r < 0)
129 return r;
130
131 std::string prefix = image.get_block_name_prefix();
132
133 librbd::group_spec_t group_spec;
134 r = image.get_group(&group_spec);
135 if (r < 0) {
136 return r;
137 }
138
139 std::string group_string = "";
140 if (-1 != group_spec.pool)
141 group_string = stringify(group_spec.pool) + "." + group_spec.name;
142
143 if (f) {
144 f->open_object_section("image");
145 if (!imgname.empty()) {
146 f->dump_string("name", imgname);
147 } else {
148 f->dump_string("id", imgid);
149 }
150 f->dump_unsigned("size", info.size);
151 f->dump_unsigned("objects", info.num_objs);
152 f->dump_int("order", info.order);
153 f->dump_unsigned("object_size", info.obj_size);
154 if (!data_pool.empty()) {
155 f->dump_string("data_pool", data_pool);
156 }
157 f->dump_string("block_name_prefix", prefix);
158 f->dump_int("format", (old_format ? 1 : 2));
159 } else {
160 std::cout << "rbd image '" << (imgname.empty() ? imgid : imgname) << "':\n"
161 << "\tsize " << prettybyte_t(info.size) << " in "
162 << info.num_objs << " objects"
163 << std::endl
164 << "\torder " << info.order
165 << " (" << prettybyte_t(info.obj_size) << " objects)"
166 << std::endl;
167 if (!data_pool.empty()) {
168 std::cout << "\tdata_pool: " << data_pool << std::endl;
169 }
170 std::cout << "\tblock_name_prefix: " << prefix
171 << std::endl
172 << "\tformat: " << (old_format ? "1" : "2")
173 << std::endl;
174 }
175
176 if (!old_format) {
177 format_features(f, features);
178 format_flags(f, flags);
179 }
180
181 if (!group_string.empty()) {
182 if (f) {
183 f->dump_string("group", group_string);
184 } else {
185 std::cout << "\tconsistency group: " << group_string
186 << std::endl;
187 }
188 }
189
190 // snapshot info, if present
191 if (!snapname.empty()) {
192 if (f) {
193 f->dump_string("protected", snap_protected ? "true" : "false");
194 } else {
195 std::cout << "\tprotected: " << (snap_protected ? "True" : "False")
196 << std::endl;
197 }
198 }
199
200 if (snap_limit < UINT64_MAX) {
201 if (f) {
202 f->dump_unsigned("snapshot_limit", snap_limit);
203 } else {
204 std::cout << "\tsnapshot_limit: " << snap_limit << std::endl;
205 }
206 }
207
208 // parent info, if present
209 std::string parent_pool, parent_name, parent_id, parent_snapname;
210 if ((image.parent_info2(&parent_pool, &parent_name, &parent_id,
211 &parent_snapname) == 0) &&
212 parent_name.length() > 0) {
213
214 librbd::trash_image_info_t trash_image_info;
215 librbd::RBD rbd;
216 r = rbd.trash_get(io_ctx, parent_id.c_str(), &trash_image_info);
217 bool trash_image_info_valid = (r == 0);
218
219 if (f) {
220 f->open_object_section("parent");
221 f->dump_string("pool", parent_pool);
222 f->dump_string("image", parent_name);
223 f->dump_string("snapshot", parent_snapname);
224 if (trash_image_info_valid) {
225 f->dump_string("trash", parent_id);
226 }
227 f->dump_unsigned("overlap", overlap);
228 f->close_section();
229 } else {
230 std::cout << "\tparent: " << parent_pool << "/" << parent_name
231 << "@" << parent_snapname;
232 if (trash_image_info_valid) {
233 std::cout << " (trash " << parent_id << ")";
234 }
235 std::cout << std::endl;
236 std::cout << "\toverlap: " << prettybyte_t(overlap) << std::endl;
237 }
238 }
239
240 // striping info, if feature is set
241 if (features & RBD_FEATURE_STRIPINGV2) {
242 if (f) {
243 f->dump_unsigned("stripe_unit", image.get_stripe_unit());
244 f->dump_unsigned("stripe_count", image.get_stripe_count());
245 } else {
246 std::cout << "\tstripe unit: " << prettybyte_t(image.get_stripe_unit())
247 << std::endl
248 << "\tstripe count: " << image.get_stripe_count() << std::endl;
249 }
250 }
251
252 if (features & RBD_FEATURE_JOURNALING) {
253 if (f) {
254 f->dump_string("journal", utils::image_id(image));
255 } else {
256 std::cout << "\tjournal: " << utils::image_id(image) << std::endl;
257 }
258 }
259
260 if (features & RBD_FEATURE_JOURNALING) {
261 if (f) {
262 f->open_object_section("mirroring");
263 f->dump_string("state",
264 utils::mirror_image_state(mirror_image.state));
265 if (mirror_image.state != RBD_MIRROR_IMAGE_DISABLED) {
266 f->dump_string("global_id", mirror_image.global_id);
267 f->dump_bool("primary", mirror_image.primary);
268 }
269 f->close_section();
270 } else {
271 std::cout << "\tmirroring state: "
272 << utils::mirror_image_state(mirror_image.state) << std::endl;
273 if (mirror_image.state != RBD_MIRROR_IMAGE_DISABLED) {
274 std::cout << "\tmirroring global id: " << mirror_image.global_id
275 << std::endl
276 << "\tmirroring primary: "
277 << (mirror_image.primary ? "true" : "false") <<std::endl;
278 }
279 }
280 }
281
282 if (f) {
283 f->close_section();
284 f->flush(std::cout);
285 }
286
287 return 0;
288 }
289
290 void get_arguments(po::options_description *positional,
291 po::options_description *options) {
292 at::add_image_or_snap_spec_options(positional, options,
293 at::ARGUMENT_MODIFIER_NONE);
294 at::add_image_id_option(options);
295 at::add_format_options(options);
296 }
297
298 int execute(const po::variables_map &vm) {
299 size_t arg_index = 0;
300 std::string pool_name;
301 std::string image_name;
302 std::string snap_name;
303 std::string image_id;
304
305 if (vm.count(at::IMAGE_ID)) {
306 image_id = vm[at::IMAGE_ID].as<std::string>();
307 }
308
309 bool has_image_spec = utils::check_if_image_spec_present(
310 vm, at::ARGUMENT_MODIFIER_NONE, arg_index);
311
312 if (!image_id.empty() && has_image_spec) {
313 std::cerr << "rbd: trying to access image using both name and id. "
314 << std::endl;
315 return -EINVAL;
316 }
317
318 int r;
319 if (image_id.empty()) {
320 r = utils::get_pool_image_snapshot_names(vm, at::ARGUMENT_MODIFIER_NONE,
321 &arg_index, &pool_name,
322 &image_name, &snap_name,
323 utils::SNAPSHOT_PRESENCE_PERMITTED,
324 utils::SPEC_VALIDATION_NONE);
325 } else {
326 r = utils::get_pool_snapshot_names(vm, at::ARGUMENT_MODIFIER_NONE,
327 &arg_index, &pool_name, &snap_name,
328 utils::SNAPSHOT_PRESENCE_PERMITTED,
329 utils::SPEC_VALIDATION_NONE);
330 }
331 if (r < 0) {
332 return r;
333 }
334
335 at::Format::Formatter formatter;
336 r = utils::get_formatter(vm, &formatter);
337 if (r < 0) {
338 return r;
339 }
340
341 librados::Rados rados;
342 librados::IoCtx io_ctx;
343 librbd::Image image;
344 r = utils::init_and_open_image(pool_name, image_name, image_id, snap_name,
345 true, &rados, &io_ctx, &image);
346 if (r < 0) {
347 return r;
348 }
349
350 r = do_show_info(io_ctx, image, image_name, image_id, snap_name,
351 formatter.get());
352 if (r < 0) {
353 std::cerr << "rbd: info: " << cpp_strerror(r) << std::endl;
354 return r;
355 }
356 return 0;
357 }
358
359 Shell::Action action(
360 {"info"}, {}, "Show information about image size, striping, etc.", "",
361 &get_arguments, &execute);
362
363 } // namespace info
364 } // namespace action
365 } // namespace rbd