From 1425be644a4b132c371589ffb6f83dc10b4ee59e Mon Sep 17 00:00:00 2001 From: Alexander Motin Date: Mon, 3 Jun 2019 19:09:39 +0000 Subject: [PATCH] 9466 add JSON output support to channel programs illumos/illumos-gate@5267591016146502784860802129b16dab6f135c Reviewed by: Matt Ahrens Reviewed by: Serapheim Dimitropoulos Reviewed by: Sara Hartse Reviewed by: John Kennedy Approved by: Dan McDonald Author: Alek Pinchuk --- cmd/zfs/zfs_main.c | 22 +++++++++++++++------- man/man1m/zfs-program.1m | 8 +++++++- man/man1m/zfs.1m | 10 ++++++++-- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/cmd/zfs/zfs_main.c b/cmd/zfs/zfs_main.c index 9bea435f27e8..c071734a41c5 100644 --- a/cmd/zfs/zfs_main.c +++ b/cmd/zfs/zfs_main.c @@ -29,6 +29,7 @@ * Copyright (c) 2014 Integros [integros.com] * Copyright 2016 Igor Kozhukhov . * Copyright 2016 Nexenta Systems, Inc. + * Copyright (c) 2018 Datto Inc. */ #include @@ -335,7 +336,7 @@ get_usage(zfs_help_t idx) case HELP_BOOKMARK: return (gettext("\tbookmark \n")); case HELP_CHANNEL_PROGRAM: - return (gettext("\tprogram [-n] [-t ] " + return (gettext("\tprogram [-jn] [-t ] " "[-m ] " "[lua args...]\n")); } @@ -7072,12 +7073,12 @@ zfs_do_channel_program(int argc, char **argv) nvlist_t *outnvl; uint64_t instrlimit = ZCP_DEFAULT_INSTRLIMIT; uint64_t memlimit = ZCP_DEFAULT_MEMLIMIT; - boolean_t sync_flag = B_TRUE; + boolean_t sync_flag = B_TRUE, json_output = B_FALSE; zpool_handle_t *zhp; /* check options */ while (-1 != - (c = getopt(argc, argv, "nt:(instr-limit)m:(memory-limit)"))) { + (c = getopt(argc, argv, "jnt:(instr-limit)m:(memory-limit)"))) { switch (c) { case 't': case 'm': { @@ -7119,6 +7120,10 @@ zfs_do_channel_program(int argc, char **argv) sync_flag = B_FALSE; break; } + case 'j': { + json_output = B_TRUE; + break; + } case '?': (void) fprintf(stderr, gettext("invalid option '%c'\n"), optopt); @@ -7237,11 +7242,14 @@ zfs_do_channel_program(int argc, char **argv) gettext("Channel program execution failed:\n%s\n"), errstring); } else { - (void) printf("Channel program fully executed "); - if (nvlist_empty(outnvl)) { - (void) printf("with no return value.\n"); + if (json_output) { + (void) nvlist_print_json(stdout, outnvl); + } else if (nvlist_empty(outnvl)) { + (void) fprintf(stdout, gettext("Channel program fully " + "executed and did not produce output.\n")); } else { - (void) printf("with return value:\n"); + (void) fprintf(stdout, gettext("Channel program fully " + "executed and produced output:\n")); dump_nvlist(outnvl, 4); } } diff --git a/man/man1m/zfs-program.1m b/man/man1m/zfs-program.1m index 5fd4302fb8dc..98d9240ff8ad 100644 --- a/man/man1m/zfs-program.1m +++ b/man/man1m/zfs-program.1m @@ -9,6 +9,7 @@ .\" .\" .\" Copyright (c) 2016, 2017 by Delphix. All rights reserved. +.\" Copyright (c) 2018 Datto Inc. .\" .Dd January 21, 2016 .Dt ZFS-PROGRAM 1M @@ -18,7 +19,7 @@ .Nd executes ZFS channel programs .Sh SYNOPSIS .Cm "zfs program" -.Op Fl n +.Op Fl jn .Op Fl t Ar instruction-limit .Op Fl m Ar memory-limit .Ar pool @@ -46,6 +47,11 @@ will be run on and any attempts to access or modify other pools will cause an error. .Sh OPTIONS .Bl -tag -width "-t" +.It Fl j +Display channel program output in JSON format. +When this flag is specified and standard output is empty - +channel program encountered an error. +The details of such an error will be printed to standard error in plain text. .It Fl n Executes a read-only channel program, which runs faster. The program cannot change on-disk state by calling functions from the diff --git a/man/man1m/zfs.1m b/man/man1m/zfs.1m index 4ce0ddcad7f1..d27a3ee49f49 100644 --- a/man/man1m/zfs.1m +++ b/man/man1m/zfs.1m @@ -28,6 +28,7 @@ .\" Copyright (c) 2014 Integros [integros.com] .\" Copyright 2017 Nexenta Systems, Inc. .\" Copyright 2018 Joyent, Inc. +.\" Copyright (c) 2018 Datto Inc. .\" .Dd Feb 10, 2018 .Dt ZFS 1M @@ -271,7 +272,7 @@ .Ar snapshot Ar snapshot Ns | Ns Ar filesystem .Nm .Cm program -.Op Fl n +.Op Fl jn .Op Fl t Ar timeout .Op Fl m Ar memory_limit .Ar pool script @@ -3472,7 +3473,7 @@ Display the path's inode change time as the first column of output. .It Xo .Nm .Cm program -.Op Fl n +.Op Fl jn .Op Fl t Ar timeout .Op Fl m Ar memory_limit .Ar pool script @@ -3493,6 +3494,11 @@ Channel programs may only be run with root privileges. For full documentation of the ZFS channel program interface, see the manual page for .Bl -tag -width "" +.It Fl j +Display channel program output in JSON format. +When this flag is specified and standard output is empty - +channel program encountered an error. +The details of such an error will be printed to standard error in plain text. .It Fl n Executes a read-only channel program, which runs faster. The program cannot change on-disk state by calling functions from