1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
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"
9 #include "common/Formatter.h"
11 #include <boost/program_options.hpp>
17 namespace at
= argument_types
;
18 namespace po
= boost::program_options
;
20 void get_arguments_init(po::options_description
*positional
,
21 po::options_description
*options
) {
22 at::add_pool_options(positional
, options
, false);
23 options
->add_options()
24 ("force", po::bool_switch(),
25 "force initialize pool for RBD use if registered by another application");
28 int execute_init(const po::variables_map
&vm
,
29 const std::vector
<std::string
> &ceph_global_init_args
) {
30 std::string pool_name
;
32 int r
= utils::get_pool_and_namespace_names(vm
, true, false, &pool_name
,
38 librados::Rados rados
;
39 librados::IoCtx io_ctx
;
40 r
= utils::init(pool_name
, "", &rados
, &io_ctx
);
46 r
= rbd
.pool_init(io_ctx
, vm
["force"].as
<bool>());
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."
53 std::cerr
<< "rbd: error registered application: " << cpp_strerror(r
)
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
);
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
;
71 int r
= utils::get_pool_and_namespace_names(vm
, true, false, &pool_name
,
72 &namespace_name
, &arg_index
);
77 at::Format::Formatter formatter
;
78 r
= utils::get_formatter(vm
, &formatter
);
83 librados::Rados rados
;
84 librados::IoCtx io_ctx
;
85 r
= utils::init(pool_name
, namespace_name
, &rados
, &io_ctx
);
92 uint64_t provisioned_bytes
;
95 uint64_t trash_provisioned_bytes
;
96 uint64_t trash_snap_count
;
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
,
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
);
108 r
= rbd
.pool_stats_get(io_ctx
, &pool_stats
);
110 std::cerr
<< "rbd: failed to query pool stats: " << cpp_strerror(r
)
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
);
130 std::cout
<< "Total Images: " << image_count
;
131 if (trash_count
> 0) {
132 std::cout
<< " (" << trash_count
<< " in trash)";
134 std::cout
<< std::endl
;
136 std::cout
<< "Total Snapshots: " << snap_count
;
137 if (trash_count
> 0) {
138 std::cout
<< " (" << trash_snap_count
<< " in trash)";
140 std::cout
<< std::endl
;
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)";
146 std::cout
<< std::endl
;
152 Shell::Action
init_action(
153 {"pool", "init"}, {}, "Initialize pool for use by RBD.", "",
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
);
161 } // namespace action