[Libguestfs] [PATCH libnbd 2/2] info: Print the URI in the output, if possible.

Richard W.M. Jones rjones at redhat.com
Wed Apr 7 16:23:32 UTC 2021


---
 info/Makefile.am       |  1 +
 info/info-list-uris.sh | 46 ++++++++++++++++++++++++++++++++++++++++++
 info/info-text.sh      |  1 +
 info/nbdinfo.c         | 12 +++++++++++
 info/nbdinfo.pod       |  2 ++
 5 files changed, 62 insertions(+)

diff --git a/info/Makefile.am b/info/Makefile.am
index 1f0a6a3..c19c394 100644
--- a/info/Makefile.am
+++ b/info/Makefile.am
@@ -22,6 +22,7 @@ info_sh_files = \
 	info-list-json.sh \
 	info-list-qemu.sh \
 	info-list-json-qemu.sh \
+	info-list-uris.sh \
 	info-json.sh \
 	info-oldstyle.sh \
 	info-null.sh \
diff --git a/info/info-list-uris.sh b/info/info-list-uris.sh
new file mode 100755
index 0000000..28693aa
--- /dev/null
+++ b/info/info-list-uris.sh
@@ -0,0 +1,46 @@
+#!/usr/bin/env bash
+# nbd client library in userspace
+# Copyright (C) 2020-2021 Red Hat Inc.
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2 of the License, or (at your option) any later version.
+#
+# This library 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
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+. ../tests/functions.sh
+
+set -e
+set -x
+
+requires nbdkit --version
+requires nbdkit file --version
+
+# This test requires nbdkit >= 1.22.
+minor=$( nbdkit --dump-config | grep ^version_minor | cut -d= -f2 )
+requires test $minor -gt 22
+
+out=info-list-uris.out
+cleanup_fn rm -f $out
+
+# nbdinfo --list is not very stable in the particular case where
+# exports come and go while it is running.  This happens if we set the
+# directory to be the current directory since other tests create
+# temporary files here.  So point this to a more stable directory.
+
+nbdkit -U - file dir=$srcdir/../examples \
+       --run '$VG nbdinfo --list "$uri"' > $out
+cat $out
+
+# We expect to see URIs corresponding to some well-known files
+# (ie. exports) in the examples directory.
+grep "uri: nbd+unix:///LICENSE-FOR-EXAMPLES?socket=" $out
+grep "uri: nbd+unix:///get-size.c?socket=" $out
diff --git a/info/info-text.sh b/info/info-text.sh
index bf98405..5585a48 100755
--- a/info/info-text.sh
+++ b/info/info-text.sh
@@ -31,4 +31,5 @@ nbdkit -U - memory size=1M \
        --run '$VG nbdinfo "nbd+unix:///?socket=$unixsocket"' > $out
 cat $out
 grep "export-size: $((1024*1024))" $out
+grep "uri: nbd+unix:///?socket=" $out
 sed -n '/contexts:/ { N; p; q }; $ q1' $out
diff --git a/info/nbdinfo.c b/info/nbdinfo.c
index 3dfc463..b35e682 100644
--- a/info/nbdinfo.c
+++ b/info/nbdinfo.c
@@ -407,6 +407,7 @@ list_one_export (struct nbd_handle *nbd, const char *desc,
   char *export_name = NULL;
   char *export_desc = NULL;
   char *content = NULL;
+  char *uri = NULL;
   int is_rotational, is_read_only;
   int can_cache, can_df, can_fast_zero, can_flush, can_fua,
     can_multi_conn, can_trim, can_zero;
@@ -433,6 +434,8 @@ list_one_export (struct nbd_handle *nbd, const char *desc,
     exit (EXIT_FAILURE);
   }
 
+  uri = nbd_get_uri (nbd);
+
   /* Prefer the server's version of the name, if available */
   export_name = nbd_get_canonical_export_name (nbd);
   if (export_name == NULL)
@@ -475,6 +478,8 @@ list_one_export (struct nbd_handle *nbd, const char *desc,
     fprintf (fp, "\texport-size: %" PRIi64 "\n", size);
     if (content)
       fprintf (fp, "\tcontent: %s\n", content);
+    if (uri)
+      fprintf (fp, "\turi: %s\n", uri);
     if (show_context) {
       fprintf (fp, "\tcontexts:\n");
       for (i = 0; i < contexts.size; ++i)
@@ -533,6 +538,12 @@ list_one_export (struct nbd_handle *nbd, const char *desc,
       fprintf (fp, ",\n");
     }
 
+    if (uri) {
+      fprintf (fp, "\t\"uri\": ");
+      print_json_string (uri);
+      fprintf (fp, ",\n");
+    }
+
     if (show_context) {
       fprintf (fp, "\t\"contexts\": [\n");
       for (i = 0; i < contexts.size; ++i) {
@@ -600,6 +611,7 @@ list_one_export (struct nbd_handle *nbd, const char *desc,
   free (content);
   free (export_name);
   free (export_desc);
+  free (uri);
   return true;
 }
 
diff --git a/info/nbdinfo.pod b/info/nbdinfo.pod
index 99c74a2..5bc624c 100644
--- a/info/nbdinfo.pod
+++ b/info/nbdinfo.pod
@@ -23,6 +23,7 @@ L<https://github.com/NetworkBlockDevice/nbd/blob/master/doc/uri.md>):
  export="":
          export-size: 1048576
          content: data
+         uri: nbd://localhost:10809/
          is_rotational: false
          is_read_only: false
          can_cache: true
@@ -63,6 +64,7 @@ the I<--json> parameter:
      {
        "export-name": "",
        "content": "DOS/MBR boot sector; partition 1 : ID=0xc, start-CHS (0x3ff,254,63), end-CHS (0x3ff,254,63), startsector 2048, 4148704 sectors",
+       "uri": "nbd://localhost:10809/",
        "is_rotational": false,
        "is_read_only": true,
        "can_cache": true,
-- 
2.29.0.rc2




More information about the Libguestfs mailing list