zpool: allow relative vdev paths

`zpool create` won't let you use relative paths to disks.  This is
annoying when you want to do:

	zpool create tank ./diskfile

But have to do..

	zpool create tank `pwd`/diskfile

This fixes it.

Reviewed-by: Tino Reichardt <milky-zfs@mcmilk.de>
Reviewed-by: Alexander Motin <mav@FreeBSD.org>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Tony Hutter <hutter2@llnl.gov>
Closes #17042
This commit is contained in:
Tony Hutter 2025-02-25 11:40:20 -08:00 committed by GitHub
parent ab3db6d15d
commit ece35e0e66
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -57,6 +57,7 @@ int
zfs_resolve_shortname(const char *name, char *path, size_t len)
{
const char *env = getenv("ZPOOL_IMPORT_PATH");
char resolved_path[PATH_MAX];
if (env) {
for (;;) {
@ -85,6 +86,20 @@ zfs_resolve_shortname(const char *name, char *path, size_t len)
}
}
/*
* The user can pass a relative path like ./file1 for the vdev. The path
* must contain a directory prefix like './file1' or '../file1'. Simply
* passing 'file1' is not allowed, as it may match a block device name.
*/
if ((strncmp(name, "./", 2) == 0 || strncmp(name, "../", 3) == 0) &&
realpath(name, resolved_path) != NULL) {
if (access(resolved_path, F_OK) == 0) {
if (strlen(resolved_path) + 1 <= len) {
if (strlcpy(path, resolved_path, len) < len)
return (0); /* success */
}
}
}
return (errno = ENOENT);
}