1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
5 * rbd-nbd - RBD in userspace
7 * Copyright (C) 2015 - 2016 Kylin Corporation
9 * Author: Yunchuan Wen <yunchuan.wen@kylin-cloud.com>
10 * Li Wang <li.wang@kylin-cloud.com>
12 * This is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License version 2.1, as published by the Free Software
15 * Foundation. See file COPYING.
19 #include "include/int_types.h"
27 #include <sys/types.h>
30 #include <linux/nbd.h>
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
39 #include <boost/algorithm/string/predicate.hpp>
41 #include "common/Formatter.h"
42 #include "common/Preforker.h"
43 #include "common/TextTable.h"
44 #include "common/ceph_argparse.h"
45 #include "common/config.h"
46 #include "common/dout.h"
47 #include "common/errno.h"
48 #include "common/module.h"
49 #include "common/safe_io.h"
50 #include "common/version.h"
52 #include "global/global_init.h"
53 #include "global/signal_handler.h"
55 #include "include/rados/librados.hpp"
56 #include "include/rbd/librbd.hpp"
57 #include "include/stringify.h"
58 #include "include/xlist.h"
60 #include "mon/MonClient.h"
62 #define dout_context g_ceph_context
63 #define dout_subsys ceph_subsys_rbd
65 #define dout_prefix *_dout << "rbd-nbd: "
72 bool exclusive
= false;
73 bool readonly
= false;
74 bool set_max_part
= false;
83 bool pretty_format
= false;
88 std::cout
<< "Usage: rbd-nbd [options] map <image-or-snap-spec> Map an image to nbd device\n"
89 << " unmap <device|image-or-snap-spec> Unmap nbd device\n"
90 << " [options] list-mapped List mapped nbd devices\n"
92 << " --device <device path> Specify nbd device path\n"
93 << " --read-only Map read-only\n"
94 << " --nbds_max <limit> Override for module param nbds_max\n"
95 << " --max_part <limit> Override for module param max_part\n"
96 << " --exclusive Forbid writes by other clients\n"
97 << " --timeout <seconds> Set nbd request timeout\n"
100 << " --format plain|json|xml Output format (default: plain)\n"
101 << " --pretty-format Pretty formatting (json and xml)\n"
103 generic_server_usage();
115 static Command cmd
= None
;
117 #define RBD_NBD_BLKSIZE 512UL
120 #define VERSION_INFO 2
122 #ifdef CEPH_BIG_ENDIAN
123 #define ntohll(a) (a)
124 #elif defined(CEPH_LITTLE_ENDIAN)
125 #define ntohll(a) swab(a)
127 #error "Could not determine endianess"
129 #define htonll(a) ntohll(a)
131 static int parse_args(vector
<const char*>& args
, std::ostream
*err_msg
,
132 Command
*command
, Config
*cfg
);
134 static void handle_signal(int signum
)
136 ceph_assert(signum
== SIGINT
|| signum
== SIGTERM
);
137 derr
<< "*** Got signal " << sig_str(signum
) << " ***" << dendl
;
138 dout(20) << __func__
<< ": " << "sending NBD_DISCONNECT" << dendl
;
139 if (ioctl(nbd
, NBD_DISCONNECT
) < 0) {
140 derr
<< "rbd-nbd: disconnect failed: " << cpp_strerror(errno
) << dendl
;
142 dout(20) << __func__
<< ": " << "disconnected" << dendl
;
150 librbd::Image
&image
;
153 NBDServer(int _fd
, librbd::Image
& _image
)
156 , lock("NBDServer::Locker")
157 , reader_thread(*this, &NBDServer::reader_entry
)
158 , writer_thread(*this, &NBDServer::writer_entry
)
163 std::atomic
<bool> terminated
= { false };
167 bool expected
= false;
168 if (terminated
.compare_exchange_strong(expected
, true)) {
169 ::shutdown(fd
, SHUT_RDWR
);
171 Mutex::Locker
l(lock
);
178 xlist
<IOContext
*>::item item
;
179 NBDServer
*server
= nullptr;
180 struct nbd_request request
;
181 struct nbd_reply reply
;
190 friend std::ostream
&operator<<(std::ostream
&os
, const IOContext
&ctx
);
194 xlist
<IOContext
*> io_pending
;
195 xlist
<IOContext
*> io_finished
;
197 void io_start(IOContext
*ctx
)
199 Mutex::Locker
l(lock
);
200 io_pending
.push_back(&ctx
->item
);
203 void io_finish(IOContext
*ctx
)
205 Mutex::Locker
l(lock
);
206 ceph_assert(ctx
->item
.is_on_list());
207 ctx
->item
.remove_myself();
208 io_finished
.push_back(&ctx
->item
);
212 IOContext
*wait_io_finish()
214 Mutex::Locker
l(lock
);
215 while(io_finished
.empty() && !terminated
)
218 if (io_finished
.empty())
221 IOContext
*ret
= io_finished
.front();
222 io_finished
.pop_front();
229 ceph_assert(!reader_thread
.is_started());
230 Mutex::Locker
l(lock
);
231 while(!io_pending
.empty())
234 while(!io_finished
.empty()) {
235 std::unique_ptr
<IOContext
> free_ctx(io_finished
.front());
236 io_finished
.pop_front();
240 static void aio_callback(librbd::completion_t cb
, void *arg
)
242 librbd::RBD::AioCompletion
*aio_completion
=
243 reinterpret_cast<librbd::RBD::AioCompletion
*>(cb
);
245 IOContext
*ctx
= reinterpret_cast<IOContext
*>(arg
);
246 int ret
= aio_completion
->get_return_value();
248 dout(20) << __func__
<< ": " << *ctx
<< dendl
;
250 if (ret
== -EINVAL
) {
251 // if shrinking an image, a pagecache writeback might reference
252 // extents outside of the range of the new image extents
253 dout(0) << __func__
<< ": masking IO out-of-bounds error" << dendl
;
259 ctx
->reply
.error
= htonl(-ret
);
260 } else if ((ctx
->command
== NBD_CMD_READ
) &&
261 ret
< static_cast<int>(ctx
->request
.len
)) {
262 int pad_byte_count
= static_cast<int> (ctx
->request
.len
) - ret
;
263 ctx
->data
.append_zero(pad_byte_count
);
264 dout(20) << __func__
<< ": " << *ctx
<< ": Pad byte count: "
265 << pad_byte_count
<< dendl
;
266 ctx
->reply
.error
= htonl(0);
268 ctx
->reply
.error
= htonl(0);
270 ctx
->server
->io_finish(ctx
);
272 aio_completion
->release();
277 while (!terminated
) {
278 std::unique_ptr
<IOContext
> ctx(new IOContext());
281 dout(20) << __func__
<< ": waiting for nbd request" << dendl
;
283 int r
= safe_read_exact(fd
, &ctx
->request
, sizeof(struct nbd_request
));
285 derr
<< "failed to read nbd request header: " << cpp_strerror(r
)
290 if (ctx
->request
.magic
!= htonl(NBD_REQUEST_MAGIC
)) {
291 derr
<< "invalid nbd request header" << dendl
;
295 ctx
->request
.from
= ntohll(ctx
->request
.from
);
296 ctx
->request
.type
= ntohl(ctx
->request
.type
);
297 ctx
->request
.len
= ntohl(ctx
->request
.len
);
299 ctx
->reply
.magic
= htonl(NBD_REPLY_MAGIC
);
300 memcpy(ctx
->reply
.handle
, ctx
->request
.handle
, sizeof(ctx
->reply
.handle
));
302 ctx
->command
= ctx
->request
.type
& 0x0000ffff;
304 dout(20) << *ctx
<< ": start" << dendl
;
306 switch (ctx
->command
)
309 // NBD_DO_IT will return when pipe is closed
310 dout(0) << "disconnect request received" << dendl
;
313 bufferptr
ptr(ctx
->request
.len
);
314 r
= safe_read_exact(fd
, ptr
.c_str(), ctx
->request
.len
);
316 derr
<< *ctx
<< ": failed to read nbd request data: "
317 << cpp_strerror(r
) << dendl
;
320 ctx
->data
.push_back(ptr
);
324 IOContext
*pctx
= ctx
.release();
326 librbd::RBD::AioCompletion
*c
= new librbd::RBD::AioCompletion(pctx
, aio_callback
);
327 switch (pctx
->command
)
330 image
.aio_write(pctx
->request
.from
, pctx
->request
.len
, pctx
->data
, c
);
333 image
.aio_read(pctx
->request
.from
, pctx
->request
.len
, pctx
->data
, c
);
339 image
.aio_discard(pctx
->request
.from
, pctx
->request
.len
, c
);
342 derr
<< *pctx
<< ": invalid request command" << dendl
;
347 dout(20) << __func__
<< ": terminated" << dendl
;
352 while (!terminated
) {
353 dout(20) << __func__
<< ": waiting for io request" << dendl
;
354 std::unique_ptr
<IOContext
> ctx(wait_io_finish());
356 dout(20) << __func__
<< ": no io requests, terminating" << dendl
;
360 dout(20) << __func__
<< ": got: " << *ctx
<< dendl
;
362 int r
= safe_write(fd
, &ctx
->reply
, sizeof(struct nbd_reply
));
364 derr
<< *ctx
<< ": failed to write reply header: " << cpp_strerror(r
)
368 if (ctx
->command
== NBD_CMD_READ
&& ctx
->reply
.error
== htonl(0)) {
369 r
= ctx
->data
.write_fd(fd
);
371 derr
<< *ctx
<< ": failed to write replay data: " << cpp_strerror(r
)
376 dout(20) << *ctx
<< ": finish" << dendl
;
378 dout(20) << __func__
<< ": terminated" << dendl
;
381 class ThreadHelper
: public Thread
384 typedef void (NBDServer::*entry_func
)();
389 ThreadHelper(NBDServer
&_server
, entry_func _func
)
394 void* entry() override
400 } reader_thread
, writer_thread
;
407 dout(10) << __func__
<< ": starting" << dendl
;
411 reader_thread
.create("rbd_reader");
412 writer_thread
.create("rbd_writer");
419 dout(10) << __func__
<< ": terminating" << dendl
;
423 reader_thread
.join();
424 writer_thread
.join();
433 std::ostream
&operator<<(std::ostream
&os
, const NBDServer::IOContext
&ctx
) {
435 os
<< "[" << std::hex
<< ntohll(*((uint64_t *)ctx
.request
.handle
));
452 os
<< " UNKNOWN(" << ctx
.command
<< ") ";
456 os
<< ctx
.request
.from
<< "~" << ctx
.request
.len
<< " "
457 << std::dec
<< ntohl(ctx
.reply
.error
) << "]";
462 class NBDWatchCtx
: public librbd::UpdateWatchCtx
466 librados::IoCtx
&io_ctx
;
467 librbd::Image
&image
;
471 librados::IoCtx
&_io_ctx
,
472 librbd::Image
&_image
,
480 ~NBDWatchCtx() override
{}
482 void handle_notify() override
484 librbd::image_info_t info
;
485 if (image
.stat(info
, sizeof(info
)) == 0) {
486 unsigned long new_size
= info
.size
;
488 if (new_size
!= size
) {
489 dout(5) << "resize detected" << dendl
;
490 if (ioctl(fd
, BLKFLSBUF
, NULL
) < 0)
491 derr
<< "invalidate page cache failed: " << cpp_strerror(errno
)
493 if (ioctl(fd
, NBD_SET_SIZE
, new_size
) < 0) {
494 derr
<< "resize failed: " << cpp_strerror(errno
) << dendl
;
498 if (ioctl(fd
, BLKRRPART
, NULL
) < 0) {
499 derr
<< "rescan of partition table failed: " << cpp_strerror(errno
)
502 if (image
.invalidate_cache() < 0)
503 derr
<< "invalidate rbd cache failed" << dendl
;
509 class NBDListIterator
{
511 bool get(int *pid
, Config
*cfg
) {
513 std::string nbd_path
= "/sys/block/nbd" + stringify(m_index
);
514 if(access(nbd_path
.c_str(), F_OK
) != 0) {
519 cfg
->devpath
= "/dev/nbd" + stringify(m_index
++);
522 ifs
.open(nbd_path
+ "/pid", std::ifstream::in
);
523 if (!ifs
.is_open()) {
528 int r
= get_mapped_info(*pid
, cfg
);
540 int get_mapped_info(int pid
, Config
*cfg
) {
542 std::string path
= "/proc/" + stringify(pid
) + "/cmdline";
545 std::vector
<const char*> args
;
547 ifs
.open(path
.c_str(), std::ifstream::in
);
552 for (unsigned i
= 0; i
< cmdline
.size(); i
++) {
553 const char *arg
= &cmdline
[i
];
555 if (strcmp(basename(arg
) , "rbd-nbd") != 0) {
562 while (cmdline
[i
] != '\0') {
567 std::ostringstream err_msg
;
569 r
= parse_args(args
, &err_msg
, &command
, cfg
);
574 if (command
!= Connect
) {
582 static int open_device(const char* path
, Config
*cfg
= nullptr, bool try_load_module
= false)
584 int nbd
= open(path
, O_RDWR
);
585 bool loaded_module
= false;
587 if (nbd
< 0 && try_load_module
&& access("/sys/module/nbd", F_OK
) != 0) {
591 param
<< "nbds_max=" << cfg
->nbds_max
;
594 param
<< " max_part=" << cfg
->max_part
;
596 r
= module_load("nbd", param
.str().c_str());
598 cerr
<< "rbd-nbd: failed to load nbd kernel module: " << cpp_strerror(-r
) << std::endl
;
601 loaded_module
= true;
603 nbd
= open(path
, O_RDWR
);
606 if (try_load_module
&& !loaded_module
&&
607 (cfg
->nbds_max
|| cfg
->set_max_part
)) {
608 cerr
<< "rbd-nbd: ignoring kernel module parameter options: nbd module already loaded"
615 static int check_device_size(int nbd_index
, unsigned long expected_size
)
617 // There are bugs with some older kernel versions that result in an
618 // overflow for large image sizes. This check is to ensure we are
621 unsigned long size
= 0;
622 std::string path
= "/sys/block/nbd" + stringify(nbd_index
) + "/size";
624 ifs
.open(path
.c_str(), std::ifstream::in
);
625 if (!ifs
.is_open()) {
626 cerr
<< "rbd-nbd: failed to open " << path
<< std::endl
;
630 size
*= RBD_NBD_BLKSIZE
;
633 // Newer kernel versions will report real size only after nbd
634 // connect. Assume this is the case and return success.
638 if (size
!= expected_size
) {
639 cerr
<< "rbd-nbd: kernel reported invalid device size (" << size
640 << ", expected " << expected_size
<< ")" << std::endl
;
647 static int do_map(int argc
, const char *argv
[], Config
*cfg
)
651 librados::Rados rados
;
653 librados::IoCtx io_ctx
;
663 librbd::image_info_t info
;
667 vector
<const char*> args
;
668 argv_to_vec(argc
, argv
, args
);
670 cerr
<< argv
[0] << ": -h or --help for usage" << std::endl
;
673 if (ceph_argparse_need_usage(args
)) {
678 auto cct
= global_init(NULL
, args
, CEPH_ENTITY_TYPE_CLIENT
,
679 CODE_ENVIRONMENT_DAEMON
,
680 CINIT_FLAG_UNPRIVILEGED_DAEMON_DEFAULTS
);
681 g_ceph_context
->_conf
.set_val_or_die("pid_file", "");
683 if (global_init_prefork(g_ceph_context
) >= 0) {
685 r
= forker
.prefork(err
);
687 cerr
<< err
<< std::endl
;
690 if (forker
.is_parent()) {
691 if (forker
.parent_wait(err
) != 0) {
696 global_init_postfork_start(g_ceph_context
);
699 common_init_finish(g_ceph_context
);
700 global_init_chdir(g_ceph_context
);
702 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fd
) == -1) {
707 r
= rados
.init_with_context(g_ceph_context
);
715 r
= rados
.ioctx_create(cfg
->poolname
.c_str(), io_ctx
);
719 io_ctx
.set_namespace(cfg
->nsname
);
721 r
= rbd
.open(io_ctx
, image
, cfg
->imgname
.c_str());
725 if (cfg
->exclusive
) {
726 r
= image
.lock_acquire(RBD_LOCK_MODE_EXCLUSIVE
);
728 cerr
<< "rbd-nbd: failed to acquire exclusive lock: " << cpp_strerror(r
)
734 if (!cfg
->snapname
.empty()) {
735 r
= image
.snap_set(cfg
->snapname
.c_str());
740 r
= image
.stat(info
, sizeof(info
));
744 if (cfg
->devpath
.empty()) {
746 bool try_load_module
= true;
747 const char *path
= "/sys/module/nbd/parameters/nbds_max";
749 if (access(path
, F_OK
) == 0) {
751 ifs
.open(path
, std::ifstream::in
);
759 snprintf(dev
, sizeof(dev
), "/dev/nbd%d", index
);
761 nbd
= open_device(dev
, cfg
, try_load_module
);
762 try_load_module
= false;
764 if (nbd
== -EPERM
&& nbds_max
!= -1 && index
< (nbds_max
-1)) {
769 cerr
<< "rbd-nbd: failed to find unused device" << std::endl
;
773 r
= ioctl(nbd
, NBD_SET_SOCK
, fd
[0]);
784 r
= sscanf(cfg
->devpath
.c_str(), "/dev/nbd%d", &index
);
786 cerr
<< "rbd-nbd: invalid device path: " << cfg
->devpath
787 << " (expected /dev/nbd{num})" << std::endl
;
790 nbd
= open_device(cfg
->devpath
.c_str(), cfg
, true);
793 cerr
<< "rbd-nbd: failed to open device: " << cfg
->devpath
<< std::endl
;
797 r
= ioctl(nbd
, NBD_SET_SOCK
, fd
[0]);
800 cerr
<< "rbd-nbd: the device " << cfg
->devpath
<< " is busy" << std::endl
;
806 flags
= NBD_FLAG_SEND_FLUSH
| NBD_FLAG_SEND_TRIM
| NBD_FLAG_HAS_FLAGS
;
807 if (!cfg
->snapname
.empty() || cfg
->readonly
) {
808 flags
|= NBD_FLAG_READ_ONLY
;
812 r
= ioctl(nbd
, NBD_SET_BLKSIZE
, RBD_NBD_BLKSIZE
);
818 if (info
.size
> ULONG_MAX
) {
820 cerr
<< "rbd-nbd: image is too large (" << byte_u_t(info
.size
)
821 << ", max is " << byte_u_t(ULONG_MAX
) << ")" << std::endl
;
827 r
= ioctl(nbd
, NBD_SET_SIZE
, size
);
833 r
= check_device_size(index
, size
);
838 ioctl(nbd
, NBD_SET_FLAGS
, flags
);
840 r
= ioctl(nbd
, BLKROSET
, (unsigned long) &read_only
);
846 if (cfg
->timeout
>= 0) {
847 r
= ioctl(nbd
, NBD_SET_TIMEOUT
, (unsigned long)cfg
->timeout
);
850 cerr
<< "rbd-nbd: failed to set timeout: " << cpp_strerror(r
)
859 NBDWatchCtx
watch_ctx(nbd
, io_ctx
, image
, info
.size
);
860 r
= image
.update_watch(&watch_ctx
, &handle
);
864 cout
<< cfg
->devpath
<< std::endl
;
866 if (g_conf()->daemonize
) {
867 global_init_postfork_finish(g_ceph_context
);
872 NBDServer
server(fd
[1], image
);
876 init_async_signal_handler();
877 register_async_signal_handler(SIGHUP
, sighup_handler
);
878 register_async_signal_handler_oneshot(SIGINT
, handle_signal
);
879 register_async_signal_handler_oneshot(SIGTERM
, handle_signal
);
881 ioctl(nbd
, NBD_DO_IT
);
883 unregister_async_signal_handler(SIGHUP
, sighup_handler
);
884 unregister_async_signal_handler(SIGINT
, handle_signal
);
885 unregister_async_signal_handler(SIGTERM
, handle_signal
);
886 shutdown_async_signal_handler();
889 r
= image
.update_unwatch(handle
);
895 ioctl(nbd
, NBD_CLEAR_SOCK
);
896 cerr
<< "rbd-nbd: failed to map, status: " << cpp_strerror(-r
) << std::endl
;
907 forker
.exit(r
< 0 ? EXIT_FAILURE
: 0);
912 static int do_unmap(const std::string
&devpath
)
916 int nbd
= open_device(devpath
.c_str());
918 cerr
<< "rbd-nbd: failed to open device: " << devpath
<< std::endl
;
922 r
= ioctl(nbd
, NBD_DISCONNECT
);
924 cerr
<< "rbd-nbd: the device is not used" << std::endl
;
932 static int parse_imgpath(const std::string
&imgpath
, Config
*cfg
,
933 std::ostream
*err_msg
) {
934 std::regex
pattern("^(?:([^/]+)/(?:([^/@]+)/)?)?([^@]+)(?:@([^/@]+))?$");
936 if (!std::regex_match(imgpath
, match
, pattern
)) {
937 std::cerr
<< "rbd-nbd: invalid spec '" << imgpath
<< "'" << std::endl
;
941 if (match
[1].matched
) {
942 cfg
->poolname
= match
[1];
945 if (match
[2].matched
) {
946 cfg
->nsname
= match
[2];
949 cfg
->imgname
= match
[3];
951 if (match
[4].matched
)
952 cfg
->snapname
= match
[4];
957 static int do_list_mapped_devices(const std::string
&format
, bool pretty_format
)
959 bool should_print
= false;
960 std::unique_ptr
<ceph::Formatter
> f
;
963 if (format
== "json") {
964 f
.reset(new JSONFormatter(pretty_format
));
965 } else if (format
== "xml") {
966 f
.reset(new XMLFormatter(pretty_format
));
967 } else if (!format
.empty() && format
!= "plain") {
968 std::cerr
<< "rbd-nbd: invalid output format: " << format
<< std::endl
;
973 f
->open_array_section("devices");
975 tbl
.define_column("id", TextTable::LEFT
, TextTable::LEFT
);
976 tbl
.define_column("pool", TextTable::LEFT
, TextTable::LEFT
);
977 tbl
.define_column("namespace", TextTable::LEFT
, TextTable::LEFT
);
978 tbl
.define_column("image", TextTable::LEFT
, TextTable::LEFT
);
979 tbl
.define_column("snap", TextTable::LEFT
, TextTable::LEFT
);
980 tbl
.define_column("device", TextTable::LEFT
, TextTable::LEFT
);
986 while (it
.get(&pid
, &cfg
)) {
988 f
->open_object_section("device");
989 f
->dump_int("id", pid
);
990 f
->dump_string("pool", cfg
.poolname
);
991 f
->dump_string("namespace", cfg
.nsname
);
992 f
->dump_string("image", cfg
.imgname
);
993 f
->dump_string("snap", cfg
.snapname
);
994 f
->dump_string("device", cfg
.devpath
);
998 if (cfg
.snapname
.empty()) {
1001 tbl
<< pid
<< cfg
.poolname
<< cfg
.nsname
<< cfg
.imgname
<< cfg
.snapname
1002 << cfg
.devpath
<< TextTable::endrow
;
1007 f
->close_section(); // devices
1008 f
->flush(std::cout
);
1016 static bool find_mapped_dev_by_spec(Config
*cfg
) {
1020 while (it
.get(&pid
, &c
)) {
1021 if (c
.poolname
== cfg
->poolname
&& c
.imgname
== cfg
->imgname
&&
1022 c
.snapname
== cfg
->snapname
) {
1031 static int parse_args(vector
<const char*>& args
, std::ostream
*err_msg
,
1032 Command
*command
, Config
*cfg
) {
1033 std::string conf_file_list
;
1034 std::string cluster
;
1035 CephInitParameters iparams
= ceph_argparse_early_args(
1036 args
, CEPH_ENTITY_TYPE_CLIENT
, &cluster
, &conf_file_list
);
1038 ConfigProxy config
{false};
1039 config
->name
= iparams
.name
;
1040 config
->cluster
= cluster
;
1042 if (!conf_file_list
.empty()) {
1043 config
.parse_config_files(conf_file_list
.c_str(), nullptr, 0);
1045 config
.parse_config_files(nullptr, nullptr, 0);
1047 config
.parse_env(CEPH_ENTITY_TYPE_CLIENT
);
1048 config
.parse_argv(args
);
1049 cfg
->poolname
= config
.get_val
<std::string
>("rbd_default_pool");
1051 std::vector
<const char*>::iterator i
;
1052 std::ostringstream err
;
1054 for (i
= args
.begin(); i
!= args
.end(); ) {
1055 if (ceph_argparse_flag(args
, i
, "-h", "--help", (char*)NULL
)) {
1057 } else if (ceph_argparse_flag(args
, i
, "-v", "--version", (char*)NULL
)) {
1058 return VERSION_INFO
;
1059 } else if (ceph_argparse_witharg(args
, i
, &cfg
->devpath
, "--device", (char *)NULL
)) {
1060 } else if (ceph_argparse_witharg(args
, i
, &cfg
->nbds_max
, err
, "--nbds_max", (char *)NULL
)) {
1061 if (!err
.str().empty()) {
1062 *err_msg
<< "rbd-nbd: " << err
.str();
1065 if (cfg
->nbds_max
< 0) {
1066 *err_msg
<< "rbd-nbd: Invalid argument for nbds_max!";
1069 } else if (ceph_argparse_witharg(args
, i
, &cfg
->max_part
, err
, "--max_part", (char *)NULL
)) {
1070 if (!err
.str().empty()) {
1071 *err_msg
<< "rbd-nbd: " << err
.str();
1074 if ((cfg
->max_part
< 0) || (cfg
->max_part
> 255)) {
1075 *err_msg
<< "rbd-nbd: Invalid argument for max_part(0~255)!";
1078 cfg
->set_max_part
= true;
1079 } else if (ceph_argparse_flag(args
, i
, "--read-only", (char *)NULL
)) {
1080 cfg
->readonly
= true;
1081 } else if (ceph_argparse_flag(args
, i
, "--exclusive", (char *)NULL
)) {
1082 cfg
->exclusive
= true;
1083 } else if (ceph_argparse_witharg(args
, i
, &cfg
->timeout
, err
, "--timeout",
1085 if (!err
.str().empty()) {
1086 *err_msg
<< "rbd-nbd: " << err
.str();
1089 if (cfg
->timeout
< 0) {
1090 *err_msg
<< "rbd-nbd: Invalid argument for timeout!";
1093 } else if (ceph_argparse_witharg(args
, i
, &cfg
->format
, err
, "--format",
1095 } else if (ceph_argparse_flag(args
, i
, "--pretty-format", (char *)NULL
)) {
1096 cfg
->pretty_format
= true;
1103 if (args
.begin() != args
.end()) {
1104 if (strcmp(*args
.begin(), "map") == 0) {
1106 } else if (strcmp(*args
.begin(), "unmap") == 0) {
1108 } else if (strcmp(*args
.begin(), "list-mapped") == 0) {
1111 *err_msg
<< "rbd-nbd: unknown command: " << *args
.begin();
1114 args
.erase(args
.begin());
1118 *err_msg
<< "rbd-nbd: must specify command";
1124 if (args
.begin() == args
.end()) {
1125 *err_msg
<< "rbd-nbd: must specify image-or-snap-spec";
1128 if (parse_imgpath(*args
.begin(), cfg
, err_msg
) < 0) {
1131 args
.erase(args
.begin());
1134 if (args
.begin() == args
.end()) {
1135 *err_msg
<< "rbd-nbd: must specify nbd device or image-or-snap-spec";
1138 if (boost::starts_with(*args
.begin(), "/dev/")) {
1139 cfg
->devpath
= *args
.begin();
1141 if (parse_imgpath(*args
.begin(), cfg
, err_msg
) < 0) {
1144 if (!find_mapped_dev_by_spec(cfg
)) {
1145 *err_msg
<< "rbd-nbd: " << *args
.begin() << " is not mapped";
1149 args
.erase(args
.begin());
1156 if (args
.begin() != args
.end()) {
1157 *err_msg
<< "rbd-nbd: unknown args: " << *args
.begin();
1165 static int rbd_nbd(int argc
, const char *argv
[])
1169 vector
<const char*> args
;
1170 argv_to_vec(argc
, argv
, args
);
1172 std::ostringstream err_msg
;
1173 r
= parse_args(args
, &err_msg
, &cmd
, &cfg
);
1174 if (r
== HELP_INFO
) {
1177 } else if (r
== VERSION_INFO
) {
1178 std::cout
<< pretty_version_to_str() << std::endl
;
1181 cerr
<< err_msg
.str() << std::endl
;
1187 if (cfg
.imgname
.empty()) {
1188 cerr
<< "rbd-nbd: image name was not specified" << std::endl
;
1192 r
= do_map(argc
, argv
, &cfg
);
1197 r
= do_unmap(cfg
.devpath
);
1202 r
= do_list_mapped_devices(cfg
.format
, cfg
.pretty_format
);
1214 int main(int argc
, const char *argv
[])
1216 int r
= rbd_nbd(argc
, argv
);
1218 return EXIT_FAILURE
;