From a911d896d1f07c314b27ccaa4cf5b6b3c5c44d9d Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Thu, 26 Nov 2026 04:26:35 +0300 Subject: [PATCH] Show NBD devices above a numbering gap in vitastor-nbd ls ls counted /sys/block/nbd0, nbd1, ... and stopped at the first missing number. netlink-map can create a device above nbds_max, and a device mapped with --nbd_destroy_on_disconnect disappears on unmap, so devices after such a gap were never listed. Read the nbd entries of /sys/block instead. --- .gitea/workflows/test.yml | 18 ++++++++++++++++++ src/client/nbd_proxy.cpp | 15 +++++++++++---- tests/run_tests.sh | 1 + tests/test_nbd_ls.sh | 20 ++++++++++++++++++++ 4 files changed, 50 insertions(+), 4 deletions(-) create mode 100755 tests/test_nbd_ls.sh diff --git a/.gitea/workflows/test.yml b/.gitea/workflows/test.yml index ad781d0c..eec7083a 100644 --- a/.gitea/workflows/test.yml +++ b/.gitea/workflows/test.yml @@ -329,6 +329,24 @@ jobs: echo "" done + test_nbd_ls: + runs-on: ubuntu-latest + needs: build + container: ${{env.TEST_IMAGE}}:${{github.sha}} + steps: + - name: Run test + id: test + timeout-minutes: 3 + run: /root/vitastor/tests/test_nbd_ls.sh + - name: Print logs + if: always() && steps.test.outcome == 'failure' + run: | + for i in /root/vitastor/testdata/*.log /root/vitastor/testdata/*.txt; do + echo "-------- $i --------" + cat $i + echo "" + done + test_img_online_resize: runs-on: ubuntu-latest needs: build diff --git a/src/client/nbd_proxy.cpp b/src/client/nbd_proxy.cpp index 2880f709..138a78ea 100644 --- a/src/client/nbd_proxy.cpp +++ b/src/client/nbd_proxy.cpp @@ -14,6 +14,7 @@ #include #include +#include #include #include #include @@ -887,12 +888,18 @@ help: json11::Json::object mapped; int dev_num = -1; int pid; - while (true) + // Not a counter: netlink devices can sit above nbds_max or above a destroyed device + DIR *sys_block = opendir("/sys/block"); + while (sys_block) { - dev_num++; - snprintf(path, sizeof(path), "/sys/block/nbd%d", dev_num); - if (access(path, F_OK) != 0) + dirent *ent = readdir(sys_block); + if (!ent) + { + closedir(sys_block); break; + } + if (sscanf(ent->d_name, "nbd%d", &dev_num) != 1) + continue; snprintf(path, sizeof(path), "/sys/block/nbd%d/pid", dev_num); std::string pid_str = read_file(path); if (pid_str == "") diff --git a/tests/run_tests.sh b/tests/run_tests.sh index caac41b7..53fa89d1 100755 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -27,6 +27,7 @@ ETCD_SCHEME=https ./test_etcd_fail.sh ETCD_SCHEME=https ANTIETCD=1 ./test_etcd_fail.sh ETCD_SCHEME=https ./test_snapshot.sh +./test_nbd_ls.sh ./test_img_online_resize.sh ./test_no_uring.sh diff --git a/tests/test_nbd_ls.sh b/tests/test_nbd_ls.sh new file mode 100755 index 00000000..deefbe89 --- /dev/null +++ b/tests/test_nbd_ls.sh @@ -0,0 +1,20 @@ +#!/bin/bash -ex + +. `dirname $0`/run_3osds.sh + +$VITASTOR_CLI create -s 16M testimg + +if ! (mount | grep '/dev type devtmpfs'); then + sudo bash -c 'mount udev /dev/ -t devtmpfs && mount devpts /dev/pts -t devpts' +fi + +# A device number above nbds_max: netlink-map creates it, with a gap below it +NBD_NUM=$((1000 + RANDOM % 30000)) +while [[ -e /sys/block/nbd$NBD_NUM ]]; do NBD_NUM=$((NBD_NUM+1)); done +NBD_DEV=$(sudo build/src/client/vitastor-nbd --config_path $VITASTOR_CFG netlink-map /dev/nbd$NBD_NUM --image testimg --nbd_destroy_on_disconnect 1) +trap "sudo build/src/client/vitastor-nbd netlink-unmap $NBD_DEV"' || true; kill -9 $(jobs -p)' EXIT + +LS_PID=$(sudo build/src/client/vitastor-nbd ls --json | jq -r '.["'$NBD_DEV'"].pid') +[[ "$LS_PID" == "$(cat /sys/block/nbd$NBD_NUM/pid)" ]] || format_error "ls doesn't show $NBD_DEV" + +format_green OK -- 2.47.3