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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "include/int_types.h"
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <iostream>
#include <memory>
#include <boost/algorithm/string/predicate.hpp>
#include <regex>
#include "common/Formatter.h"
#include "common/Preforker.h"
#include "common/TextTable.h"
#include "common/ceph_argparse.h"
#include "common/config_proxy.h"
#include "common/debug.h"
#include "common/errno.h"
#include "global/global_init.h"
#include "global/signal_handler.h"
#include "include/rados/librados.hpp"
#include "include/rbd/librbd.hpp"
#include "include/stringify.h"
#include "Driver.h"
#include "Server.h"
#include "Watcher.h"
#define dout_context g_ceph_context
#define dout_subsys ceph_subsys_rbd
#undef dout_prefix
#define dout_prefix *_dout << "rbd-ggate: " << __func__ << ": "
static void usage() {
std::cout << "Usage: rbd-ggate [options] map <image-or-snap-spec> Map an image to ggate device\n"
<< " unmap <device path> Unmap ggate device\n"
<< " list List mapped ggate devices\n"
<< "\n"
<< "Map options:\n"
<< " --device <device path> Specify ggate device path\n"
<< " --read-only Map readonly\n"
<< " --exclusive Forbid writes by other clients\n"
<< "\n"
<< "List options:\n"
<< " --format plain|json|xml Output format (default: plain)\n"
<< " --pretty-format Pretty formatting (json and xml)\n"
<< std::endl;
generic_server_usage();
}
static std::string devpath, poolname, nsname, imgname, snapname;
static bool readonly = false;
static bool exclusive = false;
static std::unique_ptr<rbd::ggate::Driver> drv;
static void handle_signal(int signum)
{
derr << "*** Got signal " << sig_str(signum) << " ***" << dendl;
ceph_assert(signum == SIGINT || signum == SIGTERM);
ceph_assert(drv);
drv->shut_down();
}
static int do_map(int argc, const char *argv[])
{
int r;
librados::Rados rados;
librbd::RBD rbd;
librados::IoCtx io_ctx;
librbd::Image image;
librbd::image_info_t info;
std::string desc;
Preforker forker;
vector<const char*> args;
argv_to_vec(argc, argv, args);
auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
CODE_ENVIRONMENT_DAEMON,
CINIT_FLAG_UNPRIVILEGED_DAEMON_DEFAULTS);
g_ceph_context->_conf.set_val_or_die("pid_file", "");
if (global_init_prefork(g_ceph_context) >= 0) {
std::string err;
r = forker.prefork(err);
if (r < 0) {
std::cerr << err << std::endl;
return r;
}
if (forker.is_parent()) {
if (forker.parent_wait(err) != 0) {
return -ENXIO;
}
return 0;
}
global_init_postfork_start(g_ceph_context);
}
common_init_finish(g_ceph_context);
global_init_chdir(g_ceph_context);
if (poolname.empty()) {
poolname = g_ceph_context->_conf.get_val<std::string>("rbd_default_pool");
}
std::string devname = boost::starts_with(devpath, "/dev/") ?
devpath.substr(5) : devpath;
std::unique_ptr<rbd::ggate::Watcher> watcher;
uint64_t handle;
r = rados.init_with_context(g_ceph_context);
if (r < 0) {
goto done;
}
r = rados.connect();
if (r < 0) {
std::cerr << "rbd-ggate: failed to connect to cluster: " << cpp_strerror(r)
<< std::endl;
goto done;
}
r = rados.ioctx_create(poolname.c_str(), io_ctx);
if (r < 0) {
std::cerr << "rbd-ggate: failed to acces pool " << poolname << ": "
<< cpp_strerror(r) << std::endl;
goto done;
}
io_ctx.set_namespace(nsname);
r = rbd.open(io_ctx, image, imgname.c_str());
if (r < 0) {
std::cerr << "rbd-ggate: failed to open image " << imgname << ": "
<< cpp_strerror(r) << std::endl;
goto done;
}
if (exclusive) {
r = image.lock_acquire(RBD_LOCK_MODE_EXCLUSIVE);
if (r < 0) {
std::cerr << "rbd-ggate: failed to acquire exclusive lock: "
<< cpp_strerror(r) << std::endl;
goto done;
}
}
desc = "RBD " + poolname + "/" + (nsname.empty() ? "" : nsname + "/") +
imgname;
if (!snapname.empty()) {
r = image.snap_set(snapname.c_str());
if (r < 0) {
std::cerr << "rbd-ggate: failed to set snapshot " << snapname << ": "
<< cpp_strerror(r) << std::endl;
goto done;
}
readonly = true;
desc += "@" + snapname;
}
r = image.stat(info, sizeof(info));
if (r < 0) {
std::cerr << "rbd-ggate: image stat failed: " << cpp_strerror(r)
<< std::endl;
goto done;
}
rbd::ggate::Driver::load();
drv.reset(new rbd::ggate::Driver(devname, 512, info.size, readonly, desc));
r = drv->init();
if (r < 0) {
r = -errno;
std::cerr << "rbd-ggate: failed to create ggate device: " << cpp_strerror(r)
<< std::endl;
goto done;
}
watcher.reset(new rbd::ggate::Watcher(drv.get(), io_ctx, image, info.size));
r = image.update_watch(watcher.get(), &handle);
if (r < 0) {
std::cerr << "rbd-ggate: failed to set watcher: " << cpp_strerror(r)
<< std::endl;
drv->shut_down();
goto done;
}
std::cout << "/dev/" << drv->get_devname() << std::endl;
if (g_conf()->daemonize) {
global_init_postfork_finish(g_ceph_context);
forker.daemonize();
}
init_async_signal_handler();
register_async_signal_handler(SIGHUP, sighup_handler);
register_async_signal_handler_oneshot(SIGINT, handle_signal);
register_async_signal_handler_oneshot(SIGTERM, handle_signal);
rbd::ggate::Server(drv.get(), image).run();
unregister_async_signal_handler(SIGHUP, sighup_handler);
unregister_async_signal_handler(SIGINT, handle_signal);
unregister_async_signal_handler(SIGTERM, handle_signal);
shutdown_async_signal_handler();
r = image.update_unwatch(handle);
ceph_assert(r == 0);
done:
image.close();
io_ctx.close();
rados.shutdown();
if (r < 0) {
std::cerr << "rbd-ggate: failed to map: " << cpp_strerror(r) << std::endl;
}
forker.exit(r < 0 ? EXIT_FAILURE : 0);
// Unreachable;
return r;
}
static int do_unmap()
{
std::string devname = boost::starts_with(devpath, "/dev/") ?
devpath.substr(5) : devpath;
int r = rbd::ggate::Driver::kill(devname);
if (r < 0) {
cerr << "rbd-ggate: failed to destroy " << devname << ": "
<< cpp_strerror(r) << std::endl;
return r;
}
return 0;
}
static int parse_imgpath(const std::string &imgpath, std::string *poolname,
std::string *nsname, std::string *imgname,
std::string *snapname) {
std::regex pattern("^(?:([^/]+)/(?:([^/@]+)/)?)?([^@]+)(?:@([^/@]+))?$");
std::smatch match;
if (!std::regex_match(imgpath, match, pattern)) {
std::cerr << "rbd-ggate: invalid spec '" << imgpath << "'" << std::endl;
return -EINVAL;
}
if (match[1].matched) {
*poolname = match[1];
}
if (match[2].matched) {
*nsname = match[2];
}
*imgname = match[3];
if (match[4].matched) {
*snapname = match[4];
}
return 0;
}
static bool find_mapped_dev_by_spec(const std::string &spec,
std::string *devname) {
std::string poolname, nsname, imgname, snapname;
int r = parse_imgpath(spec, &poolname, &nsname, &imgname, &snapname);
if (r < 0) {
return false;
}
if (poolname.empty()) {
// We could use rbd_default_pool config to set pool name but then
// we would need to initialize the global context. So right now it
// is mandatory for the user to specify a pool. Fortunately the
// preferred way for users to call rbd-ggate is via rbd, which
// cares to set the pool name.
return false;
}
std::map<std::string, rbd::ggate::Driver::DevInfo> devs;
r = rbd::ggate::Driver::list(&devs);
if (r < 0) {
return false;
}
for (auto &it : devs) {
auto &name = it.second.first;
auto &info = it.second.second;
if (!boost::starts_with(info, "RBD ")) {
continue;
}
std::string p, n, i, s;
parse_imgpath(info.substr(4), &p, &n, &i, &s);
if (p == poolname && n == nsname && i == imgname && s == snapname) {
*devname = name;
return true;
}
}
return false;
}
static int do_list(const std::string &format, bool pretty_format)
{
rbd::ggate::Driver::load();
std::map<std::string, rbd::ggate::Driver::DevInfo> devs;
int r = rbd::ggate::Driver::list(&devs);
if (r < 0) {
return -r;
}
std::unique_ptr<ceph::Formatter> f;
TextTable tbl;
if (format == "json") {
f.reset(new JSONFormatter(pretty_format));
} else if (format == "xml") {
f.reset(new XMLFormatter(pretty_format));
} else if (!format.empty() && format != "plain") {
std::cerr << "rbd-ggate: invalid output format: " << format << std::endl;
return -EINVAL;
}
if (f) {
f->open_array_section("devices");
} else {
tbl.define_column("id", TextTable::LEFT, TextTable::LEFT);
tbl.define_column("pool", TextTable::LEFT, TextTable::LEFT);
tbl.define_column("namespace", TextTable::LEFT, TextTable::LEFT);
tbl.define_column("image", TextTable::LEFT, TextTable::LEFT);
tbl.define_column("snap", TextTable::LEFT, TextTable::LEFT);
tbl.define_column("device", TextTable::LEFT, TextTable::LEFT);
}
int count = 0;
for (auto &it : devs) {
auto &id = it.first;
auto &name = it.second.first;
auto &info = it.second.second;
if (!boost::starts_with(info, "RBD ")) {
continue;
}
std::string poolname;
std::string nsname;
std::string imgname;
std::string snapname(f ? "" : "-");
parse_imgpath(info.substr(4), &poolname, &nsname, &imgname, &snapname);
if (f) {
f->open_object_section("device");
f->dump_string("id", id);
f->dump_string("pool", poolname);
f->dump_string("namespace", nsname);
f->dump_string("image", imgname);
f->dump_string("snap", snapname);
f->dump_string("device", "/dev/" + name);
f->close_section();
} else {
tbl << id << poolname << nsname << imgname << snapname << "/dev/" + name
<< TextTable::endrow;
}
count++;
}
if (f) {
f->close_section(); // devices
f->flush(std::cout);
} else if (count > 0) {
std::cout << tbl;
}
return 0;
}
int main(int argc, const char *argv[]) {
int r;
enum {
None,
Connect,
Disconnect,
List
} cmd = None;
vector<const char*> args;
argv_to_vec(argc, argv, args);
if (args.empty()) {
cerr << argv[0] << ": -h or --help for usage" << std::endl;
exit(1);
}
if (ceph_argparse_need_usage(args)) {
usage();
exit(0);
}
// filter out ceph config options
ConfigProxy{false}.parse_argv(args);
std::string format;
bool pretty_format = false;
std::vector<const char*>::iterator i;
for (i = args.begin(); i != args.end(); ) {
if (ceph_argparse_flag(args, i, "-h", "--help", (char*)NULL)) {
usage();
return 0;
} else if (ceph_argparse_witharg(args, i, &devpath, "--device",
(char *)NULL)) {
} else if (ceph_argparse_flag(args, i, "--read-only", (char *)NULL)) {
readonly = true;
} else if (ceph_argparse_flag(args, i, "--exclusive", (char *)NULL)) {
exclusive = true;
} else if (ceph_argparse_witharg(args, i, &format, "--format",
(char *)NULL)) {
} else if (ceph_argparse_flag(args, i, "--pretty-format", (char *)NULL)) {
pretty_format = true;
} else {
++i;
}
}
if (args.begin() != args.end()) {
if (strcmp(*args.begin(), "map") == 0) {
cmd = Connect;
} else if (strcmp(*args.begin(), "unmap") == 0) {
cmd = Disconnect;
} else if (strcmp(*args.begin(), "list") == 0) {
cmd = List;
} else {
cerr << "rbd-ggate: unknown command: " << *args.begin() << std::endl;
return EXIT_FAILURE;
}
args.erase(args.begin());
}
if (cmd == None) {
cerr << "rbd-ggate: must specify command" << std::endl;
return EXIT_FAILURE;
}
switch (cmd) {
case Connect:
if (args.begin() == args.end()) {
cerr << "rbd-ggate: must specify image-or-snap-spec" << std::endl;
return EXIT_FAILURE;
}
if (parse_imgpath(*args.begin(), &poolname, &nsname, &imgname,
&snapname) < 0) {
return EXIT_FAILURE;
}
args.erase(args.begin());
break;
case Disconnect:
if (args.begin() == args.end()) {
std::cerr << "rbd-ggate: must specify ggate device or image-or-snap-spec"
<< std::endl;
return EXIT_FAILURE;
}
if (boost::starts_with(*args.begin(), "/dev/") ||
!find_mapped_dev_by_spec(*args.begin(), &devpath)) {
devpath = *args.begin();
}
args.erase(args.begin());
break;
default:
break;
}
if (args.begin() != args.end()) {
cerr << "rbd-ggate: unknown args: " << *args.begin() << std::endl;
return EXIT_FAILURE;
}
switch (cmd) {
case Connect:
if (imgname.empty()) {
cerr << "rbd-ggate: image name was not specified" << std::endl;
return EXIT_FAILURE;
}
r = do_map(argc, argv);
if (r < 0)
return EXIT_FAILURE;
break;
case Disconnect:
r = do_unmap();
if (r < 0)
return EXIT_FAILURE;
break;
case List:
r = do_list(format, pretty_format);
if (r < 0)
return EXIT_FAILURE;
break;
default:
usage();
return EXIT_FAILURE;
}
return 0;
}
|