From: Josef Bacik Date: Fri, 13 Apr 2018 16:07:48 +0000 (-0400) Subject: blktests: add nbd test for online resize X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=9195490285fec9d13adbf6b008bc596952940e17;p=users%2Fsagi%2Fblktests.git blktests: add nbd test for online resize This tests resizing a connected nbd device. We connect to the existing 10gig device, and resize it online to 1gig, and make sure lsblk and parted (which use different size determination methods) both report the correctly updated size. Signed-off-by: Josef Bacik [Omar: squash commits, add note on required patches] Signed-off-by: Omar Sandoval --- diff --git a/src/.gitignore b/src/.gitignore index bdb2585..68da6e6 100644 --- a/src/.gitignore +++ b/src/.gitignore @@ -1,5 +1,6 @@ /loblksize /loop_get_status_null /openclose +/nbdsetsize /sg/dxfer-from-dev /sg/syzkaller1 diff --git a/src/Makefile b/src/Makefile index 787df37..612282d 100644 --- a/src/Makefile +++ b/src/Makefile @@ -3,7 +3,8 @@ TARGETS := \ loop_get_status_null \ openclose \ sg/dxfer-from-dev \ - sg/syzkaller1 + sg/syzkaller1 \ + nbdsetsize CFLAGS := -O2 diff --git a/src/nbdsetsize.c b/src/nbdsetsize.c new file mode 100644 index 0000000..fe73ed3 --- /dev/null +++ b/src/nbdsetsize.c @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2018 Josef Bacik + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + unsigned long long size; + char *end; + int fd = -1; + + if (argc != 3) { + fprintf(stderr, "usage: %s DEV SIZE\n", argv[0]); + return 1; + } + + errno = 0; + size = strtoull(argv[2], &end, 0); + if (errno || *end) { + fprintf(stderr, "invalid size\n"); + return 1; + } + + fd = open(argv[1], O_RDWR); + if (fd == -1) { + perror("open"); + return 1; + } + + if (ioctl(fd, NBD_SET_SIZE, size) == -1) { + int status = errno == EINVAL ? 2 : 1; + + perror("NBD_SET_SIZE"); + close(fd); + return status; + } + + close(fd); + return 0; +} diff --git a/tests/nbd/001 b/tests/nbd/001 new file mode 100644 index 0000000..8be760e --- /dev/null +++ b/tests/nbd/001 @@ -0,0 +1,45 @@ +#!/bin/bash +# +# Test nbd device resizing. Regression test for patches "nbd: update size when +# connected" and "nbd: use bd_set_size when updating disk size". +# +# Copyright (C) 2018 Josef Bacik +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +. common/nbd + +DESCRIPTION="resize a connected nbd device" +QUICK=1 + +requires() { + _have_nbd && _have_program parted && _have_src_program nbdsetsize +} + +test() { + echo "Running ${TEST_NAME}" + _start_nbd_server + nbd-client -N export localhost /dev/nbd0 > /dev/null 2>&1 + parted -s /dev/nbd0 print 2> /dev/null | grep 'Disk /dev/nbd0' + lsblk -n /dev/nbd0 + + echo "Setting size to 1gib" + src/nbdsetsize /dev/nbd0 1073741824 + + lsblk -n /dev/nbd0 + parted -s /dev/nbd0 print 2> /dev/null | grep 'Disk /dev/nbd0' + + nbd-client -d /dev/nbd0 > /dev/null 2>&1 + _stop_nbd_server +} diff --git a/tests/nbd/001.out b/tests/nbd/001.out new file mode 100644 index 0000000..2f2aff0 --- /dev/null +++ b/tests/nbd/001.out @@ -0,0 +1,6 @@ +Running nbd/001 +Disk /dev/nbd0: 10.7GB +nbd0 43:0 0 10G 0 disk +Setting size to 1gib +nbd0 43:0 0 1G 0 disk +Disk /dev/nbd0: 1074MB diff --git a/tests/nbd/group b/tests/nbd/group new file mode 100644 index 0000000..46739a0 --- /dev/null +++ b/tests/nbd/group @@ -0,0 +1,22 @@ +#!/bin/bash +# +# NBD tests. +# +# Copyright (C) 2018 Josef Bacik +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +group_requires() { + _have_root && _have_nbd && modprobe nbd +}