2024-05-17 23:04:34 +08:00
|
|
|
import os
|
|
|
|
from logging import getLogger
|
|
|
|
from builder.lib.context import ArchBuilderContext
|
|
|
|
from builder.lib.config import ArchBuilderConfigError
|
|
|
|
from builder.lib.utils import open_config
|
|
|
|
log = getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def gen_machine_info(ctx: ArchBuilderContext):
|
2024-05-20 09:56:42 +08:00
|
|
|
"""
|
|
|
|
Generate /etc/machine-info for systemd
|
|
|
|
"""
|
2024-05-17 23:04:34 +08:00
|
|
|
root = ctx.get_rootfs()
|
|
|
|
file = os.path.join(root, "etc/machine-info")
|
|
|
|
cfg = ctx.get("sysconf")
|
|
|
|
fields = [
|
|
|
|
"chassis", "location", "icon_name",
|
|
|
|
"deployment", "pretty_hostname"
|
|
|
|
]
|
|
|
|
with open_config(file) as f:
|
|
|
|
for field in fields:
|
|
|
|
if field not in cfg: continue
|
|
|
|
f.write("%s=\"%s\"\n" % (field.upper(), cfg[field]))
|
|
|
|
log.info(f"generated machine-info {file}")
|
|
|
|
|
|
|
|
|
|
|
|
def gen_hosts(ctx: ArchBuilderContext):
|
2024-05-20 09:56:42 +08:00
|
|
|
"""
|
|
|
|
Generate /etc/hosts
|
|
|
|
"""
|
2024-05-17 23:04:34 +08:00
|
|
|
addrs: list[str] = []
|
|
|
|
root = ctx.get_rootfs()
|
|
|
|
file = os.path.join(root, "etc/hosts")
|
|
|
|
hosts: list[str] = ctx.get("sysconf.hosts", [])
|
|
|
|
with open_config(file) as f:
|
|
|
|
for addr in hosts:
|
|
|
|
s = addr.split()
|
|
|
|
if len(s) <= 1: raise ArchBuilderConfigError("bad host entry")
|
|
|
|
addrs.append(s[0])
|
|
|
|
f.write(addr)
|
|
|
|
f.write(os.linesep)
|
2024-05-20 09:56:42 +08:00
|
|
|
|
|
|
|
# 127.0.1.1 not set, add for FQDN
|
2024-05-17 23:04:34 +08:00
|
|
|
name = ctx.get("sysconf.hostname")
|
|
|
|
if "127.0.1.1" not in addrs and name:
|
|
|
|
f.write(f"127.0.1.1 {name}\n")
|
|
|
|
log.info(f"generated hosts {file}")
|
|
|
|
|
|
|
|
|
|
|
|
def gen_hostname(ctx: ArchBuilderContext):
|
2024-05-20 09:56:42 +08:00
|
|
|
"""
|
|
|
|
Generate /etc/hostname
|
|
|
|
"""
|
2024-05-17 23:04:34 +08:00
|
|
|
root = ctx.get_rootfs()
|
|
|
|
file = os.path.join(root, "etc/hostname")
|
|
|
|
name = ctx.get("sysconf.hostname")
|
|
|
|
if name is None: return
|
|
|
|
with open_config(file) as f:
|
|
|
|
f.write(name)
|
|
|
|
f.write(os.linesep)
|
|
|
|
log.info(f"generated hostname {file}")
|
|
|
|
|
|
|
|
|
|
|
|
def gen_environments(ctx: ArchBuilderContext):
|
2024-05-20 09:56:42 +08:00
|
|
|
"""
|
|
|
|
Generate /etc/environments
|
|
|
|
"""
|
2024-05-17 23:04:34 +08:00
|
|
|
root = ctx.get_rootfs()
|
|
|
|
file = os.path.join(root, "etc/environment")
|
|
|
|
envs: dict[str] = ctx.get("sysconf.environments", [])
|
|
|
|
with open_config(file) as f:
|
|
|
|
for key in envs:
|
|
|
|
val = envs[key]
|
|
|
|
f.write(f"{key}=\"{val}\"\n")
|
|
|
|
log.info(f"generated environment {file}")
|
|
|
|
|
|
|
|
|
|
|
|
def proc_names(ctx: ArchBuilderContext):
|
2024-05-20 09:56:42 +08:00
|
|
|
"""
|
|
|
|
Apply names for system configs
|
|
|
|
"""
|
2024-05-17 23:04:34 +08:00
|
|
|
gen_machine_info(ctx)
|
|
|
|
gen_environments(ctx)
|
|
|
|
gen_hostname(ctx)
|
|
|
|
gen_hosts(ctx)
|