From 833e71282ad82023237c5eb407b9fa4630280c15 Mon Sep 17 00:00:00 2001 From: Prasanna Kumar Kalever Date: Thu, 17 Jun 2021 19:05:20 +0530 Subject: [PATCH] rbd-nbd: allow attach without --cookie for old kernel versions For backward compatibility allow attach without --cookie option: [root@linux-vm1]# rbd-nbd attach rbd-pool/image0 --device /dev/nbd0 /dev/nbd0 Signed-off-by: Prasanna Kumar Kalever --- src/tools/rbd_nbd/rbd-nbd.cc | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/tools/rbd_nbd/rbd-nbd.cc b/src/tools/rbd_nbd/rbd-nbd.cc index f8c24397542..5a9492daa30 100644 --- a/src/tools/rbd_nbd/rbd-nbd.cc +++ b/src/tools/rbd_nbd/rbd-nbd.cc @@ -2144,20 +2144,26 @@ static int parse_args(vector& args, std::ostream *err_msg, return -EINVAL; } + std::string cookie; switch (cmd) { case Attach: if (cfg->devpath.empty()) { *err_msg << "rbd-nbd: must specify device to attach"; return -EINVAL; } - /* FIXME: Should we allow attach for kernel versions that doesn't - * support NBD_ATTR_BACKEND_IDENTIFIER (danger!) ? - * 1. If we allow attach for lower kernel versions then, we should - * also provide a way to skip cookie check here - * 2. If we do not want to allow, then add kernel version check here - */ - if (cfg->cookie.empty()) { - *err_msg << "rbd-nbd: must specify cookie to attach"; + // Allowing attach without --cookie option for kernel without + // NBD_ATTR_BACKEND_IDENTIFIER support for compatibility + cookie = get_cookie(cfg->devpath); + if (!cookie.empty()) { + if (cfg->cookie.empty()) { + *err_msg << "rbd-nbd: must specify cookie to attach"; + return -EINVAL; + } else if (cookie != cfg->cookie) { + *err_msg << "rbd-nbd: cookie mismatch"; + return -EINVAL; + } + } else if (!cfg->cookie.empty()) { + *err_msg << "rbd-nbd: kernel does not have cookie support"; return -EINVAL; } [[fallthrough]]; -- 2.39.5