DECLARE_DEVICE_OPERATIONS(ggate);
DECLARE_DEVICE_OPERATIONS(kernel);
DECLARE_DEVICE_OPERATIONS(nbd);
+DECLARE_DEVICE_OPERATIONS(wnbd);
namespace device {
nbd::execute_unmap,
};
+const DeviceOperations wnbd_operations = {
+ wnbd::execute_list,
+ wnbd::execute_map,
+ wnbd::execute_unmap,
+};
+
enum device_type_t {
DEVICE_TYPE_GGATE,
DEVICE_TYPE_KRBD,
DEVICE_TYPE_NBD,
+ DEVICE_TYPE_WNBD,
};
struct DeviceType {};
DeviceType *target_type, int) {
po::validators::check_first_occurrence(v);
const std::string &s = po::validators::get_single_string(values);
- if (s == "ggate") {
+
+ #ifdef _WIN32
+ if (s == "wnbd") {
+ v = boost::any(DEVICE_TYPE_WNBD);
+ #else
+ if (s == "nbd") {
+ v = boost::any(DEVICE_TYPE_NBD);
+ } else if (s == "ggate") {
v = boost::any(DEVICE_TYPE_GGATE);
} else if (s == "krbd") {
v = boost::any(DEVICE_TYPE_KRBD);
- } else if (s == "nbd") {
- v = boost::any(DEVICE_TYPE_NBD);
+ #endif /* _WIN32 */
} else {
throw po::validation_error(po::validation_error::invalid_option_value);
}
void add_device_type_option(po::options_description *options) {
options->add_options()
("device-type,t", po::value<DeviceType>(),
+#ifdef _WIN32
+ "device type [wnbd]");
+#else
"device type [ggate, krbd (default), nbd]");
+#endif
}
void add_device_specific_options(po::options_description *options) {
if (vm.count("device-type")) {
return vm["device-type"].as<device_type_t>();
}
+ #ifndef _WIN32
return DEVICE_TYPE_KRBD;
+ #else
+ return DEVICE_TYPE_WNBD;
+ #endif
}
const DeviceOperations *get_device_operations(const po::variables_map &vm) {
return &krbd_operations;
case DEVICE_TYPE_NBD:
return &nbd_operations;
+ case DEVICE_TYPE_WNBD:
+ return &wnbd_operations;
default:
ceph_abort();
return nullptr;
--- /dev/null
+// -*- 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;
+
+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;
+ } else if (process.join()) {
+ std::cerr << "rbd: rbd-wnbd failed with error: " << process.err() << std::endl;
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+int get_image_or_snap_spec(const po::variables_map &vm, std::string *spec) {
+ size_t arg_index = 0;
+ std::string pool_name;
+ std::string nspace_name;
+ std::string image_name;
+ std::string snap_name;
+ int r = utils::get_pool_image_snapshot_names(
+ vm, at::ARGUMENT_MODIFIER_NONE, &arg_index, &pool_name, &nspace_name,
+ &image_name, &snap_name, true, utils::SNAPSHOT_PRESENCE_PERMITTED,
+ utils::SPEC_VALIDATION_NONE);
+ if (r < 0) {
+ return r;
+ }
+
+ if (!pool_name.empty()) {
+ spec->append(pool_name);
+ spec->append("/");
+ }
+ if (!nspace_name.empty()) {
+ spec->append(nspace_name);
+ spec->append("/");
+ }
+ spec->append(image_name);
+ if (!snap_name.empty()) {
+ spec->append("@");
+ spec->append(snap_name);
+ }
+
+ return 0;
+}
+
+int parse_options(const std::vector<std::string> &options,
+ std::vector<std::string> *args) {
+ for (auto &opts : options) {
+ std::vector<std::string> args_;
+ boost::split(args_, opts, boost::is_any_of(","));
+ for (auto &o : args_) {
+ args->push_back("--" + o);
+ }
+ }
+
+ return 0;
+}
+
+int execute_list(const po::variables_map &vm,
+ const std::vector<std::string> &ceph_global_init_args) {
+ 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);
+}
+
+int execute_map(const po::variables_map &vm,
+ const std::vector<std::string> &ceph_global_init_args) {
+ std::vector<std::string> args;
+
+ args.push_back("map");
+ std::string img;
+ int r = 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")) {
+ r = parse_options(vm["options"].as<std::vector<std::string>>(), &args);
+ if (r < 0) {
+ return r;
+ }
+ }
+
+ return call_wnbd_cmd(vm, args, ceph_global_init_args);
+}
+
+int execute_unmap(const po::variables_map &vm,
+ const std::vector<std::string> &ceph_global_init_args) {
+ std::string device_name = utils::get_positional_argument(vm, 0);
+
+ std::string image_name;
+ if (device_name.empty()) {
+ int r = get_image_or_snap_spec(vm, &image_name);
+ if (r < 0) {
+ return r;
+ }
+ }
+
+ if (device_name.empty() && image_name.empty()) {
+ std::cerr << "rbd: unmap requires either image name or device path"
+ << std::endl;
+ return -EINVAL;
+ }
+
+ std::vector<std::string> args;
+
+ args.push_back("unmap");
+ args.push_back(device_name.empty() ? image_name : device_name);
+
+ if (vm.count("options")) {
+ int r = parse_options(vm["options"].as<std::vector<std::string>>(), &args);
+ if (r < 0) {
+ return r;
+ }
+ }
+
+ return call_wnbd_cmd(vm, args, ceph_global_init_args);
+}
+
+Shell::SwitchArguments switched_arguments({"read-only", "exclusive"});
+
+} // namespace wnbd
+} // namespace action
+} // namespace rbd