Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / tools / rbd / action / Remove.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 remove {
14
15 namespace at = argument_types;
16 namespace po = boost::program_options;
17
18 static int do_delete(librbd::RBD &rbd, librados::IoCtx& io_ctx,
19                      const char *imgname, bool no_progress)
20 {
21   utils::ProgressContext pc("Removing image", no_progress);
22   int r = rbd.remove_with_progress(io_ctx, imgname, pc);
23   if (r < 0) {
24     pc.fail();
25     return r;
26   }
27   pc.finish();
28   return 0;
29 }
30
31 void get_arguments(po::options_description *positional,
32                    po::options_description *options) {
33   at::add_image_spec_options(positional, options, at::ARGUMENT_MODIFIER_NONE);
34   at::add_no_progress_option(options);
35 }
36
37 int execute(const po::variables_map &vm) {
38   size_t arg_index = 0;
39   std::string pool_name;
40   std::string image_name;
41   std::string snap_name;
42   int r = utils::get_pool_image_snapshot_names(
43     vm, at::ARGUMENT_MODIFIER_NONE, &arg_index, &pool_name, &image_name,
44     &snap_name, utils::SNAPSHOT_PRESENCE_NONE, utils::SPEC_VALIDATION_NONE);
45   if (r < 0) {
46     return r;
47   }
48
49   librados::Rados rados;
50   librados::IoCtx io_ctx;
51   r = utils::init(pool_name, &rados, &io_ctx);
52   if (r < 0) {
53     return r;
54   }
55
56   io_ctx.set_osdmap_full_try();
57
58   librbd::RBD rbd;
59   r = do_delete(rbd, io_ctx, image_name.c_str(),
60                 vm[at::NO_PROGRESS].as<bool>());
61   if (r < 0) {
62     if (r == -ENOTEMPTY) {
63       std::cerr << "rbd: image has snapshots - these must be deleted"
64                 << " with 'rbd snap purge' before the image can be removed."
65                 << std::endl;
66     } else if (r == -EBUSY) {
67       std::cerr << "rbd: error: image still has watchers"
68                 << std::endl
69                 << "This means the image is still open or the client using "
70                 << "it crashed. Try again after closing/unmapping it or "
71                 << "waiting 30s for the crashed client to timeout."
72                 << std::endl;
73     } else {
74       std::cerr << "rbd: delete error: " << cpp_strerror(r) << std::endl;
75     }
76     return r ;
77   }
78   return 0;
79 }
80
81 Shell::Action action(
82   {"remove"}, {"rm"}, "Delete an image.", "", &get_arguments, &execute);
83
84 } // namespace remove
85 } // namespace action
86 } // namespace rbd