Skip to content

Commit 01b4db2

Browse files
committed
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. Signed-off-by: Adi Gollamudi <[email protected]> Closes issue #15165
1 parent b4f073b commit 01b4db2

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

lib/libzfs/os/linux/libzfs_util_os.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,38 @@ libzfs_error_init(int error)
7878
}
7979
}
8080

81+
static int
82+
in_container(void)
83+
{
84+
char buffer[4096];
85+
ssize_t count;
86+
int fd;
87+
88+
if (access("/run/systemd/container", R_OK) == 0)
89+
return (1);
90+
91+
fd = open("/proc/1/cgroup", O_RDONLY);
92+
if (fd == -1)
93+
return (0);
94+
95+
count = read(fd, buffer, sizeof (buffer) - 1);
96+
close(fd);
97+
98+
if (count <= 0)
99+
return (0);
100+
101+
buffer[count] = '\0';
102+
103+
if (strstr(buffer, "docker") ||
104+
strstr(buffer, "containerd") ||
105+
strstr(buffer, "kubepods") ||
106+
strstr(buffer, "lxc")) {
107+
return (1);
108+
}
109+
110+
return (0);
111+
}
112+
81113
/*
82114
* zfs(4) is loaded by udev if there's a fstype=zfs device present,
83115
* but if there isn't, load them automatically;
@@ -104,6 +136,11 @@ libzfs_load_module(void)
104136

105137
const char *timeout_str = getenv("ZFS_MODULE_TIMEOUT");
106138
int seconds = 10;
139+
140+
/* Set timeout to zero if inside of a container */
141+
if (in_container())
142+
seconds = 0;
143+
107144
if (timeout_str)
108145
seconds = MIN(strtol(timeout_str, NULL, 0), 600);
109146
struct itimerspec timeout = {.it_value.tv_sec = MAX(seconds, 0)};

0 commit comments

Comments
 (0)