Skip to content

Commit

Permalink
JSON output support zfs mount
Browse files Browse the repository at this point in the history
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
  • Loading branch information
usaleem-ix authored and behlendorf committed Aug 6, 2024
1 parent 443abfc commit cad4c0e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
41 changes: 36 additions & 5 deletions cmd/zfs/zfs_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ get_usage(zfs_help_t idx)
"[-S property]... [-t type[,...]] "
"[filesystem|volume|snapshot] ...\n"));
case HELP_MOUNT:
return (gettext("\tmount\n"
return (gettext("\tmount [-j]\n"
"\tmount [-flvO] [-o opts] <-a|-R filesystem|"
"filesystem>\n"));
case HELP_PROMOTE:
Expand Down Expand Up @@ -7447,14 +7447,17 @@ share_mount(int op, int argc, char **argv)
int do_all = 0;
int recursive = 0;
boolean_t verbose = B_FALSE;
boolean_t json = B_FALSE;
int c, ret = 0;
char *options = NULL;
int flags = 0;
nvlist_t *jsobj, *data, *item;
const uint_t mount_nthr = 512;
uint_t nthr;
jsobj = data = item = NULL;

/* 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) {
switch (c) {
case 'a':
Expand All @@ -7469,6 +7472,11 @@ share_mount(int op, int argc, char **argv)
case 'l':
flags |= MS_CRYPT;
break;
case 'j':
json = B_TRUE;
jsobj = zfs_json_schema(0, 1);
data = fnvlist_alloc();
break;
case 'o':
if (*optarg == '\0') {
(void) fprintf(stderr, gettext("empty mount "
Expand Down Expand Up @@ -7503,6 +7511,11 @@ share_mount(int op, int argc, char **argv)
argc -= optind;
argv += optind;

if (json && argc != 0) {
(void) fprintf(stderr, gettext("too many arguments\n"));
usage(B_FALSE);
}

/* check number of arguments */
if (do_all || recursive) {
enum sa_protocol protocol = SA_NO_PROTOCOL;
Expand Down Expand Up @@ -7606,12 +7619,30 @@ share_mount(int op, int argc, char **argv)
if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
strchr(entry.mnt_special, '@') != NULL)
continue;

(void) printf("%-30s %s\n", entry.mnt_special,
entry.mnt_mountp);
if (json) {
item = fnvlist_alloc();
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);
if (json) {
fnvlist_add_nvlist(jsobj, "datasets", data);
if (nvlist_empty(data))
fnvlist_free(jsobj);
else
zcmd_print_json(jsobj);
fnvlist_free(data);
}
} else {
zfs_handle_t *zhp;

Expand Down
6 changes: 6 additions & 0 deletions man/man8/zfs-mount.8
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
.Sh SYNOPSIS
.Nm zfs
.Cm mount
.Op Fl j
.Nm zfs
.Cm mount
.Op Fl Oflv
Expand All @@ -54,8 +55,13 @@
.It Xo
.Nm zfs
.Cm mount
.Op Fl j
.Xc
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
.Nm zfs
.Cm mount
Expand Down

0 comments on commit cad4c0e

Please sign in to comment.