Reduce timeout to zero when running inside a container

Detect container environments and set timeout to zero unless
ZFS_MODULE_TIMEOUT is already set. This avoids an unnecessary ten
second delay after running zfs/zpool commands in a container where
/dev/zfs is unavailable.

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Adi Gollamudi <adigollamudi@gmail.com>
Closes #15165
Closes #17922
This commit is contained in:
Adi-Goll 2025-11-11 15:01:37 -08:00 committed by Brian Behlendorf
parent 1e8c96d7d5
commit 7ebb5e9b3f

View File

@ -78,6 +78,38 @@ libzfs_error_init(int error)
}
}
static int
in_container(void)
{
char buffer[4096];
ssize_t count;
int fd;
if (access("/run/systemd/container", R_OK) == 0)
return (1);
fd = open("/proc/1/cgroup", O_RDONLY);
if (fd == -1)
return (0);
count = read(fd, buffer, sizeof (buffer) - 1);
close(fd);
if (count <= 0)
return (0);
buffer[count] = '\0';
if (strstr(buffer, "docker") ||
strstr(buffer, "containerd") ||
strstr(buffer, "kubepods") ||
strstr(buffer, "lxc")) {
return (1);
}
return (0);
}
/*
* zfs(4) is loaded by udev if there's a fstype=zfs device present,
* but if there isn't, load them automatically;
@ -104,6 +136,11 @@ libzfs_load_module(void)
const char *timeout_str = getenv("ZFS_MODULE_TIMEOUT");
int seconds = 10;
/* Set timeout to zero if inside of a container */
if (in_container())
seconds = 0;
if (timeout_str)
seconds = MIN(strtol(timeout_str, NULL, 0), 600);
struct itimerspec timeout = {.it_value.tv_sec = MAX(seconds, 0)};