Start DHCP in sandbox clients
This commit is contained in:
parent
6bbc7ca72f
commit
e2ebd3f35f
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Reference in New Issue