aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/zfs
diff options
context:
space:
mode:
authorAlek P <[email protected]>2018-03-19 15:40:58 -0400
committerBrian Behlendorf <[email protected]>2018-03-19 12:40:58 -0700
commit272b5d730f9ddd1db7cd7511da7bba75d6bd2450 (patch)
tree9c7d8a1e05571bc8b157427815e05fd65f1dfe91 /cmd/zfs
parenta76f3d0437e5e974f0f748f8735af3539443b388 (diff)
Add JSON output support to channel programs
The changes piggyback JSON output support on top of channel programs (#6558). This way the JSON output support is targeted to scripting use cases and is easily maintainable since it really only touches one function (zfs_do_channel_program()). This patch ports Joyent's JSON nvlist library from illumos to enable easy JSON printing of channel program output nvlist. To keep the delta small I also took advantage of the fact that printing in zfs_do_channel_program() was almost always done before exiting the program. Reviewed by: Matt Ahrens <[email protected]> Reviewed-by: Tony Hutter <[email protected]> Reviewed-by: Richard Elling <[email protected]> Reviewed-by: Brian Behlendorf <[email protected]> Signed-off-by: Alek Pinchuk <[email protected]> Closes #7281
Diffstat (limited to 'cmd/zfs')
-rw-r--r--cmd/zfs/zfs_main.c25
1 files changed, 17 insertions, 8 deletions
diff --git a/cmd/zfs/zfs_main.c b/cmd/zfs/zfs_main.c
index e241831db..d148516f1 100644
--- a/cmd/zfs/zfs_main.c
+++ b/cmd/zfs/zfs_main.c
@@ -27,6 +27,7 @@
* Copyright (c) 2013 Steven Hartland. All rights reserved.
* Copyright 2016 Igor Kozhukhov <[email protected]>.
* Copyright 2016 Nexenta Systems, Inc.
+ * Copyright (c) 2018 Datto Inc.
*/
#include <assert.h>
@@ -358,7 +359,7 @@ get_usage(zfs_help_t idx)
case HELP_BOOKMARK:
return (gettext("\tbookmark <snapshot> <bookmark>\n"));
case HELP_CHANNEL_PROGRAM:
- return (gettext("\tprogram [-n] [-t <instruction limit>] "
+ return (gettext("\tprogram [-jn] [-t <instruction limit>] "
"[-m <memory limit (b)>] <pool> <program file> "
"[lua args...]\n"));
case HELP_LOAD_KEY:
@@ -7220,11 +7221,11 @@ 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 ((c = getopt(argc, argv, "nt:m:")) != -1) {
+ while ((c = getopt(argc, argv, "nt:m:j")) != -1) {
switch (c) {
case 't':
case 'm': {
@@ -7266,6 +7267,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);
@@ -7391,14 +7396,18 @@ zfs_do_channel_program(int argc, char **argv)
gettext("Channel program execution failed:\n%s\n"),
errstring);
if (ret == ETIME && instructions != 0)
- (void) fprintf(stderr, "%llu Lua instructions\n",
+ (void) fprintf(stderr,
+ gettext("%llu Lua instructions\n"),
(u_longlong_t)instructions);
} 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);
}
}