Skip to content

Commit

Permalink
zdb: detect cachefile automatically otherwise force import
Browse files Browse the repository at this point in the history
If a pool is created with the cache file located in a non-default 
path /etc/default/zpool.cache, removed, or the cachefile property 
is set to none, zdb fails to show the pool unless we specify the 
cache file or use the -e option. This PR automates this process.

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Alexander Motin <mav@FreeBSD.org>
Reviewed-by: Akash B <akash-b@hpe.com>
Signed-off-by: Ameer Hamza <ahamza@ixsystems.com>
Closes #16071
  • Loading branch information
ixhamza authored Jun 3, 2024
1 parent a72751a commit 23a489a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
66 changes: 61 additions & 5 deletions cmd/zdb/zdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@

#include <libnvpair.h>
#include <libzutil.h>
#include <libzfs_core.h>

#include <libzdb.h>

Expand Down Expand Up @@ -8924,6 +8925,9 @@ main(int argc, char **argv)
boolean_t target_is_spa = B_TRUE, dataset_lookup = B_FALSE;
nvlist_t *cfg = NULL;
struct sigaction action;
boolean_t force_import = B_FALSE;
boolean_t config_path_console = B_FALSE;
char pbuf[MAXPATHLEN];

dprintf_setup(&argc, argv);

Expand Down Expand Up @@ -9094,6 +9098,7 @@ main(int argc, char **argv)
}
break;
case 'U':
config_path_console = B_TRUE;
spa_config_path = optarg;
if (spa_config_path[0] != '/') {
(void) fprintf(stderr,
Expand Down Expand Up @@ -9153,9 +9158,6 @@ main(int argc, char **argv)
*/
spa_mode_readable_spacemaps = B_TRUE;

kernel_init(SPA_MODE_READ);
kernel_init_done = B_TRUE;

if (dump_all)
verbose = MAX(verbose, 1);

Expand All @@ -9174,6 +9176,53 @@ main(int argc, char **argv)
if (argc < 2 && dump_opt['R'])
usage();

target = argv[0];

/*
* Automate cachefile
*/
if (!spa_config_path_env && !config_path_console && target &&
libzfs_core_init() == 0) {
char *pname = strdup(target);
const char *value;
nvlist_t *pnvl;
nvlist_t *vnvl;

if (strpbrk(pname, "/@") != NULL)
*strpbrk(pname, "/@") = '\0';

if (pname && lzc_get_props(pname, &pnvl) == 0) {
if (nvlist_lookup_nvlist(pnvl, "cachefile",
&vnvl) == 0) {
value = fnvlist_lookup_string(vnvl,
ZPROP_VALUE);
} else {
value = "-";
}
strlcpy(pbuf, value, sizeof (pbuf));
if (pbuf[0] != '\0') {
if (pbuf[0] == '/') {
if (access(pbuf, F_OK) == 0)
spa_config_path = pbuf;
else
force_import = B_TRUE;
} else if ((strcmp(pbuf, "-") == 0 &&
access(ZPOOL_CACHE, F_OK) != 0) ||
strcmp(pbuf, "none") == 0) {
force_import = B_TRUE;
}
}
nvlist_free(vnvl);
}

free(pname);
nvlist_free(pnvl);
libzfs_core_fini();
}

kernel_init(SPA_MODE_READ);
kernel_init_done = B_TRUE;

if (dump_opt['E']) {
if (argc != 1)
usage();
Expand Down Expand Up @@ -9210,7 +9259,6 @@ main(int argc, char **argv)
fatal("internal error: %s", strerror(ENOMEM));

error = 0;
target = argv[0];

if (strpbrk(target, "/@") != NULL) {
size_t targetlen;
Expand Down Expand Up @@ -9256,9 +9304,17 @@ main(int argc, char **argv)
target_pool = target;
}

if (dump_opt['e']) {
if (dump_opt['e'] || force_import) {
importargs_t args = { 0 };

/*
* If path is not provided, search in /dev
*/
if (searchdirs == NULL) {
searchdirs = umem_alloc(sizeof (char *), UMEM_NOFAIL);
searchdirs[nsearch++] = (char *)ZFS_DEVDIR;
}

args.paths = nsearch;
args.path = searchdirs;
args.can_be_active = B_TRUE;
Expand Down
1 change: 1 addition & 0 deletions include/libzfs_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ _LIBZFS_CORE_H int lzc_snaprange_space(const char *, const char *, uint64_t *);
_LIBZFS_CORE_H int lzc_hold(nvlist_t *, int, nvlist_t **);
_LIBZFS_CORE_H int lzc_release(nvlist_t *, nvlist_t **);
_LIBZFS_CORE_H int lzc_get_holds(const char *, nvlist_t **);
_LIBZFS_CORE_H int lzc_get_props(const char *, nvlist_t **);

enum lzc_send_flags {
LZC_SEND_FLAG_EMBED_DATA = 1 << 0,
Expand Down
6 changes: 6 additions & 0 deletions lib/libzfs_core/libzfs_core.abi
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
<elf-symbol name='lzc_get_bookmarks' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
<elf-symbol name='lzc_get_bootenv' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
<elf-symbol name='lzc_get_holds' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
<elf-symbol name='lzc_get_props' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
<elf-symbol name='lzc_get_vdev_prop' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
<elf-symbol name='lzc_hold' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
<elf-symbol name='lzc_initialize' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
Expand Down Expand Up @@ -2694,6 +2695,11 @@
<parameter type-id='857bb57e' name='holdsp'/>
<return type-id='95e97e5e'/>
</function-decl>
<function-decl name='lzc_get_props' mangled-name='lzc_get_props' visibility='default' binding='global' size-in-bits='64' elf-symbol-id='lzc_get_props'>
<parameter type-id='80f4b756' name='poolname'/>
<parameter type-id='857bb57e' name='props'/>
<return type-id='95e97e5e'/>
</function-decl>
<function-decl name='lzc_send_wrapper' mangled-name='lzc_send_wrapper' visibility='default' binding='global' size-in-bits='64' elf-symbol-id='lzc_send_wrapper'>
<parameter type-id='2e711a2a' name='func'/>
<parameter type-id='95e97e5e' name='orig_fd'/>
Expand Down
6 changes: 6 additions & 0 deletions lib/libzfs_core/libzfs_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,12 @@ lzc_get_holds(const char *snapname, nvlist_t **holdsp)
return (lzc_ioctl(ZFS_IOC_GET_HOLDS, snapname, NULL, holdsp));
}

int
lzc_get_props(const char *poolname, nvlist_t **props)
{
return (lzc_ioctl(ZFS_IOC_POOL_GET_PROPS, poolname, NULL, props));
}

static unsigned int
max_pipe_buffer(int infd)
{
Expand Down

0 comments on commit 23a489a

Please sign in to comment.