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.
20 #include "include/int_types.h"
21 #include "include/scope_guard.h"
23 #include <boost/endian/conversion.hpp>
33 #include <sys/types.h>
36 #include <linux/nbd.h>
38 #include <sys/ioctl.h>
39 #include <sys/socket.h>
40 #include <sys/syscall.h>
42 #include "nbd-netlink.h"
43 #include <libnl3/netlink/genl/genl.h>
44 #include <libnl3/netlink/genl/ctrl.h>
45 #include <libnl3/netlink/genl/mngt.h>
52 #include <boost/algorithm/string/predicate.hpp>
53 #include <boost/lexical_cast.hpp>
55 #include "common/Formatter.h"
56 #include "common/Preforker.h"
57 #include "common/SubProcess.h"
58 #include "common/TextTable.h"
59 #include "common/ceph_argparse.h"
60 #include "common/config.h"
61 #include "common/dout.h"
62 #include "common/errno.h"
63 #include "common/event_socket.h"
64 #include "common/module.h"
65 #include "common/safe_io.h"
66 #include "common/version.h"
68 #include "global/global_init.h"
69 #include "global/signal_handler.h"
71 #include "include/rados/librados.hpp"
72 #include "include/rbd/librbd.hpp"
73 #include "include/stringify.h"
74 #include "include/xlist.h"
76 #include "mon/MonClient.h"
78 #define dout_context g_ceph_context
79 #define dout_subsys ceph_subsys_rbd
81 #define dout_prefix *_dout << "rbd-nbd: "
84 namespace fs
= std::filesystem
;
86 using boost::endian::big_to_native
;
87 using boost::endian::native_to_big
;
102 int reattach_timeout
= 30;
104 bool exclusive
= false;
106 bool quiesce
= false;
107 bool readonly
= false;
108 bool set_max_part
= false;
109 bool try_netlink
= false;
110 bool show_cookie
= false;
112 std::string poolname
;
115 std::string snapname
;
117 std::string quiesce_hook
= CMAKE_INSTALL_LIBEXECDIR
"/rbd-nbd/rbd-nbd_quiesce";
120 bool pretty_format
= false;
122 std::optional
<librbd::encryption_format_t
> encryption_format
;
123 std::optional
<std::string
> encryption_passphrase_file
;
125 Command command
= None
;
129 std::string
image_spec() const {
130 std::string spec
= poolname
+ "/";
132 if (!nsname
.empty()) {
133 spec
+= nsname
+ "/";
137 if (!snapname
.empty()) {
138 spec
+= "@" + snapname
;
147 std::cout
<< "Usage: rbd-nbd [options] map <image-or-snap-spec> Map image to nbd device\n"
148 << " detach <device|image-or-snap-spec> Detach image from nbd device\n"
149 << " [options] attach <image-or-snap-spec> Attach image to nbd device\n"
150 << " unmap <device|image-or-snap-spec> Unmap nbd device\n"
151 << " [options] list-mapped List mapped nbd devices\n"
152 << "Map and attach options:\n"
153 << " --device <device path> Specify nbd device path (/dev/nbd{num})\n"
154 << " --encryption-format Image encryption format\n"
155 << " (possible values: luks1, luks2)\n"
156 << " --encryption-passphrase-file Path of file containing passphrase for unlocking image encryption\n"
157 << " --exclusive Forbid writes by other clients\n"
158 << " --notrim Turn off trim/discard\n"
159 << " --io-timeout <sec> Set nbd IO timeout\n"
160 << " --max_part <limit> Override for module param max_part\n"
161 << " --nbds_max <limit> Override for module param nbds_max\n"
162 << " --quiesce Use quiesce callbacks\n"
163 << " --quiesce-hook <path> Specify quiesce hook path\n"
164 << " (default: " << Config().quiesce_hook
<< ")\n"
165 << " --read-only Map read-only\n"
166 << " --reattach-timeout <sec> Set nbd re-attach timeout\n"
167 << " (default: " << Config().reattach_timeout
<< ")\n"
168 << " --try-netlink Use the nbd netlink interface\n"
169 << " --show-cookie Show device cookie\n"
170 << " --cookie Specify device cookie\n"
173 << " --format plain|json|xml Output format (default: plain)\n"
174 << " --pretty-format Pretty formatting (json and xml)\n"
176 generic_server_usage();
180 static int nbd_index
= -1;
181 static EventSocket terminate_event_sock
;
183 #define RBD_NBD_BLKSIZE 512UL
186 #define VERSION_INFO 2
188 static int parse_args(vector
<const char*>& args
, std::ostream
*err_msg
,
190 static int netlink_disconnect(int index
);
191 static int netlink_resize(int nbd_index
, uint64_t size
);
193 static int run_quiesce_hook(const std::string
&quiesce_hook
,
194 const std::string
&devpath
,
195 const std::string
&command
);
197 static std::string
get_cookie(const std::string
&devpath
);
202 uint64_t quiesce_watch_handle
= 0;
206 librbd::Image
&image
;
210 NBDServer(int fd
, librbd::Image
& image
, Config
*cfg
)
214 , reader_thread(*this, &NBDServer::reader_entry
)
215 , writer_thread(*this, &NBDServer::writer_entry
)
216 , quiesce_thread(*this, &NBDServer::quiesce_entry
)
218 std::vector
<librbd::config_option_t
> options
;
219 image
.config_list(&options
);
220 for (auto &option
: options
) {
221 if ((option
.name
== std::string("rbd_cache") ||
222 option
.name
== std::string("rbd_cache_writethrough_until_flush")) &&
223 option
.value
== "false") {
224 allow_internal_flush
= true;
230 Config
*get_cfg() const {
235 int terminate_event_fd
= -1;
236 ceph::mutex disconnect_lock
=
237 ceph::make_mutex("NBDServer::DisconnectLocker");
238 ceph::condition_variable disconnect_cond
;
239 std::atomic
<bool> terminated
= { false };
240 std::atomic
<bool> allow_internal_flush
= { false };
244 xlist
<IOContext
*>::item item
;
245 NBDServer
*server
= nullptr;
246 struct nbd_request request
;
247 struct nbd_reply reply
;
256 friend std::ostream
&operator<<(std::ostream
&os
, const IOContext
&ctx
);
258 ceph::mutex lock
= ceph::make_mutex("NBDServer::Locker");
259 ceph::condition_variable cond
;
260 xlist
<IOContext
*> io_pending
;
261 xlist
<IOContext
*> io_finished
;
263 void io_start(IOContext
*ctx
)
265 std::lock_guard l
{lock
};
266 io_pending
.push_back(&ctx
->item
);
269 void io_finish(IOContext
*ctx
)
271 std::lock_guard l
{lock
};
272 ceph_assert(ctx
->item
.is_on_list());
273 ctx
->item
.remove_myself();
274 io_finished
.push_back(&ctx
->item
);
278 IOContext
*wait_io_finish()
280 std::unique_lock l
{lock
};
281 cond
.wait(l
, [this] {
282 return !io_finished
.empty() ||
283 (io_pending
.empty() && terminated
);
286 if (io_finished
.empty())
289 IOContext
*ret
= io_finished
.front();
290 io_finished
.pop_front();
297 std::unique_lock l
{lock
};
298 cond
.wait(l
, [this] { return io_pending
.empty(); });
300 while(!io_finished
.empty()) {
301 std::unique_ptr
<IOContext
> free_ctx(io_finished
.front());
302 io_finished
.pop_front();
308 std::unique_lock l
{lock
};
310 ceph_assert(!reader_thread
.is_started());
311 ceph_assert(!writer_thread
.is_started());
312 ceph_assert(io_pending
.empty());
313 ceph_assert(io_finished
.empty());
316 static void aio_callback(librbd::completion_t cb
, void *arg
)
318 librbd::RBD::AioCompletion
*aio_completion
=
319 reinterpret_cast<librbd::RBD::AioCompletion
*>(cb
);
321 IOContext
*ctx
= reinterpret_cast<IOContext
*>(arg
);
322 int ret
= aio_completion
->get_return_value();
324 dout(20) << __func__
<< ": " << *ctx
<< dendl
;
326 if (ret
== -EINVAL
) {
327 // if shrinking an image, a pagecache writeback might reference
328 // extents outside of the range of the new image extents
329 dout(0) << __func__
<< ": masking IO out-of-bounds error" << dendl
;
335 ctx
->reply
.error
= native_to_big
<uint32_t>(-ret
);
336 } else if ((ctx
->command
== NBD_CMD_READ
) &&
337 ret
< static_cast<int>(ctx
->request
.len
)) {
338 int pad_byte_count
= static_cast<int> (ctx
->request
.len
) - ret
;
339 ctx
->data
.append_zero(pad_byte_count
);
340 dout(20) << __func__
<< ": " << *ctx
<< ": Pad byte count: "
341 << pad_byte_count
<< dendl
;
342 ctx
->reply
.error
= native_to_big
<uint32_t>(0);
344 ctx
->reply
.error
= native_to_big
<uint32_t>(0);
346 ctx
->server
->io_finish(ctx
);
348 aio_completion
->release();
353 struct pollfd poll_fds
[2];
354 memset(poll_fds
, 0, sizeof(struct pollfd
) * 2);
356 poll_fds
[0].events
= POLLIN
;
357 poll_fds
[1].fd
= terminate_event_fd
;
358 poll_fds
[1].events
= POLLIN
;
361 std::unique_ptr
<IOContext
> ctx(new IOContext());
364 dout(20) << __func__
<< ": waiting for nbd request" << dendl
;
366 int r
= poll(poll_fds
, 2, -1);
368 if (errno
== EINTR
) {
372 derr
<< "failed to poll nbd: " << cpp_strerror(r
) << dendl
;
376 if ((poll_fds
[1].revents
& POLLIN
) != 0) {
377 dout(0) << __func__
<< ": terminate received" << dendl
;
381 if ((poll_fds
[0].revents
& POLLIN
) == 0) {
382 dout(20) << __func__
<< ": nothing to read" << dendl
;
386 r
= safe_read_exact(fd
, &ctx
->request
, sizeof(struct nbd_request
));
388 derr
<< "failed to read nbd request header: " << cpp_strerror(r
)
393 if (ctx
->request
.magic
!= htonl(NBD_REQUEST_MAGIC
)) {
394 derr
<< "invalid nbd request header" << dendl
;
398 ctx
->request
.from
= big_to_native(ctx
->request
.from
);
399 ctx
->request
.type
= big_to_native(ctx
->request
.type
);
400 ctx
->request
.len
= big_to_native(ctx
->request
.len
);
402 ctx
->reply
.magic
= native_to_big
<uint32_t>(NBD_REPLY_MAGIC
);
403 memcpy(ctx
->reply
.handle
, ctx
->request
.handle
, sizeof(ctx
->reply
.handle
));
405 ctx
->command
= ctx
->request
.type
& 0x0000ffff;
407 dout(20) << *ctx
<< ": start" << dendl
;
409 switch (ctx
->command
)
412 // NBD_DO_IT will return when pipe is closed
413 dout(0) << "disconnect request received" << dendl
;
416 bufferptr
ptr(ctx
->request
.len
);
417 r
= safe_read_exact(fd
, ptr
.c_str(), ctx
->request
.len
);
419 derr
<< *ctx
<< ": failed to read nbd request data: "
420 << cpp_strerror(r
) << dendl
;
423 ctx
->data
.push_back(ptr
);
427 IOContext
*pctx
= ctx
.release();
429 librbd::RBD::AioCompletion
*c
= new librbd::RBD::AioCompletion(pctx
, aio_callback
);
430 switch (pctx
->command
)
433 image
.aio_write(pctx
->request
.from
, pctx
->request
.len
, pctx
->data
, c
);
436 image
.aio_read(pctx
->request
.from
, pctx
->request
.len
, pctx
->data
, c
);
440 allow_internal_flush
= true;
443 image
.aio_discard(pctx
->request
.from
, pctx
->request
.len
, c
);
446 derr
<< *pctx
<< ": invalid request command" << dendl
;
453 int r
= netlink_disconnect(nbd_index
);
455 ioctl(nbd
, NBD_DISCONNECT
);
459 std::lock_guard l
{lock
};
463 std::lock_guard disconnect_l
{disconnect_lock
};
464 disconnect_cond
.notify_all();
466 dout(20) << __func__
<< ": terminated" << dendl
;
472 dout(20) << __func__
<< ": waiting for io request" << dendl
;
473 std::unique_ptr
<IOContext
> ctx(wait_io_finish());
475 dout(20) << __func__
<< ": no io requests, terminating" << dendl
;
479 dout(20) << __func__
<< ": got: " << *ctx
<< dendl
;
481 int r
= safe_write(fd
, &ctx
->reply
, sizeof(struct nbd_reply
));
483 derr
<< *ctx
<< ": failed to write reply header: " << cpp_strerror(r
)
487 if (ctx
->command
== NBD_CMD_READ
&& ctx
->reply
.error
== htonl(0)) {
488 r
= ctx
->data
.write_fd(fd
);
490 derr
<< *ctx
<< ": failed to write replay data: " << cpp_strerror(r
)
495 dout(20) << *ctx
<< ": finish" << dendl
;
500 ::shutdown(fd
, SHUT_RDWR
);
502 dout(20) << __func__
<< ": terminated" << dendl
;
505 bool wait_quiesce() {
506 dout(20) << __func__
<< dendl
;
508 std::unique_lock locker
{lock
};
509 cond
.wait(locker
, [this] { return quiesce
|| terminated
; });
515 dout(20) << __func__
<< ": got quiesce request" << dendl
;
519 void wait_unquiesce(std::unique_lock
<ceph::mutex
> &locker
) {
520 dout(20) << __func__
<< dendl
;
522 cond
.wait(locker
, [this] { return !quiesce
|| terminated
; });
524 dout(20) << __func__
<< ": got unquiesce request" << dendl
;
527 void wait_inflight_io() {
528 if (!allow_internal_flush
) {
532 uint64_t features
= 0;
533 image
.features(&features
);
534 if ((features
& RBD_FEATURE_EXCLUSIVE_LOCK
) != 0) {
535 bool is_owner
= false;
536 image
.is_exclusive_lock_owner(&is_owner
);
542 dout(20) << __func__
<< dendl
;
544 int r
= image
.flush();
546 derr
<< "flush failed: " << cpp_strerror(r
) << dendl
;
552 ceph_assert(cfg
->quiesce
);
554 while (wait_quiesce()) {
556 int r
= run_quiesce_hook(cfg
->quiesce_hook
, cfg
->devpath
, "quiesce");
561 std::unique_lock locker
{lock
};
562 ceph_assert(quiesce
== true);
564 image
.quiesce_complete(quiesce_watch_handle
, r
);
571 wait_unquiesce(locker
);
574 run_quiesce_hook(cfg
->quiesce_hook
, cfg
->devpath
, "unquiesce");
577 dout(20) << __func__
<< ": terminated" << dendl
;
580 class ThreadHelper
: public Thread
583 typedef void (NBDServer::*entry_func
)();
588 ThreadHelper(NBDServer
&_server
, entry_func _func
)
593 void* entry() override
598 } reader_thread
, writer_thread
, quiesce_thread
;
600 bool started
= false;
601 bool quiesce
= false;
607 dout(10) << __func__
<< ": starting" << dendl
;
611 terminate_event_fd
= eventfd(0, EFD_NONBLOCK
);
612 ceph_assert(terminate_event_fd
> 0);
613 int r
= terminate_event_sock
.init(terminate_event_fd
,
614 EVENT_SOCKET_TYPE_EVENTFD
);
617 reader_thread
.create("rbd_reader");
618 writer_thread
.create("rbd_writer");
620 quiesce_thread
.create("rbd_quiesce");
625 void wait_for_disconnect()
630 std::unique_lock l
{disconnect_lock
};
631 disconnect_cond
.wait(l
);
634 void notify_quiesce() {
635 dout(10) << __func__
<< dendl
;
637 ceph_assert(cfg
->quiesce
);
639 std::unique_lock locker
{lock
};
640 ceph_assert(quiesce
== false);
645 void notify_unquiesce() {
646 dout(10) << __func__
<< dendl
;
648 ceph_assert(cfg
->quiesce
);
650 std::unique_lock locker
{lock
};
651 ceph_assert(quiesce
== true);
659 dout(10) << __func__
<< ": terminating" << dendl
;
661 terminate_event_sock
.notify();
663 reader_thread
.join();
664 writer_thread
.join();
666 quiesce_thread
.join();
671 close(terminate_event_fd
);
677 std::ostream
&operator<<(std::ostream
&os
, const NBDServer::IOContext
&ctx
) {
679 os
<< "[" << std::hex
<< big_to_native(*((uint64_t *)ctx
.request
.handle
));
699 os
<< " UNKNOWN(" << ctx
.command
<< ") ";
703 os
<< ctx
.request
.from
<< "~" << ctx
.request
.len
<< " "
704 << std::dec
<< big_to_native(ctx
.reply
.error
) << "]";
709 class NBDQuiesceWatchCtx
: public librbd::QuiesceWatchCtx
712 NBDQuiesceWatchCtx(NBDServer
*server
) : server(server
) {
715 void handle_quiesce() override
{
716 server
->notify_quiesce();
719 void handle_unquiesce() override
{
720 server
->notify_unquiesce();
727 class NBDWatchCtx
: public librbd::UpdateWatchCtx
733 librados::IoCtx
&io_ctx
;
734 librbd::Image
&image
;
740 librados::IoCtx
&_io_ctx
,
741 librbd::Image
&_image
,
744 , nbd_index(_nbd_index
)
745 , use_netlink(_use_netlink
)
751 ~NBDWatchCtx() override
{}
753 void handle_notify() override
755 librbd::image_info_t info
;
756 if (image
.stat(info
, sizeof(info
)) == 0) {
757 unsigned long new_size
= info
.size
;
760 if (new_size
!= size
) {
761 dout(5) << "resize detected" << dendl
;
762 if (ioctl(fd
, BLKFLSBUF
, NULL
) < 0)
763 derr
<< "invalidate page cache failed: " << cpp_strerror(errno
)
766 ret
= netlink_resize(nbd_index
, new_size
);
768 ret
= ioctl(fd
, NBD_SET_SIZE
, new_size
);
770 derr
<< "resize failed: " << cpp_strerror(errno
) << dendl
;
776 if (ioctl(fd
, BLKRRPART
, NULL
) < 0) {
777 derr
<< "rescan of partition table failed: " << cpp_strerror(errno
)
780 if (image
.invalidate_cache() < 0)
781 derr
<< "invalidate rbd cache failed" << dendl
;
787 class NBDListIterator
{
789 bool get(Config
*cfg
) {
791 std::string nbd_path
= "/sys/block/nbd" + stringify(m_index
);
792 if(access(nbd_path
.c_str(), F_OK
) != 0) {
797 cfg
->devpath
= "/dev/nbd" + stringify(m_index
++);
801 ifs
.open(nbd_path
+ "/pid", std::ifstream::in
);
802 if (!ifs
.is_open()) {
808 // If the rbd-nbd is re-attached the pid may store garbage
809 // here. We are sure this is the case when it is negative or
810 // zero. Then we just try to find the attached process scanning
811 // /proc fs. If it is positive we check the process with this
812 // pid first and if it is not rbd-nbd fallback to searching the
816 pid
= find_attached(cfg
->devpath
);
822 if (get_mapped_info(pid
, cfg
) >= 0) {
832 std::map
<int, Config
> m_mapped_info_cache
;
834 int get_mapped_info(int pid
, Config
*cfg
) {
835 ceph_assert(!cfg
->devpath
.empty());
837 auto it
= m_mapped_info_cache
.find(pid
);
838 if (it
!= m_mapped_info_cache
.end()) {
839 if (it
->second
.devpath
!= cfg
->devpath
) {
846 m_mapped_info_cache
[pid
] = {};
849 std::string path
= "/proc/" + stringify(pid
) + "/comm";
852 ifs
.open(path
.c_str(), std::ifstream::in
);
856 if (comm
!= "rbd-nbd") {
861 path
= "/proc/" + stringify(pid
) + "/cmdline";
863 std::vector
<const char*> args
;
865 ifs
.open(path
.c_str(), std::ifstream::in
);
870 if (cmdline
.empty()) {
874 for (unsigned i
= 0; i
< cmdline
.size(); i
++) {
875 char *arg
= &cmdline
[i
];
877 if (strcmp(basename(arg
) , "rbd-nbd") != 0) {
884 while (cmdline
[i
] != '\0') {
889 std::ostringstream err_msg
;
891 r
= parse_args(args
, &err_msg
, &c
);
896 if (c
.command
!= Map
&& c
.command
!= Attach
) {
901 m_mapped_info_cache
.erase(pid
);
902 if (!c
.devpath
.empty()) {
903 m_mapped_info_cache
[pid
] = c
;
904 if (c
.devpath
!= cfg
->devpath
) {
908 c
.devpath
= cfg
->devpath
;
911 c
.cookie
= get_cookie(cfg
->devpath
);
916 int find_attached(const std::string
&devpath
) {
917 for (auto &entry
: fs::directory_iterator("/proc")) {
918 if (!fs::is_directory(entry
.status())) {
924 pid
= boost::lexical_cast
<uint64_t>(entry
.path().filename().c_str());
925 } catch (boost::bad_lexical_cast
&) {
930 cfg
.devpath
= devpath
;
931 if (get_mapped_info(pid
, &cfg
) >=0 && cfg
.command
== Attach
) {
940 static std::string
get_cookie(const std::string
&devpath
)
944 std::string path
= "/sys/block/" + devpath
.substr(sizeof("/dev/") - 1) + "/backend";
946 ifs
.open(path
, std::ifstream::in
);
948 std::getline(ifs
, cookie
);
954 static int load_module(Config
*cfg
)
960 param
<< "nbds_max=" << cfg
->nbds_max
;
963 param
<< " max_part=" << cfg
->max_part
;
965 if (!access("/sys/module/nbd", F_OK
)) {
966 if (cfg
->nbds_max
|| cfg
->set_max_part
)
967 cerr
<< "rbd-nbd: ignoring kernel module parameter options: nbd module already loaded"
972 ret
= module_load("nbd", param
.str().c_str());
974 cerr
<< "rbd-nbd: failed to load nbd kernel module: " << cpp_strerror(-ret
)
980 static int check_device_size(int nbd_index
, unsigned long expected_size
)
982 // There are bugs with some older kernel versions that result in an
983 // overflow for large image sizes. This check is to ensure we are
986 unsigned long size
= 0;
987 std::string path
= "/sys/block/nbd" + stringify(nbd_index
) + "/size";
989 ifs
.open(path
.c_str(), std::ifstream::in
);
990 if (!ifs
.is_open()) {
991 cerr
<< "rbd-nbd: failed to open " << path
<< std::endl
;
995 size
*= RBD_NBD_BLKSIZE
;
998 // Newer kernel versions will report real size only after nbd
999 // connect. Assume this is the case and return success.
1003 if (size
!= expected_size
) {
1004 cerr
<< "rbd-nbd: kernel reported invalid device size (" << size
1005 << ", expected " << expected_size
<< ")" << std::endl
;
1012 static int parse_nbd_index(const std::string
& devpath
)
1016 ret
= sscanf(devpath
.c_str(), "/dev/nbd%d", &index
);
1018 // mean an early matching failure. But some cases need a negative value.
1021 cerr
<< "rbd-nbd: invalid device path: " << devpath
1022 << " (expected /dev/nbd{num})" << std::endl
;
1029 static int try_ioctl_setup(Config
*cfg
, int fd
, uint64_t size
,
1030 uint64_t blksize
, uint64_t flags
)
1034 if (cfg
->devpath
.empty()) {
1036 const char *path
= "/sys/module/nbd/parameters/nbds_max";
1038 if (access(path
, F_OK
) == 0) {
1040 ifs
.open(path
, std::ifstream::in
);
1041 if (ifs
.is_open()) {
1048 snprintf(dev
, sizeof(dev
), "/dev/nbd%d", index
);
1050 nbd
= open(dev
, O_RDWR
);
1052 if (nbd
== -EPERM
&& nbds_max
!= -1 && index
< (nbds_max
-1)) {
1057 cerr
<< "rbd-nbd: failed to find unused device" << std::endl
;
1061 r
= ioctl(nbd
, NBD_SET_SOCK
, fd
);
1072 r
= parse_nbd_index(cfg
->devpath
);
1077 nbd
= open(cfg
->devpath
.c_str(), O_RDWR
);
1080 cerr
<< "rbd-nbd: failed to open device: " << cfg
->devpath
<< std::endl
;
1084 r
= ioctl(nbd
, NBD_SET_SOCK
, fd
);
1087 cerr
<< "rbd-nbd: the device " << cfg
->devpath
<< " is busy" << std::endl
;
1093 r
= ioctl(nbd
, NBD_SET_BLKSIZE
, blksize
);
1096 cerr
<< "rbd-nbd: NBD_SET_BLKSIZE failed" << std::endl
;
1100 r
= ioctl(nbd
, NBD_SET_SIZE
, size
);
1102 cerr
<< "rbd-nbd: NBD_SET_SIZE failed" << std::endl
;
1107 ioctl(nbd
, NBD_SET_FLAGS
, flags
);
1109 if (cfg
->io_timeout
>= 0) {
1110 r
= ioctl(nbd
, NBD_SET_TIMEOUT
, (unsigned long)cfg
->io_timeout
);
1113 cerr
<< "rbd-nbd: failed to set IO timeout: " << cpp_strerror(r
)
1119 dout(10) << "ioctl setup complete for " << cfg
->devpath
<< dendl
;
1125 ioctl(nbd
, NBD_CLEAR_SOCK
);
1126 cerr
<< "rbd-nbd: failed to map, status: " << cpp_strerror(-r
) << std::endl
;
1133 static void netlink_cleanup(struct nl_sock
*sock
)
1139 nl_socket_free(sock
);
1142 static struct nl_sock
*netlink_init(int *id
)
1144 struct nl_sock
*sock
;
1147 sock
= nl_socket_alloc();
1149 cerr
<< "rbd-nbd: Could not allocate netlink socket." << std::endl
;
1153 ret
= genl_connect(sock
);
1155 cerr
<< "rbd-nbd: Could not connect netlink socket. Error " << ret
1160 *id
= genl_ctrl_resolve(sock
, "nbd");
1162 // nbd netlink interface not supported.
1170 nl_socket_free(sock
);
1174 static int netlink_disconnect(int index
)
1176 struct nl_sock
*sock
;
1180 sock
= netlink_init(&nl_id
);
1185 nl_socket_modify_cb(sock
, NL_CB_VALID
, NL_CB_CUSTOM
, genl_handle_msg
, NULL
);
1187 msg
= nlmsg_alloc();
1189 cerr
<< "rbd-nbd: Could not allocate netlink message." << std::endl
;
1193 if (!genlmsg_put(msg
, NL_AUTO_PORT
, NL_AUTO_SEQ
, nl_id
, 0, 0,
1194 NBD_CMD_DISCONNECT
, 0)) {
1195 cerr
<< "rbd-nbd: Could not setup message." << std::endl
;
1196 goto nla_put_failure
;
1199 NLA_PUT_U32(msg
, NBD_ATTR_INDEX
, index
);
1201 ret
= nl_send_sync(sock
, msg
);
1202 netlink_cleanup(sock
);
1204 cerr
<< "rbd-nbd: netlink disconnect failed: " << nl_geterror(-ret
)
1214 netlink_cleanup(sock
);
1218 static int netlink_disconnect_by_path(const std::string
& devpath
)
1222 index
= parse_nbd_index(devpath
);
1226 return netlink_disconnect(index
);
1229 static int netlink_resize(int nbd_index
, uint64_t size
)
1231 struct nl_sock
*sock
;
1235 sock
= netlink_init(&nl_id
);
1237 cerr
<< "rbd-nbd: Netlink interface not supported." << std::endl
;
1241 nl_socket_modify_cb(sock
, NL_CB_VALID
, NL_CB_CUSTOM
, genl_handle_msg
, NULL
);
1243 msg
= nlmsg_alloc();
1245 cerr
<< "rbd-nbd: Could not allocate netlink message." << std::endl
;
1249 if (!genlmsg_put(msg
, NL_AUTO_PORT
, NL_AUTO_SEQ
, nl_id
, 0, 0,
1250 NBD_CMD_RECONFIGURE
, 0)) {
1251 cerr
<< "rbd-nbd: Could not setup message." << std::endl
;
1255 NLA_PUT_U32(msg
, NBD_ATTR_INDEX
, nbd_index
);
1256 NLA_PUT_U64(msg
, NBD_ATTR_SIZE_BYTES
, size
);
1258 ret
= nl_send_sync(sock
, msg
);
1260 cerr
<< "rbd-nbd: netlink resize failed: " << nl_geterror(ret
) << std::endl
;
1264 netlink_cleanup(sock
);
1265 dout(10) << "netlink resize complete for nbd" << nbd_index
<< dendl
;
1272 netlink_cleanup(sock
);
1276 static int netlink_connect_cb(struct nl_msg
*msg
, void *arg
)
1278 struct genlmsghdr
*gnlh
= (struct genlmsghdr
*)nlmsg_data(nlmsg_hdr(msg
));
1279 Config
*cfg
= (Config
*)arg
;
1280 struct nlattr
*msg_attr
[NBD_ATTR_MAX
+ 1];
1284 ret
= nla_parse(msg_attr
, NBD_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
1285 genlmsg_attrlen(gnlh
, 0), NULL
);
1287 cerr
<< "rbd-nbd: Unsupported netlink reply" << std::endl
;
1288 return -NLE_MSGTYPE_NOSUPPORT
;
1291 if (!msg_attr
[NBD_ATTR_INDEX
]) {
1292 cerr
<< "rbd-nbd: netlink connect reply missing device index." << std::endl
;
1293 return -NLE_MSGTYPE_NOSUPPORT
;
1296 index
= nla_get_u32(msg_attr
[NBD_ATTR_INDEX
]);
1297 cfg
->devpath
= "/dev/nbd" + stringify(index
);
1303 static int netlink_connect(Config
*cfg
, struct nl_sock
*sock
, int nl_id
, int fd
,
1304 uint64_t size
, uint64_t flags
, bool reconnect
)
1306 struct nlattr
*sock_attr
;
1307 struct nlattr
*sock_opt
;
1312 dout(10) << "netlink try reconnect for " << cfg
->devpath
<< dendl
;
1314 nl_socket_modify_cb(sock
, NL_CB_VALID
, NL_CB_CUSTOM
, genl_handle_msg
, NULL
);
1316 nl_socket_modify_cb(sock
, NL_CB_VALID
, NL_CB_CUSTOM
, netlink_connect_cb
,
1320 msg
= nlmsg_alloc();
1322 cerr
<< "rbd-nbd: Could not allocate netlink message." << std::endl
;
1326 if (!genlmsg_put(msg
, NL_AUTO_PORT
, NL_AUTO_SEQ
, nl_id
, 0, 0,
1327 reconnect
? NBD_CMD_RECONFIGURE
: NBD_CMD_CONNECT
, 0)) {
1328 cerr
<< "rbd-nbd: Could not setup message." << std::endl
;
1332 if (!cfg
->devpath
.empty()) {
1333 ret
= parse_nbd_index(cfg
->devpath
);
1337 NLA_PUT_U32(msg
, NBD_ATTR_INDEX
, ret
);
1343 if (cfg
->io_timeout
>= 0)
1344 NLA_PUT_U64(msg
, NBD_ATTR_TIMEOUT
, cfg
->io_timeout
);
1346 NLA_PUT_U64(msg
, NBD_ATTR_SIZE_BYTES
, size
);
1347 NLA_PUT_U64(msg
, NBD_ATTR_BLOCK_SIZE_BYTES
, RBD_NBD_BLKSIZE
);
1348 NLA_PUT_U64(msg
, NBD_ATTR_SERVER_FLAGS
, flags
);
1349 NLA_PUT_U64(msg
, NBD_ATTR_DEAD_CONN_TIMEOUT
, cfg
->reattach_timeout
);
1350 if (!cfg
->cookie
.empty())
1351 NLA_PUT_STRING(msg
, NBD_ATTR_BACKEND_IDENTIFIER
, cfg
->cookie
.c_str());
1353 sock_attr
= nla_nest_start(msg
, NBD_ATTR_SOCKETS
);
1355 cerr
<< "rbd-nbd: Could not init sockets in netlink message." << std::endl
;
1359 sock_opt
= nla_nest_start(msg
, NBD_SOCK_ITEM
);
1361 cerr
<< "rbd-nbd: Could not init sock in netlink message." << std::endl
;
1365 NLA_PUT_U32(msg
, NBD_SOCK_FD
, fd
);
1366 nla_nest_end(msg
, sock_opt
);
1367 nla_nest_end(msg
, sock_attr
);
1369 ret
= nl_send_sync(sock
, msg
);
1371 cerr
<< "rbd-nbd: netlink connect failed: " << nl_geterror(ret
)
1376 dout(10) << "netlink connect complete for " << cfg
->devpath
<< dendl
;
1385 static int try_netlink_setup(Config
*cfg
, int fd
, uint64_t size
, uint64_t flags
,
1388 struct nl_sock
*sock
;
1391 sock
= netlink_init(&nl_id
);
1393 cerr
<< "rbd-nbd: Netlink interface not supported. Using ioctl interface."
1398 dout(10) << "netlink interface supported." << dendl
;
1400 ret
= netlink_connect(cfg
, sock
, nl_id
, fd
, size
, flags
, reconnect
);
1401 netlink_cleanup(sock
);
1406 nbd
= open(cfg
->devpath
.c_str(), O_RDWR
);
1408 cerr
<< "rbd-nbd: failed to open device: " << cfg
->devpath
<< std::endl
;
1415 static int run_quiesce_hook(const std::string
&quiesce_hook
,
1416 const std::string
&devpath
,
1417 const std::string
&command
) {
1418 dout(10) << __func__
<< ": " << quiesce_hook
<< " " << devpath
<< " "
1419 << command
<< dendl
;
1421 SubProcess
hook(quiesce_hook
.c_str(), SubProcess::CLOSE
, SubProcess::PIPE
,
1423 hook
.add_cmd_args(devpath
.c_str(), command
.c_str(), NULL
);
1425 int r
= hook
.spawn();
1427 err
.append("subprocess spawn failed");
1429 err
.read_fd(hook
.get_stderr(), 16384);
1436 derr
<< __func__
<< ": " << quiesce_hook
<< " " << devpath
<< " "
1437 << command
<< " failed: " << err
.to_str() << dendl
;
1439 dout(10) << " succeeded: " << err
.to_str() << dendl
;
1445 static void handle_signal(int signum
)
1447 ceph_assert(signum
== SIGINT
|| signum
== SIGTERM
);
1448 derr
<< "*** Got signal " << sig_str(signum
) << " ***" << dendl
;
1450 dout(20) << __func__
<< ": " << "notifying terminate" << dendl
;
1452 ceph_assert(terminate_event_sock
.is_valid());
1453 terminate_event_sock
.notify();
1456 static NBDServer
*start_server(int fd
, librbd::Image
& image
, Config
*cfg
)
1460 server
= new NBDServer(fd
, image
, cfg
);
1463 init_async_signal_handler();
1464 register_async_signal_handler(SIGHUP
, sighup_handler
);
1465 register_async_signal_handler_oneshot(SIGINT
, handle_signal
);
1466 register_async_signal_handler_oneshot(SIGTERM
, handle_signal
);
1471 static void run_server(Preforker
& forker
, NBDServer
*server
, bool netlink_used
)
1473 if (g_conf()->daemonize
) {
1474 global_init_postfork_finish(g_ceph_context
);
1479 server
->wait_for_disconnect();
1481 ioctl(nbd
, NBD_DO_IT
);
1483 unregister_async_signal_handler(SIGHUP
, sighup_handler
);
1484 unregister_async_signal_handler(SIGINT
, handle_signal
);
1485 unregister_async_signal_handler(SIGTERM
, handle_signal
);
1486 shutdown_async_signal_handler();
1489 // Eventually it should be removed when pidfd_open is widely supported.
1491 static int wait_for_terminate_legacy(int pid
, int timeout
)
1493 for (int i
= 0; ; i
++) {
1494 if (kill(pid
, 0) == -1) {
1495 if (errno
== ESRCH
) {
1499 cerr
<< "rbd-nbd: kill(" << pid
<< ", 0) failed: "
1500 << cpp_strerror(r
) << std::endl
;
1503 if (i
>= timeout
* 2) {
1509 cerr
<< "rbd-nbd: waiting for process exit timed out" << std::endl
;
1513 // Eventually it should be replaced with glibc' pidfd_open
1514 // when it is widely available.
1516 #ifdef __NR_pidfd_open
1517 static int pidfd_open(pid_t pid
, unsigned int flags
)
1519 return syscall(__NR_pidfd_open
, pid
, flags
);
1522 static int pidfd_open(pid_t pid
, unsigned int flags
)
1529 static int wait_for_terminate(int pid
, int timeout
)
1531 int fd
= pidfd_open(pid
, 0);
1533 if (errno
== ENOSYS
) {
1534 return wait_for_terminate_legacy(pid
, timeout
);
1536 if (errno
== ESRCH
) {
1540 cerr
<< "rbd-nbd: pidfd_open(" << pid
<< ") failed: "
1541 << cpp_strerror(r
) << std::endl
;
1545 struct pollfd poll_fds
[1];
1546 memset(poll_fds
, 0, sizeof(struct pollfd
));
1547 poll_fds
[0].fd
= fd
;
1548 poll_fds
[0].events
= POLLIN
;
1550 int r
= poll(poll_fds
, 1, timeout
* 1000);
1553 cerr
<< "rbd-nbd: failed to poll rbd-nbd process: " << cpp_strerror(r
)
1560 if ((poll_fds
[0].revents
& POLLIN
) == 0) {
1561 cerr
<< "rbd-nbd: waiting for process exit timed out" << std::endl
;
1571 static int do_map(int argc
, const char *argv
[], Config
*cfg
, bool reconnect
)
1575 librados::Rados rados
;
1577 librados::IoCtx io_ctx
;
1578 librbd::Image image
;
1581 unsigned long flags
;
1583 unsigned long blksize
= RBD_NBD_BLKSIZE
;
1588 librbd::image_info_t info
;
1593 auto args
= argv_to_vec(argc
, argv
);
1595 cerr
<< argv
[0] << ": -h or --help for usage" << std::endl
;
1598 if (ceph_argparse_need_usage(args
)) {
1603 auto cct
= global_init(NULL
, args
, CEPH_ENTITY_TYPE_CLIENT
,
1604 CODE_ENVIRONMENT_DAEMON
,
1605 CINIT_FLAG_UNPRIVILEGED_DAEMON_DEFAULTS
);
1606 g_ceph_context
->_conf
.set_val_or_die("pid_file", "");
1608 if (global_init_prefork(g_ceph_context
) >= 0) {
1610 r
= forker
.prefork(err
);
1612 cerr
<< err
<< std::endl
;
1615 if (forker
.is_parent()) {
1616 if (forker
.parent_wait(err
) != 0) {
1621 global_init_postfork_start(g_ceph_context
);
1624 common_init_finish(g_ceph_context
);
1625 global_init_chdir(g_ceph_context
);
1627 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fd
) == -1) {
1632 r
= rados
.init_with_context(g_ceph_context
);
1636 r
= rados
.connect();
1640 r
= rados
.ioctx_create(cfg
->poolname
.c_str(), io_ctx
);
1644 io_ctx
.set_namespace(cfg
->nsname
);
1646 r
= rbd
.open(io_ctx
, image
, cfg
->imgname
.c_str());
1650 if (cfg
->exclusive
) {
1651 r
= image
.lock_acquire(RBD_LOCK_MODE_EXCLUSIVE
);
1653 cerr
<< "rbd-nbd: failed to acquire exclusive lock: " << cpp_strerror(r
)
1659 if (!cfg
->snapname
.empty()) {
1660 r
= image
.snap_set(cfg
->snapname
.c_str());
1665 if (cfg
->encryption_format
.has_value()) {
1666 if (!cfg
->encryption_passphrase_file
.has_value()) {
1668 cerr
<< "rbd-nbd: missing encryption-passphrase-file" << std::endl
;
1671 std::ifstream
file(cfg
->encryption_passphrase_file
.value().c_str());
1674 std::cerr
<< "rbd-nbd: unable to open passphrase file:"
1675 << cpp_strerror(errno
) << std::endl
;
1678 std::string
passphrase((std::istreambuf_iterator
<char>(file
)),
1679 (std::istreambuf_iterator
<char>()));
1680 auto sg
= make_scope_guard([&] {
1681 ceph_memzero_s(&passphrase
[0], passphrase
.size(), passphrase
.size()); });
1683 if (!passphrase
.empty() && passphrase
[passphrase
.length() - 1] == '\n') {
1684 passphrase
.erase(passphrase
.length() - 1);
1687 switch (cfg
->encryption_format
.value()) {
1688 case RBD_ENCRYPTION_FORMAT_LUKS1
: {
1689 librbd::encryption_luks1_format_options_t opts
= {};
1690 opts
.passphrase
= passphrase
;
1691 r
= image
.encryption_load(
1692 RBD_ENCRYPTION_FORMAT_LUKS1
, &opts
, sizeof(opts
));
1695 case RBD_ENCRYPTION_FORMAT_LUKS2
: {
1696 librbd::encryption_luks2_format_options_t opts
= {};
1697 opts
.passphrase
= passphrase
;
1698 r
= image
.encryption_load(
1699 RBD_ENCRYPTION_FORMAT_LUKS2
, &opts
, sizeof(opts
));
1705 cerr
<< "rbd-nbd: unsupported encryption format" << std::endl
;
1710 cerr
<< "rbd-nbd: failed to load encryption: " << cpp_strerror(r
)
1716 r
= image
.stat(info
, sizeof(info
));
1720 flags
= NBD_FLAG_SEND_FLUSH
| NBD_FLAG_HAS_FLAGS
;
1722 flags
|= NBD_FLAG_SEND_TRIM
;
1724 if (!cfg
->snapname
.empty() || cfg
->readonly
) {
1725 flags
|= NBD_FLAG_READ_ONLY
;
1729 if (info
.size
> ULONG_MAX
) {
1731 cerr
<< "rbd-nbd: image is too large (" << byte_u_t(info
.size
)
1732 << ", max is " << byte_u_t(ULONG_MAX
) << ")" << std::endl
;
1738 r
= load_module(cfg
);
1742 server
= start_server(fd
[1], image
, cfg
);
1744 use_netlink
= cfg
->try_netlink
|| reconnect
;
1746 // generate when the cookie is not supplied at CLI
1747 if (!reconnect
&& cfg
->cookie
.empty()) {
1749 uuid_gen
.generate_random();
1750 cfg
->cookie
= uuid_gen
.to_string();
1752 r
= try_netlink_setup(cfg
, fd
[0], size
, flags
, reconnect
);
1755 } else if (r
== 1) {
1756 use_netlink
= false;
1761 r
= try_ioctl_setup(cfg
, fd
[0], size
, blksize
, flags
);
1766 r
= check_device_size(nbd_index
, size
);
1770 r
= ioctl(nbd
, BLKROSET
, (unsigned long) &read_only
);
1777 NBDQuiesceWatchCtx
quiesce_watch_ctx(server
);
1779 r
= image
.quiesce_watch(&quiesce_watch_ctx
,
1780 &server
->quiesce_watch_handle
);
1788 NBDWatchCtx
watch_ctx(nbd
, nbd_index
, use_netlink
, io_ctx
, image
,
1790 r
= image
.update_watch(&watch_ctx
, &handle
);
1796 cookie
= get_cookie(cfg
->devpath
);
1797 ceph_assert(cookie
== cfg
->cookie
|| cookie
.empty());
1799 if (cfg
->show_cookie
&& !cookie
.empty()) {
1800 cout
<< cfg
->devpath
<< " " << cookie
<< std::endl
;
1802 cout
<< cfg
->devpath
<< std::endl
;
1805 run_server(forker
, server
, use_netlink
);
1808 r
= image
.quiesce_unwatch(server
->quiesce_watch_handle
);
1809 ceph_assert(r
== 0);
1812 r
= image
.update_unwatch(handle
);
1813 ceph_assert(r
== 0);
1819 netlink_disconnect(nbd_index
);
1821 ioctl(nbd
, NBD_CLEAR_SOCK
);
1822 cerr
<< "rbd-nbd: failed to map, status: " << cpp_strerror(-r
)
1837 forker
.exit(r
< 0 ? EXIT_FAILURE
: 0);
1842 static int do_detach(Config
*cfg
)
1844 int r
= kill(cfg
->pid
, SIGTERM
);
1847 cerr
<< "rbd-nbd: failed to terminate " << cfg
->pid
<< ": "
1848 << cpp_strerror(r
) << std::endl
;
1852 return wait_for_terminate(cfg
->pid
, cfg
->reattach_timeout
);
1855 static int do_unmap(Config
*cfg
)
1858 * The netlink disconnect call supports devices setup with netlink or ioctl,
1859 * so we always try that first.
1861 int r
= netlink_disconnect_by_path(cfg
->devpath
);
1867 int nbd
= open(cfg
->devpath
.c_str(), O_RDWR
);
1869 cerr
<< "rbd-nbd: failed to open device: " << cfg
->devpath
<< std::endl
;
1873 r
= ioctl(nbd
, NBD_DISCONNECT
);
1875 cerr
<< "rbd-nbd: the device is not used" << std::endl
;
1886 r
= wait_for_terminate(cfg
->pid
, cfg
->reattach_timeout
);
1892 static int parse_imgpath(const std::string
&imgpath
, Config
*cfg
,
1893 std::ostream
*err_msg
) {
1894 std::regex
pattern("^(?:([^/]+)/(?:([^/@]+)/)?)?([^@]+)(?:@([^/@]+))?$");
1896 if (!std::regex_match(imgpath
, match
, pattern
)) {
1897 std::cerr
<< "rbd-nbd: invalid spec '" << imgpath
<< "'" << std::endl
;
1901 if (match
[1].matched
) {
1902 cfg
->poolname
= match
[1];
1905 if (match
[2].matched
) {
1906 cfg
->nsname
= match
[2];
1909 cfg
->imgname
= match
[3];
1911 if (match
[4].matched
)
1912 cfg
->snapname
= match
[4];
1917 static int do_list_mapped_devices(const std::string
&format
, bool pretty_format
)
1919 bool should_print
= false;
1920 std::unique_ptr
<ceph::Formatter
> f
;
1923 if (format
== "json") {
1924 f
.reset(new JSONFormatter(pretty_format
));
1925 } else if (format
== "xml") {
1926 f
.reset(new XMLFormatter(pretty_format
));
1927 } else if (!format
.empty() && format
!= "plain") {
1928 std::cerr
<< "rbd-nbd: invalid output format: " << format
<< std::endl
;
1933 f
->open_array_section("devices");
1935 tbl
.define_column("id", TextTable::LEFT
, TextTable::LEFT
);
1936 tbl
.define_column("pool", TextTable::LEFT
, TextTable::LEFT
);
1937 tbl
.define_column("namespace", TextTable::LEFT
, TextTable::LEFT
);
1938 tbl
.define_column("image", TextTable::LEFT
, TextTable::LEFT
);
1939 tbl
.define_column("snap", TextTable::LEFT
, TextTable::LEFT
);
1940 tbl
.define_column("device", TextTable::LEFT
, TextTable::LEFT
);
1941 tbl
.define_column("cookie", TextTable::LEFT
, TextTable::LEFT
);
1946 while (it
.get(&cfg
)) {
1948 f
->open_object_section("device");
1949 f
->dump_int("id", cfg
.pid
);
1950 f
->dump_string("pool", cfg
.poolname
);
1951 f
->dump_string("namespace", cfg
.nsname
);
1952 f
->dump_string("image", cfg
.imgname
);
1953 f
->dump_string("snap", cfg
.snapname
);
1954 f
->dump_string("device", cfg
.devpath
);
1955 f
->dump_string("cookie", cfg
.cookie
);
1958 should_print
= true;
1959 if (cfg
.snapname
.empty()) {
1962 tbl
<< cfg
.pid
<< cfg
.poolname
<< cfg
.nsname
<< cfg
.imgname
1963 << cfg
.snapname
<< cfg
.devpath
<< cfg
.cookie
<< TextTable::endrow
;
1968 f
->close_section(); // devices
1969 f
->flush(std::cout
);
1977 static bool find_mapped_dev_by_spec(Config
*cfg
, int skip_pid
=-1) {
1980 while (it
.get(&c
)) {
1981 if (c
.pid
!= skip_pid
&&
1982 c
.poolname
== cfg
->poolname
&& c
.nsname
== cfg
->nsname
&&
1983 c
.imgname
== cfg
->imgname
&& c
.snapname
== cfg
->snapname
&&
1984 (cfg
->devpath
.empty() || c
.devpath
== cfg
->devpath
)) {
1992 static int find_proc_by_dev(Config
*cfg
) {
1995 while (it
.get(&c
)) {
1996 if (c
.devpath
== cfg
->devpath
) {
2004 static int parse_args(vector
<const char*>& args
, std::ostream
*err_msg
,
2006 std::string conf_file_list
;
2007 std::string cluster
;
2008 CephInitParameters iparams
= ceph_argparse_early_args(
2009 args
, CEPH_ENTITY_TYPE_CLIENT
, &cluster
, &conf_file_list
);
2011 ConfigProxy config
{false};
2012 config
->name
= iparams
.name
;
2013 config
->cluster
= cluster
;
2015 if (!conf_file_list
.empty()) {
2016 config
.parse_config_files(conf_file_list
.c_str(), nullptr, 0);
2018 config
.parse_config_files(nullptr, nullptr, 0);
2020 config
.parse_env(CEPH_ENTITY_TYPE_CLIENT
);
2021 config
.parse_argv(args
);
2022 cfg
->poolname
= config
.get_val
<std::string
>("rbd_default_pool");
2024 std::vector
<const char*>::iterator i
;
2025 std::ostringstream err
;
2026 std::string arg_value
;
2028 for (i
= args
.begin(); i
!= args
.end(); ) {
2029 if (ceph_argparse_flag(args
, i
, "-h", "--help", (char*)NULL
)) {
2031 } else if (ceph_argparse_flag(args
, i
, "-v", "--version", (char*)NULL
)) {
2032 return VERSION_INFO
;
2033 } else if (ceph_argparse_witharg(args
, i
, &cfg
->devpath
, "--device", (char *)NULL
)) {
2034 } else if (ceph_argparse_witharg(args
, i
, &cfg
->io_timeout
, err
,
2035 "--io-timeout", (char *)NULL
)) {
2036 if (!err
.str().empty()) {
2037 *err_msg
<< "rbd-nbd: " << err
.str();
2040 if (cfg
->io_timeout
< 0) {
2041 *err_msg
<< "rbd-nbd: Invalid argument for io-timeout!";
2044 } else if (ceph_argparse_witharg(args
, i
, &cfg
->nbds_max
, err
, "--nbds_max", (char *)NULL
)) {
2045 if (!err
.str().empty()) {
2046 *err_msg
<< "rbd-nbd: " << err
.str();
2049 if (cfg
->nbds_max
< 0) {
2050 *err_msg
<< "rbd-nbd: Invalid argument for nbds_max!";
2053 } else if (ceph_argparse_witharg(args
, i
, &cfg
->max_part
, err
, "--max_part", (char *)NULL
)) {
2054 if (!err
.str().empty()) {
2055 *err_msg
<< "rbd-nbd: " << err
.str();
2058 if ((cfg
->max_part
< 0) || (cfg
->max_part
> 255)) {
2059 *err_msg
<< "rbd-nbd: Invalid argument for max_part(0~255)!";
2062 cfg
->set_max_part
= true;
2063 } else if (ceph_argparse_flag(args
, i
, "--quiesce", (char *)NULL
)) {
2064 cfg
->quiesce
= true;
2065 } else if (ceph_argparse_witharg(args
, i
, &cfg
->quiesce_hook
,
2066 "--quiesce-hook", (char *)NULL
)) {
2067 } else if (ceph_argparse_flag(args
, i
, "--read-only", (char *)NULL
)) {
2068 cfg
->readonly
= true;
2069 } else if (ceph_argparse_witharg(args
, i
, &cfg
->reattach_timeout
, err
,
2070 "--reattach-timeout", (char *)NULL
)) {
2071 if (!err
.str().empty()) {
2072 *err_msg
<< "rbd-nbd: " << err
.str();
2075 if (cfg
->reattach_timeout
< 0) {
2076 *err_msg
<< "rbd-nbd: Invalid argument for reattach-timeout!";
2079 } else if (ceph_argparse_flag(args
, i
, "--exclusive", (char *)NULL
)) {
2080 cfg
->exclusive
= true;
2081 } else if (ceph_argparse_flag(args
, i
, "--notrim", (char *)NULL
)) {
2083 } else if (ceph_argparse_witharg(args
, i
, &cfg
->io_timeout
, err
,
2084 "--timeout", (char *)NULL
)) {
2085 if (!err
.str().empty()) {
2086 *err_msg
<< "rbd-nbd: " << err
.str();
2089 if (cfg
->io_timeout
< 0) {
2090 *err_msg
<< "rbd-nbd: Invalid argument for timeout!";
2093 *err_msg
<< "rbd-nbd: --timeout is deprecated (use --io-timeout)";
2094 } else if (ceph_argparse_witharg(args
, i
, &cfg
->format
, err
, "--format",
2096 } else if (ceph_argparse_flag(args
, i
, "--pretty-format", (char *)NULL
)) {
2097 cfg
->pretty_format
= true;
2098 } else if (ceph_argparse_flag(args
, i
, "--try-netlink", (char *)NULL
)) {
2099 cfg
->try_netlink
= true;
2100 } else if (ceph_argparse_flag(args
, i
, "--show-cookie", (char *)NULL
)) {
2101 cfg
->show_cookie
= true;
2102 } else if (ceph_argparse_witharg(args
, i
, &cfg
->cookie
, "--cookie", (char *)NULL
)) {
2103 } else if (ceph_argparse_witharg(args
, i
, &arg_value
,
2104 "--encryption-format", (char *)NULL
)) {
2105 if (arg_value
== "luks1") {
2106 cfg
->encryption_format
=
2107 std::make_optional(RBD_ENCRYPTION_FORMAT_LUKS1
);
2108 } else if (arg_value
== "luks2") {
2109 cfg
->encryption_format
=
2110 std::make_optional(RBD_ENCRYPTION_FORMAT_LUKS2
);
2112 *err_msg
<< "rbd-nbd: Invalid encryption format";
2115 } else if (ceph_argparse_witharg(args
, i
, &arg_value
,
2116 "--encryption-passphrase-file",
2118 cfg
->encryption_passphrase_file
= std::make_optional(arg_value
);
2125 if (args
.begin() != args
.end()) {
2126 if (strcmp(*args
.begin(), "map") == 0) {
2128 } else if (strcmp(*args
.begin(), "unmap") == 0) {
2130 } else if (strcmp(*args
.begin(), "attach") == 0) {
2132 } else if (strcmp(*args
.begin(), "detach") == 0) {
2134 } else if (strcmp(*args
.begin(), "list-mapped") == 0) {
2137 *err_msg
<< "rbd-nbd: unknown command: " << *args
.begin();
2140 args
.erase(args
.begin());
2144 *err_msg
<< "rbd-nbd: must specify command";
2151 if (cfg
->devpath
.empty()) {
2152 *err_msg
<< "rbd-nbd: must specify device to attach";
2155 // Allowing attach without --cookie option for kernel without
2156 // NBD_ATTR_BACKEND_IDENTIFIER support for compatibility
2157 cookie
= get_cookie(cfg
->devpath
);
2158 if (!cookie
.empty()) {
2159 if (cfg
->cookie
.empty()) {
2160 *err_msg
<< "rbd-nbd: must specify cookie to attach";
2162 } else if (cookie
!= cfg
->cookie
) {
2163 *err_msg
<< "rbd-nbd: cookie mismatch";
2166 } else if (!cfg
->cookie
.empty()) {
2167 *err_msg
<< "rbd-nbd: kernel does not have cookie support";
2172 if (args
.begin() == args
.end()) {
2173 *err_msg
<< "rbd-nbd: must specify image-or-snap-spec";
2176 if (parse_imgpath(*args
.begin(), cfg
, err_msg
) < 0) {
2179 args
.erase(args
.begin());
2183 if (args
.begin() == args
.end()) {
2184 *err_msg
<< "rbd-nbd: must specify nbd device or image-or-snap-spec";
2187 if (boost::starts_with(*args
.begin(), "/dev/")) {
2188 cfg
->devpath
= *args
.begin();
2190 if (parse_imgpath(*args
.begin(), cfg
, err_msg
) < 0) {
2194 args
.erase(args
.begin());
2201 if (args
.begin() != args
.end()) {
2202 *err_msg
<< "rbd-nbd: unknown args: " << *args
.begin();
2210 static int rbd_nbd(int argc
, const char *argv
[])
2214 auto args
= argv_to_vec(argc
, argv
);
2215 std::ostringstream err_msg
;
2216 r
= parse_args(args
, &err_msg
, &cfg
);
2217 if (r
== HELP_INFO
) {
2220 } else if (r
== VERSION_INFO
) {
2221 std::cout
<< pretty_version_to_str() << std::endl
;
2224 cerr
<< err_msg
.str() << std::endl
;
2228 if (!err_msg
.str().empty()) {
2229 cerr
<< err_msg
.str() << std::endl
;
2232 switch (cfg
.command
) {
2234 ceph_assert(!cfg
.devpath
.empty());
2235 if (find_mapped_dev_by_spec(&cfg
, getpid())) {
2236 cerr
<< "rbd-nbd: " << cfg
.devpath
<< " has process " << cfg
.pid
2237 << " connected" << std::endl
;
2242 if (cfg
.imgname
.empty()) {
2243 cerr
<< "rbd-nbd: image name was not specified" << std::endl
;
2247 r
= do_map(argc
, argv
, &cfg
, cfg
.command
== Attach
);
2252 if (cfg
.devpath
.empty()) {
2253 if (!find_mapped_dev_by_spec(&cfg
)) {
2254 cerr
<< "rbd-nbd: " << cfg
.image_spec() << " is not mapped"
2258 } else if (!find_proc_by_dev(&cfg
)) {
2259 cerr
<< "rbd-nbd: no process attached to " << cfg
.devpath
<< " found"
2263 r
= do_detach(&cfg
);
2268 if (cfg
.devpath
.empty()) {
2269 if (!find_mapped_dev_by_spec(&cfg
)) {
2270 cerr
<< "rbd-nbd: " << cfg
.image_spec() << " is not mapped"
2274 } else if (!find_proc_by_dev(&cfg
)) {
2275 // still try to send disconnect to the device
2282 r
= do_list_mapped_devices(cfg
.format
, cfg
.pretty_format
);
2294 int main(int argc
, const char *argv
[])
2296 int r
= rbd_nbd(argc
, argv
);
2298 return EXIT_FAILURE
;