]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
rbd-nbd: provide a flag to show device cookie with map command
authorPrasanna Kumar Kalever <prasanna.kalever@redhat.com>
Wed, 12 May 2021 13:03:54 +0000 (18:33 +0530)
committerIlya Dryomov <idryomov@gmail.com>
Wed, 23 Mar 2022 12:31:59 +0000 (13:31 +0100)
[root@linux-vm1]# rbd-nbd map rbd-pool/image0 --try-netlink --show-cookie
/dev/nbd0 c704cb91-c6cf-466e-a335-0e935c0d5e47

Signed-off-by: Prasanna Kumar Kalever <prasanna.kalever@redhat.com>
(cherry picked from commit 59c08eabf12fdcf155b54dc422fb0f67d6ad747e)

src/tools/rbd_nbd/rbd-nbd.cc

index 98090860fe11529ef388d6f94ccb4157ac807110..51abe3763a21ac26a4d4ff2eee6b824eb395af7d 100644 (file)
@@ -104,6 +104,7 @@ struct Config {
   bool readonly = false;
   bool set_max_part = false;
   bool try_netlink = false;
+  bool show_cookie = false;
 
   std::string poolname;
   std::string nsname;
@@ -161,6 +162,7 @@ static void usage()
             << "  --reattach-timeout <sec>      Set nbd re-attach timeout\n"
             << "                                (default: " << Config().reattach_timeout << ")\n"
             << "  --try-netlink                 Use the nbd netlink interface\n"
+            << "  --show-cookie                 Show device cookie\n"
             << "\n"
             << "List options:\n"
             << "  --format plain|json|xml Output format (default: plain)\n"
@@ -196,6 +198,8 @@ static int run_quiesce_hook(const std::string &quiesce_hook,
                             const std::string &devpath,
                             const std::string &command);
 
+static std::string get_cookie(const std::string &devpath);
+
 class NBDServer
 {
 public:
@@ -935,6 +939,20 @@ private:
   }
 };
 
+static std::string get_cookie(const std::string &devpath)
+{
+  std::string cookie;
+  std::ifstream ifs;
+  std::string path = "/sys/block/" + devpath.substr(sizeof("/dev/") - 1) + "/backend";
+
+  ifs.open(path, std::ifstream::in);
+  if (ifs.is_open()) {
+    std::getline(ifs, cookie);
+    ifs.close();
+  }
+  return cookie;
+}
+
 static int load_module(Config *cfg)
 {
   ostringstream param;
@@ -1772,7 +1790,16 @@ static int do_map(int argc, const char *argv[], Config *cfg, bool reconnect)
     if (r < 0)
       goto close_nbd;
 
-    cout << cfg->devpath << std::endl;
+    std::string cookie;
+    if (use_netlink && !cfg->cookie.empty()) {
+      cookie = get_cookie(cfg->devpath);
+      ceph_assert(cookie == cfg->cookie);
+    }
+    if (cfg->show_cookie && !cookie.empty()) {
+      cout << cfg->devpath << " " << cookie << std::endl;
+    } else {
+      cout << cfg->devpath << std::endl;
+    }
 
     run_server(forker, server, use_netlink);
 
@@ -2065,6 +2092,8 @@ static int parse_args(vector<const char*>& args, std::ostream *err_msg,
       cfg->pretty_format = true;
     } else if (ceph_argparse_flag(args, i, "--try-netlink", (char *)NULL)) {
       cfg->try_netlink = true;
+    } else if (ceph_argparse_flag(args, i, "--show-cookie", (char *)NULL)) {
+      cfg->show_cookie = true;
     } else if (ceph_argparse_witharg(args, i, &arg_value,
                                      "--encryption-format", (char *)NULL)) {
       if (arg_value == "luks1") {