]> 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)
committerPrasanna Kumar Kalever <prasanna.kalever@redhat.com>
Tue, 26 Oct 2021 13:54:13 +0000 (19:24 +0530)
[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>
src/tools/rbd_nbd/rbd-nbd.cc

index 5356dfb5798c3892bf5c19a7ae42c9c470f5df26..ccf9b0d2d421d5f2f7bc64ab8c7e8ccafa791f60 100644 (file)
@@ -107,6 +107,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;
@@ -165,6 +166,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"
@@ -191,6 +193,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:
@@ -930,6 +934,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;
@@ -1769,7 +1787,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);
 
@@ -2064,6 +2091,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") {