JSON output support zfs mount

This commit adds support for zfs mount to display mounted file systems
in JSON format using '-j' option. Data is collected in nvlist which is
printed in JSON format. man page for zfs mount is updated accordingly.

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 11:41:32 +05:00 committed by Brian Behlendorf
parent 443abfc71d
commit cad4c0ef1a
2 changed files with 42 additions and 5 deletions

View File

@ -315,7 +315,7 @@ get_usage(zfs_help_t idx)
"[-S property]... [-t type[,...]] " "[-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 [-j]\n"
"\tmount [-flvO] [-o opts] <-a|-R filesystem|" "\tmount [-flvO] [-o opts] <-a|-R filesystem|"
"filesystem>\n")); "filesystem>\n"));
case HELP_PROMOTE: case HELP_PROMOTE:
@ -7447,14 +7447,17 @@ share_mount(int op, int argc, char **argv)
int do_all = 0; int do_all = 0;
int recursive = 0; int recursive = 0;
boolean_t verbose = B_FALSE; boolean_t verbose = B_FALSE;
boolean_t json = B_FALSE;
int c, ret = 0; int c, ret = 0;
char *options = NULL; char *options = NULL;
int flags = 0; int flags = 0;
nvlist_t *jsobj, *data, *item;
const uint_t mount_nthr = 512; const uint_t mount_nthr = 512;
uint_t nthr; uint_t nthr;
jsobj = data = item = NULL;
/* check options */ /* check options */
while ((c = getopt(argc, argv, op == OP_MOUNT ? ":aRlvo:Of" : "al")) while ((c = getopt(argc, argv, op == OP_MOUNT ? ":ajRlvo:Of" : "al"))
!= -1) { != -1) {
switch (c) { switch (c) {
case 'a': case 'a':
@ -7469,6 +7472,11 @@ share_mount(int op, int argc, char **argv)
case 'l': case 'l':
flags |= MS_CRYPT; flags |= MS_CRYPT;
break; break;
case 'j':
json = B_TRUE;
jsobj = zfs_json_schema(0, 1);
data = fnvlist_alloc();
break;
case 'o': case 'o':
if (*optarg == '\0') { if (*optarg == '\0') {
(void) fprintf(stderr, gettext("empty mount " (void) fprintf(stderr, gettext("empty mount "
@ -7503,6 +7511,11 @@ share_mount(int op, int argc, char **argv)
argc -= optind; argc -= optind;
argv += optind; argv += optind;
if (json && argc != 0) {
(void) fprintf(stderr, gettext("too many arguments\n"));
usage(B_FALSE);
}
/* check number of arguments */ /* check number of arguments */
if (do_all || recursive) { if (do_all || recursive) {
enum sa_protocol protocol = SA_NO_PROTOCOL; enum sa_protocol protocol = SA_NO_PROTOCOL;
@ -7606,12 +7619,30 @@ share_mount(int op, int argc, char **argv)
if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 || if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
strchr(entry.mnt_special, '@') != NULL) strchr(entry.mnt_special, '@') != NULL)
continue; continue;
if (json) {
(void) printf("%-30s %s\n", entry.mnt_special, item = fnvlist_alloc();
entry.mnt_mountp); fnvlist_add_string(item, "filesystem",
entry.mnt_special);
fnvlist_add_string(item, "mountpoint",
entry.mnt_mountp);
fnvlist_add_nvlist(data, entry.mnt_special,
item);
fnvlist_free(item);
} else {
(void) printf("%-30s %s\n", entry.mnt_special,
entry.mnt_mountp);
}
} }
(void) fclose(mnttab); (void) fclose(mnttab);
if (json) {
fnvlist_add_nvlist(jsobj, "datasets", data);
if (nvlist_empty(data))
fnvlist_free(jsobj);
else
zcmd_print_json(jsobj);
fnvlist_free(data);
}
} else { } else {
zfs_handle_t *zhp; zfs_handle_t *zhp;

View File

@ -39,6 +39,7 @@
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm zfs .Nm zfs
.Cm mount .Cm mount
.Op Fl j
.Nm zfs .Nm zfs
.Cm mount .Cm mount
.Op Fl Oflv .Op Fl Oflv
@ -54,8 +55,13 @@
.It Xo .It Xo
.Nm zfs .Nm zfs
.Cm mount .Cm mount
.Op Fl j
.Xc .Xc
Displays all ZFS file systems currently mounted. Displays all ZFS file systems currently mounted.
.Bl -tag -width "-j"
.It Fl j
Displays all mounted file systems in JSON format.
.El
.It Xo .It Xo
.Nm zfs .Nm zfs
.Cm mount .Cm mount