Skip to content

Commit d92325e

Browse files
author
Umer Saleem
committed
JSON output support for zpool version
This commit adds support for zpool version to output in JSON format using '-j' option. Userland kernel module version is collected in nvlist which is later displayed in JSON format. man page for zpool is updated. Signed-off-by: Umer Saleem <[email protected]>
1 parent 623d6b0 commit d92325e

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

cmd/zpool/zpool_main.c

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,8 @@ static zpool_command_t command_table[] = {
346346

347347
#define VDEV_ALLOC_CLASS_LOGS "logs"
348348

349+
#define MAX_CMD_LEN 256
350+
349351
static zpool_command_t *current_command;
350352
static zfs_type_t current_prop_type = (ZFS_TYPE_POOL | ZFS_TYPE_VDEV);
351353
static char history_str[HIS_MAX_RECORD_LEN];
@@ -446,7 +448,7 @@ get_usage(zpool_help_t idx)
446448
case HELP_SYNC:
447449
return (gettext("\tsync [pool] ...\n"));
448450
case HELP_VERSION:
449-
return (gettext("\tversion\n"));
451+
return (gettext("\tversion [-j]\n"));
450452
case HELP_WAIT:
451453
return (gettext("\twait [-Hp] [-T d|u] [-t <activity>[,...]] "
452454
"<pool> [interval]\n"));
@@ -10241,6 +10243,35 @@ zpool_do_history(int argc, char **argv)
1024110243
return (ret);
1024210244
}
1024310245

10246+
/*
10247+
* Generates an nvlist with output version for every command based on params.
10248+
* Purpose of this is to add a version of JSON output, considering the schema
10249+
* format might be updated for each command in future.
10250+
*
10251+
* Schema:
10252+
*
10253+
* "output_version": {
10254+
* "command": string,
10255+
* "vers_major": integer,
10256+
* "vers_minor": integer,
10257+
* }
10258+
*/
10259+
static nvlist_t *
10260+
zpool_json_schema(int maj_v, int min_v)
10261+
{
10262+
char cmd[MAX_CMD_LEN];
10263+
nvlist_t *sch = fnvlist_alloc();
10264+
nvlist_t *ov = fnvlist_alloc();
10265+
10266+
snprintf(cmd, MAX_CMD_LEN, "zpool %s", current_command->name);
10267+
fnvlist_add_string(ov, "command", cmd);
10268+
fnvlist_add_uint32(ov, "vers_major", maj_v);
10269+
fnvlist_add_uint32(ov, "vers_minor", min_v);
10270+
fnvlist_add_nvlist(sch, "output_version", ov);
10271+
10272+
return (sch);
10273+
}
10274+
1024410275
typedef struct ev_opts {
1024510276
int verbose;
1024610277
int scripted;
@@ -11585,8 +11616,39 @@ find_command_idx(const char *command, int *idx)
1158511616
static int
1158611617
zpool_do_version(int argc, char **argv)
1158711618
{
11588-
(void) argc, (void) argv;
11589-
return (zfs_version_print() != 0);
11619+
int c;
11620+
nvlist_t *jsobj = NULL, *zfs_ver = NULL;
11621+
boolean_t json = B_FALSE;
11622+
while ((c = getopt(argc, argv, "j")) != -1) {
11623+
switch (c) {
11624+
case 'j':
11625+
json = B_TRUE;
11626+
jsobj = zpool_json_schema(0, 1);
11627+
break;
11628+
case '?':
11629+
(void) fprintf(stderr, gettext("invalid option '%c'\n"),
11630+
optopt);
11631+
usage(B_FALSE);
11632+
}
11633+
}
11634+
11635+
argc -= optind;
11636+
if (argc != 0) {
11637+
(void) fprintf(stderr, "too many arguments\n");
11638+
usage(B_FALSE);
11639+
}
11640+
11641+
if (json) {
11642+
zfs_ver = zfs_version_nvlist();
11643+
if (zfs_ver) {
11644+
fnvlist_add_nvlist(jsobj, "zfs_version", zfs_ver);
11645+
zcmd_print_json(jsobj);
11646+
fnvlist_free(zfs_ver);
11647+
return (0);
11648+
} else
11649+
return (-1);
11650+
} else
11651+
return (zfs_version_print() != 0);
1159011652
}
1159111653

1159211654
/* Display documentation */

man/man8/zpool.8

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
.Fl ?V
3939
.Nm
4040
.Cm version
41+
.Op Fl j
4142
.Nm
4243
.Cm subcommand
4344
.Op Ar arguments
@@ -79,10 +80,14 @@ Displays a help message.
7980
.It Xo
8081
.Nm
8182
.Cm version
83+
.Op Fl j
8284
.Xc
8385
Displays the software version of the
8486
.Nm
8587
userland utility and the ZFS kernel module.
88+
Use
89+
.Fl j
90+
option to output in JSON format.
8691
.El
8792
.
8893
.Ss Creation

0 commit comments

Comments
 (0)