Richard W.M. Jones
2018-Jun-15 13:50 UTC
[Libguestfs] [PATCH v2] lib: Convert all drive socket parameters to an absolute path (RHBZ#1588451).
Also fix two tests which assumed that you could add a non-existent socket. --- fish/test-add-uri.sh | 16 ++++++++++++---- lib/drives.c | 14 +++++++++++++- tests/disks/test-qemu-drive.sh | 19 ++++++++++++++----- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/fish/test-add-uri.sh b/fish/test-add-uri.sh index 756df997b..cb4d40199 100755 --- a/fish/test-add-uri.sh +++ b/fish/test-add-uri.sh @@ -26,6 +26,7 @@ skip_if_skipped rm -f test-add-uri.out rm -f test-add-uri.img +rm -f test-add-uri.sock $VG guestfish sparse test-add-uri.img 10M @@ -55,11 +56,17 @@ grep -sq 'add_drive "" "protocol:nbd" "server:tcp:example.com"' test-add-uri.out $VG guestfish -x -a nbd://example.com:3000 </dev/null >test-add-uri.out 2>&1 grep -sq 'add_drive "" "protocol:nbd" "server:tcp:example.com:3000"' test-add-uri.out || fail -$VG guestfish -x -a 'nbd://?socket=/sk' </dev/null >test-add-uri.out 2>&1 -grep -sq 'add_drive "" "protocol:nbd" "server:unix:/sk"' test-add-uri.out || fail +# NBD Unix domain socket tests. +# These require Perl to create the socket, but don't fail if it's not +# around. -$VG guestfish -x -a 'nbd:///export?socket=/sk' </dev/null >test-add-uri.out 2>&1 -grep -sq 'add_drive "/export" "protocol:nbd" "server:unix:/sk"' test-add-uri.out || fail +if perl -MIO::Socket -e 'IO::Socket::UNIX->new (Local => "test-add-uri.sock", Type => SOCK_STREAM(), Listen => 1); 1'; then + $VG guestfish -x -a 'nbd://?socket=test-add-uri.sock' </dev/null >test-add-uri.out 2>&1 + grep -sq 'add_drive "" "protocol:nbd" "server:unix:test-add-uri.sock"' test-add-uri.out || fail + + $VG guestfish -x -a 'nbd:///export?socket=test-add-uri.sock' </dev/null >test-add-uri.out 2>&1 + grep -sq 'add_drive "/export" "protocol:nbd" "server:unix:test-add-uri.sock"' test-add-uri.out || fail +fi # rbd $VG guestfish -x -a rbd://example.com:6789/pool/disk </dev/null >test-add-uri.out 2>&1 @@ -93,3 +100,4 @@ grep -sq 'add_drive "iqn.2015-12.com.libguestfs:test2/0" "protocol:iscsi" "serve rm test-add-uri.out rm test-add-uri.img +rm -f test-add-uri.sock diff --git a/lib/drives.c b/lib/drives.c index 82ef30093..7697f369a 100644 --- a/lib/drives.c +++ b/lib/drives.c @@ -28,6 +28,7 @@ #include <stdbool.h> #include <string.h> #include <unistd.h> +#include <limits.h> #include <netdb.h> #include <arpa/inet.h> #include <assert.h> @@ -645,7 +646,18 @@ parse_one_server (guestfs_h *g, const char *server, struct drive_server *ret) return -1; } ret->transport = drive_transport_unix; - ret->u.socket = safe_strdup (g, server+5); + + /* libvirt requires sockets to be specified as an absolute path + * (see RHBZ#1588451), and it's probably a good idea anyway to + * check the socket exists and convert it to an absolute path. + */ + ret->u.socket = realpath (server+5, NULL); + if (ret->u.socket == NULL) { + perrorf (g, _("realpath: could not convert ā%sā to an absolute path"), + server+5); + return -1; + } + ret->port = 0; return 0; } diff --git a/tests/disks/test-qemu-drive.sh b/tests/disks/test-qemu-drive.sh index 19dd60a2f..181f21401 100755 --- a/tests/disks/test-qemu-drive.sh +++ b/tests/disks/test-qemu-drive.sh @@ -43,6 +43,8 @@ function fail () rm -f "$DEBUG_QEMU_FILE" +rm -f test-qemu-drive.sock + # Ceph (RBD). guestfish <<EOF ||: @@ -111,13 +113,18 @@ check_output grep -sq -- '-drive file=nbd:1.2.3.4:1234,' "$DEBUG_QEMU_FILE" || fail rm "$DEBUG_QEMU_FILE" -guestfish <<EOF ||: - add "" "format:raw" "protocol:nbd" "server:unix:/socket" +# This test requires Perl to create the socket, but don't fail if +# it's not around. +if perl -MIO::Socket -e 'IO::Socket::UNIX->new (Local => "test-qemu-drive.sock", Type => SOCK_STREAM(), Listen => 1); 1'; then + + guestfish <<EOF ||: + add "" "format:raw" "protocol:nbd" "server:unix:test-qemu-drive.sock" run EOF -check_output -grep -sq -- '-drive file=nbd:unix:/socket,' "$DEBUG_QEMU_FILE" || fail -rm "$DEBUG_QEMU_FILE" + check_output + grep -sq -- '-drive file=nbd:unix:.*/test-qemu-drive.sock,' "$DEBUG_QEMU_FILE" || fail + rm "$DEBUG_QEMU_FILE" +fi # Sheepdog. @@ -139,3 +146,5 @@ EOF check_output grep -sq -- '-drive file=ssh://rich@example.com/disk.img,' "$DEBUG_QEMU_FILE" || fail rm "$DEBUG_QEMU_FILE" + +rm -f test-qemu-drive.sock -- 2.16.2
Pino Toscano
2018-Jun-21 08:35 UTC
Re: [Libguestfs] [PATCH v2] lib: Convert all drive socket parameters to an absolute path (RHBZ#1588451).
On Friday, 15 June 2018 15:50:49 CEST Richard W.M. Jones wrote:> Also fix two tests which assumed that you could add a non-existent > socket. > ---This approach LGTM, although maybe a simpler one can be to resolve the path when starting the appliance. This patch breaks the case when an user specifies a relative path that cannot be resolved during add_drive, but does chdir before run. -- Pino Toscano
Maybe Matching Threads
- [PATCH v2] lib: add support for disks with 4096 bytes sector size
- [PATCH] lib: allow to specify physical/logical block size for disks
- [PATCH v2] libvirt: read disk paths from pools (RHBZ#1366049)
- Re: [PATCH] fish: drop leading '/' in nbd paths (RHBZ#1379585)
- [PATCH] fish: drop leading '/' in nbd paths (RHBZ#1379585)