From 7ebb5e9b3fdf1deb79bdbcf6748d04a2f1c89631 Mon Sep 17 00:00:00 2001 From: Adi-Goll <68113680+Adi-Goll@users.noreply.github.com> Date: Tue, 11 Nov 2025 15:01:37 -0800 Subject: [PATCH] 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 Signed-off-by: Adi Gollamudi Closes #15165 Closes #17922 --- lib/libzfs/os/linux/libzfs_util_os.c | 37 ++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/lib/libzfs/os/linux/libzfs_util_os.c b/lib/libzfs/os/linux/libzfs_util_os.c index 55dfdf372..651d407b1 100644 --- a/lib/libzfs/os/linux/libzfs_util_os.c +++ b/lib/libzfs/os/linux/libzfs_util_os.c @@ -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)};