Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / tools / rbd / action / Pool.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 "common/errno.h"
9 #include "osd/osd_types.h"
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) {
22   at::add_pool_options(positional, options);
23   options->add_options()
24       ("force", po::bool_switch(),
25        "force initialize pool for RBD use if registered by another application");
26 }
27
28 int execute_init(const po::variables_map &vm) {
29   size_t arg_index = 0;
30   std::string pool_name = utils::get_pool_name(vm, &arg_index);
31
32   librados::Rados rados;
33   librados::IoCtx io_ctx;
34   int r = utils::init(pool_name, &rados, &io_ctx);
35   if (r < 0) {
36     return r;
37   }
38
39   r = io_ctx.application_enable(pg_pool_t::APPLICATION_NAME_RBD,
40                                 vm["force"].as<bool>());
41   if (r == -EOPNOTSUPP) {
42     std::cerr << "rbd: luminous or later release required." << std::endl;
43   } else if (r == -EPERM) {
44     std::cerr << "rbd: pool already registered to a different application."
45               << std::endl;
46   } else if (r < 0) {
47     std::cerr << "rbd: error registered application: " << cpp_strerror(r)
48               << std::endl;
49   }
50
51   return 0;
52 }
53
54 Shell::Action action(
55   {"pool", "init"}, {}, "Initialize pool for use by RBD.", "",
56     &get_arguments_init, &execute_init);
57
58 } // namespace pool
59 } // namespace action
60 } // namespace rbd