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
|
// -*- 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 <iostream>
#include <boost/program_options.hpp>
namespace rbd {
namespace action {
namespace children {
namespace at = argument_types;
namespace po = boost::program_options;
int do_list_children(librados::IoCtx &io_ctx, librbd::Image &image,
bool all_flag, bool descendants_flag, Formatter *f)
{
std::vector<librbd::linked_image_spec_t> children;
librbd::RBD rbd;
int r;
if (descendants_flag) {
r = image.list_descendants(&children);
} else {
r = image.list_children3(&children);
}
if (r < 0)
return r;
if (f)
f->open_array_section("children");
for (auto& child : children) {
bool trash = child.trash;
if (f) {
if (all_flag) {
f->open_object_section("child");
f->dump_string("pool", child.pool_name);
f->dump_string("pool_namespace", child.pool_namespace);
f->dump_string("image", child.image_name);
f->dump_string("id", child.image_id);
f->dump_bool("trash", child.trash);
f->close_section();
} else if (!trash) {
f->open_object_section("child");
f->dump_string("pool", child.pool_name);
f->dump_string("pool_namespace", child.pool_namespace);
f->dump_string("image", child.image_name);
f->close_section();
}
} else if (all_flag || !trash) {
if (child.pool_name.empty()) {
std::cout << "(child missing " << child.pool_id << "/";
} else {
std::cout << child.pool_name << "/";
}
if (!child.pool_namespace.empty()) {
std::cout << child.pool_namespace << "/";
}
if (child.image_name.empty()) {
std::cout << child.image_id << ")";
} else {
std::cout << child.image_name;
if (trash) {
std::cout << " (trash " << child.image_id << ")";
}
}
std::cout << std::endl;
}
}
if (f) {
f->close_section();
f->flush(std::cout);
}
return 0;
}
void get_arguments(po::options_description *positional,
po::options_description *options) {
at::add_image_or_snap_spec_options(positional, options,
at::ARGUMENT_MODIFIER_NONE);
at::add_snap_id_option(options);
options->add_options()
("all,a", po::bool_switch(), "list all children (include trash)");
options->add_options()
("descendants", po::bool_switch(), "include all descendants");
at::add_format_options(options);
}
int execute(const po::variables_map &vm,
const std::vector<std::string> &ceph_global_init_args) {
uint64_t snap_id = LIBRADOS_SNAP_HEAD;
if (vm.count(at::SNAPSHOT_ID)) {
snap_id = vm[at::SNAPSHOT_ID].as<uint64_t>();
}
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_PERMITTED,
utils::SPEC_VALIDATION_NONE);
if (r < 0) {
return r;
}
if (snap_id != LIBRADOS_SNAP_HEAD && !snap_name.empty()) {
std::cerr << "rbd: trying to access snapshot using both name and id."
<< std::endl;
return -EINVAL;
}
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, "", "",
true, &rados, &io_ctx, &image);
if (r < 0) {
return r;
}
if (!snap_name.empty()) {
r = image.snap_set(snap_name.c_str());
} else if (snap_id != LIBRADOS_SNAP_HEAD) {
r = image.snap_set_by_id(snap_id);
}
if (r == -ENOENT) {
std::cerr << "rbd: snapshot does not exist." << std::endl;
return r;
} else if (r < 0) {
std::cerr << "rbd: error setting snapshot: " << cpp_strerror(r)
<< std::endl;
return r;
}
r = do_list_children(io_ctx, image, vm["all"].as<bool>(),
vm["descendants"].as<bool>(), formatter.get());
if (r < 0) {
std::cerr << "rbd: listing children failed: " << cpp_strerror(r)
<< std::endl;
return r;
}
return 0;
}
Shell::SwitchArguments switched_arguments({"all", "a", "descendants"});
Shell::Action action(
{"children"}, {}, "Display children of an image or its snapshot.", "",
&get_arguments, &execute);
} // namespace children
} // namespace action
} // namespace rbd
|