Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / tools / rbd / action / Create.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 "common/errno.h"
8 #include <iostream>
9 #include <boost/program_options.hpp>
10
11 namespace rbd {
12 namespace action {
13 namespace create {
14
15 namespace at = argument_types;
16 namespace po = boost::program_options;
17
18 static int do_create(librbd::RBD &rbd, librados::IoCtx& io_ctx,
19                      const char *imgname, uint64_t size,
20                      librbd::ImageOptions& opts) {
21   return rbd.create4(io_ctx, imgname, size, opts);
22 }
23
24 void get_arguments(po::options_description *positional,
25                    po::options_description *options) {
26   at::add_image_spec_options(positional, options, at::ARGUMENT_MODIFIER_NONE);
27   at::add_create_image_options(options, true);
28   at::add_size_option(options);
29 }
30
31 int execute(const po::variables_map &vm) {
32   size_t arg_index = 0;
33   std::string pool_name;
34   std::string image_name;
35   std::string snap_name;
36   int r = utils::get_pool_image_snapshot_names(
37     vm, at::ARGUMENT_MODIFIER_NONE, &arg_index, &pool_name, &image_name,
38     &snap_name, utils::SNAPSHOT_PRESENCE_NONE, utils::SPEC_VALIDATION_FULL);
39   if (r < 0) {
40     return r;
41   }
42
43   librbd::ImageOptions opts;
44   r = utils::get_image_options(vm, true, &opts);
45   if (r < 0) {
46     return r;
47   }
48
49   uint64_t size;
50   r = utils::get_image_size(vm, &size);
51   if (r < 0) {
52     return r;
53   }
54
55   librados::Rados rados;
56   librados::IoCtx io_ctx;
57   r = utils::init(pool_name, &rados, &io_ctx);
58   if (r < 0) {
59     return r;
60   }
61
62   librbd::RBD rbd;
63   r = do_create(rbd, io_ctx, image_name.c_str(), size, opts);
64   if (r < 0) {
65     std::cerr << "rbd: create error: " << cpp_strerror(r) << std::endl;
66     return r;
67   }
68   return 0;
69 }
70
71 Shell::Action action(
72   {"create"}, {}, "Create an empty image.", at::get_long_features_help(),
73   &get_arguments, &execute);
74
75 } // namespace create
76 } // namespace action
77 } // namespace rbd