@@ -346,6 +346,8 @@ static zpool_command_t command_table[] = {
346
346
347
347
#define VDEV_ALLOC_CLASS_LOGS "logs"
348
348
349
+ #define MAX_CMD_LEN 256
350
+
349
351
static zpool_command_t * current_command ;
350
352
static zfs_type_t current_prop_type = (ZFS_TYPE_POOL | ZFS_TYPE_VDEV );
351
353
static char history_str [HIS_MAX_RECORD_LEN ];
@@ -446,7 +448,7 @@ get_usage(zpool_help_t idx)
446
448
case HELP_SYNC :
447
449
return (gettext ("\tsync [pool] ...\n" ));
448
450
case HELP_VERSION :
449
- return (gettext ("\tversion\n" ));
451
+ return (gettext ("\tversion [-j] \n" ));
450
452
case HELP_WAIT :
451
453
return (gettext ("\twait [-Hp] [-T d|u] [-t <activity>[,...]] "
452
454
"<pool> [interval]\n" ));
@@ -10241,6 +10243,35 @@ zpool_do_history(int argc, char **argv)
10241
10243
return (ret );
10242
10244
}
10243
10245
10246
+ /*
10247
+ * Generates an nvlist with output version for every command based on params.
10248
+ * Purpose of this is to add a version of JSON output, considering the schema
10249
+ * format might be updated for each command in future.
10250
+ *
10251
+ * Schema:
10252
+ *
10253
+ * "output_version": {
10254
+ * "command": string,
10255
+ * "vers_major": integer,
10256
+ * "vers_minor": integer,
10257
+ * }
10258
+ */
10259
+ static nvlist_t *
10260
+ zpool_json_schema (int maj_v , int min_v )
10261
+ {
10262
+ char cmd [MAX_CMD_LEN ];
10263
+ nvlist_t * sch = fnvlist_alloc ();
10264
+ nvlist_t * ov = fnvlist_alloc ();
10265
+
10266
+ snprintf (cmd , MAX_CMD_LEN , "zpool %s" , current_command -> name );
10267
+ fnvlist_add_string (ov , "command" , cmd );
10268
+ fnvlist_add_uint32 (ov , "vers_major" , maj_v );
10269
+ fnvlist_add_uint32 (ov , "vers_minor" , min_v );
10270
+ fnvlist_add_nvlist (sch , "output_version" , ov );
10271
+
10272
+ return (sch );
10273
+ }
10274
+
10244
10275
typedef struct ev_opts {
10245
10276
int verbose ;
10246
10277
int scripted ;
@@ -11585,8 +11616,39 @@ find_command_idx(const char *command, int *idx)
11585
11616
static int
11586
11617
zpool_do_version (int argc , char * * argv )
11587
11618
{
11588
- (void ) argc , (void ) argv ;
11589
- return (zfs_version_print () != 0 );
11619
+ int c ;
11620
+ nvlist_t * jsobj = NULL , * zfs_ver = NULL ;
11621
+ boolean_t json = B_FALSE ;
11622
+ while ((c = getopt (argc , argv , "j" )) != -1 ) {
11623
+ switch (c ) {
11624
+ case 'j' :
11625
+ json = B_TRUE ;
11626
+ jsobj = zpool_json_schema (0 , 1 );
11627
+ break ;
11628
+ case '?' :
11629
+ (void ) fprintf (stderr , gettext ("invalid option '%c'\n" ),
11630
+ optopt );
11631
+ usage (B_FALSE );
11632
+ }
11633
+ }
11634
+
11635
+ argc -= optind ;
11636
+ if (argc != 0 ) {
11637
+ (void ) fprintf (stderr , "too many arguments\n" );
11638
+ usage (B_FALSE );
11639
+ }
11640
+
11641
+ if (json ) {
11642
+ zfs_ver = zfs_version_nvlist ();
11643
+ if (zfs_ver ) {
11644
+ fnvlist_add_nvlist (jsobj , "zfs_version" , zfs_ver );
11645
+ zcmd_print_json (jsobj );
11646
+ fnvlist_free (zfs_ver );
11647
+ return (0 );
11648
+ } else
11649
+ return (-1 );
11650
+ } else
11651
+ return (zfs_version_print () != 0 );
11590
11652
}
11591
11653
11592
11654
/* Display documentation */
0 commit comments