From 4ab8d426530c4c32d0d6c2c40bfd93aac73b1bca Mon Sep 17 00:00:00 2001 From: Prasanna Kumar Kalever Date: Wed, 11 Aug 2021 15:32:25 +0530 Subject: [PATCH] rbd-nbd: allow user to specify cookie at map Allow user to specify cookie of choice at the time of map $ rbd device attach rbd-pool/image --device /dev/nbd0 \ --cookie 6f85d970-10b2-456b-8baf-676aa4d782e4 --options try-netlink Signed-off-by: Prasanna Kumar Kalever (cherry picked from commit 16404dede832f3908d48fc36c7714891bfcfde20) --- doc/man/8/rbd.rst | 4 ++-- src/test/cli/rbd/help.t | 11 +++++++---- src/tools/rbd/action/Device.cc | 2 ++ src/tools/rbd/action/Nbd.cc | 9 +++++++++ src/tools/rbd_nbd/rbd-nbd.cc | 9 +++++---- 5 files changed, 25 insertions(+), 10 deletions(-) diff --git a/doc/man/8/rbd.rst b/doc/man/8/rbd.rst index 5010d535cd3e8..e50086bfbaeb5 100644 --- a/doc/man/8/rbd.rst +++ b/doc/man/8/rbd.rst @@ -253,7 +253,7 @@ Commands Show the rbd images that are mapped via the rbd kernel module (default) or other supported device. -:command:`device map` [-t | --device-type *device-type*] [--show-cookie] [--read-only] [--exclusive] [-o | --options *device-options*] *image-spec* | *snap-spec* +:command:`device map` [-t | --device-type *device-type*] [--cookie *device-cookie*] [--show-cookie] [--read-only] [--exclusive] [-o | --options *device-options*] *image-spec* | *snap-spec* Map the specified image to a block device via the rbd kernel module (default) or other supported device (*nbd* on Linux or *ggate* on FreeBSD). @@ -268,7 +268,7 @@ Commands The --options argument is a comma separated list of device type specific options (opt1,opt2=val,...). -:command:`device attach` [-t | --device-type *device-type*] --device *device-path* [--cookie *device-cookie*] [--read-only] [--exclusive] [--force] [-o | --options *device-options*] *image-spec* | *snap-spec* +:command:`device attach` [-t | --device-type *device-type*] --device *device-path* [--cookie *device-cookie*] [--show-cookie] [--read-only] [--exclusive] [--force] [-o | --options *device-options*] *image-spec* | *snap-spec* Attach the specified image to the specified block device (currently only `nbd` on Linux). This operation is unsafe and should not be normally used. In particular, specifying the wrong image or the wrong block device may diff --git a/src/test/cli/rbd/help.t b/src/test/cli/rbd/help.t index fb419da45fa95..2b4c14eeacecd 100644 --- a/src/test/cli/rbd/help.t +++ b/src/test/cli/rbd/help.t @@ -575,8 +575,9 @@ rbd help device attach usage: rbd device attach [--device-type ] [--pool ] [--namespace ] [--image ] - [--snap ] --device [--cookie ] - [--read-only] [--force] [--exclusive] [--quiesce] + [--snap ] --device [--show-cookie] + [--cookie ] [--read-only] [--force] + [--exclusive] [--quiesce] [--quiesce-hook ] [--options ] @@ -595,6 +596,7 @@ --image arg image name --snap arg snapshot name --device arg specify device path + --show-cookie show device cookie --cookie arg specify device cookie --read-only attach read-only --force force attach @@ -637,8 +639,8 @@ rbd help device map usage: rbd device map [--device-type ] [--pool ] [--namespace ] [--image ] - [--snap ] [--show-cookie] [--read-only] - [--exclusive] [--quiesce] + [--snap ] [--show-cookie] [--cookie ] + [--read-only] [--exclusive] [--quiesce] [--quiesce-hook ] [--options ] @@ -657,6 +659,7 @@ --image arg image name --snap arg snapshot name --show-cookie show device cookie + --cookie arg specify device cookie --read-only map read-only --exclusive disable automatic exclusive lock transitions --quiesce use quiesce hooks diff --git a/src/tools/rbd/action/Device.cc b/src/tools/rbd/action/Device.cc index 99746e0f49511..bfe60c273df79 100644 --- a/src/tools/rbd/action/Device.cc +++ b/src/tools/rbd/action/Device.cc @@ -176,6 +176,7 @@ void get_map_arguments(po::options_description *positional, at::ARGUMENT_MODIFIER_NONE); options->add_options() ("show-cookie", po::bool_switch(), "show device cookie") + ("cookie", po::value(), "specify device cookie") ("read-only", po::bool_switch(), "map read-only") ("exclusive", po::bool_switch(), "disable automatic exclusive lock transitions") ("quiesce", po::bool_switch(), "use quiesce hooks") @@ -213,6 +214,7 @@ void get_attach_arguments(po::options_description *positional, at::ARGUMENT_MODIFIER_NONE); options->add_options() ("device", po::value()->required(), "specify device path") + ("show-cookie", po::bool_switch(), "show device cookie") ("cookie", po::value(), "specify device cookie") ("read-only", po::bool_switch(), "attach read-only") ("force", po::bool_switch(), "force attach") diff --git a/src/tools/rbd/action/Nbd.cc b/src/tools/rbd/action/Nbd.cc index b6662e0842e59..68345f43dd6c5 100644 --- a/src/tools/rbd/action/Nbd.cc +++ b/src/tools/rbd/action/Nbd.cc @@ -163,6 +163,10 @@ int execute_attach(const po::variables_map &vm, return -EINVAL; } + if (vm["show-cookie"].as()) { + args.push_back("--show-cookie"); + } + if (vm.count("cookie")) { args.push_back("--cookie"); args.push_back(vm["cookie"].as()); @@ -266,6 +270,11 @@ int execute_map(const po::variables_map &vm, args.push_back("--show-cookie"); } + if (vm.count("cookie")) { + args.push_back("--cookie"); + args.push_back(vm["cookie"].as()); + } + if (vm["read-only"].as()) { args.push_back("--read-only"); } diff --git a/src/tools/rbd_nbd/rbd-nbd.cc b/src/tools/rbd_nbd/rbd-nbd.cc index 9d146ac806d6d..eb9e858f4afe8 100644 --- a/src/tools/rbd_nbd/rbd-nbd.cc +++ b/src/tools/rbd_nbd/rbd-nbd.cc @@ -163,7 +163,7 @@ static void usage() << " (default: " << Config().reattach_timeout << ")\n" << " --try-netlink Use the nbd netlink interface\n" << " --show-cookie Show device cookie\n" - << " --cookie Specify device cookie for attach\n" + << " --cookie Specify device cookie\n" << "\n" << "List options:\n" << " --format plain|json|xml Output format (default: plain)\n" @@ -1746,7 +1746,8 @@ static int do_map(int argc, const char *argv[], Config *cfg, bool reconnect) use_netlink = cfg->try_netlink || reconnect; if (use_netlink) { - if (!reconnect) { + // generate when the cookie is not supplied at CLI + if (!reconnect && cfg->cookie.empty()) { uuid_d uuid_gen; uuid_gen.generate_random(); cfg->cookie = uuid_gen.to_string(); @@ -1794,9 +1795,9 @@ static int do_map(int argc, const char *argv[], Config *cfg, bool reconnect) goto close_nbd; std::string cookie; - if (use_netlink && !cfg->cookie.empty()) { + if (use_netlink) { cookie = get_cookie(cfg->devpath); - ceph_assert(cookie == cfg->cookie); + ceph_assert(cookie == cfg->cookie || cookie.empty()); } if (cfg->show_cookie && !cookie.empty()) { cout << cfg->devpath << " " << cookie << std::endl; -- 2.39.5