summaryrefslogtreecommitdiffstats
path: root/src/tools/rbd/action/Device.cc
blob: 878081438377c651dee24442b17ab7ac4d433eb3 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "acconfig.h"
#include "tools/rbd/ArgumentTypes.h"
#include "tools/rbd/Shell.h"

#include <boost/program_options.hpp>

#include "include/ceph_assert.h"

namespace rbd {
namespace action {

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

#define DECLARE_DEVICE_OPERATIONS(ns)                                   \
  namespace ns {                                                        \
  int execute_list(const po::variables_map &vm,                         \
                   const std::vector<std::string> &ceph_global_args);   \
  int execute_map(const po::variables_map &vm,                          \
                  const std::vector<std::string> &ceph_global_args);    \
  int execute_unmap(const po::variables_map &vm,                        \
                    const std::vector<std::string> &ceph_global_args);  \
  int execute_attach(const po::variables_map &vm,                       \
                     const std::vector<std::string> &ceph_global_args); \
  int execute_detach(const po::variables_map &vm,                       \
                     const std::vector<std::string> &ceph_global_args); \
  }

DECLARE_DEVICE_OPERATIONS(ggate);
DECLARE_DEVICE_OPERATIONS(kernel);
DECLARE_DEVICE_OPERATIONS(nbd);
DECLARE_DEVICE_OPERATIONS(wnbd);

namespace device {

namespace {

struct DeviceOperations {
  int (*execute_list)(const po::variables_map &vm,
                      const std::vector<std::string> &ceph_global_args);
  int (*execute_map)(const po::variables_map &vm,
                     const std::vector<std::string> &ceph_global_args);
  int (*execute_unmap)(const po::variables_map &vm,
                       const std::vector<std::string> &ceph_global_args);
  int (*execute_attach)(const po::variables_map &vm,
                        const std::vector<std::string> &ceph_global_args);
  int (*execute_detach)(const po::variables_map &vm,
                        const std::vector<std::string> &ceph_global_args);
};

const DeviceOperations ggate_operations = {
  ggate::execute_list,
  ggate::execute_map,
  ggate::execute_unmap,
  ggate::execute_attach,
  ggate::execute_detach,
};

const DeviceOperations krbd_operations = {
  kernel::execute_list,
  kernel::execute_map,
  kernel::execute_unmap,
  kernel::execute_attach,
  kernel::execute_detach,
};

const DeviceOperations nbd_operations = {
  nbd::execute_list,
  nbd::execute_map,
  nbd::execute_unmap,
  nbd::execute_attach,
  nbd::execute_detach,
};

const DeviceOperations wnbd_operations = {
  wnbd::execute_list,
  wnbd::execute_map,
  wnbd::execute_unmap,
  wnbd::execute_attach,
  wnbd::execute_detach,
};

enum device_type_t {
  DEVICE_TYPE_GGATE,
  DEVICE_TYPE_KRBD,
  DEVICE_TYPE_NBD,
  DEVICE_TYPE_WNBD,
};

struct DeviceType {};

void validate(boost::any& v, const std::vector<std::string>& values,
              DeviceType *target_type, int) {
  po::validators::check_first_occurrence(v);
  const std::string &s = po::validators::get_single_string(values);

  #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);
  #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) {
  options->add_options()
    ("options,o", po::value<std::vector<std::string>>(),
     "device specific options");
}

device_type_t get_device_type(const po::variables_map &vm) {
  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) {
  switch (get_device_type(vm)) {
  case DEVICE_TYPE_GGATE:
    return &ggate_operations;
  case DEVICE_TYPE_KRBD:
    return &krbd_operations;
  case DEVICE_TYPE_NBD:
    return &nbd_operations;
  case DEVICE_TYPE_WNBD:
    return &wnbd_operations;
  default:
    ceph_abort();
    return nullptr;
  }
}

} // anonymous namespace

void get_list_arguments(po::options_description *positional,
                        po::options_description *options) {
  add_device_type_option(options);
  at::add_format_options(options);
}

int execute_list(const po::variables_map &vm,
                 const std::vector<std::string> &ceph_global_init_args) {
  return (*get_device_operations(vm)->execute_list)(vm, ceph_global_init_args);
}

void get_map_arguments(po::options_description *positional,
                       po::options_description *options) {
  add_device_type_option(options);
  at::add_image_or_snap_spec_options(positional, options,
                                     at::ARGUMENT_MODIFIER_NONE);
  options->add_options()
    ("show-cookie", po::bool_switch(), "show device cookie")
    ("cookie", po::value<std::string>(), "specify device cookie")
    ("read-only", po::bool_switch(), "map read-only")
    ("exclusive", po::bool_switch(), "disable automatic exclusive lock transitions")
    ("quiesce", po::bool_switch(), "use quiesce hooks")
    ("quiesce-hook", po::value<std::string>(), "quiesce hook path");
  at::add_snap_id_option(options);
  add_device_specific_options(options);
}

int execute_map(const po::variables_map &vm,
                const std::vector<std::string> &ceph_global_init_args) {
  return (*get_device_operations(vm)->execute_map)(vm, ceph_global_init_args);
}

void get_unmap_arguments(po::options_description *positional,
                         po::options_description *options) {
  add_device_type_option(options);
  positional->add_options()
    ("image-or-snap-or-device-spec",
     "image, snapshot, or device specification\n"
     "[<pool-name>/[<namespace>/]]<image-name>[@<snap-name>] or <device-path>");
  at::add_pool_option(options, at::ARGUMENT_MODIFIER_NONE);
  at::add_namespace_option(options, at::ARGUMENT_MODIFIER_NONE);
  at::add_image_option(options, at::ARGUMENT_MODIFIER_NONE);
  at::add_snap_option(options, at::ARGUMENT_MODIFIER_NONE);
  at::add_snap_id_option(options);
  add_device_specific_options(options);
}

int execute_unmap(const po::variables_map &vm,
                  const std::vector<std::string> &ceph_global_init_args) {
  return (*get_device_operations(vm)->execute_unmap)(vm, ceph_global_init_args);
}

void get_attach_arguments(po::options_description *positional,
                          po::options_description *options) {
  add_device_type_option(options);
  at::add_image_or_snap_spec_options(positional, options,
                                     at::ARGUMENT_MODIFIER_NONE);
  options->add_options()
    ("device", po::value<std::string>()->required(), "specify device path")
    ("show-cookie", po::bool_switch(), "show device cookie")
    ("cookie", po::value<std::string>(), "specify device cookie")
    ("read-only", po::bool_switch(), "attach read-only")
    ("force", po::bool_switch(), "force attach")
    ("exclusive", po::bool_switch(), "disable automatic exclusive lock transitions")
    ("quiesce", po::bool_switch(), "use quiesce hooks")
    ("quiesce-hook", po::value<std::string>(), "quiesce hook path");
  at::add_snap_id_option(options);
  add_device_specific_options(options);
}

int execute_attach(const po::variables_map &vm,
                   const std::vector<std::string> &ceph_global_init_args) {
  return (*get_device_operations(vm)->execute_attach)(vm, ceph_global_init_args);
}

void get_detach_arguments(po::options_description *positional,
                          po::options_description *options) {
  add_device_type_option(options);
  positional->add_options()
    ("image-or-snap-or-device-spec",
     "image, snapshot, or device specification\n"
     "[<pool-name>/[<namespace>/]]<image-name>[@<snap-name>] or <device-path>");
  at::add_pool_option(options, at::ARGUMENT_MODIFIER_NONE);
  at::add_namespace_option(options, at::ARGUMENT_MODIFIER_NONE);
  at::add_image_option(options, at::ARGUMENT_MODIFIER_NONE);
  at::add_snap_option(options, at::ARGUMENT_MODIFIER_NONE);
  at::add_snap_id_option(options);
  add_device_specific_options(options);
}

int execute_detach(const po::variables_map &vm,
                   const std::vector<std::string> &ceph_global_init_args) {
  return (*get_device_operations(vm)->execute_detach)(vm, ceph_global_init_args);
}

Shell::SwitchArguments switched_arguments({"exclusive", "force", "quiesce",
                                           "read-only", "show-cookie"});

Shell::Action action_list(
  {"device", "list"}, {"showmapped"}, "List mapped rbd images.", "",
  &get_list_arguments, &execute_list);
// yet another alias for list command
Shell::Action action_ls(
  {"device", "ls"}, {}, "List mapped rbd images.", "",
  &get_list_arguments, &execute_list, false);

Shell::Action action_map(
  {"device", "map"}, {"map"}, "Map an image to a block device.", "",
  &get_map_arguments, &execute_map);

Shell::Action action_unmap(
  {"device", "unmap"}, {"unmap"}, "Unmap a rbd device.", "",
  &get_unmap_arguments, &execute_unmap);

Shell::Action action_attach(
  {"device", "attach"}, {}, "Attach image to device.", "",
  &get_attach_arguments, &execute_attach);

Shell::Action action_detach(
  {"device", "detach"}, {}, "Detach image from device.", "",
  &get_detach_arguments, &execute_detach);

} // namespace device
} // namespace action
} // namespace rbd