Add possibility to pick root for some domains

This commit is contained in:
Théophile Bastian 2024-02-22 16:30:58 +01:00
parent bdfe0e1878
commit db3cd51fad
4 changed files with 23 additions and 6 deletions

View file

@ -69,6 +69,7 @@ links:
domains: domains:
b: b:
enable_v4: false enable_v4: false
root_path: /path/to/root
``` ```
The `links` element is mandatory, each link containing a mandatory `domains` The `links` element is mandatory, each link containing a mandatory `domains`
@ -86,3 +87,5 @@ domain-specific options.
The valid options for domains are: The valid options for domains are:
* `enable_v4`: boolean, specifies whether the domain has IPv4 addresses on its * `enable_v4`: boolean, specifies whether the domain has IPv4 addresses on its
NICs. NICs.
* `root_path`: an optional path to a directory containing the root to use for
this domain

View file

@ -131,7 +131,9 @@ class Container(util.LibvirtObject):
def __str__(self): def __str__(self):
return "This container is already instanciated" return "This container is already instanciated"
def __init__(self, conn, networks, name=None, mem=int(1e6), enable_v4=True): def __init__(
self, conn, networks, name=None, mem=int(1e6), enable_v4=True, root_path=None
):
"""Parameters: """Parameters:
* conn: connection to libvirt, * conn: connection to libvirt,
* networks: iterable of Network instances this container is connected to, * networks: iterable of Network instances this container is connected to,
@ -139,6 +141,8 @@ class Container(util.LibvirtObject):
* mem: KiB of memory available to this container, * mem: KiB of memory available to this container,
* enable_v4: is IPv4 enabled for this container? Defaults to True. If False, * enable_v4: is IPv4 enabled for this container? Defaults to True. If False,
this container won't be given an IPv4 address. this container won't be given an IPv4 address.
* root_path: if this is not None, the overlayfs for this container will be
based on this root directory.
""" """
super().__init__(conn) super().__init__(conn)
@ -151,6 +155,7 @@ class Container(util.LibvirtObject):
self.mem = mem self.mem = mem
self.enable_v4 = enable_v4 self.enable_v4 = enable_v4
self.root_path = root_path or settings.BASE_SYSTEM_ROOT
self.overlay_root = None self.overlay_root = None
self.lxc_container = None self.lxc_container = None
@ -175,9 +180,11 @@ class Container(util.LibvirtObject):
for net in self.networks: for net in self.networks:
net_config = net_config_templ.inst( net_config = net_config_templ.inst(
mac=util.MACAddress(net.id, self.id), mac=util.MACAddress(net.id, self.id),
ipv4=util.Addrv4(net.id, self.id) ipv4=(
if self.enable_v4 and net.enable_v4 util.Addrv4(net.id, self.id)
else None, if self.enable_v4 and net.enable_v4
else None
),
ipv6=util.Addrv6(net.id, self.id), ipv6=util.Addrv6(net.id, self.id),
) )
net_config_path = net_conf_dir / "11-{link:02d}-{name}.network".format( net_config_path = net_conf_dir / "11-{link:02d}-{name}.network".format(
@ -190,10 +197,15 @@ class Container(util.LibvirtObject):
if self.lxc_container: if self.lxc_container:
raise self.AlreadyExists() raise self.AlreadyExists()
self.overlay_root = OverlayDirectory(settings.BASE_SYSTEM_ROOT, name=self.name) self.overlay_root = OverlayDirectory(self.root_path, name=self.name)
self._create_network_files() self._create_network_files()
with (self.overlay_root.mount_point / "etc/hostname").open("w") as h:
h.write(self.name)
with (self.overlay_root.mount_point / "etc/hosts").open("a") as h:
h.write(f"127.0.0.1\t{self.name}\n::1\t{self.name}\n")
xml = JinjaTemplate("container.xml").inst( xml = JinjaTemplate("container.xml").inst(
name=self.name, name=self.name,
uuid=self.uuid, uuid=self.uuid,

View file

@ -239,6 +239,7 @@ class YamlTopology(Topology):
dom_conf = topology["domains"][dom_conf_name] dom_conf = topology["domains"][dom_conf_name]
dom_descr[dom_conf_name]["enable_v4"] = dom_conf.get("enable_v4", True) dom_descr[dom_conf_name]["enable_v4"] = dom_conf.get("enable_v4", True)
dom_descr[dom_conf_name]["root_path"] = dom_conf.get("root_path", None)
sorted_dom_names = sorted(list(dom_descr.keys())) sorted_dom_names = sorted(list(dom_descr.keys()))
@ -247,6 +248,7 @@ class YamlTopology(Topology):
self.conn, self.conn,
dom_descr[dom]["links"], dom_descr[dom]["links"],
enable_v4=dom_descr[dom].get("enable_v4", True), enable_v4=dom_descr[dom].get("enable_v4", True),
root_path=dom_descr[dom].get("root_path", None),
name=dom, name=dom,
) )
for dom in sorted_dom_names for dom in sorted_dom_names

View file

@ -10,7 +10,7 @@ CONTAINER_BASE_ROOT = "/var/lib/machines/lxc-base-" + PREFIX
OVERLAYFS_BASE_DIR = "/tmp/{}-overlays/".format(PREFIX) OVERLAYFS_BASE_DIR = "/tmp/{}-overlays/".format(PREFIX)
# Base root # Base root
BASE_SYSTEM_ROOT = "/home/tobast/Machines/lxc-network/_base" BASE_SYSTEM_ROOT = "/home/tbastian/Machines/lxc-network/deb12_base"
# The ID of the whole generated network -- below 0xff # The ID of the whole generated network -- below 0xff
NETWORK_ID = 132 NETWORK_ID = 132