JSON output support for zfs list

This commit adds support for JSON output for zfs list using '-j' option.
Information is collected in JSON format which is later printed in jSON
format. Existing options for zfs list also work with '-j'. man pages are
updated with relevant information.

Reviewed-by: Tony Hutter <hutter2@llnl.gov>
Reviewed-by: Ameer Hamza <ahamza@ixsystems.com>
Signed-off-by: Umer Saleem <usaleem@ixsystems.com>
Closes #16217
This commit is contained in:
Umer Saleem 2024-04-18 22:09:15 +05:00 committed by Brian Behlendorf
parent aa15b60e58
commit 443abfc71d
2 changed files with 297 additions and 45 deletions

View File

@ -310,8 +310,9 @@ get_usage(zfs_help_t idx)
return (gettext("\tupgrade [-v]\n" return (gettext("\tupgrade [-v]\n"
"\tupgrade [-r] [-V version] <-a | filesystem ...>\n")); "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
case HELP_LIST: case HELP_LIST:
return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] " return (gettext("\tlist [-Hp] [-j [--json-int]] [-r|-d max] "
"[-s property]...\n\t [-S property]... [-t type[,...]] " "[-o property[,...]] [-s property]...\n\t "
"[-S property]... [-t type[,...]] "
"[filesystem|volume|snapshot] ...\n")); "[filesystem|volume|snapshot] ...\n"));
case HELP_MOUNT: case HELP_MOUNT:
return (gettext("\tmount\n" return (gettext("\tmount\n"
@ -3609,6 +3610,9 @@ typedef struct list_cbdata {
boolean_t cb_literal; boolean_t cb_literal;
boolean_t cb_scripted; boolean_t cb_scripted;
zprop_list_t *cb_proplist; zprop_list_t *cb_proplist;
boolean_t cb_json;
nvlist_t *cb_jsobj;
boolean_t cb_json_as_int;
} list_cbdata_t; } list_cbdata_t;
/* /*
@ -3679,10 +3683,11 @@ zfs_list_avail_color(zfs_handle_t *zhp)
/* /*
* Given a dataset and a list of fields, print out all the properties according * Given a dataset and a list of fields, print out all the properties according
* to the described layout. * to the described layout, or return an nvlist containing all the fields, later
* to be printed out as JSON object.
*/ */
static void static void
print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb) collect_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
{ {
zprop_list_t *pl = cb->cb_proplist; zprop_list_t *pl = cb->cb_proplist;
boolean_t first = B_TRUE; boolean_t first = B_TRUE;
@ -3691,9 +3696,23 @@ print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
nvlist_t *propval; nvlist_t *propval;
const char *propstr; const char *propstr;
boolean_t right_justify; boolean_t right_justify;
nvlist_t *item, *d, *props;
item = d = props = NULL;
zprop_source_t sourcetype = ZPROP_SRC_NONE;
char source[ZFS_MAX_DATASET_NAME_LEN];
if (cb->cb_json) {
d = fnvlist_lookup_nvlist(cb->cb_jsobj, "datasets");
if (d == NULL) {
fprintf(stderr, "datasets obj not found.\n");
exit(1);
}
item = fnvlist_alloc();
props = fnvlist_alloc();
fill_dataset_info(item, zhp, cb->cb_json_as_int);
}
for (; pl != NULL; pl = pl->pl_next) { for (; pl != NULL; pl = pl->pl_next) {
if (!first) { if (!cb->cb_json && !first) {
if (cb->cb_scripted) if (cb->cb_scripted)
(void) putchar('\t'); (void) putchar('\t');
else else
@ -3709,69 +3728,112 @@ print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
right_justify = zfs_prop_align_right(pl->pl_prop); right_justify = zfs_prop_align_right(pl->pl_prop);
} else if (pl->pl_prop != ZPROP_USERPROP) { } else if (pl->pl_prop != ZPROP_USERPROP) {
if (zfs_prop_get(zhp, pl->pl_prop, property, if (zfs_prop_get(zhp, pl->pl_prop, property,
sizeof (property), NULL, NULL, 0, sizeof (property), &sourcetype, source,
cb->cb_literal) != 0) sizeof (source), cb->cb_literal) != 0)
propstr = "-"; propstr = "-";
else else
propstr = property; propstr = property;
right_justify = zfs_prop_align_right(pl->pl_prop); right_justify = zfs_prop_align_right(pl->pl_prop);
} else if (zfs_prop_userquota(pl->pl_user_prop)) { } else if (zfs_prop_userquota(pl->pl_user_prop)) {
sourcetype = ZPROP_SRC_LOCAL;
if (zfs_prop_get_userquota(zhp, pl->pl_user_prop, if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
property, sizeof (property), cb->cb_literal) != 0) property, sizeof (property), cb->cb_literal) != 0) {
sourcetype = ZPROP_SRC_NONE;
propstr = "-"; propstr = "-";
else } else {
propstr = property; propstr = property;
}
right_justify = B_TRUE; right_justify = B_TRUE;
} else if (zfs_prop_written(pl->pl_user_prop)) { } else if (zfs_prop_written(pl->pl_user_prop)) {
sourcetype = ZPROP_SRC_LOCAL;
if (zfs_prop_get_written(zhp, pl->pl_user_prop, if (zfs_prop_get_written(zhp, pl->pl_user_prop,
property, sizeof (property), cb->cb_literal) != 0) property, sizeof (property), cb->cb_literal) != 0) {
sourcetype = ZPROP_SRC_NONE;
propstr = "-"; propstr = "-";
else } else {
propstr = property; propstr = property;
}
right_justify = B_TRUE; right_justify = B_TRUE;
} else { } else {
if (nvlist_lookup_nvlist(userprops, if (nvlist_lookup_nvlist(userprops,
pl->pl_user_prop, &propval) != 0) pl->pl_user_prop, &propval) != 0) {
propstr = "-"; propstr = "-";
else } else {
propstr = fnvlist_lookup_string(propval, propstr = fnvlist_lookup_string(propval,
ZPROP_VALUE); ZPROP_VALUE);
strlcpy(source,
fnvlist_lookup_string(propval,
ZPROP_SOURCE), ZFS_MAX_DATASET_NAME_LEN);
if (strcmp(source,
zfs_get_name(zhp)) == 0) {
sourcetype = ZPROP_SRC_LOCAL;
} else if (strcmp(source,
ZPROP_SOURCE_VAL_RECVD) == 0) {
sourcetype = ZPROP_SRC_RECEIVED;
} else {
sourcetype = ZPROP_SRC_INHERITED;
}
}
right_justify = B_FALSE; right_justify = B_FALSE;
} }
/* if (cb->cb_json) {
* zfs_list_avail_color() needs ZFS_PROP_AVAILABLE + USED if (pl->pl_prop == ZFS_PROP_NAME)
* - so we need another for() search for the USED part continue;
* - when no colors wanted, we can skip the whole thing if (zprop_nvlist_one_property(
*/ zfs_prop_to_name(pl->pl_prop), propstr,
if (use_color() && pl->pl_prop == ZFS_PROP_AVAILABLE) { sourcetype, source, NULL, props,
zprop_list_t *pl2 = cb->cb_proplist; cb->cb_json_as_int) != 0)
for (; pl2 != NULL; pl2 = pl2->pl_next) { nomem();
if (pl2->pl_prop == ZFS_PROP_USED) { } else {
color_start(zfs_list_avail_color(zhp)); /*
/* found it, no need for more loops */ * zfs_list_avail_color() needs
break; * ZFS_PROP_AVAILABLE + USED, so we need another
* for() search for the USED part when no colors
* wanted, we can skip the whole thing
*/
if (use_color() && pl->pl_prop == ZFS_PROP_AVAILABLE) {
zprop_list_t *pl2 = cb->cb_proplist;
for (; pl2 != NULL; pl2 = pl2->pl_next) {
if (pl2->pl_prop == ZFS_PROP_USED) {
color_start(
zfs_list_avail_color(zhp));
/*
* found it, no need for more
* loops
*/
break;
}
} }
} }
/*
* If this is being called in scripted mode, or if
* this is the last column and it is left-justified,
* don't include a width format specifier.
*/
if (cb->cb_scripted || (pl->pl_next == NULL &&
!right_justify))
(void) fputs(propstr, stdout);
else if (right_justify) {
(void) printf("%*s", (int)pl->pl_width,
propstr);
} else {
(void) printf("%-*s", (int)pl->pl_width,
propstr);
}
if (pl->pl_prop == ZFS_PROP_AVAILABLE)
color_end();
} }
/*
* If this is being called in scripted mode, or if this is the
* last column and it is left-justified, don't include a width
* format specifier.
*/
if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
(void) fputs(propstr, stdout);
else if (right_justify)
(void) printf("%*s", (int)pl->pl_width, propstr);
else
(void) printf("%-*s", (int)pl->pl_width, propstr);
if (pl->pl_prop == ZFS_PROP_AVAILABLE)
color_end();
} }
if (cb->cb_json) {
(void) putchar('\n'); fnvlist_add_nvlist(item, "properties", props);
fnvlist_add_nvlist(d, zfs_get_name(zhp), item);
fnvlist_free(props);
fnvlist_free(item);
} else
(void) putchar('\n');
} }
/* /*
@ -3783,12 +3845,12 @@ list_callback(zfs_handle_t *zhp, void *data)
list_cbdata_t *cbp = data; list_cbdata_t *cbp = data;
if (cbp->cb_first) { if (cbp->cb_first) {
if (!cbp->cb_scripted) if (!cbp->cb_scripted && !cbp->cb_json)
print_header(cbp); print_header(cbp);
cbp->cb_first = B_FALSE; cbp->cb_first = B_FALSE;
} }
print_dataset(zhp, cbp); collect_dataset(zhp, cbp);
return (0); return (0);
} }
@ -3807,9 +3869,16 @@ zfs_do_list(int argc, char **argv)
int ret = 0; int ret = 0;
zfs_sort_column_t *sortcol = NULL; zfs_sort_column_t *sortcol = NULL;
int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS; int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
nvlist_t *data = NULL;
struct option long_options[] = {
{"json-int", no_argument, NULL, ZFS_OPTION_JSON_NUMS_AS_INT},
{0, 0, 0, 0}
};
/* check options */ /* check options */
while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) { while ((c = getopt_long(argc, argv, "jHS:d:o:prs:t:", long_options,
NULL)) != -1) {
switch (c) { switch (c) {
case 'o': case 'o':
fields = optarg; fields = optarg;
@ -3824,6 +3893,17 @@ zfs_do_list(int argc, char **argv)
case 'r': case 'r':
flags |= ZFS_ITER_RECURSE; flags |= ZFS_ITER_RECURSE;
break; break;
case 'j':
cb.cb_json = B_TRUE;
cb.cb_jsobj = zfs_json_schema(0, 1);
data = fnvlist_alloc();
fnvlist_add_nvlist(cb.cb_jsobj, "datasets", data);
fnvlist_free(data);
break;
case ZFS_OPTION_JSON_NUMS_AS_INT:
cb.cb_json_as_int = B_TRUE;
cb.cb_literal = B_TRUE;
break;
case 'H': case 'H':
cb.cb_scripted = B_TRUE; cb.cb_scripted = B_TRUE;
break; break;
@ -3897,6 +3977,12 @@ found3:;
argc -= optind; argc -= optind;
argv += optind; argv += optind;
if (!cb.cb_json && cb.cb_json_as_int) {
(void) fprintf(stderr, gettext("'--json-int' only works with"
" '-j' option\n"));
usage(B_FALSE);
}
/* /*
* If "-o space" and no types were specified, don't display snapshots. * If "-o space" and no types were specified, don't display snapshots.
*/ */
@ -3936,6 +4022,11 @@ found3:;
ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist, ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
limit, list_callback, &cb); limit, list_callback, &cb);
if (ret == 0 && cb.cb_json)
zcmd_print_json(cb.cb_jsobj);
else if (ret != 0 && cb.cb_json)
nvlist_free(cb.cb_jsobj);
zprop_free_list(cb.cb_proplist); zprop_free_list(cb.cb_proplist);
zfs_free_sort_columns(sortcol); zfs_free_sort_columns(sortcol);

View File

@ -41,6 +41,7 @@
.Cm list .Cm list
.Op Fl r Ns | Ns Fl d Ar depth .Op Fl r Ns | Ns Fl d Ar depth
.Op Fl Hp .Op Fl Hp
.Op Fl j Op Ar --json-int
.Oo Fl o Ar property Ns Oo , Ns Ar property Oc Ns Oc .Oo Fl o Ar property Ns Oo , Ns Ar property Oc Ns Oc
.Oo Fl s Ar property Oc Ns .Oo Fl s Ar property Oc Ns
.Oo Fl S Ar property Oc Ns .Oo Fl S Ar property Oc Ns
@ -70,6 +71,11 @@ The following fields are displayed:
Used for scripting mode. Used for scripting mode.
Do not print headers and separate fields by a single tab instead of arbitrary Do not print headers and separate fields by a single tab instead of arbitrary
white space. white space.
.It Fl j Op Ar --json-int
Print the output in JSON format.
Specify
.Sy --json-int
to print the numbers in integer format instead of strings in JSON output.
.It Fl d Ar depth .It Fl d Ar depth
Recursively display any children of the dataset, limiting the recursion to Recursively display any children of the dataset, limiting the recursion to
.Ar depth . .Ar depth .
@ -186,6 +192,161 @@ pool/home 315K 457G 21K /export/home
pool/home/anne 18K 457G 18K /export/home/anne pool/home/anne 18K 457G 18K /export/home/anne
pool/home/bob 276K 457G 276K /export/home/bob pool/home/bob 276K 457G 276K /export/home/bob
.Ed .Ed
.Ss Example 2 : No Listing ZFS filesystems and snapshots in JSON format
.Bd -literal -compact -offset Ds
.No # Nm zfs Cm list Fl j Fl t Ar filesystem,snapshot | Cm jq
{
"output_version": {
"command": "zfs list",
"vers_major": 0,
"vers_minor": 1
},
"datasets": {
"pool": {
"name": "pool",
"type": "FILESYSTEM",
"pool": "pool",
"properties": {
"used": {
"value": "290K",
"source": {
"type": "NONE",
"data": "-"
}
},
"available": {
"value": "30.5G",
"source": {
"type": "NONE",
"data": "-"
}
},
"referenced": {
"value": "24K",
"source": {
"type": "NONE",
"data": "-"
}
},
"mountpoint": {
"value": "/pool",
"source": {
"type": "DEFAULT",
"data": "-"
}
}
}
},
"pool/home": {
"name": "pool/home",
"type": "FILESYSTEM",
"pool": "pool",
"properties": {
"used": {
"value": "48K",
"source": {
"type": "NONE",
"data": "-"
}
},
"available": {
"value": "30.5G",
"source": {
"type": "NONE",
"data": "-"
}
},
"referenced": {
"value": "24K",
"source": {
"type": "NONE",
"data": "-"
}
},
"mountpoint": {
"value": "/mnt/home",
"source": {
"type": "LOCAL",
"data": "-"
}
}
}
},
"pool/home/bob": {
"name": "pool/home/bob",
"type": "FILESYSTEM",
"pool": "pool",
"properties": {
"used": {
"value": "24K",
"source": {
"type": "NONE",
"data": "-"
}
},
"available": {
"value": "30.5G",
"source": {
"type": "NONE",
"data": "-"
}
},
"referenced": {
"value": "24K",
"source": {
"type": "NONE",
"data": "-"
}
},
"mountpoint": {
"value": "/mnt/home/bob",
"source": {
"type": "INHERITED",
"data": "pool/home"
}
}
}
},
"pool/home/bob@v1": {
"name": "pool/home/bob@v1",
"type": "SNAPSHOT",
"pool": "pool",
"dataset": "pool/home/bob",
"snapshot_name": "v1",
"properties": {
"used": {
"value": "0B",
"source": {
"type": "NONE",
"data": "-"
}
},
"available": {
"value": "-",
"source": {
"type": "NONE",
"data": "-"
}
},
"referenced": {
"value": "24K",
"source": {
"type": "NONE",
"data": "-"
}
},
"mountpoint": {
"value": "-",
"source": {
"type": "NONE",
"data": "-"
}
}
}
}
}
}
.Ed
. .
.Sh SEE ALSO .Sh SEE ALSO
.Xr zfsprops 7 , .Xr zfsprops 7 ,