From 7fde3933fb15032ccdd8dbe443df1bb4668a5817 Mon Sep 17 00:00:00 2001 From: Rob Norris Date: Tue, 15 Apr 2025 10:10:48 +1000 Subject: [PATCH] vdev_to_nvlist_iter: ignore draid parameters when matching names (#17228) Various tools will display draid vdev names with parameters embedded in them, but would not accept them as valid vdev names when looking them up, making it difficult to build pipelines involving draid vdevs. This commit makes it so that if a full draid name is offered for match, it gets truncated at the first ':' character. Sponsored-by: Klara, Inc. Sponsored-by: Wasabi Technology, Inc. Signed-off-by: Rob Norris Reviewed-by: Alexander Motin Reviewed-by: Tony Hutter (cherry picked from commit 131df3bbf2b36fee05f8894ad4a867e2bf27688b) --- lib/libzfs/libzfs_pool.c | 12 +++ tests/runfiles/common.run | 3 +- tests/zfs-tests/tests/Makefile.am | 1 + .../cli_root/zpool_get/vdev_get_all.ksh | 92 +++++++++++++++++++ 4 files changed, 107 insertions(+), 1 deletion(-) create mode 100755 tests/zfs-tests/tests/functional/cli_root/zpool_get/vdev_get_all.ksh diff --git a/lib/libzfs/libzfs_pool.c b/lib/libzfs/libzfs_pool.c index 320bdb55c..17677d0f8 100644 --- a/lib/libzfs/libzfs_pool.c +++ b/lib/libzfs/libzfs_pool.c @@ -2961,6 +2961,18 @@ vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare, idx = p + 1; *p = '\0'; + /* + * draid names are presented like: draid2:4d:6c:0s + * We match them up to the first ':' so we can still + * do the parity check below, but the other params + * are ignored. + */ + if ((p = strchr(type, ':')) != NULL) { + if (strncmp(type, VDEV_TYPE_DRAID, + strlen(VDEV_TYPE_DRAID)) == 0) + *p = '\0'; + } + /* * If the types don't match then keep looking. */ diff --git a/tests/runfiles/common.run b/tests/runfiles/common.run index 0857e0430..81b4ead84 100644 --- a/tests/runfiles/common.run +++ b/tests/runfiles/common.run @@ -444,7 +444,8 @@ tags = ['functional', 'cli_root', 'zpool_export'] [tests/functional/cli_root/zpool_get] tests = ['zpool_get_001_pos', 'zpool_get_002_pos', 'zpool_get_003_pos', - 'zpool_get_004_neg', 'zpool_get_005_pos', 'vdev_get_001_pos'] + 'zpool_get_004_neg', 'zpool_get_005_pos', 'vdev_get_001_pos', + 'vdev_get_all'] tags = ['functional', 'cli_root', 'zpool_get'] [tests/functional/cli_root/zpool_history] diff --git a/tests/zfs-tests/tests/Makefile.am b/tests/zfs-tests/tests/Makefile.am index 15009f87f..f31cb4fd0 100644 --- a/tests/zfs-tests/tests/Makefile.am +++ b/tests/zfs-tests/tests/Makefile.am @@ -1101,6 +1101,7 @@ nobase_dist_datadir_zfs_tests_tests_SCRIPTS += \ functional/cli_root/zpool_get/cleanup.ksh \ functional/cli_root/zpool_get/setup.ksh \ functional/cli_root/zpool_get/vdev_get_001_pos.ksh \ + functional/cli_root/zpool_get/vdev_get_all.ksh \ functional/cli_root/zpool_get/zpool_get_001_pos.ksh \ functional/cli_root/zpool_get/zpool_get_002_pos.ksh \ functional/cli_root/zpool_get/zpool_get_003_pos.ksh \ diff --git a/tests/zfs-tests/tests/functional/cli_root/zpool_get/vdev_get_all.ksh b/tests/zfs-tests/tests/functional/cli_root/zpool_get/vdev_get_all.ksh new file mode 100755 index 000000000..0a63b6059 --- /dev/null +++ b/tests/zfs-tests/tests/functional/cli_root/zpool_get/vdev_get_all.ksh @@ -0,0 +1,92 @@ +#!/bin/ksh -p +# SPDX-License-Identifier: CDDL-1.0 +# +# CDDL HEADER START +# +# The contents of this file are subject to the terms of the +# Common Development and Distribution License (the "License"). +# You may not use this file except in compliance with the License. +# +# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE +# or https://opensource.org/licenses/CDDL-1.0. +# See the License for the specific language governing permissions +# and limitations under the License. +# +# When distributing Covered Code, include this CDDL HEADER in each +# file and include the License file at usr/src/OPENSOLARIS.LICENSE. +# If applicable, add the following below this CDDL HEADER, with the +# fields enclosed by brackets "[]" replaced with your own identifying +# information: Portions Copyright [yyyy] [name of copyright owner] +# +# CDDL HEADER END +# + +# +# Copyright (c) 2025, Klara, Inc. +# + +. $STF_SUITE/include/libtest.shlib + +# +# DESCRIPTION: +# +# zpool get name all-vdevs works as expected +# +# STRATEGY: +# +# 1. create various kinds of pools +# 2. get all vdev names +# 3. make sure we get all the names back and they look correct +# + +verify_runnable "global" + +function cleanup { + zpool destroy -f $TESTPOOL1 + [[ -e $TESTDIR ]] && rm -rf $TESTDIR/* +} +log_onexit cleanup + +log_assert "zpool get all-vdevs works as expected" + +# map of vdev spec -> summary form +# +# left side is normal args to zpool create; single number will be replaced +# with that number test file +# +# right side is a summary of the vdev tree, one char per vdev +# ! root +# 0-9 file number +# m mirror +# r raidz +# d draid +typeset -A specs=( + ["{0..9}"]="!0123456789" + ["mirror {0..9}"]="!m0123456789" + ["mirror 0 1 mirror 2 3 mirror 4 5 mirror 6 7"]="!m01m23m45m67" + ["raidz1 {0..9}"]="!r0123456789" + ["raidz1 {0..4} raidz1 {5..9}"]="!r01234r56789" + ["raidz2 {0..9}"]="!r0123456789" + ["raidz2 {0..4} raidz2 {5..9}"]="!r01234r56789" + ["raidz3 {0..9}"]="!r0123456789" + ["raidz3 {0..4} raidz3 {5..9}"]="!r01234r56789" + ["draid1 {0..9}"]="!d0123456789" + ["draid2 {0..9}"]="!d0123456789" + ["draid3 {0..9}"]="!d0123456789" +) + +for spec in "${!specs[@]}" ; do + log_must truncate -s 100M $TESTDIR/$TESTFILE1.{0..9} + log_must zpool create -f $TESTPOOL1 \ + $(echo $spec | sed -E "s#(^| )([0-9])#\1$TESTDIR/$TESTFILE1.\2#g") + typeset desc=$( zpool get -Ho name name $TESTPOOL1 all-vdevs | awk ' + /^\// { t = t substr($1,length($1)) ; next } + /^root/ { t = t "!" last ; next } + /^[a-z]/ { t = t substr($1,0,1) last ; next } + END { print t } + ') + log_must test "${specs[$spec]}" == "$desc" + cleanup +done + +log_pass