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
shortenNetName = name:
if builtins.match "priv(.*)" name != null
then "pr" + builtins.substring 4 9 name
then "p" + builtins.substring 4 9 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
netConfig = ctName: interfaces:
let
@ -35,17 +41,17 @@ let
ifData = interfaces.${netName};
in {
type = ifData.type;
name = netName;
name = checkIfname netName;
flags = "up";
hwaddr = if ifData ? hwaddr && ifData.hwaddr != null
then ifData.hwaddr
else "0A:14:48:xx:xx:xx";
} // (lib.optionalAttrs (ifData.type == "veth") {
veth.pair = "${ctName}-${shortenNetName netName}";
veth.mode = "bridge";
link = "${netName}";
veth.pair = checkIfname "${shortenNetName ctName}-${shortenNetName netName}";
veth.mode = checkIfname "bridge";
link = checkIfname netName;
}) // (lib.optionalAttrs (ifData.type == "phys") {
link = "ext-${netName}";
link = checkIfname "ext-${netName}";
})
) (builtins.attrNames interfaces);