]> git.proxmox.com Git - ceph.git/blob - ceph/src/tools/rbd/action/Wnbd.cc
bump version to 18.2.2-pve1
[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 #endif
67
68 int execute_list(const po::variables_map &vm,
69 const std::vector<std::string> &ceph_global_init_args) {
70 #if !defined(_WIN32)
71 std::cerr << "rbd: wnbd is only supported on Windows" << std::endl;
72 return -EOPNOTSUPP;
73 #else
74 std::vector<std::string> args;
75
76 args.push_back("list");
77
78 if (vm.count("format")) {
79 args.push_back("--format");
80 args.push_back(vm["format"].as<at::Format>().value);
81 }
82 if (vm["pretty-format"].as<bool>()) {
83 args.push_back("--pretty-format");
84 }
85
86 return call_wnbd_cmd(vm, args, ceph_global_init_args);
87 #endif
88 }
89
90 int execute_map(const po::variables_map &vm,
91 const std::vector<std::string> &ceph_global_init_args) {
92 #if !defined(_WIN32)
93 std::cerr << "rbd: wnbd is only supported on Windows" << std::endl;
94 return -EOPNOTSUPP;
95 #else
96 std::vector<std::string> args;
97
98 args.push_back("map");
99 std::string img;
100 int r = utils::get_image_or_snap_spec(vm, &img);
101 if (r < 0) {
102 return r;
103 }
104 args.push_back(img);
105
106 if (vm["read-only"].as<bool>()) {
107 args.push_back("--read-only");
108 }
109
110 if (vm["exclusive"].as<bool>()) {
111 args.push_back("--exclusive");
112 }
113
114 if (vm.count("options")) {
115 utils::append_options_as_args(vm["options"].as<std::vector<std::string>>(),
116 &args);
117 }
118
119 return call_wnbd_cmd(vm, args, ceph_global_init_args);
120 #endif
121 }
122
123 int execute_unmap(const po::variables_map &vm,
124 const std::vector<std::string> &ceph_global_init_args) {
125 #if !defined(_WIN32)
126 std::cerr << "rbd: wnbd is only supported on Windows" << std::endl;
127 return -EOPNOTSUPP;
128 #else
129 std::string image_name;
130
131 int r = utils::get_image_or_snap_spec(vm, &image_name);
132 if (r < 0) {
133 return r;
134 }
135
136 std::vector<std::string> args;
137
138 args.push_back("unmap");
139 args.push_back(image_name);
140
141 if (vm.count("options")) {
142 utils::append_options_as_args(vm["options"].as<std::vector<std::string>>(),
143 &args);
144 }
145
146 return call_wnbd_cmd(vm, args, ceph_global_init_args);
147 #endif
148 }
149
150 int execute_attach(const po::variables_map &vm,
151 const std::vector<std::string> &ceph_global_init_args) {
152 #if !defined(_WIN32)
153 std::cerr << "rbd: wnbd is only supported on Windows" << std::endl;
154 #else
155 std::cerr << "rbd: wnbd attach command not supported" << std::endl;
156 #endif
157 return -EOPNOTSUPP;
158 }
159
160 int execute_detach(const po::variables_map &vm,
161 const std::vector<std::string> &ceph_global_init_args) {
162 #if !defined(_WIN32)
163 std::cerr << "rbd: wnbd is only supported on Windows" << std::endl;
164 #else
165 std::cerr << "rbd: wnbd detach command not supported" << std::endl;
166 #endif
167 return -EOPNOTSUPP;
168 }
169
170 } // namespace wnbd
171 } // namespace action
172 } // namespace rbd