Refuse to revive an NBD device with another image #155

Open
koolkhel wants to merge 1 commits from koolkhel/vitastor:nbd-backend-identifier into master
8 changed files with 64 additions and 2 deletions

View File

@@ -347,6 +347,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

@@ -133,3 +133,5 @@ vitastor-nbd netlink-revive /dev/nbdX (--image <image> | --pool <pool> --inode <
```
Restart a dead NBD netlink-mapped device without removing it. Supports the same options as `netlink-map`.
The kernel refuses to revive the device with another image than `netlink-map` used (Linux 5.6+),
and an older `vitastor-nbd` can't revive a device mapped by this version.

View File

@@ -143,3 +143,5 @@ vitastor-nbd netlink-revive /dev/nbdX (--image <image> | --pool <pool> --inode <
Оживить мёртвое NBD-устройство, ранее подключённое через netlink, без удаления. Поддерживает
те же опции, что и `netlink-map`.
Ядро не даст оживить устройство с другим образом, чем при `netlink-map` (Linux 5.6+), а более
старая версия `vitastor-nbd` не оживит устройство, подключённое этой версией.

View File

@@ -4,6 +4,7 @@ project(vitastor)
include(GNUInstallDirs)
include(CheckIncludeFile)
include(CheckCSourceCompiles)
find_package(PkgConfig)
@@ -68,6 +69,8 @@ macro(install_symlink _filepath _sympath)
endmacro(install_symlink)
check_include_file("linux/nbd-netlink.h" HAVE_NBD_NETLINK_H)
# NBD_ATTR_BACKEND_IDENTIFIER is an enum value, so #ifdef can't detect it
check_c_source_compiles("#include <linux/nbd-netlink.h>\nint main() { return NBD_ATTR_BACKEND_IDENTIFIER; }" HAVE_NBD_BACKEND_IDENTIFIER)
find_package(PkgConfig)
if (${WITH_QEMU})

View File

@@ -95,6 +95,9 @@ target_include_directories(vitastor-nbd PUBLIC ${NL3_INCLUDE_DIRS})
target_link_libraries(vitastor-nbd vitastor_client ${NL3_LIBRARIES})
if (HAVE_NBD_NETLINK_H AND NL3_LIBRARIES)
target_compile_definitions(vitastor-nbd PUBLIC HAVE_NBD_NETLINK_H)
if (HAVE_NBD_BACKEND_IDENTIFIER)
target_compile_definitions(vitastor-nbd PUBLIC HAVE_NBD_BACKEND_IDENTIFIER)
endif (HAVE_NBD_BACKEND_IDENTIFIER)
endif (HAVE_NBD_NETLINK_H AND NL3_LIBRARIES)
# vitastor-ublk

View File

@@ -165,7 +165,7 @@ static int netlink_configure(const int *sockfd, int sock_size, int dev_num, uint
NLA_PUT_U64(msg, NBD_ATTR_DEAD_CONN_TIMEOUT, conn_timeout);
}
#ifdef NBD_ATTR_BACKEND_IDENTIFIER
#ifdef HAVE_NBD_BACKEND_IDENTIFIER
if (backend)
{
// Backend is an attribute useful for identication of the device
@@ -652,7 +652,10 @@ help:
if (!cfg["nbd_disconnect_on_close"].is_null())
cflags |= NBD_CFLAG_DISCONNECT_ON_CLOSE;
#endif
int err = netlink_configure(sockfd + 1, 1, dev_num, device_size, 4096, flags, cflags, nbd_timeout, nbd_conn_timeout, NULL, revive);
// pool:inode rather than the name: it survives a rename and is the same for --image and --pool/--inode
uint64_t backend_inode = inode ? inode : watch->cfg.num;
std::string backend = std::to_string(INODE_POOL(backend_inode))+":"+std::to_string(INODE_NO_POOL(backend_inode));
int err = netlink_configure(sockfd + 1, 1, dev_num, device_size, 4096, flags, cflags, nbd_timeout, nbd_conn_timeout, backend.c_str(), revive);
if (err < 0)
{
errno = (err == -NLE_BUSY ? EBUSY : EIO);

View File

@@ -28,6 +28,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,30 @@
#!/bin/bash -ex
. `dirname $0`/run_3osds.sh
$VITASTOR_CLI create -s 16M testimg
$VITASTOR_CLI create -s 16M testimg2
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)
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
NBD_PID=$(cat /sys/block/${NBD_DEV#/dev/}/pid)
sudo kill -9 $NBD_PID
while sudo kill -0 $NBD_PID 2>/dev/null; do sleep 0.1; done
if sudo build/src/client/vitastor-nbd --config_path $VITASTOR_CFG netlink-revive $NBD_DEV --image testimg2; then
format_error "netlink-revive attached $NBD_DEV of testimg to testimg2"
fi
sudo build/src/client/vitastor-nbd --config_path $VITASTOR_CFG netlink-revive $NBD_DEV --image testimg
sudo dd if=$NBD_DEV of=./testdata/img1.read bs=4096 count=1 iflag=direct
cmp ./testdata/img1.bin ./testdata/img1.read
format_green OK