[PATCH 1/3] rteval: Add container detection module
John Kacur <[email protected]> Tue, 16 Jun 2026 12:30:13 -0400
| Newsgroups | org.kernel.vger.linux-rt-users |
|---|---|
| Message-ID | <[email protected]> |
Add a new containercheck module to detect if rteval is running inside a container. The module checks multiple indicators: - Presence of /.dockerenv file (Docker) - /proc/1/cgroup patterns (docker, lxc, kubepods, libpod) - Container-related environment variables - Kubernetes environment indicators - systemd-detect-virt utility if available The module includes a unit_test() function for standalone testing, following the pattern used by other sysinfo modules. Assisted-by: Claude:claude-sonnet-4-5 Signed-off-by: John Kacur <[email protected]> --- rteval/sysinfo/containercheck.py | 68 ++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 rteval/sysinfo/containercheck.py diff --git a/rteval/sysinfo/containercheck.py b/rteval/sysinfo/containercheck.py new file mode 100644 index 000000000000..c558ee05dd8c --- /dev/null +++ b/rteval/sysinfo/containercheck.py @@ -0,0 +1,68 @@ +# -*- coding: utf-8 -*- +# SPDX-License-Identifier: GPL-2.0-or-later +# +# Copyright 2026 - John Kacur <[email protected]> +# +"""Module for detecting if rteval is running in a container""" + +import os +import re +import subprocess + + +def is_container(): + """ + Detect if running in a container (comprehensive check). + + Returns: + bool: True if running in a container, False otherwise + """ + + # Check 1: .dockerenv file + if os.path.exists('/.dockerenv'): + return True + + # Check 2: /proc/1/cgroup + try: + with open('/proc/1/cgroup', 'r') as f: + if re.search(r'docker|lxc|kubepods|libpod', f.read()): + return True + except (FileNotFoundError, PermissionError): + pass + + # Check 3: Environment variables + if os.environ.get('container'): + return True + + # Check 4: Kubernetes + if os.environ.get('KUBERNETES_SERVICE_HOST'): + return True + + # Check 5: systemd-detect-virt (if available) + try: + result = subprocess.run( + ['systemd-detect-virt', '-c'], + capture_output=True, + text=True, + timeout=1 + ) + if result.returncode == 0: + return True + except (FileNotFoundError, subprocess.TimeoutExpired): + pass + + return False + + +def unit_test(rootdir): + """Simple test of container detection""" + result = is_container() + print(f"Container detection result: {result}") + if result: + print("Running in a container") + else: + print("Not running in a container") + + +if __name__ == '__main__': + unit_test(None) -- 2.54.0