Skip to content

Commit 5cbdd5e

Browse files
Umer Saleembehlendorf
Umer Saleem
authored andcommitted
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. Reviewed-by: Tony Hutter <[email protected]> Reviewed-by: Ameer Hamza <[email protected]> Signed-off-by: Umer Saleem <[email protected]> Closes #16217
1 parent cad4c0e commit 5cbdd5e

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
@@ -349,6 +349,8 @@ static zpool_command_t command_table[] = {
349349

350350
#define VDEV_ALLOC_CLASS_LOGS "logs"
351351

352+
#define MAX_CMD_LEN 256
353+
352354
static zpool_command_t *current_command;
353355
static zfs_type_t current_prop_type = (ZFS_TYPE_POOL | ZFS_TYPE_VDEV);
354356
static char history_str[HIS_MAX_RECORD_LEN];
@@ -452,7 +454,7 @@ get_usage(zpool_help_t idx)
452454
case HELP_SYNC:
453455
return (gettext("\tsync [pool] ...\n"));
454456
case HELP_VERSION:
455-
return (gettext("\tversion\n"));
457+
return (gettext("\tversion [-j]\n"));
456458
case HELP_WAIT:
457459
return (gettext("\twait [-Hp] [-T d|u] [-t <activity>[,...]] "
458460
"<pool> [interval]\n"));
@@ -10344,6 +10346,35 @@ zpool_do_history(int argc, char **argv)
1034410346
return (ret);
1034510347
}
1034610348

10349+
/*
10350+
* Generates an nvlist with output version for every command based on params.
10351+
* Purpose of this is to add a version of JSON output, considering the schema
10352+
* format might be updated for each command in future.
10353+
*
10354+
* Schema:
10355+
*
10356+
* "output_version": {
10357+
* "command": string,
10358+
* "vers_major": integer,
10359+
* "vers_minor": integer,
10360+
* }
10361+
*/
10362+
static nvlist_t *
10363+
zpool_json_schema(int maj_v, int min_v)
10364+
{
10365+
char cmd[MAX_CMD_LEN];
10366+
nvlist_t *sch = fnvlist_alloc();
10367+
nvlist_t *ov = fnvlist_alloc();
10368+
10369+
snprintf(cmd, MAX_CMD_LEN, "zpool %s", current_command->name);
10370+
fnvlist_add_string(ov, "command", cmd);
10371+
fnvlist_add_uint32(ov, "vers_major", maj_v);
10372+
fnvlist_add_uint32(ov, "vers_minor", min_v);
10373+
fnvlist_add_nvlist(sch, "output_version", ov);
10374+
10375+
return (sch);
10376+
}
10377+
1034710378
typedef struct ev_opts {
1034810379
int verbose;
1034910380
int scripted;
@@ -11688,8 +11719,39 @@ find_command_idx(const char *command, int *idx)
1168811719
static int
1168911720
zpool_do_version(int argc, char **argv)
1169011721
{
11691-
(void) argc, (void) argv;
11692-
return (zfs_version_print() != 0);
11722+
int c;
11723+
nvlist_t *jsobj = NULL, *zfs_ver = NULL;
11724+
boolean_t json = B_FALSE;
11725+
while ((c = getopt(argc, argv, "j")) != -1) {
11726+
switch (c) {
11727+
case 'j':
11728+
json = B_TRUE;
11729+
jsobj = zpool_json_schema(0, 1);
11730+
break;
11731+
case '?':
11732+
(void) fprintf(stderr, gettext("invalid option '%c'\n"),
11733+
optopt);
11734+
usage(B_FALSE);
11735+
}
11736+
}
11737+
11738+
argc -= optind;
11739+
if (argc != 0) {
11740+
(void) fprintf(stderr, "too many arguments\n");
11741+
usage(B_FALSE);
11742+
}
11743+
11744+
if (json) {
11745+
zfs_ver = zfs_version_nvlist();
11746+
if (zfs_ver) {
11747+
fnvlist_add_nvlist(jsobj, "zfs_version", zfs_ver);
11748+
zcmd_print_json(jsobj);
11749+
fnvlist_free(zfs_ver);
11750+
return (0);
11751+
} else
11752+
return (-1);
11753+
} else
11754+
return (zfs_version_print() != 0);
1169311755
}
1169411756

1169511757
/* 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)