From 391cc394d384fc0d91832fb6aba91a0c4bdb6f2d Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Wed, 25 Nov 2026 20:01:29 +0300 Subject: [PATCH] Show netlink-revived devices in vitastor-nbd ls ls listed a device only when /sys/block/nbdN/pid was a running map or netlink-map process. The kernel sets that pid on CONNECT and keeps it on RECONFIGURE, so after netlink-revive it names the dead process, and the netlink-revive command wasn't accepted anyway: ls showed nothing for a working device. Look such a device up among the running netlink-revive processes in /proc. The help and docs said netlink devices never show up in ls; say instead that the sysfs pid of a revived device stays the old one. --- .gitea/workflows/test.yml | 18 ++++++ docs/usage/nbd.en.md | 2 +- docs/usage/nbd.ru.md | 2 +- src/client/nbd_proxy.cpp | 94 ++++++++++++++++++++++---------- tests/run_tests.sh | 1 + tests/test_nbd_netlink_revive.sh | 28 ++++++++++ 6 files changed, 113 insertions(+), 32 deletions(-) create mode 100755 tests/test_nbd_netlink_revive.sh diff --git a/.gitea/workflows/test.yml b/.gitea/workflows/test.yml index 38c77787..47d010a3 100644 --- a/.gitea/workflows/test.yml +++ b/.gitea/workflows/test.yml @@ -365,6 +365,24 @@ jobs: echo "" done + test_nbd_netlink_revive: + 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_netlink_revive.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_no_uring: runs-on: ubuntu-latest needs: build diff --git a/docs/usage/nbd.en.md b/docs/usage/nbd.en.md index 8d636661..de029ade 100644 --- a/docs/usage/nbd.en.md +++ b/docs/usage/nbd.en.md @@ -104,7 +104,7 @@ This is an experimental feature because it doesn't solve all issues of NBD. Diff 1. netlink-map can create new `/dev/nbdN` devices (those not present in /dev/). 2. netlink-mapped devices can be unmapped only using `netlink-unmap` command. -3. netlink-mapped devices don't show up `ls` output (yet). +3. After `netlink-revive`, `/sys/block/nbdN/pid` still shows the process that mapped the device, `ls` shows the one serving it. 4. Dead netlink-mapped devices can be 'revived' using `netlink-revive`. However, old I/O requests will hang forever if `nbd_timeout` is not specified. 5. netlink-map supports additional options: diff --git a/docs/usage/nbd.ru.md b/docs/usage/nbd.ru.md index f67f1d0e..c160639a 100644 --- a/docs/usage/nbd.ru.md +++ b/docs/usage/nbd.ru.md @@ -113,7 +113,7 @@ vitastor-nbd netlink-map [/dev/nbdN] (--image | --pool --inode ] (--image | --pool --inode --size )\n" " Map a device using netlink interface. Experimental mode. Differences from 'map':\n" " 1) netlink-map can create new /dev/nbdN devices.\n" " 2) netlink-mapped devices can be unmapped only using netlink-unmap command.\n" - " 3) netlink-mapped devices don't show up `ls` output (yet).\n" + " 3) /sys/block/nbdN/pid of a revived device keeps the pid of the process that mapped it, `ls` shows the new one.\n" " 4) dead netlink-mapped devices can be 'revived' (however, old I/O may hang forever without timeout).\n" " 5) netlink-map supports additional options:\n" " --nbd_conn_timeout 0\n" @@ -887,6 +887,14 @@ help: } char path[64] = { 0 }; json11::Json::object mapped; + auto add_mapped = [&](int dev_num, int pid, json11::Json::object & cfg) + { + cfg["interface"] = (cfg["command"] == "map") ? "nbd" : "netlink"; + cfg.erase("command"); + cfg["pid"] = pid; + mapped["/dev/nbd"+std::to_string(dev_num)] = cfg; + }; + std::vector unserved; int dev_num = -1; int pid; // Not a counter: netlink devices can sit above nbds_max or above a destroyed device @@ -910,42 +918,68 @@ help: printf("Failed to read pid from /sys/block/nbd%d/pid\n", dev_num); continue; } - snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); - std::string cmdline = read_file(path); - std::vector argv; - int last = 0; - for (int i = 0; i < cmdline.size(); i++) + json11::Json::object cfg = read_own_cmdline(pid, self_filename); + if (cfg["command"] == "map" || cfg["command"] == "netlink-map") + add_mapped(dev_num, pid, cfg); + else + unserved.push_back(dev_num); + } + // The kernel keeps the pid of the process that mapped the device, so find netlink-revive by its arguments + DIR *dir = unserved.size() ? opendir("/proc") : NULL; + while (dir) + { + dirent *ent = readdir(dir); + if (!ent) { - if (cmdline[i] == 0) - { - argv.push_back(cmdline.c_str()+last); - last = i+1; - } + closedir(dir); + break; } - if (argv.size() > 0) + pid = atoi(ent->d_name); + if (pid <= 0) + continue; + json11::Json::object cfg = read_own_cmdline(pid, self_filename); + if (cfg["command"] != "netlink-revive") + continue; + for (int dev_num: unserved) { - const char *pid_filename = argv[0]; - for (int i = 0; argv[0][i] != 0; i++) - { - if (argv[0][i] == '/') - pid_filename = argv[0]+i+1; - } - if (!strcmp(pid_filename, self_filename)) - { - json11::Json::object cfg = nbd_proxy::parse_args(argv.size(), argv.data()); - if (cfg["command"] == "map" || cfg["command"] == "netlink-map") - { - cfg["interface"] = (cfg["command"] == "netlink-map") ? "netlink" : "nbd"; - cfg.erase("command"); - cfg["pid"] = pid; - mapped["/dev/nbd"+std::to_string(dev_num)] = cfg; - } - } + if (cfg["dev_num"].as_string() == std::to_string(dev_num)) + add_mapped(dev_num, pid, cfg); } } return mapped; } + // Parsed arguments of the process if it is ours, an empty object otherwise + static json11::Json::object read_own_cmdline(int pid, const char *self_filename) + { + int fd = open(("/proc/"+std::to_string(pid)+"/cmdline").c_str(), O_RDONLY); + if (fd < 0) + return {}; + std::string cmdline = read_all_fd(fd); + close(fd); + std::vector argv; + int last = 0; + for (int i = 0; i < cmdline.size(); i++) + { + if (cmdline[i] == 0) + { + argv.push_back(cmdline.c_str()+last); + last = i+1; + } + } + if (!argv.size()) + return {}; + const char *pid_filename = argv[0]; + for (int i = 0; argv[0][i] != 0; i++) + { + if (argv[0][i] == '/') + pid_filename = argv[0]+i+1; + } + if (strcmp(pid_filename, self_filename)) + return {}; + return nbd_proxy::parse_args(argv.size(), argv.data()); + } + void print_mapped(json11::Json mapped, bool json) { if (json) diff --git a/tests/run_tests.sh b/tests/run_tests.sh index f96853d2..406b9866 100755 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -29,6 +29,7 @@ ETCD_SCHEME=https ANTIETCD=1 ./test_etcd_fail.sh ETCD_SCHEME=https ./test_snapshot.sh ./test_img_online_resize.sh +./test_nbd_netlink_revive.sh ./test_no_uring.sh diff --git a/tests/test_nbd_netlink_revive.sh b/tests/test_nbd_netlink_revive.sh new file mode 100755 index 00000000..243ca2f9 --- /dev/null +++ b/tests/test_nbd_netlink_revive.sh @@ -0,0 +1,28 @@ +#!/bin/bash -ex + +. `dirname $0`/run_3osds.sh + +$VITASTOR_CLI create -s 16M testimg +dd if=/dev/urandom of=./testdata/img1.bin bs=4096 count=1 +$VITASTOR_CLI dd if=./testdata/img1.bin oimg=testimg bs=4096 count=1 + +if ! (mount | grep '/dev type devtmpfs'); then + sudo bash -c 'mount udev /dev/ -t devtmpfs && mount devpts /dev/pts -t devpts' +fi + +NBD_DEV=$(sudo build/src/client/vitastor-nbd --config_path $VITASTOR_CFG netlink-map --image testimg --logfile $(pwd)/testdata/nbd.log) +trap "sudo build/src/client/vitastor-nbd netlink-unmap $NBD_DEV"' || true; kill -9 $(jobs -p)' EXIT + +# Kill the daemon: the device stays and waits for netlink-revive +# Not /sys/block/nbdN/pid: that is a pid of the kernel's pid namespace, a container has its own +for p in /proc/[0-9]*; do [[ "$(tr '\0' ' ' 2>/dev/null < $p/cmdline)" == *" netlink-map --image testimg "* ]] && NBD_PID=${p#/proc/}; done +sudo kill -9 $NBD_PID +while grep -qs '^State:\s*[^Z]' /proc/$NBD_PID/status; do sleep 0.1; done + +sudo build/src/client/vitastor-nbd --config_path $VITASTOR_CFG netlink-revive $NBD_DEV --image testimg --logfile $(pwd)/testdata/nbd.log +sudo dd if=$NBD_DEV of=./testdata/img1.read bs=4096 count=1 iflag=direct +cmp ./testdata/img1.bin ./testdata/img1.read +LS_PID=$(sudo build/src/client/vitastor-nbd ls --json | jq -r '.["'$NBD_DEV'"].pid') +[[ "$LS_PID" != "null" && "$LS_PID" != "$NBD_PID" ]] && sudo kill -0 $LS_PID || format_error "ls doesn't show the revived $NBD_DEV" + +format_green OK -- 2.47.3