Skip to content

Commit 8768ef2

Browse files
committed
Allow appending to default props for zfs and zpool list.
This commit allows users to add specific options in addition to the defaults when using `zfs list` or `zpool list`. For example, `zpool list -o +guid` will run `zpool list` showing the default properties in addition to guid. Resolves #17112. Signed-off-by: Shreshth Srivastava <[email protected]>
1 parent e72f305 commit 8768ef2

File tree

6 files changed

+176
-2
lines changed

6 files changed

+176
-2
lines changed

cmd/zfs/zfs_main.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3863,6 +3863,7 @@ zfs_do_list(int argc, char **argv)
38633863
zfs_sort_column_t *sortcol = NULL;
38643864
int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
38653865
nvlist_t *data = NULL;
3866+
char full_fields[1024];
38663867

38673868
struct option long_options[] = {
38683869
{"json", no_argument, NULL, 'j'},
@@ -3875,7 +3876,23 @@ zfs_do_list(int argc, char **argv)
38753876
NULL)) != -1) {
38763877
switch (c) {
38773878
case 'o':
3878-
fields = optarg;
3879+
if (optarg[0] == '+') {
3880+
/* +2 for the , and the null terminator */
3881+
if (strlen(fields) + strlen(optarg + 1) + 2 <=
3882+
1024) {
3883+
(void) snprintf(full_fields,
3884+
sizeof (full_fields), "%s,%s",
3885+
fields, optarg + 1);
3886+
} else {
3887+
(void) fprintf(stderr, gettext(
3888+
"argument too long for '-o'"
3889+
" option.\n"));
3890+
usage(B_FALSE);
3891+
}
3892+
fields = full_fields;
3893+
} else {
3894+
fields = optarg;
3895+
}
38793896
break;
38803897
case 'p':
38813898
cb.cb_literal = B_TRUE;

cmd/zpool/zpool_main.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7400,6 +7400,7 @@ zpool_do_list(int argc, char **argv)
74007400
boolean_t first = B_TRUE;
74017401
nvlist_t *data = NULL;
74027402
current_prop_type = ZFS_TYPE_POOL;
7403+
char full_props[1024];
74037404

74047405
struct option long_options[] = {
74057406
{"json", no_argument, NULL, 'j'},
@@ -7423,7 +7424,23 @@ zpool_do_list(int argc, char **argv)
74237424
cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS;
74247425
break;
74257426
case 'o':
7426-
props = optarg;
7427+
if (optarg[0] == '+') {
7428+
/* +2 for the , and the null terminator */
7429+
if (strlen(props) + strlen(optarg + 1) + 2 <=
7430+
1024) {
7431+
(void) snprintf(full_props,
7432+
sizeof (full_props), "%s,%s",
7433+
props, optarg + 1);
7434+
} else {
7435+
(void) fprintf(stderr, gettext(
7436+
"argument too long for '-o'"
7437+
" option.\n"));
7438+
usage(B_FALSE);
7439+
}
7440+
props = full_props;
7441+
} else {
7442+
props = optarg;
7443+
}
74277444
break;
74287445
case 'P':
74297446
cb.cb_name_flags |= VDEV_NAME_PATH;

man/man8/zfs-list.8

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ This is a shortcut for specifying
107107
.Sy usedds , Ns Sy usedrefreserv , Ns Sy usedchild
108108
.Fl t Sy filesystem , Ns Sy volume .
109109
.El
110+
.Pp
111+
The default list of properties can be extended by using
112+
.Sy +property
113+
instead of
114+
.Sy property .
115+
For example,
116+
.Sy zfs list -o +guid .
110117
.It Fl p
111118
Display numbers in parsable
112119
.Pq exact

man/man8/zpool-list.8

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ manual page for a list of valid properties.
8484
The default list is
8585
.Sy name , size , allocated , free , checkpoint, expandsize , fragmentation ,
8686
.Sy capacity , dedupratio , health , altroot .
87+
.Pp
88+
The default list of properties can be extended by using
89+
.Sy +property
90+
instead of
91+
.Sy property .
92+
For example,
93+
.Sy zpool list -o +guid .
8794
.It Fl L
8895
Display real paths for vdevs resolving all symbolic links.
8996
This can be used to look up the current block device name regardless of the
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/bin/ksh -p
2+
# SPDX-License-Identifier: CDDL-1.0
3+
#
4+
# CDDL HEADER START
5+
#
6+
# The contents of this file are subject to the terms of the
7+
# Common Development and Distribution License (the "License").
8+
# You may not use this file except in compliance with the License.
9+
#
10+
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11+
# or https://opensource.org/licenses/CDDL-1.0.
12+
# See the License for the specific language governing permissions
13+
# and limitations under the License.
14+
#
15+
# When distributing Covered Code, include this CDDL HEADER in each
16+
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17+
# If applicable, add the following below this CDDL HEADER, with the
18+
# fields enclosed by brackets "[]" replaced with your own identifying
19+
# information: Portions Copyright [yyyy] [name of copyright owner]
20+
#
21+
# CDDL HEADER END
22+
#
23+
24+
#
25+
# Copyright 2007 Sun Microsystems, Inc. All rights reserved.
26+
# Use is subject to license terms.
27+
#
28+
29+
#
30+
# Copyright (c) 2013, 2016 by Delphix. All rights reserved.
31+
#
32+
33+
. $STF_SUITE/include/libtest.shlib
34+
35+
#
36+
# DESCRIPTION:
37+
# Verify 'zfs list -o+' allows users to append a column to the defaults
38+
#
39+
# STRATEGY:
40+
# 1. Add a user comment to dataset
41+
# 2. Execute `zfs list -o +comment:`.
42+
# 3. Verify the first column of the defaults gets printed.
43+
# 4. Verify the user comment appears as the last column.
44+
# 5. Verify we see both one of the default entries and the user comment in JSON.
45+
46+
verify_runnable "both"
47+
48+
log_assert "Verify 'zfs list -o+<...>' appends columns to the defaults."
49+
50+
log_must zfs set comment:=helloworld $TESTPOOL
51+
52+
log_must eval zfs list $TESTPOOL -o +comment: | grep -E '^NAME.+COMMENT:$'
53+
log_must eval zfs list $TESTPOOL -o +comment: | grep -E "^$TESTPOOL.+helloworld$"
54+
val=$(zfs list -j -o +comment: $TESTPOOL | jq -r '.datasets.'$TESTPOOL'.properties."comment:".value')
55+
+log_must test $val == "helloworld"
56+
val=$(zfs list -j -o +comment: $TESTPOOL | jq -r ".datasets.$TESTPOOL.properties.mountpoint.value")
57+
+log_must test $val == "/$TESTPOOL"
58+
59+
log_pass "'zfs list -o+<...>' successfully added columns to the defaults."
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/bin/ksh -p
2+
# SPDX-License-Identifier: CDDL-1.0
3+
#
4+
# CDDL HEADER START
5+
#
6+
# The contents of this file are subject to the terms of the
7+
# Common Development and Distribution License (the "License").
8+
# You may not use this file except in compliance with the License.
9+
#
10+
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11+
# or https://opensource.org/licenses/CDDL-1.0.
12+
# See the License for the specific language governing permissions
13+
# and limitations under the License.
14+
#
15+
# When distributing Covered Code, include this CDDL HEADER in each
16+
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17+
# If applicable, add the following below this CDDL HEADER, with the
18+
# fields enclosed by brackets "[]" replaced with your own identifying
19+
# information: Portions Copyright [yyyy] [name of copyright owner]
20+
#
21+
# CDDL HEADER END
22+
#
23+
24+
#
25+
# Copyright 2008 Sun Microsystems, Inc. All rights reserved.
26+
# Use is subject to license terms.
27+
#
28+
29+
#
30+
# Copyright (c) 2013, 2016 by Delphix. All rights reserved.
31+
#
32+
33+
. $STF_SUITE/include/libtest.shlib
34+
35+
#
36+
# DESCRIPTION:
37+
# Verify that 'zpool list -o+' allows users to append a column to the defaults.
38+
#
39+
# STRATEGY:
40+
# 1. Add a user comment to the pool
41+
# 2. Execute `zfs list -o +comment`.
42+
# 3. Verify the first column of the defaults gets printed.
43+
# 4. Verify the user comment appears as the last column.
44+
# 5. Verify we see both one of the default entries and the user comment in JSON.
45+
46+
verify_runnable "both"
47+
log_onexit cleanup
48+
49+
if ! is_global_zone; then
50+
TESTPOOL=${TESTPOOL%%/*}
51+
fi
52+
53+
log_assert "Verify 'zpool list -o+<...>' appends columns to the defaults."
54+
55+
log_must zpool set comment="helloworld" $TESTPOOL
56+
57+
# Verify the first and last columns are correct
58+
log_must eval zpool list -o +comment | grep -Eq '^NAME.+COMMENT$'
59+
log_must eval zpool list -o +comment | grep -Eq "^$TESTPOOL.+helloworld$"
60+
61+
# Verify we see both a default value and our added value in the JSON
62+
val=$(zpool list -j -o +comment | jq -r ".pools.$TESTPOOL.properties.comment.value")
63+
+log_must test "$val" == "helloworld"
64+
val=$(zpool list -j -o +comment | jq -r ".pools.$TESTPOOL.properties.health.value")
65+
+log_must test "$val" == "ONLINE"
66+
67+
log_pass "'zpool list -o+<...>' successfully added columns to the defaults."

0 commit comments

Comments
 (0)