From 636bc3e61a0365e7a450d1b580e0437a1e4f3276 Mon Sep 17 00:00:00 2001 From: Pascal Vizeli Date: Fri, 29 Jan 2021 11:28:16 +0100 Subject: [PATCH] Handle GPIO / VIDEO map with subystem (#2478) * Handle GPIO / VIDEO mapping with subystem * fix tests * add udev support * init udev data * fix --- scripts/run-supervisor.sh | 27 ++++++++++++++++++++++++--- supervisor/docker/addon.py | 31 ++++++++++++++++++++++++------- supervisor/hardware/const.py | 8 ++++++++ supervisor/hardware/policy.py | 4 ++-- tests/hardware/test_policy.py | 4 ++++ 5 files changed, 62 insertions(+), 12 deletions(-) diff --git a/scripts/run-supervisor.sh b/scripts/run-supervisor.sh index c746ef588..c792d0ef0 100755 --- a/scripts/run-supervisor.sh +++ b/scripts/run-supervisor.sh @@ -23,9 +23,10 @@ function run_supervisor() { --privileged \ --security-opt seccomp=unconfined \ --security-opt apparmor:unconfined \ - -v /run/docker.sock:/run/docker.sock \ - -v /run/dbus:/run/dbus \ - -v "/workspaces/test_supervisor":/data \ + -v /run/docker.sock:/run/docker.sock:rw \ + -v /run/dbus:/run/dbus:ro \ + -v /run/udev:/run/udev:ro \ + -v "/workspaces/test_supervisor":/data:rw \ -v /etc/machine-id:/etc/machine-id:ro \ -v /workspaces/supervisor:/usr/src/supervisor \ -e SUPERVISOR_SHARE="/workspaces/test_supervisor" \ @@ -55,6 +56,24 @@ function init_dbus() { dbus-daemon --system --print-address } + +function init_udev() { + if pgrep systemd-udevd; then + echo "udev is running" + return 0 + fi + + echo "Startup udev" + + # cleanups + mkdir -p /run/udev + + # run + /lib/systemd/systemd-udevd --daemon + sleep 3 + udevadm trigger && udevadm settle +} + echo "Run Supervisor" start_docker @@ -65,6 +84,7 @@ if [ "$( docker container inspect -f '{{.State.Status}}' hassio_supervisor )" == echo "Restarting Supervisor" docker rm -f hassio_supervisor init_dbus + init_udev cleanup_lastboot run_supervisor stop_docker @@ -76,6 +96,7 @@ else cleanup_lastboot cleanup_docker init_dbus + init_udev run_supervisor stop_docker fi \ No newline at end of file diff --git a/supervisor/docker/addon.py b/supervisor/docker/addon.py index 2c035f649..bb05f48c5 100644 --- a/supervisor/docker/addon.py +++ b/supervisor/docker/addon.py @@ -130,34 +130,51 @@ class DockerAddon(DockerInterface): devices = set() map_strict = False + def _create_dev(device_path: Path) -> str: + """Add device to list.""" + devices.add(f"{device_path.as_posix()}:{device_path.as_posix()}:rwm") + # Static devices for device_path in self.addon.static_devices: if not self.sys_hardware.exists_device_node(device_path): _LOGGER.debug("Ignore static device path %s", device_path) continue - devices.add(f"{device_path.as_posix()}:{device_path.as_posix()}:rwm") + _create_dev(device_path) # Dynamic devices for device in self.addon.devices: map_strict = True - devices.add(f"{device.path.as_posix()}:{device.path.as_posix()}:rwm") + _create_dev(device.path) # Auto mapping UART devices / LINKS if self.addon.with_uart: for device in self.sys_hardware.filter_devices( subsystem=UdevSubsystem.SERIAL ): - devices.add(f"{device.path.as_posix()}:{device.path.as_posix()}:rwm") + _create_dev(device.path) if map_strict or not device.by_id: continue - devices.add(f"{device.by_id.as_posix()}:{device.by_id.as_posix()}:rwm") + _create_dev(device.by_id) # Auto mapping GPIO if self.addon.with_gpio: - for device in self.sys_hardware.filter_devices( - subsystem=UdevSubsystem.SERIAL + for subsystem in ( + UdevSubsystem.GPIO, + UdevSubsystem.GPIOMEM, ): - devices.add(f"{device.path.as_posix()}:{device.path.as_posix()}:rwm") + for device in self.sys_hardware.filter_devices(subsystem=subsystem): + _create_dev(device.path) + + # Auto mapping Video + if self.addon.with_video: + for subsystem in ( + UdevSubsystem.VIDEO, + UdevSubsystem.CEC, + UdevSubsystem.VCHIQ, + UdevSubsystem.MEDIA, + ): + for device in self.sys_hardware.filter_devices(subsystem=subsystem): + _create_dev(device.path) # Return None if no devices is present if devices: diff --git a/supervisor/hardware/const.py b/supervisor/hardware/const.py index 736c479f3..6ef14b4bc 100644 --- a/supervisor/hardware/const.py +++ b/supervisor/hardware/const.py @@ -17,6 +17,14 @@ class UdevSubsystem(str, Enum): DISK = "block" PCI = "pci" AUDIO = "sound" + VIDEO = "video4linux" + MEDIA = "media" + GPIO = "GPIO" + GPIOMEM = "GPIOMEM" + VCHIQ = "vchiq" + GRAPHICS = "graphics" + CEC = "CEC" + DRM = "drm" class PolicyGroup(str, Enum): diff --git a/supervisor/hardware/policy.py b/supervisor/hardware/policy.py index c77a5b9e2..ace663f61 100644 --- a/supervisor/hardware/policy.py +++ b/supervisor/hardware/policy.py @@ -11,8 +11,8 @@ _LOGGER: logging.Logger = logging.getLogger(__name__) GROUP_CGROUPS: Dict[PolicyGroup, List[int]] = { PolicyGroup.UART: [204, 188, 166, 244], - PolicyGroup.GPIO: [254], - PolicyGroup.VIDEO: [239, 29], + PolicyGroup.GPIO: [254, 245], + PolicyGroup.VIDEO: [239, 29, 81, 251, 242, 226], PolicyGroup.AUDIO: [116], } diff --git a/tests/hardware/test_policy.py b/tests/hardware/test_policy.py index 8183212b2..928e167b8 100644 --- a/tests/hardware/test_policy.py +++ b/tests/hardware/test_policy.py @@ -26,4 +26,8 @@ def test_policy_group(coresys): assert coresys.hardware.policy.get_cgroups_rules(PolicyGroup.VIDEO) == [ "c 239:* rwm", "c 29:* rwm", + "c 81:* rwm", + "c 251:* rwm", + "c 242:* rwm", + "c 226:* rwm", ]