Containers: setup network addresses

Use systemd-networkd and config files
This commit is contained in:
Théophile Bastian 2020-03-11 11:28:47 +01:00
parent 8eb49fd01d
commit 5b8f16163d
2 changed files with 28 additions and 0 deletions

View file

@ -150,12 +150,32 @@ class Container(util.LibvirtObject):
for net in self.networks
]
def _create_network_files(self):
""" Creates systemd-networkd .network files to set IP addresses """
if not self.overlay_root:
raise Exception("No root directory specified yet")
net_config_templ = JinjaTemplate("nic.network")
net_conf_dir = self.overlay_root.mount_point / "etc/systemd/network/"
for net in self.networks:
net_config = net_config_templ.inst(
mac=util.MACAddress(net.id, self.id),
ipv4=util.Addrv4(net.id, self.id),
ipv6=util.Addrv6(net.id, self.id),
)
net_config_path = net_conf_dir / "11-{link:02d}-{name}.network".format(
link=net.id, name=net.name
)
with open(net_config_path, "w") as handle:
handle.write(net_config)
def create(self):
if self.lxc_container:
raise self.AlreadyExists()
self.overlay_root = OverlayDirectory(settings.BASE_SYSTEM_ROOT, name=self.name)
self._create_network_files()
xml = JinjaTemplate("container.xml").inst(
name=self.name,
uuid=self.uuid,

View file

@ -0,0 +1,8 @@
[Match]
MACAddress={{ mac }}
[Network]
DHCP=no
{% if ipv4 %}Address={{ ipv4 }}/{{ ipv4.prefix }}{% endif %}
{% if ipv6 %}Address={{ ipv6 }}/{{ ipv6.prefix }}{% endif %}
LinkLocalAddressing=ipv6