]> git.proxmox.com Git - ceph.git/blob - ceph/src/tools/rbd/action/List.cc
update sources to v12.1.0
[ceph.git] / ceph / src / tools / rbd / action / List.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 "include/types.h"
9 #include "common/errno.h"
10 #include "common/Formatter.h"
11 #include "common/TextTable.h"
12 #include <iostream>
13 #include <boost/program_options.hpp>
14
15 namespace rbd {
16 namespace action {
17 namespace list {
18
19 namespace at = argument_types;
20 namespace po = boost::program_options;
21
22 int do_list(librbd::RBD &rbd, librados::IoCtx& io_ctx, bool lflag,
23 Formatter *f) {
24 std::vector<std::string> names;
25 int r = rbd.list(io_ctx, names);
26 if (r < 0)
27 return r;
28
29 if (!lflag) {
30 if (f)
31 f->open_array_section("images");
32 for (std::vector<std::string>::const_iterator i = names.begin();
33 i != names.end(); ++i) {
34 if (f)
35 f->dump_string("name", *i);
36 else
37 std::cout << *i << std::endl;
38 }
39 if (f) {
40 f->close_section();
41 f->flush(std::cout);
42 }
43 return 0;
44 }
45
46 TextTable tbl;
47
48 if (f) {
49 f->open_array_section("images");
50 } else {
51 tbl.define_column("NAME", TextTable::LEFT, TextTable::LEFT);
52 tbl.define_column("SIZE", TextTable::RIGHT, TextTable::RIGHT);
53 tbl.define_column("PARENT", TextTable::LEFT, TextTable::LEFT);
54 tbl.define_column("FMT", TextTable::RIGHT, TextTable::RIGHT);
55 tbl.define_column("PROT", TextTable::LEFT, TextTable::LEFT);
56 tbl.define_column("LOCK", TextTable::LEFT, TextTable::LEFT);
57 }
58
59 std::string pool, image, snap, parent;
60
61 for (std::vector<std::string>::const_iterator i = names.begin();
62 i != names.end(); ++i) {
63 librbd::image_info_t info;
64 librbd::Image im;
65
66 r = rbd.open_read_only(io_ctx, im, i->c_str(), NULL);
67 // image might disappear between rbd.list() and rbd.open(); ignore
68 // that, warn about other possible errors (EPERM, say, for opening
69 // an old-format image, because you need execute permission for the
70 // class method)
71 if (r < 0) {
72 if (r != -ENOENT) {
73 std::cerr << "rbd: error opening " << *i << ": " << cpp_strerror(r)
74 << std::endl;
75 }
76 // in any event, continue to next image
77 continue;
78 }
79
80 // handle second-nth trips through loop
81 parent.clear();
82 r = im.parent_info(&pool, &image, &snap);
83 if (r < 0 && r != -ENOENT)
84 goto out;
85 bool has_parent = false;
86 if (r != -ENOENT) {
87 parent = pool + "/" + image + "@" + snap;
88 has_parent = true;
89 }
90
91 if (im.stat(info, sizeof(info)) < 0) {
92 r = -EINVAL;
93 goto out;
94 }
95
96 uint8_t old_format;
97 im.old_format(&old_format);
98
99 std::list<librbd::locker_t> lockers;
100 bool exclusive;
101 r = im.list_lockers(&lockers, &exclusive, NULL);
102 if (r < 0)
103 goto out;
104 std::string lockstr;
105 if (!lockers.empty()) {
106 lockstr = (exclusive) ? "excl" : "shr";
107 }
108
109 if (f) {
110 f->open_object_section("image");
111 f->dump_string("image", *i);
112 f->dump_unsigned("size", info.size);
113 if (has_parent) {
114 f->open_object_section("parent");
115 f->dump_string("pool", pool);
116 f->dump_string("image", image);
117 f->dump_string("snapshot", snap);
118 f->close_section();
119 }
120 f->dump_int("format", old_format ? 1 : 2);
121 if (!lockers.empty())
122 f->dump_string("lock_type", exclusive ? "exclusive" : "shared");
123 f->close_section();
124 } else {
125 tbl << *i
126 << stringify(si_t(info.size))
127 << parent
128 << ((old_format) ? '1' : '2')
129 << "" // protect doesn't apply to images
130 << lockstr
131 << TextTable::endrow;
132 }
133
134 std::vector<librbd::snap_info_t> snaplist;
135 if (im.snap_list(snaplist) >= 0 && !snaplist.empty()) {
136 for (std::vector<librbd::snap_info_t>::iterator s = snaplist.begin();
137 s != snaplist.end(); ++s) {
138 bool is_protected;
139 bool has_parent = false;
140 parent.clear();
141 im.snap_set(s->name.c_str());
142 r = im.snap_is_protected(s->name.c_str(), &is_protected);
143 if (r < 0)
144 goto out;
145 if (im.parent_info(&pool, &image, &snap) >= 0) {
146 parent = pool + "/" + image + "@" + snap;
147 has_parent = true;
148 }
149 if (f) {
150 f->open_object_section("snapshot");
151 f->dump_string("image", *i);
152 f->dump_string("snapshot", s->name);
153 f->dump_unsigned("size", s->size);
154 if (has_parent) {
155 f->open_object_section("parent");
156 f->dump_string("pool", pool);
157 f->dump_string("image", image);
158 f->dump_string("snapshot", snap);
159 f->close_section();
160 }
161 f->dump_int("format", old_format ? 1 : 2);
162 f->dump_string("protected", is_protected ? "true" : "false");
163 f->close_section();
164 } else {
165 tbl << *i + "@" + s->name
166 << stringify(si_t(s->size))
167 << parent
168 << ((old_format) ? '1' : '2')
169 << (is_protected ? "yes" : "")
170 << "" // locks don't apply to snaps
171 << TextTable::endrow;
172 }
173 }
174 }
175 }
176
177 out:
178 if (f) {
179 f->close_section();
180 f->flush(std::cout);
181 } else if (!names.empty()) {
182 std::cout << tbl;
183 }
184
185 return r < 0 ? r : 0;
186 }
187
188 void get_arguments(po::options_description *positional,
189 po::options_description *options) {
190 options->add_options()
191 ("long,l", po::bool_switch(), "long listing format");
192 at::add_pool_options(positional, options);
193 at::add_format_options(options);
194 }
195
196 int execute(const po::variables_map &vm) {
197 size_t arg_index = 0;
198 std::string pool_name = utils::get_pool_name(vm, &arg_index);
199
200 at::Format::Formatter formatter;
201 int r = utils::get_formatter(vm, &formatter);
202 if (r < 0) {
203 return r;
204 }
205
206 librados::Rados rados;
207 librados::IoCtx io_ctx;
208 r = utils::init(pool_name, &rados, &io_ctx);
209 if (r < 0) {
210 return r;
211 }
212
213 librbd::RBD rbd;
214 r = do_list(rbd, io_ctx, vm["long"].as<bool>(), formatter.get());
215 if (r < 0) {
216 std::cerr << "rbd: list: " << cpp_strerror(r) << std::endl;
217 return r;
218 }
219
220 return 0;
221 }
222
223 Shell::SwitchArguments switched_arguments({"long", "l"});
224 Shell::Action action(
225 {"list"}, {"ls"}, "List rbd images.", "", &get_arguments, &execute);
226
227 } // namespace list
228 } // namespace action
229 } // namespace rbd