@@ -349,6 +349,8 @@ static zpool_command_t command_table[] = {
349
349
350
350
#define VDEV_ALLOC_CLASS_LOGS "logs"
351
351
352
+ #define MAX_CMD_LEN 256
353
+
352
354
static zpool_command_t * current_command ;
353
355
static zfs_type_t current_prop_type = (ZFS_TYPE_POOL | ZFS_TYPE_VDEV );
354
356
static char history_str [HIS_MAX_RECORD_LEN ];
@@ -452,7 +454,7 @@ get_usage(zpool_help_t idx)
452
454
case HELP_SYNC :
453
455
return (gettext ("\tsync [pool] ...\n" ));
454
456
case HELP_VERSION :
455
- return (gettext ("\tversion\n" ));
457
+ return (gettext ("\tversion [-j] \n" ));
456
458
case HELP_WAIT :
457
459
return (gettext ("\twait [-Hp] [-T d|u] [-t <activity>[,...]] "
458
460
"<pool> [interval]\n" ));
@@ -10344,6 +10346,35 @@ zpool_do_history(int argc, char **argv)
10344
10346
return (ret );
10345
10347
}
10346
10348
10349
+ /*
10350
+ * Generates an nvlist with output version for every command based on params.
10351
+ * Purpose of this is to add a version of JSON output, considering the schema
10352
+ * format might be updated for each command in future.
10353
+ *
10354
+ * Schema:
10355
+ *
10356
+ * "output_version": {
10357
+ * "command": string,
10358
+ * "vers_major": integer,
10359
+ * "vers_minor": integer,
10360
+ * }
10361
+ */
10362
+ static nvlist_t *
10363
+ zpool_json_schema (int maj_v , int min_v )
10364
+ {
10365
+ char cmd [MAX_CMD_LEN ];
10366
+ nvlist_t * sch = fnvlist_alloc ();
10367
+ nvlist_t * ov = fnvlist_alloc ();
10368
+
10369
+ snprintf (cmd , MAX_CMD_LEN , "zpool %s" , current_command -> name );
10370
+ fnvlist_add_string (ov , "command" , cmd );
10371
+ fnvlist_add_uint32 (ov , "vers_major" , maj_v );
10372
+ fnvlist_add_uint32 (ov , "vers_minor" , min_v );
10373
+ fnvlist_add_nvlist (sch , "output_version" , ov );
10374
+
10375
+ return (sch );
10376
+ }
10377
+
10347
10378
typedef struct ev_opts {
10348
10379
int verbose ;
10349
10380
int scripted ;
@@ -11688,8 +11719,39 @@ find_command_idx(const char *command, int *idx)
11688
11719
static int
11689
11720
zpool_do_version (int argc , char * * argv )
11690
11721
{
11691
- (void ) argc , (void ) argv ;
11692
- return (zfs_version_print () != 0 );
11722
+ int c ;
11723
+ nvlist_t * jsobj = NULL , * zfs_ver = NULL ;
11724
+ boolean_t json = B_FALSE ;
11725
+ while ((c = getopt (argc , argv , "j" )) != -1 ) {
11726
+ switch (c ) {
11727
+ case 'j' :
11728
+ json = B_TRUE ;
11729
+ jsobj = zpool_json_schema (0 , 1 );
11730
+ break ;
11731
+ case '?' :
11732
+ (void ) fprintf (stderr , gettext ("invalid option '%c'\n" ),
11733
+ optopt );
11734
+ usage (B_FALSE );
11735
+ }
11736
+ }
11737
+
11738
+ argc -= optind ;
11739
+ if (argc != 0 ) {
11740
+ (void ) fprintf (stderr , "too many arguments\n" );
11741
+ usage (B_FALSE );
11742
+ }
11743
+
11744
+ if (json ) {
11745
+ zfs_ver = zfs_version_nvlist ();
11746
+ if (zfs_ver ) {
11747
+ fnvlist_add_nvlist (jsobj , "zfs_version" , zfs_ver );
11748
+ zcmd_print_json (jsobj );
11749
+ fnvlist_free (zfs_ver );
11750
+ return (0 );
11751
+ } else
11752
+ return (-1 );
11753
+ } else
11754
+ return (zfs_version_print () != 0 );
11693
11755
}
11694
11756
11695
11757
/* Display documentation */
0 commit comments