]> git.proxmox.com Git - ceph.git/blob - ceph/src/tools/rbd/action/Wnbd.cc
ae40fe538e3ce65b298e13ac1fc0ff73e76c7b4c
[ceph.git] / ceph / src / tools / rbd / action / Wnbd.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 "include/stringify.h"
8 #include "common/SubProcess.h"
9 #include <iostream>
10 #include <boost/algorithm/string.hpp>
11 #include <boost/algorithm/string/predicate.hpp>
12 #include <boost/program_options.hpp>
13
14 namespace rbd {
15 namespace action {
16 namespace wnbd {
17
18 namespace at = argument_types;
19 namespace po = boost::program_options;
20
21 #if defined(_WIN32)
22 static int call_wnbd_cmd(const po::variables_map &vm,
23 const std::vector<std::string> &args,
24 const std::vector<std::string> &ceph_global_init_args) {
25 char exe_path[PATH_MAX];
26 ssize_t exe_path_bytes = get_self_exe_path(exe_path, PATH_MAX);
27
28 if (exe_path_bytes > 4) {
29 // Drop .exe suffix as we're going to add the "-wnbd" suffix.
30 exe_path[strlen(exe_path) - 4] = '\0';
31 exe_path_bytes -= 4;
32 }
33
34 if (exe_path_bytes < 0) {
35 strcpy(exe_path, "rbd-wnbd");
36 } else {
37 if (snprintf(exe_path + exe_path_bytes,
38 sizeof(exe_path) - exe_path_bytes,
39 "-wnbd") < 0) {
40 return -EOVERFLOW;
41 }
42 }
43
44 SubProcess process(exe_path, SubProcess::KEEP, SubProcess::KEEP, SubProcess::KEEP);
45
46 for (auto &arg : ceph_global_init_args) {
47 process.add_cmd_arg(arg.c_str());
48 }
49
50 for (auto &arg : args) {
51 process.add_cmd_arg(arg.c_str());
52 }
53
54 if (process.spawn()) {
55 std::cerr << "rbd: failed to run rbd-wnbd: " << process.err() << std::endl;
56 return -EINVAL;
57 }
58 int exit_code = process.join();
59 if (exit_code) {
60 std::cerr << "rbd: rbd-wnbd failed with error: " << process.err() << std::endl;
61 return exit_code;
62 }
63
64 return 0;
65 }
66
67 int get_image_or_snap_spec(const po::variables_map &vm, std::string *spec) {
68 size_t arg_index = 0;
69 std::string pool_name;
70 std::string nspace_name;
71 std::string image_name;
72 std::string snap_name;
73 int r = utils::get_pool_image_snapshot_names(
74 vm, at::ARGUMENT_MODIFIER_NONE, &arg_index, &pool_name, &nspace_name,
75 &image_name, &snap_name, true, utils::SNAPSHOT_PRESENCE_PERMITTED,
76 utils::SPEC_VALIDATION_NONE);
77 if (r < 0) {
78 return r;
79 }
80
81 if (!pool_name.empty()) {
82 spec->append(pool_name);
83 spec->append("/");
84 }
85 if (!nspace_name.empty()) {
86 spec->append(nspace_name);
87 spec->append("/");
88 }
89 spec->append(image_name);
90 if (!snap_name.empty()) {
91 spec->append("@");
92 spec->append(snap_name);
93 }
94
95 return 0;
96 }
97
98 int parse_options(const std::vector<std::string> &options,
99 std::vector<std::string> *args) {
100 for (auto &opts : options) {
101 std::vector<std::string> args_;
102 boost::split(args_, opts, boost::is_any_of(","));
103 for (auto &o : args_) {
104 args->push_back("--" + o);
105 }
106 }
107
108 return 0;
109 }
110 #endif
111
112 int execute_list(const po::variables_map &vm,
113 const std::vector<std::string> &ceph_global_init_args) {
114 #if !defined(_WIN32)
115 std::cerr << "rbd: wnbd is only supported on Windows" << std::endl;
116 return -EOPNOTSUPP;
117 #else
118 std::vector<std::string> args;
119
120 args.push_back("list");
121
122 if (vm.count("format")) {
123 args.push_back("--format");
124 args.push_back(vm["format"].as<at::Format>().value);
125 }
126 if (vm["pretty-format"].as<bool>()) {
127 args.push_back("--pretty-format");
128 }
129
130 return call_wnbd_cmd(vm, args, ceph_global_init_args);
131 #endif
132 }
133
134 int execute_map(const po::variables_map &vm,
135 const std::vector<std::string> &ceph_global_init_args) {
136 #if !defined(_WIN32)
137 std::cerr << "rbd: wnbd is only supported on Windows" << std::endl;
138 return -EOPNOTSUPP;
139 #else
140 std::vector<std::string> args;
141
142 args.push_back("map");
143 std::string img;
144 int r = get_image_or_snap_spec(vm, &img);
145 if (r < 0) {
146 return r;
147 }
148 args.push_back(img);
149
150 if (vm["read-only"].as<bool>()) {
151 args.push_back("--read-only");
152 }
153
154 if (vm["exclusive"].as<bool>()) {
155 args.push_back("--exclusive");
156 }
157
158 if (vm.count("options")) {
159 r = parse_options(vm["options"].as<std::vector<std::string>>(), &args);
160 if (r < 0) {
161 return r;
162 }
163 }
164
165 return call_wnbd_cmd(vm, args, ceph_global_init_args);
166 #endif
167 }
168
169 int execute_unmap(const po::variables_map &vm,
170 const std::vector<std::string> &ceph_global_init_args) {
171 #if !defined(_WIN32)
172 std::cerr << "rbd: wnbd is only supported on Windows" << std::endl;
173 return -EOPNOTSUPP;
174 #else
175 std::string device_name = utils::get_positional_argument(vm, 0);
176
177 std::string image_name;
178 if (device_name.empty()) {
179 int r = get_image_or_snap_spec(vm, &image_name);
180 if (r < 0) {
181 return r;
182 }
183 }
184
185 if (device_name.empty() && image_name.empty()) {
186 std::cerr << "rbd: unmap requires either image name or device path"
187 << std::endl;
188 return -EINVAL;
189 }
190
191 std::vector<std::string> args;
192
193 args.push_back("unmap");
194 args.push_back(device_name.empty() ? image_name : device_name);
195
196 if (vm.count("options")) {
197 int r = parse_options(vm["options"].as<std::vector<std::string>>(), &args);
198 if (r < 0) {
199 return r;
200 }
201 }
202
203 return call_wnbd_cmd(vm, args, ceph_global_init_args);
204 #endif
205 }
206
207 int execute_attach(const po::variables_map &vm,
208 const std::vector<std::string> &ceph_global_init_args) {
209 #if !defined(_WIN32)
210 std::cerr << "rbd: wnbd is only supported on Windows" << std::endl;
211 #else
212 std::cerr << "rbd: wnbd attach command not supported" << std::endl;
213 #endif
214 return -EOPNOTSUPP;
215 }
216
217 int execute_detach(const po::variables_map &vm,
218 const std::vector<std::string> &ceph_global_init_args) {
219 #if !defined(_WIN32)
220 std::cerr << "rbd: wnbd is only supported on Windows" << std::endl;
221 #else
222 std::cerr << "rbd: wnbd detach command not supported" << std::endl;
223 #endif
224 return -EOPNOTSUPP;
225 }
226
227 } // namespace wnbd
228 } // namespace action
229 } // namespace rbd