[libnbd PATCH 0/3] Add nbddump --offset
I finally found time to finish a small project I had left half-baked in one of my local branches. Eric Blake (3): dump: Document --length dump: Add --offset for further limiting the dump dump: Add a test of --length and --offset dump/nbddump.pod | 8 +++++++- dump/Makefile.am | 2 ++ dump/dump-subset.sh | 48 +++++++++++++++++++++++++++++++++++++++++++++ dump/dump.c | 27 +++++++++++++++++++++---- 4 files changed, 80 insertions(+), 5 deletions(-) create mode 100755 dump/dump-subset.sh -- 2.48.1
This option has been present since the introduction of nbddump (commit c4107b9a, v1.13.4), but was undocumented unless you read further into the man page. Mentioning it in --help and in the synopsis is useful, and will set the stage for adding other options that should be documented. Signed-off-by: Eric Blake <eblake@redhat.com> --- dump/nbddump.pod | 2 +- dump/dump.c | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/dump/nbddump.pod b/dump/nbddump.pod index 6f1c9b51..b05472fe 100644 --- a/dump/nbddump.pod +++ b/dump/nbddump.pod @@ -4,7 +4,7 @@ nbddump - hexdump the content of a disk over NBD =head1 SYNOPSIS - nbddump NBD + nbddump [-n N|--length N] NBD C<NBD> is an NBD URI or subprocess: diff --git a/dump/dump.c b/dump/dump.c index 71053277..71bedf6a 100644 --- a/dump/dump.c +++ b/dump/dump.c @@ -65,7 +65,13 @@ usage (FILE *fp, int exitcode) "\n" "Hexdump the content of a disk over NBD:\n" "\n" -" nbddump NBD-URI | [ CMD ARGS ... ]\n" +" nbddump [ OPTIONS ...] NBD-URI | [ CMD ARGS ... ]\n" +"\n" +"Options include:\n" +" --color, --colour Force ANSI color on output to non-terminal\n" +" --no-color, --no-colour\n" +" Suppress ANSI color even on output to terminal\n" +" -n LEN, --length=LEN Truncate output after LEN bytes dumped\n" "\n" "Other options:\n" "\n" -- 2.48.1
We already have --length (aka --limit) for limiting how far the dump goes; the obvious counterpart is --offset (aka --skip) for controlling where to start the dump. I did not bother to try and print an unaligned partial first line so that subsequent lines can be nicely aligned, but rather the dump always starts with a full line even when it displays with unusual offsets. Signed-off-by: Eric Blake <eblake@redhat.com> --- dump/nbddump.pod | 8 +++++++- dump/dump.c | 19 ++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/dump/nbddump.pod b/dump/nbddump.pod index b05472fe..6acac426 100644 --- a/dump/nbddump.pod +++ b/dump/nbddump.pod @@ -4,7 +4,7 @@ nbddump - hexdump the content of a disk over NBD =head1 SYNOPSIS - nbddump [-n N|--length N] NBD + nbddump [-n N|--length N] [-o N|--offset N] NBD C<NBD> is an NBD URI or subprocess: @@ -103,6 +103,12 @@ if the output seems to be a terminal, and disable them if not. Dump up to I<N> bytes and then stop. +=item B<--offset=>N + +=item B<-o> N + +Start the dump at offset I<N> bytes. + =item B<-V> =item B<--version> diff --git a/dump/dump.c b/dump/dump.c index 71bedf6a..7a475196 100644 --- a/dump/dump.c +++ b/dump/dump.c @@ -44,6 +44,7 @@ static const char *progname; static struct nbd_handle *nbd; bool colour; static uint64_t limit = UINT64_MAX; /* --length (unlimited by default) */ +static uint64_t skip; /* --offset (0 by default) */ static int64_t size; /* actual size */ static bool can_meta_context; /* did we get extent data? */ @@ -72,6 +73,7 @@ usage (FILE *fp, int exitcode) " --no-color, --no-colour\n" " Suppress ANSI color even on output to terminal\n" " -n LEN, --length=LEN Truncate output after LEN bytes dumped\n" +" -o OFF, --offset=OFF Skip to offset OFF bytes before dumping\n" "\n" "Other options:\n" "\n" @@ -99,7 +101,7 @@ main (int argc, char *argv[]) COLOUR_OPTION, NO_COLOUR_OPTION, }; - const char *short_options = "n:V"; + const char *short_options = "n:o:V"; const struct option long_options[] = { { "help", no_argument, NULL, HELP_OPTION }, { "long-options", no_argument, NULL, LONG_OPTIONS }, @@ -116,6 +118,8 @@ main (int argc, char *argv[]) { "no-colours", no_argument, NULL, NO_COLOUR_OPTION }, { "length", required_argument, NULL, 'n' }, { "limit", required_argument, NULL, 'n' }, + { "offset", required_argument, NULL, 'o' }, + { "skip", required_argument, NULL, 'o' }, { NULL } }; int c; @@ -165,6 +169,15 @@ main (int argc, char *argv[]) } break; + case 'o': + /* XXX Allow human sizes here. */ + if (sscanf (optarg, "%" SCNu64, &skip) != 1) { + fprintf (stderr, "%s: could not parse --offset option: %s\n", + progname, optarg); + exit (EXIT_FAILURE); + } + break; + case 'V': display_version ("nbddump"); exit (EXIT_SUCCESS); @@ -342,8 +355,8 @@ do_dump (void) const char *splat = colour ? "☆" : "*"; const char *pipe = colour ? "│" : "|"; const char *dot = colour ? "·" : "."; - uint64_t offset = 0; - uint64_t count = size > limit ? limit : size; + uint64_t offset = skip; + uint64_t count = size - offset > limit ? limit : size - offset; size_t i, j; char last[16]; bool printed_splat = false, same; -- 2.48.1
Prove that our subset-limiting options work. Signed-off-by: Eric Blake <eblake@redhat.com> --- dump/Makefile.am | 2 ++ dump/dump-subset.sh | 48 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100755 dump/dump-subset.sh diff --git a/dump/Makefile.am b/dump/Makefile.am index 734f149b..335ff8db 100644 --- a/dump/Makefile.am +++ b/dump/Makefile.am @@ -21,6 +21,7 @@ EXTRA_DIST = \ dump-data.sh \ dump-empty-qcow2.sh \ dump-pattern.sh \ + dump-subset.sh \ nbddump.pod \ test-long-options.sh \ test-short-options.sh \ @@ -70,6 +71,7 @@ TESTS = \ dump-data.sh \ dump-empty-qcow2.sh \ dump-pattern.sh \ + dump-subset.sh \ test-long-options.sh \ test-short-options.sh \ test-version.sh \ diff --git a/dump/dump-subset.sh b/dump/dump-subset.sh new file mode 100755 index 00000000..42444ae1 --- /dev/null +++ b/dump/dump-subset.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash +# nbd client library in userspace +# Copyright Red Hat +# +# 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 pattern --dump-plugin +requires $NBDKIT -U - null --run 'test "$uri" != ""' +requires nbdsh -c 'exit(not h.supports_uri())' + +output=dump-subset.out +rm -f $output +cleanup_fn rm -f $output + +$NBDKIT -U - pattern size=100 --run 'nbddump --offset=15 --length=33 "$uri"' > $output + +# Repeat with alternate spellings +$NBDKIT -U - pattern size=100 --run 'nbddump --skip 15 --limit 33 "$uri"' >> $output + +cat $output + +if [ "$(cat $output)" != '000000000f: 08 00 00 00 00 00 00 00 10 00 00 00 00 00 00 00 |................| +000000001f: 18 00 00 00 00 00 00 00 20 00 00 00 00 00 00 00 |........ .......| +000000002f: 28 |( | +000000000f: 08 00 00 00 00 00 00 00 10 00 00 00 00 00 00 00 |................| +000000001f: 18 00 00 00 00 00 00 00 20 00 00 00 00 00 00 00 |........ .......| +000000002f: 28 |( |' ]; then + echo "$0: unexpected output from nbddump command" + exit 1 +fi -- 2.48.1
ACK series Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-top is 'top' for virtual machines. Tiny program with many powerful monitoring features, net stats, disk stats, logging, etc. http://people.redhat.com/~rjones/virt-top
On Wed, Feb 05, 2025 at 08:56:15AM +0000, Richard W.M. Jones wrote:
ACK series
Now upstream as f61a7446..a33e7a93 -- Eric Blake, Principal Software Engineer Red Hat, Inc. Virtualization: qemu.org | libguestfs.org
participants (2)
-
Eric Blake -
Richard W.M. Jones