nixos/nspawn-container: hash name, remove assertion (#539117)

This commit is contained in:
Martin Weinelt
2026-07-08 01:19:17 +00:00
committed by GitHub
4 changed files with 12 additions and 32 deletions

View File

@@ -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 = [

View File

@@ -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(

View File

@@ -84,7 +84,7 @@ import ../make-test-python.nix (
};
};
nodes = builtins.mapAttrs (
containers = builtins.mapAttrs (
_: val:
mkMerge [
val
@@ -97,7 +97,7 @@ import ../make-test-python.nix (
meta.maintainers = [ ];
inherit nodes;
inherit containers;
testScript = ''
start_all()

View File

@@ -183,7 +183,7 @@ import ../../make-test-python.nix (
};
};
nodes = builtins.mapAttrs (
containers = builtins.mapAttrs (
_: val:
mkMerge [
val
@@ -196,7 +196,7 @@ import ../../make-test-python.nix (
meta.maintainers = [ ];
inherit nodes;
inherit containers;
testScript = ''
start_all()