]> git.proxmox.com Git - ceph.git/blob - ceph/src/tools/rbd/action/Rename.cc
bump version to 18.2.2-pve1
[ceph.git] / ceph / src / tools / rbd / action / Rename.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 rename {
14
15 namespace at = argument_types;
16 namespace po = boost::program_options;
17
18 static int do_rename(librbd::RBD &rbd, librados::IoCtx& io_ctx,
19 const char *imgname, const char *destname)
20 {
21 int r = rbd.rename(io_ctx, imgname, destname);
22 if (r < 0)
23 return r;
24 return 0;
25 }
26
27 void get_arguments(po::options_description *positional,
28 po::options_description *options) {
29 at::add_image_spec_options(positional, options, at::ARGUMENT_MODIFIER_SOURCE);
30 at::add_image_spec_options(positional, options, at::ARGUMENT_MODIFIER_DEST);
31 }
32
33 int execute(const po::variables_map &vm,
34 const std::vector<std::string> &ceph_global_init_args) {
35 size_t arg_index = 0;
36 std::string pool_name;
37 std::string namespace_name;
38 std::string image_name;
39 std::string snap_name;
40 int r = utils::get_pool_image_snapshot_names(
41 vm, at::ARGUMENT_MODIFIER_SOURCE, &arg_index, &pool_name, &namespace_name,
42 &image_name, &snap_name, true, utils::SNAPSHOT_PRESENCE_NONE,
43 utils::SPEC_VALIDATION_NONE);
44 if (r < 0) {
45 return r;
46 }
47
48 std::string dst_image_name;
49 std::string dst_snap_name;
50 std::string dst_pool_name = pool_name;
51 std::string dst_namespace_name = namespace_name;
52 r = utils::get_pool_image_snapshot_names(
53 vm, at::ARGUMENT_MODIFIER_DEST, &arg_index, &dst_pool_name,
54 &dst_namespace_name, &dst_image_name, &dst_snap_name, true,
55 utils::SNAPSHOT_PRESENCE_NONE, utils::SPEC_VALIDATION_FULL);
56 if (r < 0) {
57 return r;
58 }
59
60 if (pool_name != dst_pool_name) {
61 std::cerr << "rbd: mv/rename across pools not supported" << std::endl
62 << "source pool: " << pool_name << " dest pool: " << dst_pool_name
63 << std::endl;
64 return -EINVAL;
65 } else if (namespace_name != dst_namespace_name) {
66 std::cerr << "rbd: mv/rename across namespaces not supported" << std::endl
67 << "source namespace: " << namespace_name << " dest namespace: "
68 << dst_namespace_name << std::endl;
69 return -EINVAL;
70 }
71
72 librados::Rados rados;
73 librados::IoCtx io_ctx;
74 r = utils::init(pool_name, namespace_name, &rados, &io_ctx);
75 if (r < 0) {
76 return r;
77 }
78
79 librbd::RBD rbd;
80 r = do_rename(rbd, io_ctx, image_name.c_str(), dst_image_name.c_str());
81 if (r < 0) {
82 std::cerr << "rbd: rename error: " << cpp_strerror(r) << std::endl;
83 return r;
84 }
85 return 0;
86 }
87
88 Shell::Action action(
89 {"rename"}, {"mv"}, "Rename image within pool.", "", &get_arguments,
90 &execute);
91
92 } // namespace rename
93 } // namespace action
94 } // namespace rbd