]>
Commit | Line | Data |
---|---|---|
c07f9fc5 FG |
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 "common/errno.h" | |
11fdf7f2 | 9 | #include "common/Formatter.h" |
c07f9fc5 FG |
10 | #include <iostream> |
11 | #include <boost/program_options.hpp> | |
12 | ||
13 | namespace rbd { | |
14 | namespace action { | |
15 | namespace pool { | |
16 | ||
17 | namespace at = argument_types; | |
18 | namespace po = boost::program_options; | |
19 | ||
20 | void get_arguments_init(po::options_description *positional, | |
21 | po::options_description *options) { | |
11fdf7f2 | 22 | at::add_pool_options(positional, options, false); |
c07f9fc5 FG |
23 | options->add_options() |
24 | ("force", po::bool_switch(), | |
25 | "force initialize pool for RBD use if registered by another application"); | |
26 | } | |
27 | ||
11fdf7f2 TL |
28 | int execute_init(const po::variables_map &vm, |
29 | const std::vector<std::string> &ceph_global_init_args) { | |
30 | std::string pool_name; | |
c07f9fc5 | 31 | size_t arg_index = 0; |
11fdf7f2 TL |
32 | int r = utils::get_pool_and_namespace_names(vm, true, false, &pool_name, |
33 | nullptr, &arg_index); | |
34 | if (r < 0) { | |
35 | return r; | |
36 | } | |
c07f9fc5 FG |
37 | |
38 | librados::Rados rados; | |
39 | librados::IoCtx io_ctx; | |
11fdf7f2 | 40 | r = utils::init(pool_name, "", &rados, &io_ctx); |
c07f9fc5 FG |
41 | if (r < 0) { |
42 | return r; | |
43 | } | |
44 | ||
11fdf7f2 TL |
45 | librbd::RBD rbd; |
46 | r = rbd.pool_init(io_ctx, vm["force"].as<bool>()); | |
c07f9fc5 FG |
47 | if (r == -EOPNOTSUPP) { |
48 | std::cerr << "rbd: luminous or later release required." << std::endl; | |
49 | } else if (r == -EPERM) { | |
50 | std::cerr << "rbd: pool already registered to a different application." | |
51 | << std::endl; | |
52 | } else if (r < 0) { | |
53 | std::cerr << "rbd: error registered application: " << cpp_strerror(r) | |
54 | << std::endl; | |
55 | } | |
56 | ||
57 | return 0; | |
58 | } | |
59 | ||
11fdf7f2 TL |
60 | void get_arguments_stats(po::options_description *positional, |
61 | po::options_description *options) { | |
62 | at::add_pool_options(positional, options, true); | |
63 | at::add_format_options(options); | |
64 | } | |
65 | ||
66 | int execute_stats(const po::variables_map &vm, | |
67 | const std::vector<std::string> &ceph_global_init_args) { | |
68 | std::string pool_name; | |
69 | std::string namespace_name; | |
70 | size_t arg_index = 0; | |
71 | int r = utils::get_pool_and_namespace_names(vm, true, false, &pool_name, | |
72 | &namespace_name, &arg_index); | |
73 | if (r < 0) { | |
74 | return r; | |
75 | } | |
76 | ||
77 | at::Format::Formatter formatter; | |
78 | r = utils::get_formatter(vm, &formatter); | |
79 | if (r < 0) { | |
80 | return r; | |
81 | } | |
82 | ||
83 | librados::Rados rados; | |
84 | librados::IoCtx io_ctx; | |
85 | r = utils::init(pool_name, namespace_name, &rados, &io_ctx); | |
86 | if (r < 0) { | |
87 | return r; | |
88 | } | |
89 | ||
90 | librbd::RBD rbd; | |
91 | uint64_t image_count; | |
92 | uint64_t provisioned_bytes; | |
93 | uint64_t snap_count; | |
94 | uint64_t trash_count; | |
95 | uint64_t trash_provisioned_bytes; | |
96 | uint64_t trash_snap_count; | |
97 | ||
98 | librbd::PoolStats pool_stats; | |
99 | pool_stats.add(RBD_POOL_STAT_OPTION_IMAGES, &image_count); | |
100 | pool_stats.add(RBD_POOL_STAT_OPTION_IMAGE_MAX_PROVISIONED_BYTES, | |
101 | &provisioned_bytes); | |
102 | pool_stats.add(RBD_POOL_STAT_OPTION_IMAGE_SNAPSHOTS, &snap_count); | |
103 | pool_stats.add(RBD_POOL_STAT_OPTION_TRASH_IMAGES, &trash_count); | |
104 | pool_stats.add(RBD_POOL_STAT_OPTION_TRASH_MAX_PROVISIONED_BYTES, | |
105 | &trash_provisioned_bytes); | |
106 | pool_stats.add(RBD_POOL_STAT_OPTION_TRASH_SNAPSHOTS, &trash_snap_count); | |
107 | ||
108 | r = rbd.pool_stats_get(io_ctx, &pool_stats); | |
109 | if (r < 0) { | |
110 | std::cerr << "rbd: failed to query pool stats: " << cpp_strerror(r) | |
111 | << std::endl; | |
112 | return r; | |
113 | } | |
114 | ||
115 | if (formatter) { | |
116 | formatter->open_object_section("stats"); | |
117 | formatter->open_object_section("images"); | |
118 | formatter->dump_unsigned("count", image_count); | |
119 | formatter->dump_unsigned("provisioned_bytes", provisioned_bytes); | |
120 | formatter->dump_unsigned("snap_count", snap_count); | |
121 | formatter->close_section(); | |
122 | formatter->open_object_section("trash"); | |
123 | formatter->dump_unsigned("count", trash_count); | |
124 | formatter->dump_unsigned("provisioned_bytes", trash_provisioned_bytes); | |
125 | formatter->dump_unsigned("snap_count", trash_snap_count); | |
126 | formatter->close_section(); | |
127 | formatter->close_section(); | |
128 | formatter->flush(std::cout); | |
129 | } else { | |
130 | std::cout << "Total Images: " << image_count; | |
131 | if (trash_count > 0) { | |
132 | std::cout << " (" << trash_count << " in trash)"; | |
133 | } | |
134 | std::cout << std::endl; | |
135 | ||
136 | std::cout << "Total Snapshots: " << snap_count; | |
137 | if (trash_count > 0) { | |
138 | std::cout << " (" << trash_snap_count << " in trash)"; | |
139 | } | |
140 | std::cout << std::endl; | |
141 | ||
142 | std::cout << "Provisioned Size: " << byte_u_t(provisioned_bytes); | |
143 | if (trash_count > 0) { | |
144 | std::cout << " (" << byte_u_t(trash_provisioned_bytes) << " in trash)"; | |
145 | } | |
146 | std::cout << std::endl; | |
147 | } | |
148 | ||
149 | return 0; | |
150 | } | |
151 | ||
152 | Shell::Action init_action( | |
c07f9fc5 | 153 | {"pool", "init"}, {}, "Initialize pool for use by RBD.", "", |
11fdf7f2 TL |
154 | &get_arguments_init, &execute_init); |
155 | Shell::Action stat_action( | |
156 | {"pool", "stats"}, {}, "Display pool statistics.", | |
157 | "Note: legacy v1 images are not included in stats", | |
158 | &get_arguments_stats, &execute_stats); | |
c07f9fc5 FG |
159 | |
160 | } // namespace pool | |
161 | } // namespace action | |
162 | } // namespace rbd |