Store and restore static routes upon DHCP renew

This commit is contained in:
2023-01-24 13:19:24 +01:00
parent 728bc97cae
commit 972f796b4d
+17
View File
@@ -354,6 +354,15 @@ class ContainerDHCPManager:
elif event_type == udhcpc.EventType.BOUND: elif event_type == udhcpc.EventType.BOUND:
logger.info('[dhcp container] BOUND Event %s', _event) logger.info('[dhcp container] BOUND Event %s', _event)
result = subprocess.run(['nsenter', f'-n{dhcp.netns}', '--', '/sbin/ip', 'route', 'show', 'type', 'unicast', 'proto', 'static', 'table', 'all'], timeout=1, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
if result.returncode == 0:
routes = result.stdout
routes = routes.splitlines()
if routes:
logger.info('[dhcp container] Store existing static routes')
for route in routes:
logger.info('[dhcp container] Stored static route: %s', route)
logger.info('[dhcp container] Flushing IP addresses') logger.info('[dhcp container] Flushing IP addresses')
subprocess.check_call(['nsenter', f'-n{dhcp.netns}', '--', '/sbin/ip', 'address', 'flush', 'dev', subprocess.check_call(['nsenter', f'-n{dhcp.netns}', '--', '/sbin/ip', 'address', 'flush', 'dev',
str(dhcp.iface['ifname']), 'label', str(dhcp.iface['ifname'])], str(dhcp.iface['ifname']), 'label', str(dhcp.iface['ifname'])],
@@ -369,6 +378,14 @@ class ContainerDHCPManager:
subprocess.check_call(['nsenter', f'-n{dhcp.netns}', '--', '/sbin/ip', 'route', 'replace', 'default', 'via', subprocess.check_call(['nsenter', f'-n{dhcp.netns}', '--', '/sbin/ip', 'route', 'replace', 'default', 'via',
str(dhcp.gateway)], str(dhcp.gateway)],
timeout=1, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) timeout=1, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
if routes:
logger.info('[dhcp container] Restore static routes')
for route in routes:
parts = route.split()
command = ['nsenter', f'-n{dhcp.netns}', '--', '/sbin/ip', 'route', 'add'] + parts + ['proto', 'static']
output = subprocess.run(command, timeout=1, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
logger.info('[dhcp container] Restored static route: %s', route)
else: else:
logger.info('[dhcp container] Unhandled Event %s: %s', event_type,_event) logger.info('[dhcp container] Unhandled Event %s: %s', event_type,_event)