Skip to content

Commit

Permalink
rbd-nbd: fix unused nbd device search bug in container
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Yunchuan Wen <[email protected]>
  • Loading branch information
dragonylffly committed Nov 3, 2017
1 parent 07d5b81 commit be0f958
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/tools/rbd_nbd/rbd-nbd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -605,12 +605,27 @@ static int do_map(int argc, const char *argv[], Config *cfg)
if (cfg->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, cfg, 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;
Expand Down

0 comments on commit be0f958

Please sign in to comment.