From 58169f4ed255adb18409edf7a562347cc0b53f14 Mon Sep 17 00:00:00 2001 From: "Gerd v. Egidy" Date: Sun, 11 Dec 2022 21:16:30 +0100 Subject: [PATCH] support configuring entries for the /etc/hosts file in the YAML config Example config: sysconfig: hosts: "192.168.1.1": "example.net.lan" "192.168.1.10": "foo.net.lan foo" --- .../scripts/sysrescue-initialize-prenet | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/airootfs/etc/systemd/scripts/sysrescue-initialize-prenet b/airootfs/etc/systemd/scripts/sysrescue-initialize-prenet index 3947ee0..bc6d781 100755 --- a/airootfs/etc/systemd/scripts/sysrescue-initialize-prenet +++ b/airootfs/etc/systemd/scripts/sysrescue-initialize-prenet @@ -122,6 +122,27 @@ if 'sysconfig' in config and 'sysctl' in config['sysconfig'] and \ print (f"Some or all sysctl options couldn't be set") errcnt+=1 +# ============================================================================== +# configure hosts file +# Should be pre-network to be available throughout the whole network setup +# ============================================================================== + +if 'sysconfig' in config and 'hosts' in config['sysconfig'] and \ + config['sysconfig']['hosts'] and isinstance(config['sysconfig']['hosts'], dict): + print(f"====> Configuring /etc/hosts ...") + + try: + # append all our entries to the hosts file + with open("/etc/hosts", "a") as hostsfile: + # key is the IPv4/IPv6, value the hostname(s) + for ip, hostname in config['sysconfig']['hosts'].items(): + hostsfile.write(f"{ip}\t{hostname}\n") + hostsfile.close() + + except Exception as e: + print(e) + errcnt+=1 + # ============================================================================== # End of the script # ==============================================================================