From 785dc6478776bede93b8f56f94fe363a13f520e6 Mon Sep 17 00:00:00 2001 From: Pascal Vizeli Date: Mon, 22 Feb 2021 11:39:18 +0100 Subject: [PATCH] Imrove the LXC detection (#2599) --- supervisor/docker/__init__.py | 6 ------ supervisor/resolution/evaluations/lxc.py | 7 ++++++- tests/resolution/evaluation/test_evaluate_lxc.py | 11 +++++++---- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/supervisor/docker/__init__.py b/supervisor/docker/__init__.py index d2249f7a8..9b67b0017 100644 --- a/supervisor/docker/__init__.py +++ b/supervisor/docker/__init__.py @@ -2,7 +2,6 @@ from contextlib import suppress from ipaddress import IPv4Address import logging -from pathlib import Path from typing import Any, Dict, Optional import attr @@ -56,11 +55,6 @@ class DockerInfo: """Return true, if docker version is supported.""" return self.version >= MIN_SUPPORTED_DOCKER - @property - def inside_lxc(self) -> bool: - """Return True if the docker run inside lxc.""" - return Path("/dev/lxd/sock").exists() - class DockerConfig(JsonConfig): """Home Assistant core object for Docker configuration.""" diff --git a/supervisor/resolution/evaluations/lxc.py b/supervisor/resolution/evaluations/lxc.py index ae6a40466..59904fc42 100644 --- a/supervisor/resolution/evaluations/lxc.py +++ b/supervisor/resolution/evaluations/lxc.py @@ -1,4 +1,6 @@ """Evaluation class for lxc.""" +from contextlib import suppress +from pathlib import Path from typing import List from ...const import CoreState @@ -26,4 +28,7 @@ class EvaluateLxc(EvaluateBase): async def evaluate(self): """Run evaluation.""" - return self.sys_docker.info.inside_lxc + with suppress(OSError): + if "container=lxc" in Path("/proc/1/environ").read_text(): + return True + return Path("/dev/lxd/sock").exists() diff --git a/tests/resolution/evaluation/test_evaluate_lxc.py b/tests/resolution/evaluation/test_evaluate_lxc.py index 365ba9761..12a960c68 100644 --- a/tests/resolution/evaluation/test_evaluate_lxc.py +++ b/tests/resolution/evaluation/test_evaluate_lxc.py @@ -1,5 +1,6 @@ """Test evaluation base.""" # pylint: disable=import-error,protected-access +from pathlib import Path from unittest.mock import patch from supervisor.const import CoreState @@ -14,12 +15,14 @@ async def test_evaluation(coresys: CoreSys): assert lxc.reason not in coresys.resolution.unsupported - coresys.docker.info.inside_lxc = True - await lxc() + with patch.object(Path, "exists") as mock_exists: + mock_exists.return_value = True + await lxc() assert lxc.reason in coresys.resolution.unsupported - coresys.docker.info.inside_lxc = False - await lxc() + with patch.object(Path, "exists") as mock_exists: + mock_exists.return_value = False + await lxc() assert lxc.reason not in coresys.resolution.unsupported