]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 if (r == -EMLINK) { | |
74 | librbd::Image image; | |
75 | int image_r = utils::open_image(io_ctx, image_name, true, &image); | |
76 | librbd::group_spec_t group_spec; | |
77 | if (image_r == 0) { | |
78 | image_r = image.get_group(&group_spec); | |
79 | } | |
80 | if (image_r == 0) | |
81 | std::cerr << "rbd: error: image belongs to a consistency group " | |
82 | << group_spec.pool << "." << group_spec.name; | |
83 | else | |
84 | std::cerr << "rbd: error: image belongs to a consistency group"; | |
85 | ||
86 | std::cerr << std::endl | |
87 | << "Remove the image from the consistency group and try again." | |
88 | << std::endl; | |
89 | image.close(); | |
90 | } else { | |
91 | std::cerr << "rbd: delete error: " << cpp_strerror(r) << std::endl; | |
92 | } | |
93 | return r ; | |
94 | } | |
95 | return 0; | |
96 | } | |
97 | ||
98 | Shell::Action action( | |
99 | {"remove"}, {"rm"}, "Delete an image.", "", &get_arguments, &execute); | |
100 | ||
101 | } // namespace remove | |
102 | } // namespace action | |
103 | } // namespace rbd |