From 67ffee4e9e9c8755c83a7719e5b6ae0d28a308af Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Thu, 26 Nov 2026 18:53:07 +0100 Subject: [PATCH] fix(services-restart): an LXC container's processes are left out (NetOrk/netork#2750) On a Proxmox node the /proc scan sees every container's processes: an LXC container updates itself, and its own systemd's units (lxc//ns/... .service) would read as the host's services needing a restart. They are left out like Docker's and Podman's: /lxc// (Proxmox VE, cgroup v1) and /lxc.payload./ (LXC 4 and later). The monitor, lxc.monitor/, runs the host's lxc-start and still counts. --- CHANGELOG.md | 4 +++- README.md | 3 ++- napalm_device_types/services_restart.py | 15 ++++++++----- tests/test_services_restart.py | 29 +++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c5bf83..3af2fc9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,7 +18,9 @@ recorded here; their history is the git log. `/proc//maps` for executable mappings of deleted files under `/usr`, `/lib*`, `/bin`, `/sbin` or `/opt`, each process mapped to its service through `/proc//cgroup` as the listening sockets do. A user's own systemd counts as `user@`, a container's process is - left out, and a process in no service is listed under `processes`. A refused root gives + left out (Docker, Podman, and LXC: `/lxc//`, Proxmox VE's `/lxc//ns/`, or + `/lxc.payload./`; a container updates itself, and its own systemd's units are no + services of the host), and a process in no service is listed under `processes`. A refused root gives no report and raises, never a clean reading. A driver supplies `_run_services_restart_command(command)`. - `parse_services_restart`, `SERVICES_RESTART_COMMAND`, `DELETED_CODE_PATTERN`, and the diff --git a/README.md b/README.md index 4a3c87c..8fc1e3f 100644 --- a/README.md +++ b/README.md @@ -214,7 +214,8 @@ finished until they restart. One fixed, read-only command, as root: `needs-resta where dnf-utils has it, else `needrestart -b -r l` (list only, whatever `needrestart.conf` says), else the processes whose `/proc//maps` maps deleted code (`DELETED_CODE_PATTERN`), each named by the service its cgroup gives, as for the listening -sockets. A driver supplies `_run_services_restart_command(command)`; the command arrives as +sockets. A container's processes are left out, an LXC container's as well (a Proxmox node's +containers update themselves). A driver supplies `_run_services_restart_command(command)`; the command arrives as one `sh -c` argument and reads no stdin. Without root there is no reading: one that saw only the login user's processes would read as clean. diff --git a/napalm_device_types/services_restart.py b/napalm_device_types/services_restart.py index af630db..495d9a4 100644 --- a/napalm_device_types/services_restart.py +++ b/napalm_device_types/services_restart.py @@ -21,10 +21,11 @@ here once, and a driver only carries the command across. **Units from cgroups.** For the ``/proc`` scan, ``/proc//cgroup`` names the systemd service a process runs in, as for the listening sockets: a process of a -user's own systemd is that user's manager (``user@1000``), one of a container is -left out (the container runs its image's libraries, which no host update -replaces), and one in no service -- a login session, systemd itself -- is listed -as a process. The host's tools name services only, so ``processes`` stays empty +user's own systemd is that user's manager (``user@1000``), one of a container -- +Docker, Podman, or LXC as on a Proxmox node -- is left out (the container runs its +own libraries, which no host update replaces, and its own systemd's units are none +of the host's services), and one in no service -- a login session, systemd itself -- +is listed as a process. The host's tools name services only, so ``processes`` stays empty for them. **Root, or nothing.** Only root sees every process's memory. The script goes to @@ -84,6 +85,10 @@ _SERVICE_LINE = re.compile(r"^([\w:.@\\-]+)\.service$") _NEEDRESTART_SVC = re.compile(r"^NEEDRESTART-SVC:\s*(.+?)\s*$") #: A process of a user's own systemd: that user's manager is what restarts it. _USER_MANAGER = re.compile(r"/(user@\d+)\.service(?=/|$)") +#: A process inside an LXC container: ``/lxc//`` (Proxmox VE, which nests the +#: container's own cgroups under ``ns``; cgroup v1) or ``/lxc.payload./`` (LXC 4 +#: and later). Not ``/lxc.monitor/``: ``lxc-start`` runs the host's binary. +_LXC = re.compile(r"^/lxc(?:/|\.payload\.)[^/]+(?:/|$)") _SERVICE_SUFFIX = ".service" @@ -130,7 +135,7 @@ def _maps(sections: dict[str, list[str]]) -> tuple[list[str], list[RestartProces if not pid.isdigit(): continue path = paths.get(int(pid)) - if _container(path): + if _container(path) or _LXC.match(path or ""): continue unit = _process_unit(path) if unit: diff --git a/tests/test_services_restart.py b/tests/test_services_restart.py index 629795d..958754b 100644 --- a/tests/test_services_restart.py +++ b/tests/test_services_restart.py @@ -115,6 +115,35 @@ class TestTheMapsScan: assert reading == {"source": "maps", "units": [], "processes": []} + @pytest.mark.parametrize( + "cgroup", + [ + # Proxmox VE: the container's own systemd, under lxc//ns. + "0::/lxc/101/ns/system.slice/nginx.service", + "0::/lxc/101/ns/init.scope", + # LXC 4 and later by default. + "0::/lxc.payload.web/system.slice/nginx.service", + # cgroup v1, systemd's hierarchy. + "12:name=systemd:/lxc/web/system.slice/nginx.service", + ], + ) + def test_an_lxc_container_is_left_out(self, cgroup): + """A Proxmox node's containers update themselves: the host sees their processes' + mappings, and their own systemd's units, which are no services of the host's.""" + cgroups = f"2100 {cgroup}\n" + + reading = parse_services_restart(_wire(("maps", "2100 nginx\n"), ("cgroups", cgroups))) + + assert reading == {"source": "maps", "units": [], "processes": []} + + def test_an_lxc_monitor_is_the_hosts(self): + """``lxc-start`` runs the host's own binary, outside the container.""" + reading = parse_services_restart( + _wire(("maps", "2090 lxc-start\n"), ("cgroups", "2090 0::/lxc.monitor/101\n")) + ) + + assert reading["processes"] == [{"pid": 2090, "name": "lxc-start"}] + def test_a_process_gone_before_its_cgroup_was_read_has_no_unit(self): reading = parse_services_restart(_wire(("maps", "4242 cron\n"), ("cgroups", "")))