[Libguestfs] [nbdkit PATCH v2 2/7] server: Expose -e in $exportname during --run

Eric Blake eblake at redhat.com
Sat Sep 28 04:48:44 UTC 2019


Now that the export name can affect contents, we should let --run
favor connecting to the preferred export name passed to nbdkit via the
-e command line option.  In addition to exposing $exportname, update
the recently-added $uri to include the exportname, but leave the older
$nbd alone.

Signed-off-by: Eric Blake <eblake at redhat.com>
---
 docs/nbdkit-captive.pod | 12 ++++++++++--
 server/captive.c        | 22 ++++++++++++++++++----
 tests/test-long-name.sh | 20 ++++++++++++++++++++
 3 files changed, 48 insertions(+), 6 deletions(-)

diff --git a/docs/nbdkit-captive.pod b/docs/nbdkit-captive.pod
index 0adf0a0b..9c7044f1 100644
--- a/docs/nbdkit-captive.pod
+++ b/docs/nbdkit-captive.pod
@@ -45,12 +45,14 @@ The following shell variables are available in the I<--run> argument:
 =item C<$uri>

 A URI that refers to the nbdkit port or socket in the preferred form
-documented by the NBD project.
+documented by the NBD project.  If nbdkit was started with the B<-e>
+option to set the preferred export name, this is included in the URI.

 =item C<$nbd>

 An older URL that refers to the nbdkit port or socket in a manner
-specific to certain tools.
+specific to certain tools.  This form does not include an export name,
+even if B<-e> was used.

 Note there is some magic here, since qemu and guestfish URLs have a
 different format, so nbdkit tries to guess which you are running.  If
@@ -64,6 +66,12 @@ If E<ne> "", the port number that nbdkit is listening on.

 If E<ne> "", the Unix domain socket that nbdkit is listening on.

+=item C<$exportname>
+
+The default export name (which may be "") that nbdkit will advertise
+in response to NBD_OPT_LIST.  This comes from the B<-e>
+(B<--exportname>) command line option.
+
 =back

 I<--run> implies I<--foreground>.  It is not possible, and probably
diff --git a/server/captive.c b/server/captive.c
index 90e42050..c4cec238 100644
--- a/server/captive.c
+++ b/server/captive.c
@@ -71,12 +71,26 @@ run_command (void)
   if (port) {
     fprintf (fp, "nbd://localhost:");
     shell_quote (port, fp);
+    if (exportname) {
+      putc ('/', fp);
+      uri_quote (exportname, fp);
+    }
   }
   else if (unixsocket) {
-    fprintf (fp, "nbd+unix://\\?socket=");
+    fprintf (fp, "nbd+unix://");
+    if (exportname) {
+      putc ('/', fp);
+      uri_quote (exportname, fp);
+    }
+    fprintf (fp, "\\?socket=");
     uri_quote (unixsocket, fp);
   }
-  fprintf (fp, "\n");
+  putc ('\n', fp);
+
+  /* Expose $exportname. */
+  fprintf (fp, "exportname=");
+  shell_quote (exportname, fp);
+  putc ('\n', fp);

   /* Construct older $nbd "URL".  Unfortunately guestfish and qemu take
    * different syntax, so try to guess which one we need.
@@ -106,13 +120,13 @@ run_command (void)
     else
       abort ();
   }
-  fprintf (fp, "\n");
+  putc ('\n', fp);

   /* Construct $port and $unixsocket. */
   fprintf (fp, "port=");
   if (port)
     shell_quote (port, fp);
-  fprintf (fp, "\n");
+  putc ('\n', fp);
   fprintf (fp, "unixsocket=");
   if (unixsocket)
     shell_quote (unixsocket, fp);
diff --git a/tests/test-long-name.sh b/tests/test-long-name.sh
index 214a5e7a..7b0b43ad 100755
--- a/tests/test-long-name.sh
+++ b/tests/test-long-name.sh
@@ -51,6 +51,26 @@ almost4k=${name4k%8$name16}
 nbdkit -U - -e $name4k null --run true || fail=1
 nbdkit -U - -e a$name4k null --run true && fail=1

+# Test that $exportname and $uri reflect the name
+out=$(nbdkit -U - -e $name4k null --run 'echo $exportname')
+if test "$name4k" != "$out"; then
+    echo "$0: \$exportname contains wrong contents" >&2
+    fail=1
+fi
+out=$(nbdkit -U - -e $name4k null --run 'echo $uri')
+case $out in
+    nbd+unix:///$name4k\?socket=*) ;;
+    *) echo "$0: \$uri contains wrong contents" >&2
+       fail=1 ;;
+esac
+pick_unused_port
+out=$(nbdkit -i localhost -p $port -e $name4k null --run 'echo $uri')
+case $out in
+    nbd://localhost:$port/$name4k) ;;
+    *) echo "$0: \$uri contains wrong contents" >&2
+       fail=1 ;;
+esac
+
 # The rest of this test uses the ‘qemu-nbd --list’ option added in qemu 4.0.
 if ! qemu-nbd --help | grep -sq -- --list; then
     echo "$0: skipping because qemu-nbd does not support the --list option"
-- 
2.21.0




More information about the Libguestfs mailing list