]> git.proxmox.com Git - ceph.git/blob - ceph/src/tools/rbd/action/Rename.cc
add subtree-ish sources for 12.0.3
[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 size_t arg_index = 0;
35 std::string pool_name;
36 std::string image_name;
37 std::string snap_name;
38 int r = utils::get_pool_image_snapshot_names(
39 vm, at::ARGUMENT_MODIFIER_SOURCE, &arg_index, &pool_name, &image_name,
40 &snap_name, utils::SNAPSHOT_PRESENCE_NONE, utils::SPEC_VALIDATION_NONE);
41 if (r < 0) {
42 return r;
43 }
44
45 std::string dst_image_name;
46 std::string dst_snap_name;
47 std::string dst_pool_name = pool_name;
48 r = utils::get_pool_image_snapshot_names(
49 vm, at::ARGUMENT_MODIFIER_DEST, &arg_index, &dst_pool_name, &dst_image_name,
50 &dst_snap_name, utils::SNAPSHOT_PRESENCE_NONE, utils::SPEC_VALIDATION_FULL);
51 if (r < 0) {
52 return r;
53 }
54
55 if (pool_name != dst_pool_name) {
56 std::cerr << "rbd: mv/rename across pools not supported" << std::endl
57 << "source pool: " << pool_name<< " dest pool: " << dst_pool_name
58 << std::endl;
59 return -EINVAL;
60 }
61
62 librados::Rados rados;
63 librados::IoCtx io_ctx;
64 r = utils::init(pool_name, &rados, &io_ctx);
65 if (r < 0) {
66 return r;
67 }
68
69 librbd::RBD rbd;
70 r = do_rename(rbd, io_ctx, image_name.c_str(), dst_image_name.c_str());
71 if (r < 0) {
72 std::cerr << "rbd: rename error: " << cpp_strerror(r) << std::endl;
73 return r;
74 }
75 return 0;
76 }
77
78 Shell::Action action(
79 {"rename"}, {"mv"}, "Rename image within pool.", "", &get_arguments,
80 &execute);
81
82 } // namespace rename
83 } // namespace action
84 } // namespace rbd