From 92359e72bb96f55f4e5f1a9c4936e9a40d3692b2 Mon Sep 17 00:00:00 2001 From: Li Wang Date: Wed, 1 Nov 2017 09:21:29 +0000 Subject: [PATCH] rbd-nbd: fix unused nbd device search bug in container In some container scenarios, the host may choose to map a specific nbd device, for example, /dev/nbd6 into the container, in that case, the nbd device available in the container is not numbered from 0. The current unused nbd device search function will return no result. This patch fixes it. Fixes: http://tracker.ceph.com/issues/22012 Signed-off-by: Li Wang Reviewed-by: Yunchuan Wen (cherry picked from commit be0f9581f9727187ca03232e0b368e7da7a60609) --- src/tools/rbd_nbd/rbd-nbd.cc | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/tools/rbd_nbd/rbd-nbd.cc b/src/tools/rbd_nbd/rbd-nbd.cc index f9115a9e5dd31..ed28922d43f3f 100644 --- a/src/tools/rbd_nbd/rbd-nbd.cc +++ b/src/tools/rbd_nbd/rbd-nbd.cc @@ -558,12 +558,27 @@ static int do_map(int argc, const char *argv[]) if (devpath.empty()) { char dev[64]; bool try_load_module = true; + const char *path = "/sys/module/nbd/parameters/nbds_max"; + int nbds_max = -1; + if (access(path, F_OK) == 0) { + std::ifstream ifs; + ifs.open(path, std::ifstream::in); + if (ifs.is_open()) { + ifs >> nbds_max; + ifs.close(); + } + } + while (true) { snprintf(dev, sizeof(dev), "/dev/nbd%d", index); nbd = open_device(dev, try_load_module); try_load_module = false; if (nbd < 0) { + if (nbd == -EPERM && nbds_max != -1 && index < (nbds_max-1)) { + ++index; + continue; + } r = nbd; cerr << "rbd-nbd: failed to find unused device" << std::endl; goto close_fd; -- 2.39.5