Show netlink-revived devices in vitastor-nbd ls #209

Open
koolkhel wants to merge 1 commits from koolkhel/vitastor:nbd-ls-revive into master
6 changed files with 113 additions and 32 deletions

View File

@@ -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

View File

@@ -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:

View File

@@ -113,7 +113,7 @@ vitastor-nbd netlink-map [/dev/nbdN] (--image <image> | --pool <POOL> --inode <I
1. Можно создавать новые `/dev/nbdN` устройства (отсутствующие в /dev/).
2. Отключать netlink-устройства можно только командой `netlink-unmap`.
3. netlink-устройства не видно в выводе `ls` (пока что).
3. После `netlink-revive` в `/sys/block/nbdN/pid` остаётся PID процесса, подключившего устройство, а `ls` показывает обслуживающий его процесс.
4. Мёртвые netlink-устройства можно "оживить" командой `netlink-revive`. Правда, предыдущие
запросы ввода-вывода всё равно зависнут навсегда, если `nbd_timeout` не задан.
5. Поддерживаются дополнительные опции:

View File

@@ -286,14 +286,14 @@ const char *help_text =
" Unmap an ioctl-mapped NBD device. Do not check if it's actually mapped if --force is specified.\n"
"\n"
"vitastor-nbd ls [--json]\n"
" List ioctl-mapped Vitastor NBD devices, optionally in JSON format.\n"
" List mapped Vitastor NBD devices, optionally in JSON format.\n"
"\n"
#ifdef HAVE_NBD_NETLINK_H
"vitastor-nbd netlink-map [/dev/nbd<number>] (--image <image> | --pool <pool> --inode <inode> --size <size in bytes>)\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<int> 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<const char*> 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<const char*> 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)

View File

@@ -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

View File

@@ -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