summaryrefslogtreecommitdiffstats
path: root/src/tools/rbd/action/Wnbd.cc
blob: 85d2c7057bfab6db4ce404206eb3372c5a7bcf6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "tools/rbd/ArgumentTypes.h"
#include "tools/rbd/Shell.h"
#include "tools/rbd/Utils.h"
#include "include/stringify.h"
#include "common/SubProcess.h"
#include <iostream>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/program_options.hpp>

namespace rbd {
namespace action {
namespace wnbd {

namespace at = argument_types;
namespace po = boost::program_options;

#if defined(_WIN32)
static int call_wnbd_cmd(const po::variables_map &vm,
                        const std::vector<std::string> &args,
                        const std::vector<std::string> &ceph_global_init_args) {
  char exe_path[PATH_MAX];
  ssize_t exe_path_bytes = get_self_exe_path(exe_path, PATH_MAX);

  if (exe_path_bytes > 4) {
    // Drop .exe suffix as we're going to add the "-wnbd" suffix.
    exe_path[strlen(exe_path) - 4] = '\0';
    exe_path_bytes -= 4;
  }

  if (exe_path_bytes < 0) {
    strcpy(exe_path, "rbd-wnbd");
  } else {
    if (snprintf(exe_path + exe_path_bytes,
                 sizeof(exe_path) - exe_path_bytes,
                 "-wnbd") < 0) {
      return -EOVERFLOW;
    }
  }

  SubProcess process(exe_path, SubProcess::KEEP, SubProcess::KEEP, SubProcess::KEEP);

  for (auto &arg : ceph_global_init_args) {
    process.add_cmd_arg(arg.c_str());
  }

  for (auto &arg : args) {
    process.add_cmd_arg(arg.c_str());
  }

  if (process.spawn()) {
    std::cerr << "rbd: failed to run rbd-wnbd: " << process.err() << std::endl;
    return -EINVAL;
  }
  int exit_code = process.join();
  if (exit_code) {
    std::cerr << "rbd: rbd-wnbd failed with error: " << process.err() << std::endl;
    return exit_code;
  }

  return 0;
}
#endif

int execute_list(const po::variables_map &vm,
                 const std::vector<std::string> &ceph_global_init_args) {
#if !defined(_WIN32)
  std::cerr << "rbd: wnbd is only supported on Windows" << std::endl;
  return -EOPNOTSUPP;
#else
  std::vector<std::string> args;

  args.push_back("list");

  if (vm.count("format")) {
    args.push_back("--format");
    args.push_back(vm["format"].as<at::Format>().value);
  }
  if (vm["pretty-format"].as<bool>()) {
    args.push_back("--pretty-format");
  }

  return call_wnbd_cmd(vm, args, ceph_global_init_args);
#endif
}

int execute_map(const po::variables_map &vm,
                const std::vector<std::string> &ceph_global_init_args) {
#if !defined(_WIN32)
  std::cerr << "rbd: wnbd is only supported on Windows" << std::endl;
  return -EOPNOTSUPP;
#else
  std::vector<std::string> args;

  args.push_back("map");
  std::string img;
  int r = utils::get_image_or_snap_spec(vm, &img);
  if (r < 0) {
    return r;
  }
  args.push_back(img);

  if (vm["read-only"].as<bool>()) {
    args.push_back("--read-only");
  }

  if (vm["exclusive"].as<bool>()) {
    args.push_back("--exclusive");
  }

  if (vm.count("options")) {
    utils::append_options_as_args(vm["options"].as<std::vector<std::string>>(),
                                  &args);
  }

  return call_wnbd_cmd(vm, args, ceph_global_init_args);
#endif
}

int execute_unmap(const po::variables_map &vm,
                  const std::vector<std::string> &ceph_global_init_args) {
#if !defined(_WIN32)
  std::cerr << "rbd: wnbd is only supported on Windows" << std::endl;
  return -EOPNOTSUPP;
#else
  std::string image_name;

  int r = utils::get_image_or_snap_spec(vm, &image_name);
  if (r < 0) {
    return r;
  }

  std::vector<std::string> args;

  args.push_back("unmap");
  args.push_back(image_name);

  if (vm.count("options")) {
    utils::append_options_as_args(vm["options"].as<std::vector<std::string>>(),
                                  &args);
  }

  return call_wnbd_cmd(vm, args, ceph_global_init_args);
#endif
}

int execute_attach(const po::variables_map &vm,
                   const std::vector<std::string> &ceph_global_init_args) {
#if !defined(_WIN32)
  std::cerr << "rbd: wnbd is only supported on Windows" << std::endl;
#else
  std::cerr << "rbd: wnbd attach command not supported" << std::endl;
#endif
  return -EOPNOTSUPP;
}

int execute_detach(const po::variables_map &vm,
                   const std::vector<std::string> &ceph_global_init_args) {
#if !defined(_WIN32)
  std::cerr << "rbd: wnbd is only supported on Windows" << std::endl;
#else
  std::cerr << "rbd: wnbd detach command not supported" << std::endl;
#endif
  return -EOPNOTSUPP;
}

} // namespace wnbd
} // namespace action
} // namespace rbd