From 14e376b0370f909388deee5ac33c08f1a0a58ef1 Mon Sep 17 00:00:00 2001 From: Juan Pedro Bretti Mandarano Date: Fri, 27 Jan 2023 18:43:54 +0100 Subject: [PATCH 1/4] Add an id for Docker containers Usual Linux images for Docker containers, does not have the originally listed files. Following the conversation at https://github.com/denisbrodbeck/machineid/issues/10, I am including the following: Because `/proc/self/cgroup` seams to not be working on same Docker versions, I am also including `/proc/self/mountinfo`. Tested on `Python 3.11` running on `Debian GNU/Linux 11 (bullseye)` inside a Docker. --- machineid/__init__.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/machineid/__init__.py b/machineid/__init__.py index bf6d7be..cd119cc 100644 --- a/machineid/__init__.py +++ b/machineid/__init__.py @@ -74,7 +74,11 @@ def id() -> str: id = __read__('/var/lib/dbus/machine-id') if not id: id = __read__('/etc/machine-id') - + if not id: + id = __exec__('head -1 /proc/self/cgroup|cut -d/ -f3') + if not id: + id = __exec__("grep 'systemd' /proc/self/mountinfo|cut -d/ -f3") + if platform.startswith('openbsd') or platform.startswith('freebsd'): id = __read__('/etc/hostid') if not id: From 7b12ce30376af8a190b7d31407c912821c009f94 Mon Sep 17 00:00:00 2001 From: Juan Pedro Bretti Mandarano Date: Mon, 30 Jan 2023 23:18:44 +0100 Subject: [PATCH 2/4] Included "if 'docker' in mountinfo" for "linux" Following your review, I made some updates on the patch. --- machineid/__init__.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/machineid/__init__.py b/machineid/__init__.py index cd119cc..cb2b883 100644 --- a/machineid/__init__.py +++ b/machineid/__init__.py @@ -75,10 +75,14 @@ def id() -> str: if not id: id = __read__('/etc/machine-id') if not id: - id = __exec__('head -1 /proc/self/cgroup|cut -d/ -f3') + cgroup = __read__('/proc/self/cgroup') + if 'docker' in cgroup: + id = __exec__('head -1 /proc/self/cgroup | cut -d/ -f3') if not id: - id = __exec__("grep 'systemd' /proc/self/mountinfo|cut -d/ -f3") - + mountinfo = __read__('/proc/self/mountinfo') + if 'docker' in mountinfo: + id = __exec__("grep 'systemd' /proc/self/mountinfo | cut -d/ -f3") + if platform.startswith('openbsd') or platform.startswith('freebsd'): id = __read__('/etc/hostid') if not id: From 53a5807652f5a9c6a6c9e1732d772ae472317395 Mon Sep 17 00:00:00 2001 From: Juan Pedro Bretti Mandarano Date: Tue, 31 Jan 2023 23:02:05 +0100 Subject: [PATCH 3/4] Update __init__.py * Fixed the indentation to 2. * Checked on a Docker container the `if 'docker' in cgroup:` works when the `docker` is not found. Same test with `mountinfo`. * When the full code does not find an ID raises the following: `Python 3.11.0 (main, Nov 15 2022, 19:58:01) [GCC 10.2.1 20210110] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import machineid >>> print(machineid.id()) Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python3.11/site-packages/machineid/__init__.py", line 90, in id raise Exception('failed to obtain id on platform {}'.format(platform)) Exception: failed to obtain id on platform linux` --- machineid/__init__.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/machineid/__init__.py b/machineid/__init__.py index cb2b883..a4085e5 100644 --- a/machineid/__init__.py +++ b/machineid/__init__.py @@ -75,13 +75,13 @@ def id() -> str: if not id: id = __read__('/etc/machine-id') if not id: - cgroup = __read__('/proc/self/cgroup') - if 'docker' in cgroup: - id = __exec__('head -1 /proc/self/cgroup | cut -d/ -f3') + cgroup = __read__('/proc/self/cgroup') + if 'docker' in cgroup: + id = __exec__('head -1 /proc/self/cgroup | cut -d/ -f3') if not id: - mountinfo = __read__('/proc/self/mountinfo') - if 'docker' in mountinfo: - id = __exec__("grep 'systemd' /proc/self/mountinfo | cut -d/ -f3") + mountinfo = __read__('/proc/self/mountinfo') + if 'docker' in mountinfo: + id = __exec__("grep 'systemd' /proc/self/mountinfo | cut -d/ -f3") if platform.startswith('openbsd') or platform.startswith('freebsd'): id = __read__('/etc/hostid') From 5ffae30f8aad7c0e1e83259400571744abb4286c Mon Sep 17 00:00:00 2001 From: Juan Pedro Bretti Mandarano Date: Wed, 1 Feb 2023 09:58:33 +0100 Subject: [PATCH 4/4] Update __init__.py Check if `cgroup` and `mountinfo` are not None, before checking if `docker` is inside the file. To not rise a possible error when is trying to check `if 'docker' in None` (when the file does not exist). --- machineid/__init__.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/machineid/__init__.py b/machineid/__init__.py index a4085e5..3928569 100644 --- a/machineid/__init__.py +++ b/machineid/__init__.py @@ -76,12 +76,14 @@ def id() -> str: id = __read__('/etc/machine-id') if not id: cgroup = __read__('/proc/self/cgroup') - if 'docker' in cgroup: - id = __exec__('head -1 /proc/self/cgroup | cut -d/ -f3') + if cgroup: + if 'docker' in cgroup: + id = __exec__('head -1 /proc/self/cgroup | cut -d/ -f3') if not id: mountinfo = __read__('/proc/self/mountinfo') - if 'docker' in mountinfo: - id = __exec__("grep 'systemd' /proc/self/mountinfo | cut -d/ -f3") + if mountinfo: + if 'docker' in mountinfo: + id = __exec__("grep 'systemd' /proc/self/mountinfo | cut -d/ -f3") if platform.startswith('openbsd') or platform.startswith('freebsd'): id = __read__('/etc/hostid')