]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
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 sparsify { | |
14 | ||
15 | namespace at = argument_types; | |
16 | namespace po = boost::program_options; | |
17 | ||
18 | static int do_sparsify(librbd::Image& image, size_t sparse_size, | |
19 | bool no_progress) | |
20 | { | |
21 | utils::ProgressContext pc("Image sparsify", no_progress); | |
22 | int r = image.sparsify_with_progress(sparse_size, 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 | at::add_sparse_size_option(options); | |
36 | } | |
37 | ||
38 | int execute(const po::variables_map &vm, | |
39 | const std::vector<std::string> &ceph_global_init_args) { | |
40 | size_t arg_index = 0; | |
41 | std::string pool_name; | |
42 | std::string namespace_name; | |
43 | std::string image_name; | |
44 | std::string snap_name; | |
45 | int r = utils::get_pool_image_snapshot_names( | |
46 | vm, at::ARGUMENT_MODIFIER_NONE, &arg_index, &pool_name, &namespace_name, | |
47 | &image_name, &snap_name, true, utils::SNAPSHOT_PRESENCE_NONE, | |
48 | utils::SPEC_VALIDATION_NONE); | |
49 | if (r < 0) { | |
50 | return r; | |
51 | } | |
52 | ||
53 | librados::Rados rados; | |
54 | librados::IoCtx io_ctx; | |
55 | librbd::Image image; | |
56 | r = utils::init_and_open_image(pool_name, namespace_name, image_name, "", "", | |
57 | false, &rados, &io_ctx, &image); | |
58 | if (r < 0) { | |
59 | return r; | |
60 | } | |
61 | ||
62 | size_t sparse_size = utils::RBD_DEFAULT_SPARSE_SIZE; | |
63 | if (vm.count(at::IMAGE_SPARSE_SIZE)) { | |
64 | sparse_size = vm[at::IMAGE_SPARSE_SIZE].as<size_t>(); | |
65 | } | |
66 | ||
67 | r = do_sparsify(image, sparse_size, vm[at::NO_PROGRESS].as<bool>()); | |
68 | if (r < 0) { | |
69 | std::cerr << "rbd: sparsify error: " << cpp_strerror(r) << std::endl; | |
70 | return r; | |
71 | } | |
72 | return 0; | |
73 | } | |
74 | ||
75 | Shell::Action action( | |
76 | {"sparsify"}, {}, | |
77 | "Reclaim space for zeroed image extents.", "", | |
78 | &get_arguments, &execute); | |
79 | ||
80 | } // namespace sparsify | |
81 | } // namespace action | |
82 | } // namespace rbd |