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)};