From e2ebd3f35f9338639cb0c58e5d155d17034cb72d Mon Sep 17 00:00:00 2001 From: "juvenal.diaz" Date: Fri, 6 Nov 2026 22:39:26 -0600 Subject: [PATCH] Start DHCP in sandbox clients --- scripts/network-sandbox-lifecycle-host.py | 22 +++++++++++++++++++ .../jeannie/test_network_sandbox_lifecycle.py | 7 ++++++ 2 files changed, 29 insertions(+) diff --git a/scripts/network-sandbox-lifecycle-host.py b/scripts/network-sandbox-lifecycle-host.py index 82a1cab..c90d75f 100644 --- a/scripts/network-sandbox-lifecycle-host.py +++ b/scripts/network-sandbox-lifecycle-host.py @@ -75,6 +75,26 @@ def status(command, guest): return output.endswith('running') +def ensure_client_network(guest): + """Start DHCP if this minimal Alpine image did not start networking.""" + command = r'''set -eu +ip link set dev eth0 up +if command -v rc-update >/dev/null 2>&1 && [ -f /etc/init.d/networking ]; then + rc-update add networking boot >/dev/null +fi +if ip -4 addr show dev eth0 | grep -q 'inet '; then + echo 'IPv4 address already present' +elif busybox pidof udhcpc >/dev/null 2>&1; then + echo 'DHCP client already running' +else + test -x /usr/share/udhcpc/default.script || { echo 'Alpine DHCP script is missing' >&2; exit 1; } + mkdir -p /run + busybox udhcpc -b -i eth0 -p /run/jeannie-sandbox-udhcpc.pid -s /usr/share/udhcpc/default.script + echo 'DHCP client started; a lease may take time' +fi''' + print(f'Container {guest}: {run("pct", "exec", str(guest), "--", "sh", "-c", command)}', flush=True) + + def owned_existing_guests(): resources = json.loads(run('pvesh', 'get', '/cluster/resources', '--type', 'vm', '--output-format', 'json')) indexed = {int(item['vmid']): item.get('type') for item in resources if 'vmid' in item} @@ -114,6 +134,8 @@ def main(): started.append((command, guest)) if not status(command, guest): raise RuntimeError(f'{command} guest {guest} did not remain running') + for guest in (9110, 9111): + ensure_client_network(guest) print('Sandbox guests started with the router WAN disconnected; internet access is not enabled.') print('Guest startup does not prove firewall or VLAN isolation; validate both before connecting WAN.') else: diff --git a/tests/jeannie/test_network_sandbox_lifecycle.py b/tests/jeannie/test_network_sandbox_lifecycle.py index f396908..9e09714 100644 --- a/tests/jeannie/test_network_sandbox_lifecycle.py +++ b/tests/jeannie/test_network_sandbox_lifecycle.py @@ -33,6 +33,13 @@ class SandboxLifecycleTests(unittest.TestCase): with self.assertRaisesRegex(RuntimeError, 'unexpected network interface'): lifecycle.verify_guests() + def test_start_network_targets_only_the_selected_client(self): + with patch.object(lifecycle, 'run', return_value='DHCP client started') as command: + lifecycle.ensure_client_network(9110) + args = command.call_args.args + self.assertEqual(args[:5], ('pct', 'exec', '9110', '--', 'sh')) + self.assertIn('busybox udhcpc -b -i eth0', args[-1]) + if __name__ == '__main__': unittest.main()