diff --git a/nixos/modules/virtualisation/nspawn-container/default.nix b/nixos/modules/virtualisation/nspawn-container/default.nix index 82a06929e925..621f2bcb9f33 100644 --- a/nixos/modules/virtualisation/nspawn-container/default.nix +++ b/nixos/modules/virtualisation/nspawn-container/default.nix @@ -81,34 +81,6 @@ in which is prohibited within the Nix build sandbox where the test is run. ''; } - { - # Check every interface defined in allInterfaces. - # Containers try to create a bridge "${config.system.name}-${interfaceName}" - assertion = lib.all ( - iface: - let - hostName = "${config.system.name}-${iface.name}"; - in - lib.stringLength hostName <= 15 - ) (lib.attrValues cfg.allInterfaces); - - message = - let - offendingInterfaces = lib.filter ( - iface: lib.stringLength "${config.system.name}-${iface.name}" > 15 - ) (lib.attrValues cfg.allInterfaces); - offenderList = map ( - i: - "${config.system.name}-${i.name} (${toString (lib.stringLength "${config.system.name}-${i.name}")} chars)" - ) offendingInterfaces; - in - '' - The following generated host interface names exceed the Linux 15-character limit: - ${lib.concatStringsSep "\n " offenderList} - - Please shorten 'config.system.name' or the interface names in 'virtualisation.interfaces'. - ''; - } ]; virtualisation.systemd-nspawn.options = [ diff --git a/nixos/modules/virtualisation/nspawn-container/run-nspawn/src/run_nspawn/__init__.py b/nixos/modules/virtualisation/nspawn-container/run-nspawn/src/run_nspawn/__init__.py index f733ad1845c4..9d5d73433e66 100644 --- a/nixos/modules/virtualisation/nspawn-container/run-nspawn/src/run_nspawn/__init__.py +++ b/nixos/modules/virtualisation/nspawn-container/run-nspawn/src/run_nspawn/__init__.py @@ -1,6 +1,7 @@ import contextlib import dataclasses import fcntl +import hashlib import logging import os import signal @@ -124,6 +125,13 @@ def mk_veth( vlan: int, ) -> typing.Generator[None, None, None]: host_intf_name = f"{container_name}-{container_intf_name}" + # If the names for systemd-nspawn containers are too long, + # the generated bridge interface names will surpass the + # kernel limit IFNAMSIZ (15 characters + '\0'). + if len(host_intf_name) > 15: + hashed = hashlib.sha256(host_intf_name.encode()).hexdigest()[:6] + host_intf_name = f"{host_intf_name[:8]}-{hashed}" + with ensure_vlan_bridge(vlan) as bridge_name: logger.info("creating interface %s", host_intf_name) run_ip(