Skip to content

Commit

Permalink
Guess lxc or docker from /proc/1/mounts
Browse files Browse the repository at this point in the history
At the moment this is used to make the memory meter report sane values even
if the host has ZFS and that leaks through into a containerized environment

Fixes #863

Includes a clever check for magic PROC_PID_INIT_INO in /proc/self/ns/pid thanks to Pavel Snajdr (snajpa)
  • Loading branch information
Daniel Lange authored and BenBE committed Apr 2, 2022
1 parent 2b7504b commit 7039abe
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion linux/Platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ enum CapMode {
};
#endif

bool Running_containerized = false;

const ScreenDefaults Platform_defaultScreens[] = {
{
.name = "Main",
Expand Down Expand Up @@ -355,7 +357,7 @@ void Platform_setMemoryValues(Meter* this) {
this->values[3] = pl->cachedMem;
this->values[4] = pl->availableMem;

if (lpl->zfs.enabled != 0) {
if (lpl->zfs.enabled != 0 && !Running_containerized) {
this->values[0] -= lpl->zfs.size;
this->values[3] += lpl->zfs.size;
}
Expand Down Expand Up @@ -1017,6 +1019,31 @@ bool Platform_init(void) {
LibSensors_init();
#endif

char target[PATH_MAX];
ssize_t ret = readlink(PROCDIR "/self/ns/pid", target, sizeof(target) - 1);
if (ret > 0) {
target[ret] = '\0';

if (!String_eq("pid:[4026531836]", target)) { // magic constant PROC_PID_INIT_INO from include/linux/proc_ns.h#L46
Running_containerized = true;
return true; // early return
}
}

FILE* fd = fopen(PROCDIR "/1/mounts", "r");
if (fd) {
char lineBuffer[256];
while (fgets(lineBuffer, sizeof(lineBuffer), fd)) {
// detect lxc or overlayfs and guess that this means we are running containerized
if (String_startsWith(lineBuffer, "lxcfs ") || String_startsWith(lineBuffer, "overlay ")) {

This comment has been minimized.

Copy link
@ilyam8

ilyam8 May 8, 2022

Contributor

There is a problem with this detection method (at least) on a Proxmox host:

  • host

    $ grep ^lxcfs /proc/1/mounts
    lxcfs /var/lib/lxcfs fuse.lxcfs 
    rw,nosuid,nodev,relatime,user_id=0,group_id=0,allow_other 0 0
  • LXC container

    grep ^lxcfs /proc/1/mounts
    lxcfs /proc/cpuinfo fuse.lxcfs rw,nosuid,nodev,relatime,user_id=0,group_id=0,allow_other 0 0
    lxcfs /proc/diskstats fuse.lxcfs rw,nosuid,nodev,relatime,user_id=0,group_id=0,allow_other 0 0
    lxcfs /proc/loadavg fuse.lxcfs rw,nosuid,nodev,relatime,user_id=0,group_id=0,allow_other 0 0
    lxcfs /proc/meminfo fuse.lxcfs rw,nosuid,nodev,relatime,user_id=0,group_id=0,allow_other 0 0
    lxcfs /proc/stat fuse.lxcfs rw,nosuid,nodev,relatime,user_id=0,group_id=0,allow_other 0 0
    lxcfs /proc/swaps fuse.lxcfs rw,nosuid,nodev,relatime,user_id=0,group_id=0,allow_other 0 0
    lxcfs /proc/uptime fuse.lxcfs rw,nosuid,nodev,relatime,user_id=0,group_id=0,allow_other 0 0
    lxcfs /sys/devices/system/cpu/online fuse.lxcfs rw,nosuid,nodev,relatime,user_id=0,group_id=0,allow_other 0 0
fprintf(stderr, "%s\n", lineBuffer);
Running_containerized = true;
break;
}
}
fclose(fd);
} // if (fd)

return true;
}

Expand Down

0 comments on commit 7039abe

Please sign in to comment.