diff --git a/lxc_net/container.py b/lxc_net/container.py index 357f66e..20776b8 100644 --- a/lxc_net/container.py +++ b/lxc_net/container.py @@ -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, diff --git a/lxc_net/templates/nic.network b/lxc_net/templates/nic.network new file mode 100644 index 0000000..a200f62 --- /dev/null +++ b/lxc_net/templates/nic.network @@ -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