Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / tools / rbd / action / Resize.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 resize {
14
15 namespace at = argument_types;
16 namespace po = boost::program_options;
17
18 static int do_resize(librbd::Image& image, uint64_t size, bool allow_shrink, bool no_progress)
19 {
20   utils::ProgressContext pc("Resizing image", no_progress);
21   int r = image.resize2(size, allow_shrink, pc);
22   if (r < 0) {
23     pc.fail();
24     return r;
25   }
26   pc.finish();
27   return 0;
28 }
29
30 void get_arguments(po::options_description *positional,
31                    po::options_description *options) {
32   at::add_image_spec_options(positional, options, at::ARGUMENT_MODIFIER_NONE);
33   at::add_size_option(options);
34   options->add_options()
35     ("allow-shrink", po::bool_switch(), "permit shrinking");
36   at::add_no_progress_option(options);
37 }
38
39 int execute(const po::variables_map &vm) {
40   size_t arg_index = 0;
41   std::string pool_name;
42   std::string image_name;
43   std::string snap_name;
44   int r = utils::get_pool_image_snapshot_names(
45     vm, at::ARGUMENT_MODIFIER_NONE, &arg_index, &pool_name, &image_name,
46     &snap_name, utils::SNAPSHOT_PRESENCE_NONE, utils::SPEC_VALIDATION_NONE);
47   if (r < 0) {
48     return r;
49   }
50
51   uint64_t size;
52   r = utils::get_image_size(vm, &size);
53   if (r < 0) {
54     return r;
55   }
56
57   librados::Rados rados;
58   librados::IoCtx io_ctx;
59   librbd::Image image;
60   r = utils::init_and_open_image(pool_name, image_name, "", snap_name, false,
61                                  &rados, &io_ctx, &image);
62   if (r < 0) {
63     return r;
64   }
65
66   librbd::image_info_t info;
67   r = image.stat(info, sizeof(info));
68   if (r < 0) {
69     std::cerr << "rbd: resize error: " << cpp_strerror(r) << std::endl;
70     return r;
71   }
72
73   r = do_resize(image, size, vm["allow-shrink"].as<bool>(), vm[at::NO_PROGRESS].as<bool>());
74   if (r < 0) {
75     if (r == -EINVAL && !vm["allow-shrink"].as<bool>()) {
76       std::cerr << "rbd: shrinking an image is only allowed with the "
77                 << "--allow-shrink flag" << std::endl;
78       return r;
79     }
80     std::cerr << "rbd: resize error: " << cpp_strerror(r) << std::endl;
81     return r;
82   }
83   return 0;
84 }
85
86 Shell::SwitchArguments switched_arguments({"allow-shrink"});
87 Shell::Action action(
88   {"resize"}, {}, "Resize (expand or shrink) image.", "", &get_arguments,
89   &execute);
90
91 } // namespace resize
92 } // namespace action
93 } // namespace rbd