Skip to content

Commit 2a8b84b

Browse files
xen0lbehlendorf
authored andcommitted
OpenZFS 3993, 4700
3993 zpool(1M) and zfs(1M) should support -p for "list" and "get" 4700 "zpool get" doesn't support -H or -o options Reviewed by: Dan McDonald <[email protected]> Reviewed by: Matthew Ahrens <[email protected]> Approved by: Robert Mustacchi <[email protected]> Ported by: Tony Hutter <[email protected]> Signed-off-by: Brian Behlendorf <[email protected]> OpenZFS-issue: https://www.illumos.org/issues/3993 OpenZFS-issue: https://www.illumos.org/issues/4700 OpenZFS-commit: openzfs/openzfs@c58b352 Porting notes: I removed ZoL's zpool_get_prop_literal() in favor of zpool_get_prop(..., boolean_t literal) since that's what OpenZFS uses. The functionality is the same.
1 parent f00828e commit 2a8b84b

File tree

5 files changed

+139
-50
lines changed

5 files changed

+139
-50
lines changed

cmd/zpool/zpool_main.c

Lines changed: 92 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ get_usage(zpool_help_t idx) {
242242
case HELP_LABELCLEAR:
243243
return (gettext("\tlabelclear [-f] <vdev>\n"));
244244
case HELP_LIST:
245-
return (gettext("\tlist [-gHLPv] [-o property[,...]] "
245+
return (gettext("\tlist [-gHLpPv] [-o property[,...]] "
246246
"[-T d|u] [pool] ... [interval [count]]\n"));
247247
case HELP_OFFLINE:
248248
return (gettext("\toffline [-t] <pool> <device> ...\n"));
@@ -267,8 +267,8 @@ get_usage(zpool_help_t idx) {
267267
case HELP_EVENTS:
268268
return (gettext("\tevents [-vHfc]\n"));
269269
case HELP_GET:
270-
return (gettext("\tget [-pH] <\"all\" | property[,...]> "
271-
"<pool> ...\n"));
270+
return (gettext("\tget [-Hp] [-o \"all\" | field[,...]] "
271+
"<\"all\" | property[,...]> <pool> ...\n"));
272272
case HELP_SET:
273273
return (gettext("\tset <property=value> <pool> \n"));
274274
case HELP_SPLIT:
@@ -3024,6 +3024,7 @@ typedef struct list_cbdata {
30243024
int cb_namewidth;
30253025
boolean_t cb_scripted;
30263026
zprop_list_t *cb_proplist;
3027+
boolean_t cb_literal;
30273028
} list_cbdata_t;
30283029

30293030
/*
@@ -3115,7 +3116,7 @@ print_pool(zpool_handle_t *zhp, list_cbdata_t *cb)
31153116
right_justify = B_FALSE;
31163117
if (pl->pl_prop != ZPROP_INVAL) {
31173118
if (zpool_get_prop(zhp, pl->pl_prop, property,
3118-
sizeof (property), NULL) != 0)
3119+
sizeof (property), NULL, cb->cb_literal) != 0)
31193120
propstr = "-";
31203121
else
31213122
propstr = property;
@@ -3325,7 +3326,7 @@ list_callback(zpool_handle_t *zhp, void *data)
33253326
}
33263327

33273328
/*
3328-
* zpool list [-gHLP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
3329+
* zpool list [-gHLpP] [-o prop[,prop]*] [-T d|u] [pool] ... [interval [count]]
33293330
*
33303331
* -g Display guid for individual vdev name.
33313332
* -H Scripted mode. Don't display headers, and separate properties
@@ -3334,6 +3335,7 @@ list_callback(zpool_handle_t *zhp, void *data)
33343335
* -o List of properties to display. Defaults to
33353336
* "name,size,allocated,free,expandsize,fragmentation,capacity,"
33363337
* "dedupratio,health,altroot"
3338+
* -p Display values in parsable (exact) format.
33373339
* -P Display full path for vdev name.
33383340
* -T Display a timestamp in date(1) or Unix format
33393341
*
@@ -3355,7 +3357,7 @@ zpool_do_list(int argc, char **argv)
33553357
boolean_t first = B_TRUE;
33563358

33573359
/* check options */
3358-
while ((c = getopt(argc, argv, ":gHLo:PT:v")) != -1) {
3360+
while ((c = getopt(argc, argv, ":gHLo:pPT:v")) != -1) {
33593361
switch (c) {
33603362
case 'g':
33613363
cb.cb_name_flags |= VDEV_NAME_GUID;
@@ -3372,6 +3374,9 @@ zpool_do_list(int argc, char **argv)
33723374
case 'P':
33733375
cb.cb_name_flags |= VDEV_NAME_PATH;
33743376
break;
3377+
case 'p':
3378+
cb.cb_literal = B_TRUE;
3379+
break;
33753380
case 'T':
33763381
get_timestamp_arg(*optarg);
33773382
break;
@@ -5886,7 +5891,7 @@ get_callback(zpool_handle_t *zhp, void *data)
58865891
NULL, NULL);
58875892
}
58885893
} else {
5889-
if (zpool_get_prop_literal(zhp, pl->pl_prop, value,
5894+
if (zpool_get_prop(zhp, pl->pl_prop, value,
58905895
sizeof (value), &srctype, cbp->cb_literal) != 0)
58915896
continue;
58925897

@@ -5898,24 +5903,99 @@ get_callback(zpool_handle_t *zhp, void *data)
58985903
return (0);
58995904
}
59005905

5906+
/*
5907+
* zpool get [-Hp] [-o "all" | field[,...]] <"all" | property[,...]> <pool> ...
5908+
*
5909+
* -H Scripted mode. Don't display headers, and separate properties
5910+
* by a single tab.
5911+
* -o List of columns to display. Defaults to
5912+
* "name,property,value,source".
5913+
* -p Diplay values in parsable (exact) format.
5914+
*
5915+
* Get properties of pools in the system. Output space statistics
5916+
* for each one as well as other attributes.
5917+
*/
59015918
int
59025919
zpool_do_get(int argc, char **argv)
59035920
{
59045921
zprop_get_cbdata_t cb = { 0 };
59055922
zprop_list_t fake_name = { 0 };
5906-
int c, ret;
5923+
int ret;
5924+
int c, i;
5925+
char *value;
5926+
5927+
cb.cb_first = B_TRUE;
5928+
5929+
/*
5930+
* Set up default columns and sources.
5931+
*/
5932+
cb.cb_sources = ZPROP_SRC_ALL;
5933+
cb.cb_columns[0] = GET_COL_NAME;
5934+
cb.cb_columns[1] = GET_COL_PROPERTY;
5935+
cb.cb_columns[2] = GET_COL_VALUE;
5936+
cb.cb_columns[3] = GET_COL_SOURCE;
5937+
cb.cb_type = ZFS_TYPE_POOL;
59075938

59085939
/* check options */
5909-
while ((c = getopt(argc, argv, "pH")) != -1) {
5940+
while ((c = getopt(argc, argv, ":Hpo:")) != -1) {
59105941
switch (c) {
59115942
case 'p':
59125943
cb.cb_literal = B_TRUE;
59135944
break;
5914-
59155945
case 'H':
59165946
cb.cb_scripted = B_TRUE;
59175947
break;
5948+
case 'o':
5949+
bzero(&cb.cb_columns, sizeof (cb.cb_columns));
5950+
i = 0;
5951+
while (*optarg != '\0') {
5952+
static char *col_subopts[] =
5953+
{ "name", "property", "value", "source",
5954+
"all", NULL };
5955+
5956+
if (i == ZFS_GET_NCOLS) {
5957+
(void) fprintf(stderr, gettext("too "
5958+
"many fields given to -o "
5959+
"option\n"));
5960+
usage(B_FALSE);
5961+
}
59185962

5963+
switch (getsubopt(&optarg, col_subopts,
5964+
&value)) {
5965+
case 0:
5966+
cb.cb_columns[i++] = GET_COL_NAME;
5967+
break;
5968+
case 1:
5969+
cb.cb_columns[i++] = GET_COL_PROPERTY;
5970+
break;
5971+
case 2:
5972+
cb.cb_columns[i++] = GET_COL_VALUE;
5973+
break;
5974+
case 3:
5975+
cb.cb_columns[i++] = GET_COL_SOURCE;
5976+
break;
5977+
case 4:
5978+
if (i > 0) {
5979+
(void) fprintf(stderr,
5980+
gettext("\"all\" conflicts "
5981+
"with specific fields "
5982+
"given to -o option\n"));
5983+
usage(B_FALSE);
5984+
}
5985+
cb.cb_columns[0] = GET_COL_NAME;
5986+
cb.cb_columns[1] = GET_COL_PROPERTY;
5987+
cb.cb_columns[2] = GET_COL_VALUE;
5988+
cb.cb_columns[3] = GET_COL_SOURCE;
5989+
i = ZFS_GET_NCOLS;
5990+
break;
5991+
default:
5992+
(void) fprintf(stderr,
5993+
gettext("invalid column name "
5994+
"'%s'\n"), value);
5995+
usage(B_FALSE);
5996+
}
5997+
}
5998+
break;
59195999
case '?':
59206000
(void) fprintf(stderr, gettext("invalid option '%c'\n"),
59216001
optopt);
@@ -5932,15 +6012,8 @@ zpool_do_get(int argc, char **argv)
59326012
usage(B_FALSE);
59336013
}
59346014

5935-
cb.cb_first = B_TRUE;
5936-
cb.cb_sources = ZPROP_SRC_ALL;
5937-
cb.cb_columns[0] = GET_COL_NAME;
5938-
cb.cb_columns[1] = GET_COL_PROPERTY;
5939-
cb.cb_columns[2] = GET_COL_VALUE;
5940-
cb.cb_columns[3] = GET_COL_SOURCE;
5941-
cb.cb_type = ZFS_TYPE_POOL;
5942-
5943-
if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist, ZFS_TYPE_POOL) != 0)
6015+
if (zprop_get_list(g_zfs, argv[0], &cb.cb_proplist,
6016+
ZFS_TYPE_POOL) != 0)
59446017
usage(B_FALSE);
59456018

59466019
argc--;

include/libzfs.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,6 @@ extern int zpool_label_disk(libzfs_handle_t *, zpool_handle_t *, char *);
287287
*/
288288
extern int zpool_set_prop(zpool_handle_t *, const char *, const char *);
289289
extern int zpool_get_prop(zpool_handle_t *, zpool_prop_t, char *,
290-
size_t proplen, zprop_source_t *);
291-
extern int zpool_get_prop_literal(zpool_handle_t *, zpool_prop_t, char *,
292290
size_t proplen, zprop_source_t *, boolean_t literal);
293291
extern uint64_t zpool_get_prop_int(zpool_handle_t *, zpool_prop_t,
294292
zprop_source_t *);

lib/libzfs/libzfs_dataset.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2372,8 +2372,8 @@ zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
23722372
}
23732373

23742374
if ((zpool_get_prop(zhp->zpool_hdl,
2375-
ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
2376-
(strcmp(root, "-") == 0))
2375+
ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL,
2376+
B_FALSE)) || (strcmp(root, "-") == 0))
23772377
root[0] = '\0';
23782378
/*
23792379
* Special case an alternate root of '/'. This will

lib/libzfs/libzfs_pool.c

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -235,22 +235,12 @@ zpool_pool_state_to_name(pool_state_t state)
235235
return (gettext("UNKNOWN"));
236236
}
237237

238-
/*
239-
* API compatibility wrapper around zpool_get_prop_literal
240-
*/
241-
int
242-
zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
243-
zprop_source_t *srctype)
244-
{
245-
return (zpool_get_prop_literal(zhp, prop, buf, len, srctype, B_FALSE));
246-
}
247-
248238
/*
249239
* Get a zpool property value for 'prop' and return the value in
250240
* a pre-allocated buffer.
251241
*/
252242
int
253-
zpool_get_prop_literal(zpool_handle_t *zhp, zpool_prop_t prop, char *buf,
243+
zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf,
254244
size_t len, zprop_source_t *srctype, boolean_t literal)
255245
{
256246
uint64_t intval;
@@ -283,9 +273,7 @@ zpool_get_prop_literal(zpool_handle_t *zhp, zpool_prop_t prop, char *buf,
283273
(void) strlcpy(buf,
284274
zpool_get_prop_string(zhp, prop, &src),
285275
len);
286-
if (srctype != NULL)
287-
*srctype = src;
288-
return (0);
276+
break;
289277
}
290278
/* FALLTHROUGH */
291279
default:
@@ -337,8 +325,13 @@ zpool_get_prop_literal(zpool_handle_t *zhp, zpool_prop_t prop, char *buf,
337325
break;
338326

339327
case ZPOOL_PROP_CAPACITY:
340-
(void) snprintf(buf, len, "%llu%%",
341-
(u_longlong_t)intval);
328+
if (literal) {
329+
(void) snprintf(buf, len, "%llu",
330+
(u_longlong_t)intval);
331+
} else {
332+
(void) snprintf(buf, len, "%llu%%",
333+
(u_longlong_t)intval);
334+
}
342335
break;
343336

344337
case ZPOOL_PROP_FRAGMENTATION:
@@ -443,7 +436,7 @@ zpool_is_bootable(zpool_handle_t *zhp)
443436
char bootfs[ZPOOL_MAXNAMELEN];
444437

445438
return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
446-
sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-",
439+
sizeof (bootfs), NULL, B_FALSE) == 0 && strncmp(bootfs, "-",
447440
sizeof (bootfs)) != 0);
448441
}
449442

@@ -873,7 +866,7 @@ zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
873866

874867
if (entry->pl_prop != ZPROP_INVAL &&
875868
zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
876-
NULL) == 0) {
869+
NULL, B_FALSE) == 0) {
877870
if (strlen(buf) > entry->pl_width)
878871
entry->pl_width = strlen(buf);
879872
}

0 commit comments

Comments
 (0)