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::vector
<librbd::encryption_format_t
> encryption_formats
;
123 std::vector
<std::string
> encryption_passphrase_files
;
125 Command command
= None
;
128 uint64_t snapid
= CEPH_NOSNAP
;
130 std::string
image_spec() const {
131 std::string spec
= poolname
+ "/";
133 if (!nsname
.empty()) {
134 spec
+= nsname
+ "/";
138 if (!snapname
.empty()) {
139 spec
+= "@" + snapname
;
148 std::cout
<< "Usage: rbd-nbd [options] map <image-or-snap-spec> Map image to nbd device\n"
149 << " detach <device|image-or-snap-spec> Detach image from nbd device\n"
150 << " [options] attach <image-or-snap-spec> Attach image to nbd device\n"
151 << " unmap <device|image-or-snap-spec> Unmap nbd device\n"
152 << " [options] list-mapped List mapped nbd devices\n"
153 << "Map and attach options:\n"
154 << " --device <device path> Specify nbd device path (/dev/nbd{num})\n"
155 << " --encryption-format luks|luks1|luks2\n"
156 << " Image encryption format (default: luks)\n"
157 << " --encryption-passphrase-file Path of file containing passphrase for unlocking image encryption\n"
158 << " --exclusive Forbid writes by other clients\n"
159 << " --notrim Turn off trim/discard\n"
160 << " --io-timeout <sec> Set nbd IO timeout\n"
161 << " --max_part <limit> Override for module param max_part\n"
162 << " --nbds_max <limit> Override for module param nbds_max\n"
163 << " --quiesce Use quiesce callbacks\n"
164 << " --quiesce-hook <path> Specify quiesce hook path\n"
165 << " (default: " << Config().quiesce_hook
<< ")\n"
166 << " --read-only Map read-only\n"
167 << " --reattach-timeout <sec> Set nbd re-attach timeout\n"
168 << " (default: " << Config().reattach_timeout
<< ")\n"
169 << " --try-netlink Use the nbd netlink interface\n"
170 << " --show-cookie Show device cookie\n"
171 << " --cookie Specify device cookie\n"
172 << " --snap-id <snap-id> Specify snapshot by ID instead of by name\n"
174 << "Unmap and detach options:\n"
175 << " --device <device path> Specify nbd device path (/dev/nbd{num})\n"
176 << " --snap-id <snap-id> Specify snapshot by ID instead of by name\n"
179 << " --format plain|json|xml Output format (default: plain)\n"
180 << " --pretty-format Pretty formatting (json and xml)\n"
182 generic_server_usage();
186 static int nbd_index
= -1;
187 static EventSocket terminate_event_sock
;
189 #define RBD_NBD_BLKSIZE 512UL
192 #define VERSION_INFO 2
194 static int parse_args(vector
<const char*>& args
, std::ostream
*err_msg
,
196 static int netlink_disconnect(int index
);
197 static int netlink_resize(int nbd_index
, uint64_t size
);
199 static int run_quiesce_hook(const std::string
&quiesce_hook
,
200 const std::string
&devpath
,
201 const std::string
&command
);
203 static std::string
get_cookie(const std::string
&devpath
);
208 uint64_t quiesce_watch_handle
= 0;
212 librbd::Image
&image
;
216 NBDServer(int fd
, librbd::Image
& image
, Config
*cfg
)
220 , reader_thread(*this, &NBDServer::reader_entry
)
221 , writer_thread(*this, &NBDServer::writer_entry
)
222 , quiesce_thread(*this, &NBDServer::quiesce_entry
)
224 std::vector
<librbd::config_option_t
> options
;
225 image
.config_list(&options
);
226 for (auto &option
: options
) {
227 if ((option
.name
== std::string("rbd_cache") ||
228 option
.name
== std::string("rbd_cache_writethrough_until_flush")) &&
229 option
.value
== "false") {
230 allow_internal_flush
= true;
236 Config
*get_cfg() const {
241 int terminate_event_fd
= -1;
242 ceph::mutex disconnect_lock
=
243 ceph::make_mutex("NBDServer::DisconnectLocker");
244 ceph::condition_variable disconnect_cond
;
245 std::atomic
<bool> terminated
= { false };
246 std::atomic
<bool> allow_internal_flush
= { false };
250 xlist
<IOContext
*>::item item
;
251 NBDServer
*server
= nullptr;
252 struct nbd_request request
;
253 struct nbd_reply reply
;
262 friend std::ostream
&operator<<(std::ostream
&os
, const IOContext
&ctx
);
264 ceph::mutex lock
= ceph::make_mutex("NBDServer::Locker");
265 ceph::condition_variable cond
;
266 xlist
<IOContext
*> io_pending
;
267 xlist
<IOContext
*> io_finished
;
269 void io_start(IOContext
*ctx
)
271 std::lock_guard l
{lock
};
272 io_pending
.push_back(&ctx
->item
);
275 void io_finish(IOContext
*ctx
)
277 std::lock_guard l
{lock
};
278 ceph_assert(ctx
->item
.is_on_list());
279 ctx
->item
.remove_myself();
280 io_finished
.push_back(&ctx
->item
);
284 IOContext
*wait_io_finish()
286 std::unique_lock l
{lock
};
287 cond
.wait(l
, [this] {
288 return !io_finished
.empty() ||
289 (io_pending
.empty() && terminated
);
292 if (io_finished
.empty())
295 IOContext
*ret
= io_finished
.front();
296 io_finished
.pop_front();
303 std::unique_lock l
{lock
};
304 cond
.wait(l
, [this] { return io_pending
.empty(); });
306 while(!io_finished
.empty()) {
307 std::unique_ptr
<IOContext
> free_ctx(io_finished
.front());
308 io_finished
.pop_front();
314 std::unique_lock l
{lock
};
316 ceph_assert(!reader_thread
.is_started());
317 ceph_assert(!writer_thread
.is_started());
318 ceph_assert(io_pending
.empty());
319 ceph_assert(io_finished
.empty());
322 static void aio_callback(librbd::completion_t cb
, void *arg
)
324 librbd::RBD::AioCompletion
*aio_completion
=
325 reinterpret_cast<librbd::RBD::AioCompletion
*>(cb
);
327 IOContext
*ctx
= reinterpret_cast<IOContext
*>(arg
);
328 int ret
= aio_completion
->get_return_value();
330 dout(20) << __func__
<< ": " << *ctx
<< dendl
;
332 if (ret
== -EINVAL
) {
333 // if shrinking an image, a pagecache writeback might reference
334 // extents outside of the range of the new image extents
335 dout(0) << __func__
<< ": masking IO out-of-bounds error" << dendl
;
341 ctx
->reply
.error
= native_to_big
<uint32_t>(-ret
);
342 } else if ((ctx
->command
== NBD_CMD_READ
) &&
343 ret
< static_cast<int>(ctx
->request
.len
)) {
344 int pad_byte_count
= static_cast<int> (ctx
->request
.len
) - ret
;
345 ctx
->data
.append_zero(pad_byte_count
);
346 dout(20) << __func__
<< ": " << *ctx
<< ": Pad byte count: "
347 << pad_byte_count
<< dendl
;
348 ctx
->reply
.error
= native_to_big
<uint32_t>(0);
350 ctx
->reply
.error
= native_to_big
<uint32_t>(0);
352 ctx
->server
->io_finish(ctx
);
354 aio_completion
->release();
359 struct pollfd poll_fds
[2];
360 memset(poll_fds
, 0, sizeof(struct pollfd
) * 2);
362 poll_fds
[0].events
= POLLIN
;
363 poll_fds
[1].fd
= terminate_event_fd
;
364 poll_fds
[1].events
= POLLIN
;
367 std::unique_ptr
<IOContext
> ctx(new IOContext());
370 dout(20) << __func__
<< ": waiting for nbd request" << dendl
;
372 int r
= poll(poll_fds
, 2, -1);
374 if (errno
== EINTR
) {
378 derr
<< "failed to poll nbd: " << cpp_strerror(r
) << dendl
;
382 if ((poll_fds
[1].revents
& POLLIN
) != 0) {
383 dout(0) << __func__
<< ": terminate received" << dendl
;
387 if ((poll_fds
[0].revents
& POLLIN
) == 0) {
388 dout(20) << __func__
<< ": nothing to read" << dendl
;
392 r
= safe_read_exact(fd
, &ctx
->request
, sizeof(struct nbd_request
));
394 derr
<< "failed to read nbd request header: " << cpp_strerror(r
)
399 if (ctx
->request
.magic
!= htonl(NBD_REQUEST_MAGIC
)) {
400 derr
<< "invalid nbd request header" << dendl
;
404 ctx
->request
.from
= big_to_native(ctx
->request
.from
);
405 ctx
->request
.type
= big_to_native(ctx
->request
.type
);
406 ctx
->request
.len
= big_to_native(ctx
->request
.len
);
408 ctx
->reply
.magic
= native_to_big
<uint32_t>(NBD_REPLY_MAGIC
);
409 memcpy(ctx
->reply
.handle
, ctx
->request
.handle
, sizeof(ctx
->reply
.handle
));
411 ctx
->command
= ctx
->request
.type
& 0x0000ffff;
413 dout(20) << *ctx
<< ": start" << dendl
;
415 switch (ctx
->command
)
418 // NBD_DO_IT will return when pipe is closed
419 dout(0) << "disconnect request received" << dendl
;
422 bufferptr
ptr(ctx
->request
.len
);
423 r
= safe_read_exact(fd
, ptr
.c_str(), ctx
->request
.len
);
425 derr
<< *ctx
<< ": failed to read nbd request data: "
426 << cpp_strerror(r
) << dendl
;
429 ctx
->data
.push_back(ptr
);
433 IOContext
*pctx
= ctx
.release();
435 librbd::RBD::AioCompletion
*c
= new librbd::RBD::AioCompletion(pctx
, aio_callback
);
436 switch (pctx
->command
)
439 image
.aio_write(pctx
->request
.from
, pctx
->request
.len
, pctx
->data
, c
);
442 image
.aio_read(pctx
->request
.from
, pctx
->request
.len
, pctx
->data
, c
);
446 allow_internal_flush
= true;
449 image
.aio_discard(pctx
->request
.from
, pctx
->request
.len
, c
);
452 derr
<< *pctx
<< ": invalid request command" << dendl
;
459 int r
= netlink_disconnect(nbd_index
);
461 ioctl(nbd
, NBD_DISCONNECT
);
465 std::lock_guard l
{lock
};
469 std::lock_guard disconnect_l
{disconnect_lock
};
470 disconnect_cond
.notify_all();
472 dout(20) << __func__
<< ": terminated" << dendl
;
478 dout(20) << __func__
<< ": waiting for io request" << dendl
;
479 std::unique_ptr
<IOContext
> ctx(wait_io_finish());
481 dout(20) << __func__
<< ": no io requests, terminating" << dendl
;
485 dout(20) << __func__
<< ": got: " << *ctx
<< dendl
;
487 int r
= safe_write(fd
, &ctx
->reply
, sizeof(struct nbd_reply
));
489 derr
<< *ctx
<< ": failed to write reply header: " << cpp_strerror(r
)
493 if (ctx
->command
== NBD_CMD_READ
&& ctx
->reply
.error
== htonl(0)) {
494 r
= ctx
->data
.write_fd(fd
);
496 derr
<< *ctx
<< ": failed to write replay data: " << cpp_strerror(r
)
501 dout(20) << *ctx
<< ": finish" << dendl
;
506 ::shutdown(fd
, SHUT_RDWR
);
508 dout(20) << __func__
<< ": terminated" << dendl
;
511 bool wait_quiesce() {
512 dout(20) << __func__
<< dendl
;
514 std::unique_lock locker
{lock
};
515 cond
.wait(locker
, [this] { return quiesce
|| terminated
; });
521 dout(20) << __func__
<< ": got quiesce request" << dendl
;
525 void wait_unquiesce(std::unique_lock
<ceph::mutex
> &locker
) {
526 dout(20) << __func__
<< dendl
;
528 cond
.wait(locker
, [this] { return !quiesce
|| terminated
; });
530 dout(20) << __func__
<< ": got unquiesce request" << dendl
;
533 void wait_inflight_io() {
534 if (!allow_internal_flush
) {
538 uint64_t features
= 0;
539 image
.features(&features
);
540 if ((features
& RBD_FEATURE_EXCLUSIVE_LOCK
) != 0) {
541 bool is_owner
= false;
542 image
.is_exclusive_lock_owner(&is_owner
);
548 dout(20) << __func__
<< dendl
;
550 int r
= image
.flush();
552 derr
<< "flush failed: " << cpp_strerror(r
) << dendl
;
558 ceph_assert(cfg
->quiesce
);
560 while (wait_quiesce()) {
562 int r
= run_quiesce_hook(cfg
->quiesce_hook
, cfg
->devpath
, "quiesce");
567 std::unique_lock locker
{lock
};
568 ceph_assert(quiesce
== true);
570 image
.quiesce_complete(quiesce_watch_handle
, r
);
577 wait_unquiesce(locker
);
580 run_quiesce_hook(cfg
->quiesce_hook
, cfg
->devpath
, "unquiesce");
583 dout(20) << __func__
<< ": terminated" << dendl
;
586 class ThreadHelper
: public Thread
589 typedef void (NBDServer::*entry_func
)();
594 ThreadHelper(NBDServer
&_server
, entry_func _func
)
599 void* entry() override
604 } reader_thread
, writer_thread
, quiesce_thread
;
606 bool started
= false;
607 bool quiesce
= false;
613 dout(10) << __func__
<< ": starting" << dendl
;
617 terminate_event_fd
= eventfd(0, EFD_NONBLOCK
);
618 ceph_assert(terminate_event_fd
> 0);
619 int r
= terminate_event_sock
.init(terminate_event_fd
,
620 EVENT_SOCKET_TYPE_EVENTFD
);
623 reader_thread
.create("rbd_reader");
624 writer_thread
.create("rbd_writer");
626 quiesce_thread
.create("rbd_quiesce");
631 void wait_for_disconnect()
636 std::unique_lock l
{disconnect_lock
};
637 disconnect_cond
.wait(l
);
640 void notify_quiesce() {
641 dout(10) << __func__
<< dendl
;
643 ceph_assert(cfg
->quiesce
);
645 std::unique_lock locker
{lock
};
646 ceph_assert(quiesce
== false);
651 void notify_unquiesce() {
652 dout(10) << __func__
<< dendl
;
654 ceph_assert(cfg
->quiesce
);
656 std::unique_lock locker
{lock
};
657 ceph_assert(quiesce
== true);
665 dout(10) << __func__
<< ": terminating" << dendl
;
667 terminate_event_sock
.notify();
669 reader_thread
.join();
670 writer_thread
.join();
672 quiesce_thread
.join();
677 close(terminate_event_fd
);
683 std::ostream
&operator<<(std::ostream
&os
, const NBDServer::IOContext
&ctx
) {
685 os
<< "[" << std::hex
<< big_to_native(*((uint64_t *)ctx
.request
.handle
));
705 os
<< " UNKNOWN(" << ctx
.command
<< ") ";
709 os
<< ctx
.request
.from
<< "~" << ctx
.request
.len
<< " "
710 << std::dec
<< big_to_native(ctx
.reply
.error
) << "]";
715 class NBDQuiesceWatchCtx
: public librbd::QuiesceWatchCtx
718 NBDQuiesceWatchCtx(NBDServer
*server
) : server(server
) {
721 void handle_quiesce() override
{
722 server
->notify_quiesce();
725 void handle_unquiesce() override
{
726 server
->notify_unquiesce();
733 class NBDWatchCtx
: public librbd::UpdateWatchCtx
739 librados::IoCtx
&io_ctx
;
740 librbd::Image
&image
;
746 librados::IoCtx
&_io_ctx
,
747 librbd::Image
&_image
,
750 , nbd_index(_nbd_index
)
751 , use_netlink(_use_netlink
)
757 ~NBDWatchCtx() override
{}
759 void handle_notify() override
761 librbd::image_info_t info
;
762 if (image
.stat(info
, sizeof(info
)) == 0) {
763 unsigned long new_size
= info
.size
;
766 if (new_size
!= size
) {
767 dout(5) << "resize detected" << dendl
;
768 if (ioctl(fd
, BLKFLSBUF
, NULL
) < 0)
769 derr
<< "invalidate page cache failed: " << cpp_strerror(errno
)
772 ret
= netlink_resize(nbd_index
, new_size
);
774 ret
= ioctl(fd
, NBD_SET_SIZE
, new_size
);
776 derr
<< "resize failed: " << cpp_strerror(errno
) << dendl
;
782 if (ioctl(fd
, BLKRRPART
, NULL
) < 0) {
783 derr
<< "rescan of partition table failed: " << cpp_strerror(errno
)
786 if (image
.invalidate_cache() < 0)
787 derr
<< "invalidate rbd cache failed" << dendl
;
793 class NBDListIterator
{
795 bool get(Config
*cfg
) {
797 std::string nbd_path
= "/sys/block/nbd" + stringify(m_index
);
798 if(access(nbd_path
.c_str(), F_OK
) != 0) {
803 cfg
->devpath
= "/dev/nbd" + stringify(m_index
++);
807 ifs
.open(nbd_path
+ "/pid", std::ifstream::in
);
808 if (!ifs
.is_open()) {
814 // If the rbd-nbd is re-attached the pid may store garbage
815 // here. We are sure this is the case when it is negative or
816 // zero. Then we just try to find the attached process scanning
817 // /proc fs. If it is positive we check the process with this
818 // pid first and if it is not rbd-nbd fallback to searching the
822 pid
= find_attached(cfg
->devpath
);
828 if (get_mapped_info(pid
, cfg
) >= 0) {
838 std::map
<int, Config
> m_mapped_info_cache
;
840 int get_mapped_info(int pid
, Config
*cfg
) {
841 ceph_assert(!cfg
->devpath
.empty());
843 auto it
= m_mapped_info_cache
.find(pid
);
844 if (it
!= m_mapped_info_cache
.end()) {
845 if (it
->second
.devpath
!= cfg
->devpath
) {
852 m_mapped_info_cache
[pid
] = {};
855 std::string path
= "/proc/" + stringify(pid
) + "/comm";
858 ifs
.open(path
.c_str(), std::ifstream::in
);
862 if (comm
!= "rbd-nbd") {
867 path
= "/proc/" + stringify(pid
) + "/cmdline";
869 std::vector
<const char*> args
;
871 ifs
.open(path
.c_str(), std::ifstream::in
);
876 if (cmdline
.empty()) {
880 for (unsigned i
= 0; i
< cmdline
.size(); i
++) {
881 char *arg
= &cmdline
[i
];
883 if (strcmp(basename(arg
) , "rbd-nbd") != 0) {
890 while (cmdline
[i
] != '\0') {
895 std::ostringstream err_msg
;
897 r
= parse_args(args
, &err_msg
, &c
);
902 if (c
.command
!= Map
&& c
.command
!= Attach
) {
907 m_mapped_info_cache
.erase(pid
);
908 if (!c
.devpath
.empty()) {
909 m_mapped_info_cache
[pid
] = c
;
910 if (c
.devpath
!= cfg
->devpath
) {
914 c
.devpath
= cfg
->devpath
;
917 c
.cookie
= get_cookie(cfg
->devpath
);
922 int find_attached(const std::string
&devpath
) {
923 for (auto &entry
: fs::directory_iterator("/proc")) {
924 if (!fs::is_directory(entry
.status())) {
930 pid
= boost::lexical_cast
<uint64_t>(entry
.path().filename().c_str());
931 } catch (boost::bad_lexical_cast
&) {
936 cfg
.devpath
= devpath
;
937 if (get_mapped_info(pid
, &cfg
) >=0 && cfg
.command
== Attach
) {
946 struct EncryptionOptions
{
947 std::vector
<librbd::encryption_spec_t
> specs
;
949 ~EncryptionOptions() {
950 for (auto& spec
: specs
) {
951 switch (spec
.format
) {
952 case RBD_ENCRYPTION_FORMAT_LUKS
: {
954 static_cast<librbd::encryption_luks_format_options_t
*>(spec
.opts
);
955 ceph_memzero_s(opts
->passphrase
.data(), opts
->passphrase
.size(),
956 opts
->passphrase
.size());
960 case RBD_ENCRYPTION_FORMAT_LUKS1
: {
962 static_cast<librbd::encryption_luks1_format_options_t
*>(spec
.opts
);
963 ceph_memzero_s(opts
->passphrase
.data(), opts
->passphrase
.size(),
964 opts
->passphrase
.size());
968 case RBD_ENCRYPTION_FORMAT_LUKS2
: {
970 static_cast<librbd::encryption_luks2_format_options_t
*>(spec
.opts
);
971 ceph_memzero_s(opts
->passphrase
.data(), opts
->passphrase
.size(),
972 opts
->passphrase
.size());
983 static std::string
get_cookie(const std::string
&devpath
)
987 std::string path
= "/sys/block/" + devpath
.substr(sizeof("/dev/") - 1) + "/backend";
989 ifs
.open(path
, std::ifstream::in
);
991 std::getline(ifs
, cookie
);
997 static int load_module(Config
*cfg
)
1003 param
<< "nbds_max=" << cfg
->nbds_max
;
1006 param
<< " max_part=" << cfg
->max_part
;
1008 if (!access("/sys/module/nbd", F_OK
)) {
1009 if (cfg
->nbds_max
|| cfg
->set_max_part
)
1010 cerr
<< "rbd-nbd: ignoring kernel module parameter options: nbd module already loaded"
1015 ret
= module_load("nbd", param
.str().c_str());
1017 cerr
<< "rbd-nbd: failed to load nbd kernel module: " << cpp_strerror(-ret
)
1023 static int check_device_size(int nbd_index
, unsigned long expected_size
)
1025 // There are bugs with some older kernel versions that result in an
1026 // overflow for large image sizes. This check is to ensure we are
1029 unsigned long size
= 0;
1030 std::string path
= "/sys/block/nbd" + stringify(nbd_index
) + "/size";
1032 ifs
.open(path
.c_str(), std::ifstream::in
);
1033 if (!ifs
.is_open()) {
1034 cerr
<< "rbd-nbd: failed to open " << path
<< std::endl
;
1038 size
*= RBD_NBD_BLKSIZE
;
1041 // Newer kernel versions will report real size only after nbd
1042 // connect. Assume this is the case and return success.
1046 if (size
!= expected_size
) {
1047 cerr
<< "rbd-nbd: kernel reported invalid device size (" << size
1048 << ", expected " << expected_size
<< ")" << std::endl
;
1055 static int parse_nbd_index(const std::string
& devpath
)
1059 ret
= sscanf(devpath
.c_str(), "/dev/nbd%d", &index
);
1061 // mean an early matching failure. But some cases need a negative value.
1064 cerr
<< "rbd-nbd: invalid device path: " << devpath
1065 << " (expected /dev/nbd{num})" << std::endl
;
1072 static int try_ioctl_setup(Config
*cfg
, int fd
, uint64_t size
,
1073 uint64_t blksize
, uint64_t flags
)
1077 if (cfg
->devpath
.empty()) {
1079 const char *path
= "/sys/module/nbd/parameters/nbds_max";
1081 if (access(path
, F_OK
) == 0) {
1083 ifs
.open(path
, std::ifstream::in
);
1084 if (ifs
.is_open()) {
1091 snprintf(dev
, sizeof(dev
), "/dev/nbd%d", index
);
1093 nbd
= open(dev
, O_RDWR
);
1095 if (nbd
== -EPERM
&& nbds_max
!= -1 && index
< (nbds_max
-1)) {
1100 cerr
<< "rbd-nbd: failed to find unused device" << std::endl
;
1104 r
= ioctl(nbd
, NBD_SET_SOCK
, fd
);
1115 r
= parse_nbd_index(cfg
->devpath
);
1120 nbd
= open(cfg
->devpath
.c_str(), O_RDWR
);
1123 cerr
<< "rbd-nbd: failed to open device: " << cfg
->devpath
<< std::endl
;
1127 r
= ioctl(nbd
, NBD_SET_SOCK
, fd
);
1130 cerr
<< "rbd-nbd: the device " << cfg
->devpath
<< " is busy" << std::endl
;
1136 r
= ioctl(nbd
, NBD_SET_BLKSIZE
, blksize
);
1139 cerr
<< "rbd-nbd: NBD_SET_BLKSIZE failed" << std::endl
;
1143 r
= ioctl(nbd
, NBD_SET_SIZE
, size
);
1145 cerr
<< "rbd-nbd: NBD_SET_SIZE failed" << std::endl
;
1150 ioctl(nbd
, NBD_SET_FLAGS
, flags
);
1152 if (cfg
->io_timeout
>= 0) {
1153 r
= ioctl(nbd
, NBD_SET_TIMEOUT
, (unsigned long)cfg
->io_timeout
);
1156 cerr
<< "rbd-nbd: failed to set IO timeout: " << cpp_strerror(r
)
1162 dout(10) << "ioctl setup complete for " << cfg
->devpath
<< dendl
;
1168 ioctl(nbd
, NBD_CLEAR_SOCK
);
1169 cerr
<< "rbd-nbd: failed to map, status: " << cpp_strerror(-r
) << std::endl
;
1176 static void netlink_cleanup(struct nl_sock
*sock
)
1182 nl_socket_free(sock
);
1185 static struct nl_sock
*netlink_init(int *id
)
1187 struct nl_sock
*sock
;
1190 sock
= nl_socket_alloc();
1192 cerr
<< "rbd-nbd: Could not allocate netlink socket." << std::endl
;
1196 ret
= genl_connect(sock
);
1198 cerr
<< "rbd-nbd: Could not connect netlink socket. Error " << ret
1203 *id
= genl_ctrl_resolve(sock
, "nbd");
1205 // nbd netlink interface not supported.
1213 nl_socket_free(sock
);
1217 static int netlink_disconnect(int index
)
1219 struct nl_sock
*sock
;
1223 sock
= netlink_init(&nl_id
);
1228 nl_socket_modify_cb(sock
, NL_CB_VALID
, NL_CB_CUSTOM
, genl_handle_msg
, NULL
);
1230 msg
= nlmsg_alloc();
1232 cerr
<< "rbd-nbd: Could not allocate netlink message." << std::endl
;
1236 if (!genlmsg_put(msg
, NL_AUTO_PORT
, NL_AUTO_SEQ
, nl_id
, 0, 0,
1237 NBD_CMD_DISCONNECT
, 0)) {
1238 cerr
<< "rbd-nbd: Could not setup message." << std::endl
;
1239 goto nla_put_failure
;
1242 NLA_PUT_U32(msg
, NBD_ATTR_INDEX
, index
);
1244 ret
= nl_send_sync(sock
, msg
);
1245 netlink_cleanup(sock
);
1247 cerr
<< "rbd-nbd: netlink disconnect failed: " << nl_geterror(-ret
)
1257 netlink_cleanup(sock
);
1261 static int netlink_disconnect_by_path(const std::string
& devpath
)
1265 index
= parse_nbd_index(devpath
);
1269 return netlink_disconnect(index
);
1272 static int netlink_resize(int nbd_index
, uint64_t size
)
1274 struct nl_sock
*sock
;
1278 sock
= netlink_init(&nl_id
);
1280 cerr
<< "rbd-nbd: Netlink interface not supported." << std::endl
;
1284 nl_socket_modify_cb(sock
, NL_CB_VALID
, NL_CB_CUSTOM
, genl_handle_msg
, NULL
);
1286 msg
= nlmsg_alloc();
1288 cerr
<< "rbd-nbd: Could not allocate netlink message." << std::endl
;
1292 if (!genlmsg_put(msg
, NL_AUTO_PORT
, NL_AUTO_SEQ
, nl_id
, 0, 0,
1293 NBD_CMD_RECONFIGURE
, 0)) {
1294 cerr
<< "rbd-nbd: Could not setup message." << std::endl
;
1298 NLA_PUT_U32(msg
, NBD_ATTR_INDEX
, nbd_index
);
1299 NLA_PUT_U64(msg
, NBD_ATTR_SIZE_BYTES
, size
);
1301 ret
= nl_send_sync(sock
, msg
);
1303 cerr
<< "rbd-nbd: netlink resize failed: " << nl_geterror(ret
) << std::endl
;
1307 netlink_cleanup(sock
);
1308 dout(10) << "netlink resize complete for nbd" << nbd_index
<< dendl
;
1315 netlink_cleanup(sock
);
1319 static int netlink_connect_cb(struct nl_msg
*msg
, void *arg
)
1321 struct genlmsghdr
*gnlh
= (struct genlmsghdr
*)nlmsg_data(nlmsg_hdr(msg
));
1322 Config
*cfg
= (Config
*)arg
;
1323 struct nlattr
*msg_attr
[NBD_ATTR_MAX
+ 1];
1327 ret
= nla_parse(msg_attr
, NBD_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
1328 genlmsg_attrlen(gnlh
, 0), NULL
);
1330 cerr
<< "rbd-nbd: Unsupported netlink reply" << std::endl
;
1331 return -NLE_MSGTYPE_NOSUPPORT
;
1334 if (!msg_attr
[NBD_ATTR_INDEX
]) {
1335 cerr
<< "rbd-nbd: netlink connect reply missing device index." << std::endl
;
1336 return -NLE_MSGTYPE_NOSUPPORT
;
1339 index
= nla_get_u32(msg_attr
[NBD_ATTR_INDEX
]);
1340 cfg
->devpath
= "/dev/nbd" + stringify(index
);
1346 static int netlink_connect(Config
*cfg
, struct nl_sock
*sock
, int nl_id
, int fd
,
1347 uint64_t size
, uint64_t flags
, bool reconnect
)
1349 struct nlattr
*sock_attr
;
1350 struct nlattr
*sock_opt
;
1355 dout(10) << "netlink try reconnect for " << cfg
->devpath
<< dendl
;
1357 nl_socket_modify_cb(sock
, NL_CB_VALID
, NL_CB_CUSTOM
, genl_handle_msg
, NULL
);
1359 nl_socket_modify_cb(sock
, NL_CB_VALID
, NL_CB_CUSTOM
, netlink_connect_cb
,
1363 msg
= nlmsg_alloc();
1365 cerr
<< "rbd-nbd: Could not allocate netlink message." << std::endl
;
1369 if (!genlmsg_put(msg
, NL_AUTO_PORT
, NL_AUTO_SEQ
, nl_id
, 0, 0,
1370 reconnect
? NBD_CMD_RECONFIGURE
: NBD_CMD_CONNECT
, 0)) {
1371 cerr
<< "rbd-nbd: Could not setup message." << std::endl
;
1375 if (!cfg
->devpath
.empty()) {
1376 ret
= parse_nbd_index(cfg
->devpath
);
1380 NLA_PUT_U32(msg
, NBD_ATTR_INDEX
, ret
);
1386 if (cfg
->io_timeout
>= 0)
1387 NLA_PUT_U64(msg
, NBD_ATTR_TIMEOUT
, cfg
->io_timeout
);
1389 NLA_PUT_U64(msg
, NBD_ATTR_SIZE_BYTES
, size
);
1390 NLA_PUT_U64(msg
, NBD_ATTR_BLOCK_SIZE_BYTES
, RBD_NBD_BLKSIZE
);
1391 NLA_PUT_U64(msg
, NBD_ATTR_SERVER_FLAGS
, flags
);
1392 NLA_PUT_U64(msg
, NBD_ATTR_DEAD_CONN_TIMEOUT
, cfg
->reattach_timeout
);
1393 if (!cfg
->cookie
.empty())
1394 NLA_PUT_STRING(msg
, NBD_ATTR_BACKEND_IDENTIFIER
, cfg
->cookie
.c_str());
1396 sock_attr
= nla_nest_start(msg
, NBD_ATTR_SOCKETS
);
1398 cerr
<< "rbd-nbd: Could not init sockets in netlink message." << std::endl
;
1402 sock_opt
= nla_nest_start(msg
, NBD_SOCK_ITEM
);
1404 cerr
<< "rbd-nbd: Could not init sock in netlink message." << std::endl
;
1408 NLA_PUT_U32(msg
, NBD_SOCK_FD
, fd
);
1409 nla_nest_end(msg
, sock_opt
);
1410 nla_nest_end(msg
, sock_attr
);
1412 ret
= nl_send_sync(sock
, msg
);
1414 cerr
<< "rbd-nbd: netlink connect failed: " << nl_geterror(ret
)
1419 dout(10) << "netlink connect complete for " << cfg
->devpath
<< dendl
;
1428 static int try_netlink_setup(Config
*cfg
, int fd
, uint64_t size
, uint64_t flags
,
1431 struct nl_sock
*sock
;
1434 sock
= netlink_init(&nl_id
);
1436 cerr
<< "rbd-nbd: Netlink interface not supported. Using ioctl interface."
1441 dout(10) << "netlink interface supported." << dendl
;
1443 ret
= netlink_connect(cfg
, sock
, nl_id
, fd
, size
, flags
, reconnect
);
1444 netlink_cleanup(sock
);
1449 nbd
= open(cfg
->devpath
.c_str(), O_RDWR
);
1451 cerr
<< "rbd-nbd: failed to open device: " << cfg
->devpath
<< std::endl
;
1458 static int run_quiesce_hook(const std::string
&quiesce_hook
,
1459 const std::string
&devpath
,
1460 const std::string
&command
) {
1461 dout(10) << __func__
<< ": " << quiesce_hook
<< " " << devpath
<< " "
1462 << command
<< dendl
;
1464 SubProcess
hook(quiesce_hook
.c_str(), SubProcess::CLOSE
, SubProcess::PIPE
,
1466 hook
.add_cmd_args(devpath
.c_str(), command
.c_str(), NULL
);
1468 int r
= hook
.spawn();
1470 err
.append("subprocess spawn failed");
1472 err
.read_fd(hook
.get_stderr(), 16384);
1479 derr
<< __func__
<< ": " << quiesce_hook
<< " " << devpath
<< " "
1480 << command
<< " failed: " << err
.to_str() << dendl
;
1482 dout(10) << " succeeded: " << err
.to_str() << dendl
;
1488 static void handle_signal(int signum
)
1490 ceph_assert(signum
== SIGINT
|| signum
== SIGTERM
);
1491 derr
<< "*** Got signal " << sig_str(signum
) << " ***" << dendl
;
1493 dout(20) << __func__
<< ": " << "notifying terminate" << dendl
;
1495 ceph_assert(terminate_event_sock
.is_valid());
1496 terminate_event_sock
.notify();
1499 static NBDServer
*start_server(int fd
, librbd::Image
& image
, Config
*cfg
)
1503 server
= new NBDServer(fd
, image
, cfg
);
1506 init_async_signal_handler();
1507 register_async_signal_handler(SIGHUP
, sighup_handler
);
1508 register_async_signal_handler_oneshot(SIGINT
, handle_signal
);
1509 register_async_signal_handler_oneshot(SIGTERM
, handle_signal
);
1514 static void run_server(Preforker
& forker
, NBDServer
*server
, bool netlink_used
)
1516 if (g_conf()->daemonize
) {
1517 global_init_postfork_finish(g_ceph_context
);
1522 server
->wait_for_disconnect();
1524 ioctl(nbd
, NBD_DO_IT
);
1526 unregister_async_signal_handler(SIGHUP
, sighup_handler
);
1527 unregister_async_signal_handler(SIGINT
, handle_signal
);
1528 unregister_async_signal_handler(SIGTERM
, handle_signal
);
1529 shutdown_async_signal_handler();
1532 // Eventually it should be removed when pidfd_open is widely supported.
1534 static int wait_for_terminate_legacy(int pid
, int timeout
)
1536 for (int i
= 0; ; i
++) {
1537 if (kill(pid
, 0) == -1) {
1538 if (errno
== ESRCH
) {
1542 cerr
<< "rbd-nbd: kill(" << pid
<< ", 0) failed: "
1543 << cpp_strerror(r
) << std::endl
;
1546 if (i
>= timeout
* 2) {
1552 cerr
<< "rbd-nbd: waiting for process exit timed out" << std::endl
;
1556 // Eventually it should be replaced with glibc' pidfd_open
1557 // when it is widely available.
1559 #ifdef __NR_pidfd_open
1560 static int pidfd_open(pid_t pid
, unsigned int flags
)
1562 return syscall(__NR_pidfd_open
, pid
, flags
);
1565 static int pidfd_open(pid_t pid
, unsigned int flags
)
1572 static int wait_for_terminate(int pid
, int timeout
)
1574 int fd
= pidfd_open(pid
, 0);
1576 if (errno
== ENOSYS
) {
1577 return wait_for_terminate_legacy(pid
, timeout
);
1579 if (errno
== ESRCH
) {
1583 cerr
<< "rbd-nbd: pidfd_open(" << pid
<< ") failed: "
1584 << cpp_strerror(r
) << std::endl
;
1588 struct pollfd poll_fds
[1];
1589 memset(poll_fds
, 0, sizeof(struct pollfd
));
1590 poll_fds
[0].fd
= fd
;
1591 poll_fds
[0].events
= POLLIN
;
1593 int r
= poll(poll_fds
, 1, timeout
* 1000);
1596 cerr
<< "rbd-nbd: failed to poll rbd-nbd process: " << cpp_strerror(r
)
1603 if ((poll_fds
[0].revents
& POLLIN
) == 0) {
1604 cerr
<< "rbd-nbd: waiting for process exit timed out" << std::endl
;
1614 static int do_map(int argc
, const char *argv
[], Config
*cfg
, bool reconnect
)
1618 librados::Rados rados
;
1620 librados::IoCtx io_ctx
;
1621 librbd::Image image
;
1624 unsigned long flags
;
1626 unsigned long blksize
= RBD_NBD_BLKSIZE
;
1631 librbd::image_info_t info
;
1636 auto args
= argv_to_vec(argc
, argv
);
1638 cerr
<< argv
[0] << ": -h or --help for usage" << std::endl
;
1641 if (ceph_argparse_need_usage(args
)) {
1646 auto cct
= global_init(NULL
, args
, CEPH_ENTITY_TYPE_CLIENT
,
1647 CODE_ENVIRONMENT_DAEMON
,
1648 CINIT_FLAG_UNPRIVILEGED_DAEMON_DEFAULTS
);
1649 g_ceph_context
->_conf
.set_val_or_die("pid_file", "");
1651 if (global_init_prefork(g_ceph_context
) >= 0) {
1653 r
= forker
.prefork(err
);
1655 cerr
<< err
<< std::endl
;
1658 if (forker
.is_parent()) {
1659 if (forker
.parent_wait(err
) != 0) {
1664 global_init_postfork_start(g_ceph_context
);
1667 common_init_finish(g_ceph_context
);
1668 global_init_chdir(g_ceph_context
);
1670 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fd
) == -1) {
1675 r
= rados
.init_with_context(g_ceph_context
);
1679 r
= rados
.connect();
1683 r
= rados
.ioctx_create(cfg
->poolname
.c_str(), io_ctx
);
1687 io_ctx
.set_namespace(cfg
->nsname
);
1689 r
= rbd
.open(io_ctx
, image
, cfg
->imgname
.c_str());
1693 if (cfg
->exclusive
) {
1694 r
= image
.lock_acquire(RBD_LOCK_MODE_EXCLUSIVE
);
1696 cerr
<< "rbd-nbd: failed to acquire exclusive lock: " << cpp_strerror(r
)
1702 if (cfg
->snapid
!= CEPH_NOSNAP
) {
1703 r
= image
.snap_set_by_id(cfg
->snapid
);
1705 cerr
<< "rbd-nbd: failed to set snap id: " << cpp_strerror(r
)
1709 } else if (!cfg
->snapname
.empty()) {
1710 r
= image
.snap_set(cfg
->snapname
.c_str());
1712 cerr
<< "rbd-nbd: failed to set snap name: " << cpp_strerror(r
)
1718 if (!cfg
->encryption_formats
.empty()) {
1719 EncryptionOptions encryption_options
;
1720 encryption_options
.specs
.reserve(cfg
->encryption_formats
.size());
1722 for (size_t i
= 0; i
< cfg
->encryption_formats
.size(); ++i
) {
1723 std::ifstream
file(cfg
->encryption_passphrase_files
[i
],
1724 std::ios::in
| std::ios::binary
);
1727 std::cerr
<< "rbd-nbd: unable to open passphrase file '"
1728 << cfg
->encryption_passphrase_files
[i
] << "': "
1729 << cpp_strerror(r
) << std::endl
;
1732 std::string
passphrase((std::istreambuf_iterator
<char>(file
)),
1733 std::istreambuf_iterator
<char>());
1736 switch (cfg
->encryption_formats
[i
]) {
1737 case RBD_ENCRYPTION_FORMAT_LUKS
: {
1738 auto opts
= new librbd::encryption_luks_format_options_t
{
1739 std::move(passphrase
)};
1740 encryption_options
.specs
.push_back(
1741 {RBD_ENCRYPTION_FORMAT_LUKS
, opts
, sizeof(*opts
)});
1744 case RBD_ENCRYPTION_FORMAT_LUKS1
: {
1745 auto opts
= new librbd::encryption_luks1_format_options_t
{
1746 .passphrase
= std::move(passphrase
)};
1747 encryption_options
.specs
.push_back(
1748 {RBD_ENCRYPTION_FORMAT_LUKS1
, opts
, sizeof(*opts
)});
1751 case RBD_ENCRYPTION_FORMAT_LUKS2
: {
1752 auto opts
= new librbd::encryption_luks2_format_options_t
{
1753 .passphrase
= std::move(passphrase
)};
1754 encryption_options
.specs
.push_back(
1755 {RBD_ENCRYPTION_FORMAT_LUKS2
, opts
, sizeof(*opts
)});
1763 r
= image
.encryption_load2(encryption_options
.specs
.data(),
1764 encryption_options
.specs
.size());
1766 cerr
<< "rbd-nbd: failed to load encryption: " << cpp_strerror(r
)
1771 // luks2 block size can vary upto 4096, while luks1 always uses 512
1772 // currently we don't have an rbd API for querying the loaded encryption
1776 r
= image
.stat(info
, sizeof(info
));
1780 flags
= NBD_FLAG_SEND_FLUSH
| NBD_FLAG_HAS_FLAGS
;
1782 flags
|= NBD_FLAG_SEND_TRIM
;
1784 if (!cfg
->snapname
.empty() || cfg
->readonly
) {
1785 flags
|= NBD_FLAG_READ_ONLY
;
1789 if (info
.size
> ULONG_MAX
) {
1791 cerr
<< "rbd-nbd: image is too large (" << byte_u_t(info
.size
)
1792 << ", max is " << byte_u_t(ULONG_MAX
) << ")" << std::endl
;
1798 r
= load_module(cfg
);
1802 server
= start_server(fd
[1], image
, cfg
);
1804 use_netlink
= cfg
->try_netlink
|| reconnect
;
1806 // generate when the cookie is not supplied at CLI
1807 if (!reconnect
&& cfg
->cookie
.empty()) {
1809 uuid_gen
.generate_random();
1810 cfg
->cookie
= uuid_gen
.to_string();
1812 r
= try_netlink_setup(cfg
, fd
[0], size
, flags
, reconnect
);
1815 } else if (r
== 1) {
1816 use_netlink
= false;
1821 r
= try_ioctl_setup(cfg
, fd
[0], size
, blksize
, flags
);
1826 r
= check_device_size(nbd_index
, size
);
1830 r
= ioctl(nbd
, BLKROSET
, (unsigned long) &read_only
);
1837 NBDQuiesceWatchCtx
quiesce_watch_ctx(server
);
1839 r
= image
.quiesce_watch(&quiesce_watch_ctx
,
1840 &server
->quiesce_watch_handle
);
1848 NBDWatchCtx
watch_ctx(nbd
, nbd_index
, use_netlink
, io_ctx
, image
,
1850 r
= image
.update_watch(&watch_ctx
, &handle
);
1856 cookie
= get_cookie(cfg
->devpath
);
1857 ceph_assert(cookie
== cfg
->cookie
|| cookie
.empty());
1859 if (cfg
->show_cookie
&& !cookie
.empty()) {
1860 cout
<< cfg
->devpath
<< " " << cookie
<< std::endl
;
1862 cout
<< cfg
->devpath
<< std::endl
;
1865 run_server(forker
, server
, use_netlink
);
1868 r
= image
.quiesce_unwatch(server
->quiesce_watch_handle
);
1869 ceph_assert(r
== 0);
1872 r
= image
.update_unwatch(handle
);
1873 ceph_assert(r
== 0);
1879 netlink_disconnect(nbd_index
);
1881 ioctl(nbd
, NBD_CLEAR_SOCK
);
1882 cerr
<< "rbd-nbd: failed to map, status: " << cpp_strerror(-r
)
1897 forker
.exit(r
< 0 ? EXIT_FAILURE
: 0);
1902 static int do_detach(Config
*cfg
)
1904 int r
= kill(cfg
->pid
, SIGTERM
);
1907 cerr
<< "rbd-nbd: failed to terminate " << cfg
->pid
<< ": "
1908 << cpp_strerror(r
) << std::endl
;
1912 return wait_for_terminate(cfg
->pid
, cfg
->reattach_timeout
);
1915 static int do_unmap(Config
*cfg
)
1918 * The netlink disconnect call supports devices setup with netlink or ioctl,
1919 * so we always try that first.
1921 int r
= netlink_disconnect_by_path(cfg
->devpath
);
1927 int nbd
= open(cfg
->devpath
.c_str(), O_RDWR
);
1929 cerr
<< "rbd-nbd: failed to open device: " << cfg
->devpath
<< std::endl
;
1933 r
= ioctl(nbd
, NBD_DISCONNECT
);
1935 cerr
<< "rbd-nbd: the device is not used" << std::endl
;
1946 r
= wait_for_terminate(cfg
->pid
, cfg
->reattach_timeout
);
1952 static int parse_imgpath(const std::string
&imgpath
, Config
*cfg
,
1953 std::ostream
*err_msg
) {
1954 std::regex
pattern("^(?:([^/]+)/(?:([^/@]+)/)?)?([^@]+)(?:@([^/@]+))?$");
1956 if (!std::regex_match(imgpath
, match
, pattern
)) {
1957 std::cerr
<< "rbd-nbd: invalid spec '" << imgpath
<< "'" << std::endl
;
1961 if (match
[1].matched
) {
1962 cfg
->poolname
= match
[1];
1965 if (match
[2].matched
) {
1966 cfg
->nsname
= match
[2];
1969 cfg
->imgname
= match
[3];
1971 if (match
[4].matched
)
1972 cfg
->snapname
= match
[4];
1977 static int do_list_mapped_devices(const std::string
&format
, bool pretty_format
)
1979 bool should_print
= false;
1980 std::unique_ptr
<ceph::Formatter
> f
;
1983 if (format
== "json") {
1984 f
.reset(new JSONFormatter(pretty_format
));
1985 } else if (format
== "xml") {
1986 f
.reset(new XMLFormatter(pretty_format
));
1987 } else if (!format
.empty() && format
!= "plain") {
1988 std::cerr
<< "rbd-nbd: invalid output format: " << format
<< std::endl
;
1993 f
->open_array_section("devices");
1995 tbl
.define_column("id", TextTable::LEFT
, TextTable::LEFT
);
1996 tbl
.define_column("pool", TextTable::LEFT
, TextTable::LEFT
);
1997 tbl
.define_column("namespace", TextTable::LEFT
, TextTable::LEFT
);
1998 tbl
.define_column("image", TextTable::LEFT
, TextTable::LEFT
);
1999 tbl
.define_column("snap", TextTable::LEFT
, TextTable::LEFT
);
2000 tbl
.define_column("device", TextTable::LEFT
, TextTable::LEFT
);
2001 tbl
.define_column("cookie", TextTable::LEFT
, TextTable::LEFT
);
2006 while (it
.get(&cfg
)) {
2007 std::string snap
= (cfg
.snapid
!= CEPH_NOSNAP
?
2008 "@" + std::to_string(cfg
.snapid
) : cfg
.snapname
);
2010 f
->open_object_section("device");
2011 f
->dump_int("id", cfg
.pid
);
2012 f
->dump_string("pool", cfg
.poolname
);
2013 f
->dump_string("namespace", cfg
.nsname
);
2014 f
->dump_string("image", cfg
.imgname
);
2015 f
->dump_string("snap", snap
);
2016 f
->dump_string("device", cfg
.devpath
);
2017 f
->dump_string("cookie", cfg
.cookie
);
2020 should_print
= true;
2021 tbl
<< cfg
.pid
<< cfg
.poolname
<< cfg
.nsname
<< cfg
.imgname
2022 << (snap
.empty() ? "-" : snap
) << cfg
.devpath
<< cfg
.cookie
2023 << TextTable::endrow
;
2028 f
->close_section(); // devices
2029 f
->flush(std::cout
);
2037 static bool find_mapped_dev_by_spec(Config
*cfg
, int skip_pid
=-1) {
2040 while (it
.get(&c
)) {
2041 if (c
.pid
!= skip_pid
&&
2042 c
.poolname
== cfg
->poolname
&& c
.nsname
== cfg
->nsname
&&
2043 c
.imgname
== cfg
->imgname
&& c
.snapname
== cfg
->snapname
&&
2044 (cfg
->devpath
.empty() || c
.devpath
== cfg
->devpath
) &&
2045 c
.snapid
== cfg
->snapid
) {
2053 static int find_proc_by_dev(Config
*cfg
) {
2056 while (it
.get(&c
)) {
2057 if (c
.devpath
== cfg
->devpath
) {
2065 static int parse_args(vector
<const char*>& args
, std::ostream
*err_msg
,
2067 std::string conf_file_list
;
2068 std::string cluster
;
2069 CephInitParameters iparams
= ceph_argparse_early_args(
2070 args
, CEPH_ENTITY_TYPE_CLIENT
, &cluster
, &conf_file_list
);
2072 ConfigProxy config
{false};
2073 config
->name
= iparams
.name
;
2074 config
->cluster
= cluster
;
2076 if (!conf_file_list
.empty()) {
2077 config
.parse_config_files(conf_file_list
.c_str(), nullptr, 0);
2079 config
.parse_config_files(nullptr, nullptr, 0);
2081 config
.parse_env(CEPH_ENTITY_TYPE_CLIENT
);
2082 config
.parse_argv(args
);
2083 cfg
->poolname
= config
.get_val
<std::string
>("rbd_default_pool");
2085 std::vector
<const char*>::iterator i
;
2086 std::ostringstream err
;
2087 std::string arg_value
;
2090 for (i
= args
.begin(); i
!= args
.end(); ) {
2091 if (ceph_argparse_flag(args
, i
, "-h", "--help", (char*)NULL
)) {
2093 } else if (ceph_argparse_flag(args
, i
, "-v", "--version", (char*)NULL
)) {
2094 return VERSION_INFO
;
2095 } else if (ceph_argparse_witharg(args
, i
, &cfg
->devpath
, "--device", (char *)NULL
)) {
2096 } else if (ceph_argparse_witharg(args
, i
, &cfg
->io_timeout
, err
,
2097 "--io-timeout", (char *)NULL
)) {
2098 if (!err
.str().empty()) {
2099 *err_msg
<< "rbd-nbd: " << err
.str();
2102 if (cfg
->io_timeout
< 0) {
2103 *err_msg
<< "rbd-nbd: Invalid argument for io-timeout!";
2106 } else if (ceph_argparse_witharg(args
, i
, &cfg
->nbds_max
, err
, "--nbds_max", (char *)NULL
)) {
2107 if (!err
.str().empty()) {
2108 *err_msg
<< "rbd-nbd: " << err
.str();
2111 if (cfg
->nbds_max
< 0) {
2112 *err_msg
<< "rbd-nbd: Invalid argument for nbds_max!";
2115 } else if (ceph_argparse_witharg(args
, i
, &cfg
->max_part
, err
, "--max_part", (char *)NULL
)) {
2116 if (!err
.str().empty()) {
2117 *err_msg
<< "rbd-nbd: " << err
.str();
2120 if ((cfg
->max_part
< 0) || (cfg
->max_part
> 255)) {
2121 *err_msg
<< "rbd-nbd: Invalid argument for max_part(0~255)!";
2124 cfg
->set_max_part
= true;
2125 } else if (ceph_argparse_flag(args
, i
, "--quiesce", (char *)NULL
)) {
2126 cfg
->quiesce
= true;
2127 } else if (ceph_argparse_witharg(args
, i
, &cfg
->quiesce_hook
,
2128 "--quiesce-hook", (char *)NULL
)) {
2129 } else if (ceph_argparse_flag(args
, i
, "--read-only", (char *)NULL
)) {
2130 cfg
->readonly
= true;
2131 } else if (ceph_argparse_witharg(args
, i
, &cfg
->reattach_timeout
, err
,
2132 "--reattach-timeout", (char *)NULL
)) {
2133 if (!err
.str().empty()) {
2134 *err_msg
<< "rbd-nbd: " << err
.str();
2137 if (cfg
->reattach_timeout
< 0) {
2138 *err_msg
<< "rbd-nbd: Invalid argument for reattach-timeout!";
2141 } else if (ceph_argparse_flag(args
, i
, "--exclusive", (char *)NULL
)) {
2142 cfg
->exclusive
= true;
2143 } else if (ceph_argparse_flag(args
, i
, "--notrim", (char *)NULL
)) {
2145 } else if (ceph_argparse_witharg(args
, i
, &cfg
->io_timeout
, err
,
2146 "--timeout", (char *)NULL
)) {
2147 if (!err
.str().empty()) {
2148 *err_msg
<< "rbd-nbd: " << err
.str();
2151 if (cfg
->io_timeout
< 0) {
2152 *err_msg
<< "rbd-nbd: Invalid argument for timeout!";
2155 *err_msg
<< "rbd-nbd: --timeout is deprecated (use --io-timeout)";
2156 } else if (ceph_argparse_witharg(args
, i
, &cfg
->format
, err
, "--format",
2158 } else if (ceph_argparse_flag(args
, i
, "--pretty-format", (char *)NULL
)) {
2159 cfg
->pretty_format
= true;
2160 } else if (ceph_argparse_flag(args
, i
, "--try-netlink", (char *)NULL
)) {
2161 cfg
->try_netlink
= true;
2162 } else if (ceph_argparse_flag(args
, i
, "--show-cookie", (char *)NULL
)) {
2163 cfg
->show_cookie
= true;
2164 } else if (ceph_argparse_witharg(args
, i
, &cfg
->cookie
, "--cookie", (char *)NULL
)) {
2165 } else if (ceph_argparse_witharg(args
, i
, &snapid
, err
,
2166 "--snap-id", (char *)NULL
)) {
2167 if (!err
.str().empty()) {
2168 *err_msg
<< "rbd-nbd: " << err
.str();
2172 *err_msg
<< "rbd-nbd: Invalid argument for snap-id!";
2175 cfg
->snapid
= snapid
;
2176 } else if (ceph_argparse_witharg(args
, i
, &arg_value
,
2177 "--encryption-format", (char *)NULL
)) {
2178 if (arg_value
== "luks1") {
2179 cfg
->encryption_formats
.push_back(RBD_ENCRYPTION_FORMAT_LUKS1
);
2180 } else if (arg_value
== "luks2") {
2181 cfg
->encryption_formats
.push_back(RBD_ENCRYPTION_FORMAT_LUKS2
);
2182 } else if (arg_value
== "luks") {
2183 cfg
->encryption_formats
.push_back(RBD_ENCRYPTION_FORMAT_LUKS
);
2185 *err_msg
<< "rbd-nbd: Invalid encryption format";
2188 } else if (ceph_argparse_witharg(args
, i
, &arg_value
,
2189 "--encryption-passphrase-file",
2191 cfg
->encryption_passphrase_files
.push_back(arg_value
);
2197 if (cfg
->encryption_formats
.empty() &&
2198 !cfg
->encryption_passphrase_files
.empty()) {
2199 cfg
->encryption_formats
.resize(cfg
->encryption_passphrase_files
.size(),
2200 RBD_ENCRYPTION_FORMAT_LUKS
);
2203 if (cfg
->encryption_formats
.size() != cfg
->encryption_passphrase_files
.size()) {
2204 *err_msg
<< "rbd-nbd: Encryption formats count does not match "
2205 << "passphrase files count";
2210 if (args
.begin() != args
.end()) {
2211 if (strcmp(*args
.begin(), "map") == 0) {
2213 } else if (strcmp(*args
.begin(), "unmap") == 0) {
2215 } else if (strcmp(*args
.begin(), "attach") == 0) {
2217 } else if (strcmp(*args
.begin(), "detach") == 0) {
2219 } else if (strcmp(*args
.begin(), "list-mapped") == 0) {
2222 *err_msg
<< "rbd-nbd: unknown command: " << *args
.begin();
2225 args
.erase(args
.begin());
2229 *err_msg
<< "rbd-nbd: must specify command";
2236 if (cfg
->devpath
.empty()) {
2237 *err_msg
<< "rbd-nbd: must specify device to attach";
2240 // Allowing attach without --cookie option for kernel without
2241 // NBD_ATTR_BACKEND_IDENTIFIER support for compatibility
2242 cookie
= get_cookie(cfg
->devpath
);
2243 if (!cookie
.empty()) {
2244 if (cfg
->cookie
.empty()) {
2245 *err_msg
<< "rbd-nbd: must specify cookie to attach";
2247 } else if (cookie
!= cfg
->cookie
) {
2248 *err_msg
<< "rbd-nbd: cookie mismatch";
2251 } else if (!cfg
->cookie
.empty()) {
2252 *err_msg
<< "rbd-nbd: kernel does not have cookie support";
2257 if (args
.begin() == args
.end()) {
2258 *err_msg
<< "rbd-nbd: must specify image-or-snap-spec";
2261 if (parse_imgpath(*args
.begin(), cfg
, err_msg
) < 0) {
2264 args
.erase(args
.begin());
2268 if (args
.begin() == args
.end()) {
2269 *err_msg
<< "rbd-nbd: must specify nbd device or image-or-snap-spec";
2272 if (boost::starts_with(*args
.begin(), "/dev/")) {
2273 cfg
->devpath
= *args
.begin();
2275 if (parse_imgpath(*args
.begin(), cfg
, err_msg
) < 0) {
2279 args
.erase(args
.begin());
2286 if (cfg
->snapid
!= CEPH_NOSNAP
&& !cfg
->snapname
.empty()) {
2287 *err_msg
<< "rbd-nbd: use either snapname or snapid, not both";
2291 if (args
.begin() != args
.end()) {
2292 *err_msg
<< "rbd-nbd: unknown args: " << *args
.begin();
2300 static int rbd_nbd(int argc
, const char *argv
[])
2304 auto args
= argv_to_vec(argc
, argv
);
2305 std::ostringstream err_msg
;
2306 r
= parse_args(args
, &err_msg
, &cfg
);
2307 if (r
== HELP_INFO
) {
2310 } else if (r
== VERSION_INFO
) {
2311 std::cout
<< pretty_version_to_str() << std::endl
;
2314 cerr
<< err_msg
.str() << std::endl
;
2318 if (!err_msg
.str().empty()) {
2319 cerr
<< err_msg
.str() << std::endl
;
2322 switch (cfg
.command
) {
2324 ceph_assert(!cfg
.devpath
.empty());
2325 if (find_mapped_dev_by_spec(&cfg
, getpid())) {
2326 cerr
<< "rbd-nbd: " << cfg
.devpath
<< " has process " << cfg
.pid
2327 << " connected" << std::endl
;
2332 if (cfg
.imgname
.empty()) {
2333 cerr
<< "rbd-nbd: image name was not specified" << std::endl
;
2337 r
= do_map(argc
, argv
, &cfg
, cfg
.command
== Attach
);
2342 if (cfg
.devpath
.empty()) {
2343 if (!find_mapped_dev_by_spec(&cfg
)) {
2344 cerr
<< "rbd-nbd: " << cfg
.image_spec() << " is not mapped"
2348 } else if (!find_proc_by_dev(&cfg
)) {
2349 cerr
<< "rbd-nbd: no process attached to " << cfg
.devpath
<< " found"
2353 r
= do_detach(&cfg
);
2358 if (cfg
.devpath
.empty()) {
2359 if (!find_mapped_dev_by_spec(&cfg
)) {
2360 cerr
<< "rbd-nbd: " << cfg
.image_spec() << " is not mapped"
2364 } else if (!find_proc_by_dev(&cfg
)) {
2365 // still try to send disconnect to the device
2372 r
= do_list_mapped_devices(cfg
.format
, cfg
.pretty_format
);
2384 int main(int argc
, const char *argv
[])
2386 int r
= rbd_nbd(argc
, argv
);
2388 return EXIT_FAILURE
;