Skip to content

Instantly share code, notes, and snippets.

@jborean93
Created June 3, 2024 00:34
Show Gist options
  • Save jborean93/df343bfcc6da3ff16e8f113c070ff397 to your computer and use it in GitHub Desktop.
Save jborean93/df343bfcc6da3ff16e8f113c070ff397 to your computer and use it in GitHub Desktop.
Script that can automatically configure DNS domain resolvers for systemd-resolved on QEMU network adapters
#!/usr/bin/python
import os.path
import subprocess
import sys
import xml.etree.ElementTree as ET
def main():
iface = sys.argv[1]
hook_case = sys.argv[2]
if hook_case != "started":
return
libvirt_dir = os.path.abspath(os.path.join(
os.path.relpath(__file__), "..", ".."))
iface_def = ET.parse(os.path.join(libvirt_dir, "qemu", "networks", f"{iface}.xml"))
bridge_info = iface_def.find("bridge")
if bridge_info is None or "name" not in bridge_info.attrib:
return
bridge = bridge_info.attrib["name"]
domain_info = iface_def.find("domain")
if domain_info is None or "name" not in domain_info.attrib:
return
domain = domain_info.attrib["name"]
ip_info = iface_def.find("ip")
if ip_info is None or "address" not in ip_info.attrib:
return
host_ip = ip_info.attrib["address"]
dns_ip = host_ip.split(".")
dns_ip[-1] = "10"
subprocess.check_call(["resolvectl", "dns", bridge, ".".join(dns_ip)])
subprocess.check_call(["resolvectl", "domain", bridge, f"~{domain}"])
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment