diff --git a/cmd/zdb/zdb.c b/cmd/zdb/zdb.c index 7f2cab382500..063358a0432c 100644 --- a/cmd/zdb/zdb.c +++ b/cmd/zdb/zdb.c @@ -4273,7 +4273,7 @@ main(int argc, char **argv) int error = 0; char **searchdirs = NULL; int nsearch = 0; - char *target; + char *target, *target_pool; nvlist_t *policy = NULL; uint64_t max_txg = UINT64_MAX; int flags = ZFS_IMPORT_MISSING_LOG; @@ -4476,6 +4476,20 @@ main(int argc, char **argv) error = 0; target = argv[0]; + if (strpbrk(target, "/@") != NULL) { + size_t targetlen; + + target_pool = strdup(target); + *strpbrk(target_pool, "/@") = '\0'; + + target_is_spa = B_FALSE; + targetlen = strlen(target); + if (targetlen && target[targetlen - 1] == '/') + target[targetlen - 1] = '\0'; + } else { + target_pool = target; + } + if (dump_opt['e']) { importargs_t args = { 0 }; nvlist_t *cfg = NULL; @@ -4484,8 +4498,10 @@ main(int argc, char **argv) args.path = searchdirs; args.can_be_active = B_TRUE; - error = zpool_tryimport(g_zfs, target, &cfg, &args); + error = zpool_tryimport(g_zfs, target_pool, &cfg, &args); + if (error == 0) { + if (nvlist_add_nvlist(cfg, ZPOOL_REWIND_POLICY, policy) != 0) { fatal("can't open '%s': %s", @@ -4500,19 +4516,13 @@ main(int argc, char **argv) (void) printf("\nConfiguration for import:\n"); dump_nvlist(cfg, 8); } - error = spa_import(target, cfg, NULL, + error = spa_import(target_pool, cfg, NULL, flags | ZFS_IMPORT_SKIP_MMP); } } - if (strpbrk(target, "/@") != NULL) { - size_t targetlen; - - target_is_spa = B_FALSE; - targetlen = strlen(target); - if (targetlen && target[targetlen - 1] == '/') - target[targetlen - 1] = '\0'; - } + if (target_pool != target) + free(target_pool); if (error == 0) { if (target_is_spa || dump_opt['R']) { diff --git a/tests/runfiles/linux.run b/tests/runfiles/linux.run index 16c10d41a7ff..6c60e0ba8631 100644 --- a/tests/runfiles/linux.run +++ b/tests/runfiles/linux.run @@ -77,7 +77,7 @@ tags = ['functional', 'clean_mirror'] [tests/functional/cli_root/zdb] tests = ['zdb_001_neg', 'zdb_002_pos', 'zdb_003_pos', 'zdb_004_pos', - 'zdb_005_pos'] + 'zdb_005_pos', 'zdb_006_pos'] pre = post = tags = ['functional', 'cli_root', 'zdb'] diff --git a/tests/zfs-tests/tests/functional/cli_root/zdb/Makefile.am b/tests/zfs-tests/tests/functional/cli_root/zdb/Makefile.am index 51170fbc894d..d37bcf607f46 100644 --- a/tests/zfs-tests/tests/functional/cli_root/zdb/Makefile.am +++ b/tests/zfs-tests/tests/functional/cli_root/zdb/Makefile.am @@ -4,4 +4,5 @@ dist_pkgdata_SCRIPTS = \ zdb_002_pos.ksh \ zdb_003_pos.ksh \ zdb_004_pos.ksh \ - zdb_005_pos.ksh + zdb_005_pos.ksh \ + zdb_006_pos.ksh diff --git a/tests/zfs-tests/tests/functional/cli_root/zdb/zdb_005_pos.ksh b/tests/zfs-tests/tests/functional/cli_root/zdb/zdb_005_pos.ksh index c98caed426f6..cf5de3cad02e 100755 --- a/tests/zfs-tests/tests/functional/cli_root/zdb/zdb_005_pos.ksh +++ b/tests/zfs-tests/tests/functional/cli_root/zdb/zdb_005_pos.ksh @@ -36,6 +36,9 @@ log_onexit cleanup function cleanup { datasetexists $TESTPOOL && destroy_pool $TESTPOOL + for DISK in $DISKS; do + zpool labelclear -f $DEV_RDSKDIR/$DISK + done rm -f $TEMPFILE } diff --git a/tests/zfs-tests/tests/functional/cli_root/zdb/zdb_006_pos.ksh b/tests/zfs-tests/tests/functional/cli_root/zdb/zdb_006_pos.ksh new file mode 100755 index 000000000000..9c1d4148fd1b --- /dev/null +++ b/tests/zfs-tests/tests/functional/cli_root/zdb/zdb_006_pos.ksh @@ -0,0 +1,61 @@ +#!/bin/ksh + +# +# This file and its contents are supplied under the terms of the +# Common Development and Distribution License ("CDDL"), version 1.0. +# You may only use this file in accordance with the terms of version +# 1.0 of the CDDL. +# +# A full copy of the text of the CDDL should have accompanied this +# source. A copy of the CDDL is also available via the Internet at +# http://www.illumos.org/license/CDDL. +# + +# +# Copyright (c) 2018 by Nutanix. All rights reserved. +# + +. $STF_SUITE/include/libtest.shlib + +# +# Description: +# zdb -d will work on imported/exported pool with pool/dataset argument +# +# Strategy: +# 1. Create a pool +# 2. Run zdb -d with pool and dataset arguments. +# 3. Export the pool +# 4. Run zdb -ed with pool and dataset arguments. +# + +function cleanup +{ + datasetexists $TESTPOOL && destroy_pool $TESTPOOL + for DISK in $DISKS; do + zpool labelclear -f $DEV_RDSKDIR/$DISK + done +} + +log_assert "Verify zdb -d works on imported/exported pool with pool/dataset argument" +log_onexit cleanup + +verify_runnable "global" +verify_disk_count "$DISKS" 2 + +default_mirror_setup_noexit $DISKS + +log_must zdb -d $TESTPOOL +log_must zdb -d $TESTPOOL/ +log_must zdb -d $TESTPOOL/$TESTFS + +log_must zpool export $TESTPOOL + +log_must zdb -ed -p $DEV_RDSKDIR $TESTPOOL +log_must zdb -ed -p $DEV_RDSKDIR $TESTPOOL/ +log_must zdb -ed -p $DEV_RDSKDIR $TESTPOOL/$TESTFS + +log_must zpool import $TESTPOOL + +cleanup + +log_pass "zdb -d works on imported/exported pool with pool/dataset argument"