]> git.proxmox.com Git - ceph.git/blob - ceph/src/tools/rbd/action/Ggate.cc
update sources to v12.1.3
[ceph.git] / ceph / src / tools / rbd / action / Ggate.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 <sys/param.h>
5 #include <errno.h>
6 #include <unistd.h>
7
8 #include "include/stringify.h"
9 #include "common/SubProcess.h"
10
11 #include "tools/rbd/ArgumentTypes.h"
12 #include "tools/rbd/Shell.h"
13 #include "tools/rbd/Utils.h"
14
15 #include <boost/algorithm/string/predicate.hpp>
16 #include <boost/scope_exit.hpp>
17 #include <boost/program_options.hpp>
18
19 #include <iostream>
20
21 namespace rbd {
22 namespace action {
23 namespace ggate {
24
25 namespace at = argument_types;
26 namespace po = boost::program_options;
27
28 static int call_ggate_cmd(const po::variables_map &vm,
29 const std::vector<const char*> &args)
30 {
31 SubProcess process("rbd-ggate", SubProcess::KEEP, SubProcess::KEEP,
32 SubProcess::KEEP);
33
34 if (vm.count("conf")) {
35 process.add_cmd_arg("--conf");
36 process.add_cmd_arg(vm["conf"].as<std::string>().c_str());
37 }
38 if (vm.count("cluster")) {
39 process.add_cmd_arg("--cluster");
40 process.add_cmd_arg(vm["cluster"].as<std::string>().c_str());
41 }
42 if (vm.count("id")) {
43 process.add_cmd_arg("--id");
44 process.add_cmd_arg(vm["id"].as<std::string>().c_str());
45 }
46 if (vm.count("name")) {
47 process.add_cmd_arg("--name");
48 process.add_cmd_arg(vm["name"].as<std::string>().c_str());
49 }
50 if (vm.count("mon_host")) {
51 process.add_cmd_arg("--mon_host");
52 process.add_cmd_arg(vm["mon_host"].as<std::string>().c_str());
53 }
54 if (vm.count("keyfile")) {
55 process.add_cmd_arg("--keyfile");
56 process.add_cmd_arg(vm["keyfile"].as<std::string>().c_str());
57 }
58 if (vm.count("keyring")) {
59 process.add_cmd_arg("--keyring");
60 process.add_cmd_arg(vm["keyring"].as<std::string>().c_str());
61 }
62
63 for (std::vector<const char*>::const_iterator p = args.begin();
64 p != args.end(); ++p)
65 process.add_cmd_arg(*p);
66
67 if (process.spawn()) {
68 std::cerr << "rbd: failed to run rbd-ggate: " << process.err() << std::endl;
69 return -EINVAL;
70 } else if (process.join()) {
71 std::cerr << "rbd: rbd-ggate failed with error: " << process.err()
72 << std::endl;
73 return -EINVAL;
74 }
75
76 return 0;
77 }
78
79 void get_list_arguments(po::options_description *positional,
80 po::options_description *options)
81 { }
82
83 int execute_list(const po::variables_map &vm)
84 {
85 std::vector<const char*> args;
86
87 args.push_back("list");
88
89 return call_ggate_cmd(vm, args);
90 }
91
92 void get_map_arguments(po::options_description *positional,
93 po::options_description *options)
94 {
95 at::add_image_or_snap_spec_options(positional, options,
96 at::ARGUMENT_MODIFIER_NONE);
97 options->add_options()
98 ("read-only", po::bool_switch(), "map read-only")
99 ("exclusive", po::bool_switch(), "forbid writes by other clients")
100 ("device", po::value<std::string>(), "specify ggate device");
101 }
102
103 int execute_map(const po::variables_map &vm)
104 {
105 size_t arg_index = 0;
106 std::string pool_name;
107 std::string image_name;
108 std::string snap_name;
109 int r = utils::get_pool_image_snapshot_names(
110 vm, at::ARGUMENT_MODIFIER_NONE, &arg_index, &pool_name, &image_name,
111 &snap_name, utils::SNAPSHOT_PRESENCE_PERMITTED,
112 utils::SPEC_VALIDATION_NONE);
113 if (r < 0) {
114 return r;
115 }
116
117 std::vector<const char*> args;
118
119 args.push_back("map");
120 std::string img;
121 img.append(pool_name);
122 img.append("/");
123 img.append(image_name);
124 if (!snap_name.empty()) {
125 img.append("@");
126 img.append(snap_name);
127 }
128 args.push_back(img.c_str());
129
130 if (vm["read-only"].as<bool>())
131 args.push_back("--read-only");
132
133 if (vm["exclusive"].as<bool>())
134 args.push_back("--exclusive");
135
136 if (vm.count("device")) {
137 args.push_back("--device");
138 args.push_back(vm["device"].as<std::string>().c_str());
139 }
140
141 return call_ggate_cmd(vm, args);
142 }
143
144 void get_unmap_arguments(po::options_description *positional,
145 po::options_description *options)
146 {
147 positional->add_options()
148 ("device-spec", "specify ggate device");
149 }
150
151 int execute_unmap(const po::variables_map &vm)
152 {
153 std::string device_name = utils::get_positional_argument(vm, 0);
154 if (!boost::starts_with(device_name, "/dev/")) {
155 device_name.clear();
156 }
157
158 if (device_name.empty()) {
159 std::cerr << "rbd: ggate unmap requires device path" << std::endl;
160 return -EINVAL;
161 }
162
163 std::vector<const char*> args;
164
165 args.push_back("unmap");
166 args.push_back(device_name.c_str());
167
168 return call_ggate_cmd(vm, args);
169 }
170
171 Shell::SwitchArguments switched_arguments({"read-only", "exclusive"});
172
173 Shell::Action action_list(
174 {"ggate", "list"}, {"ggate", "ls"}, "List mapped ggate devices.", "",
175 &get_list_arguments, &execute_list);
176
177 Shell::Action action_map(
178 {"ggate", "map"}, {}, "Map an image to a ggate device.", "",
179 &get_map_arguments, &execute_map);
180
181 Shell::Action action_unmap(
182 {"ggate", "unmap"}, {}, "Unmap a ggate device.", "",
183 &get_unmap_arguments, &execute_unmap);
184
185 } // namespace ggate
186 } // namespace action
187 } // namespace rbd