Skip to content

Commit cad4c0e

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

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

cmd/zfs/zfs_main.c

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ get_usage(zfs_help_t idx)
315315
"[-S property]... [-t type[,...]] "
316316
"[filesystem|volume|snapshot] ...\n"));
317317
case HELP_MOUNT:
318-
return (gettext("\tmount\n"
318+
return (gettext("\tmount [-j]\n"
319319
"\tmount [-flvO] [-o opts] <-a|-R filesystem|"
320320
"filesystem>\n"));
321321
case HELP_PROMOTE:
@@ -7447,14 +7447,17 @@ share_mount(int op, int argc, char **argv)
74477447
int do_all = 0;
74487448
int recursive = 0;
74497449
boolean_t verbose = B_FALSE;
7450+
boolean_t json = B_FALSE;
74507451
int c, ret = 0;
74517452
char *options = NULL;
74527453
int flags = 0;
7454+
nvlist_t *jsobj, *data, *item;
74537455
const uint_t mount_nthr = 512;
74547456
uint_t nthr;
7457+
jsobj = data = item = NULL;
74557458

74567459
/* check options */
7457-
while ((c = getopt(argc, argv, op == OP_MOUNT ? ":aRlvo:Of" : "al"))
7460+
while ((c = getopt(argc, argv, op == OP_MOUNT ? ":ajRlvo:Of" : "al"))
74587461
!= -1) {
74597462
switch (c) {
74607463
case 'a':
@@ -7469,6 +7472,11 @@ share_mount(int op, int argc, char **argv)
74697472
case 'l':
74707473
flags |= MS_CRYPT;
74717474
break;
7475+
case 'j':
7476+
json = B_TRUE;
7477+
jsobj = zfs_json_schema(0, 1);
7478+
data = fnvlist_alloc();
7479+
break;
74727480
case 'o':
74737481
if (*optarg == '\0') {
74747482
(void) fprintf(stderr, gettext("empty mount "
@@ -7503,6 +7511,11 @@ share_mount(int op, int argc, char **argv)
75037511
argc -= optind;
75047512
argv += optind;
75057513

7514+
if (json && argc != 0) {
7515+
(void) fprintf(stderr, gettext("too many arguments\n"));
7516+
usage(B_FALSE);
7517+
}
7518+
75067519
/* check number of arguments */
75077520
if (do_all || recursive) {
75087521
enum sa_protocol protocol = SA_NO_PROTOCOL;
@@ -7606,12 +7619,30 @@ share_mount(int op, int argc, char **argv)
76067619
if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
76077620
strchr(entry.mnt_special, '@') != NULL)
76087621
continue;
7609-
7610-
(void) printf("%-30s %s\n", entry.mnt_special,
7611-
entry.mnt_mountp);
7622+
if (json) {
7623+
item = fnvlist_alloc();
7624+
fnvlist_add_string(item, "filesystem",
7625+
entry.mnt_special);
7626+
fnvlist_add_string(item, "mountpoint",
7627+
entry.mnt_mountp);
7628+
fnvlist_add_nvlist(data, entry.mnt_special,
7629+
item);
7630+
fnvlist_free(item);
7631+
} else {
7632+
(void) printf("%-30s %s\n", entry.mnt_special,
7633+
entry.mnt_mountp);
7634+
}
76127635
}
76137636

76147637
(void) fclose(mnttab);
7638+
if (json) {
7639+
fnvlist_add_nvlist(jsobj, "datasets", data);
7640+
if (nvlist_empty(data))
7641+
fnvlist_free(jsobj);
7642+
else
7643+
zcmd_print_json(jsobj);
7644+
fnvlist_free(data);
7645+
}
76157646
} else {
76167647
zfs_handle_t *zhp;
76177648

man/man8/zfs-mount.8

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
.Sh SYNOPSIS
4040
.Nm zfs
4141
.Cm mount
42+
.Op Fl j
4243
.Nm zfs
4344
.Cm mount
4445
.Op Fl Oflv
@@ -54,8 +55,13 @@
5455
.It Xo
5556
.Nm zfs
5657
.Cm mount
58+
.Op Fl j
5759
.Xc
5860
Displays all ZFS file systems currently mounted.
61+
.Bl -tag -width "-j"
62+
.It Fl j
63+
Displays all mounted file systems in JSON format.
64+
.El
5965
.It Xo
6066
.Nm zfs
6167
.Cm mount

0 commit comments

Comments
 (0)