From cad4c0ef1a1d038582627d7839c50074caf718a9 Mon Sep 17 00:00:00 2001 From: Umer Saleem Date: Thu, 18 Apr 2024 11:41:32 +0500 Subject: [PATCH] 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 Reviewed-by: Ameer Hamza Signed-off-by: Umer Saleem Closes #16217 --- cmd/zfs/zfs_main.c | 41 ++++++++++++++++++++++++++++++++++++----- man/man8/zfs-mount.8 | 6 ++++++ 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/cmd/zfs/zfs_main.c b/cmd/zfs/zfs_main.c index dca18853df..bbc63cc618 100644 --- a/cmd/zfs/zfs_main.c +++ b/cmd/zfs/zfs_main.c @@ -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: @@ -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': @@ -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 " @@ -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; @@ -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; diff --git a/man/man8/zfs-mount.8 b/man/man8/zfs-mount.8 index 20dbe4d0e6..6116fbaab7 100644 --- a/man/man8/zfs-mount.8 +++ b/man/man8/zfs-mount.8 @@ -39,6 +39,7 @@ .Sh SYNOPSIS .Nm zfs .Cm mount +.Op Fl j .Nm zfs .Cm mount .Op Fl Oflv @@ -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