nixos-module/server/lxc-containers: check and shorten ifnames more

This commit is contained in:
Astro 2021-06-09 21:37:21 +02:00
parent 46ca027d80
commit 58047f565e
1 changed files with 12 additions and 6 deletions

View File

@ -24,9 +24,15 @@ let
# linux iface name max length = 15 # linux iface name max length = 15
shortenNetName = name: shortenNetName = name:
if builtins.match "priv(.*)" name != null if builtins.match "priv(.*)" name != null
then "pr" + builtins.substring 4 9 name then "p" + builtins.substring 4 9 name
else name; else name;
checkIfname = ifname: let
len = builtins.stringLength ifname;
in if len > 15
then throw "Interface name ${ifname} is ${toString (len - 15)} chars too long."
else ifname;
# `lxc.net.*` formatter for lxc.container.conf files # `lxc.net.*` formatter for lxc.container.conf files
netConfig = ctName: interfaces: netConfig = ctName: interfaces:
let let
@ -35,17 +41,17 @@ let
ifData = interfaces.${netName}; ifData = interfaces.${netName};
in { in {
type = ifData.type; type = ifData.type;
name = netName; name = checkIfname netName;
flags = "up"; flags = "up";
hwaddr = if ifData ? hwaddr && ifData.hwaddr != null hwaddr = if ifData ? hwaddr && ifData.hwaddr != null
then ifData.hwaddr then ifData.hwaddr
else "0A:14:48:xx:xx:xx"; else "0A:14:48:xx:xx:xx";
} // (lib.optionalAttrs (ifData.type == "veth") { } // (lib.optionalAttrs (ifData.type == "veth") {
veth.pair = "${ctName}-${shortenNetName netName}"; veth.pair = checkIfname "${shortenNetName ctName}-${shortenNetName netName}";
veth.mode = "bridge"; veth.mode = checkIfname "bridge";
link = "${netName}"; link = checkIfname netName;
}) // (lib.optionalAttrs (ifData.type == "phys") { }) // (lib.optionalAttrs (ifData.type == "phys") {
link = "ext-${netName}"; link = checkIfname "ext-${netName}";
}) })
) (builtins.attrNames interfaces); ) (builtins.attrNames interfaces);