From: Prasanna Kumar Kalever Date: Wed, 12 May 2021 13:03:54 +0000 (+0530) Subject: rbd-nbd: provide a flag to show device cookie with map command X-Git-Tag: v17.1.0~589^2~6 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=59c08eabf12fdcf155b54dc422fb0f67d6ad747e;p=ceph.git rbd-nbd: provide a flag to show device cookie with map command [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 --- diff --git a/src/tools/rbd_nbd/rbd-nbd.cc b/src/tools/rbd_nbd/rbd-nbd.cc index 5356dfb5798c3..ccf9b0d2d421d 100644 --- a/src/tools/rbd_nbd/rbd-nbd.cc +++ b/src/tools/rbd_nbd/rbd-nbd.cc @@ -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 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& 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") {