Run sandbox DHCP without nested shell
This commit is contained in:
parent
e2ebd3f35f
commit
00ce4d6710
|
|
@ -76,23 +76,19 @@ def status(command, guest):
|
|||
|
||||
|
||||
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)
|
||||
"""Obtain a DHCP lease with direct pct exec arguments, without a shell."""
|
||||
prefix = ('pct', 'exec', str(guest), '--')
|
||||
run(*prefix, 'ip', 'link', 'set', 'dev', 'eth0', 'up')
|
||||
address = run(*prefix, 'ip', '-4', 'addr', 'show', 'dev', 'eth0')
|
||||
if 'inet ' in address:
|
||||
print(f'Container {guest}: IPv4 address already present', flush=True)
|
||||
return
|
||||
run(*prefix, 'busybox', 'udhcpc', '-n', '-t', '3', '-T', '2',
|
||||
'-i', 'eth0', '-s', '/usr/share/udhcpc/default.script')
|
||||
address = run(*prefix, 'ip', '-4', 'addr', 'show', 'dev', 'eth0')
|
||||
if 'inet ' not in address:
|
||||
raise RuntimeError(f'Container {guest} did not obtain an IPv4 address')
|
||||
print(f'Container {guest}: DHCP lease acquired', flush=True)
|
||||
|
||||
|
||||
def owned_existing_guests():
|
||||
|
|
|
|||
|
|
@ -60,15 +60,14 @@ def client_address(guest, network):
|
|||
print(f'Client {guest} address check: {exc}', file=sys.stderr)
|
||||
print('Interface state:', file=sys.stderr)
|
||||
print(output[:2000], file=sys.stderr)
|
||||
diagnostic = subprocess.run(
|
||||
['pct', 'exec', str(guest), '--', 'sh', '-c',
|
||||
"printf '%s\\n' '-- network configuration'; "
|
||||
"if [ -f /etc/network/interfaces ]; then cat /etc/network/interfaces; else echo 'No /etc/network/interfaces'; fi; "
|
||||
"printf '%s\\n' '-- DHCP processes'; ps | grep '[u]dhcpc' || true"],
|
||||
capture_output=True, text=True, timeout=20)
|
||||
print(diagnostic.stdout[:3000], file=sys.stderr)
|
||||
if diagnostic.returncode:
|
||||
print(f'Client diagnostics exit: {diagnostic.returncode}', file=sys.stderr)
|
||||
for label, command in [('network configuration', ('cat', '/etc/network/interfaces')),
|
||||
('processes', ('ps',))]:
|
||||
diagnostic = subprocess.run(['pct', 'exec', str(guest), '--', *command],
|
||||
capture_output=True, text=True, timeout=20)
|
||||
print(f'-- {label}', file=sys.stderr)
|
||||
print(diagnostic.stdout[:3000], file=sys.stderr)
|
||||
if diagnostic.returncode:
|
||||
print(f'Client diagnostics exit: {diagnostic.returncode}', file=sys.stderr)
|
||||
raise
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -34,11 +34,11 @@ class SandboxLifecycleTests(unittest.TestCase):
|
|||
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:
|
||||
with patch.object(lifecycle, 'run', side_effect=['', '', '', 'inet 172.29.210.101/24']) 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])
|
||||
calls = [entry.args for entry in command.call_args_list]
|
||||
self.assertTrue(all(args[:4] == ('pct', 'exec', '9110', '--') for args in calls))
|
||||
self.assertIn(('pct', 'exec', '9110', '--', 'busybox', 'udhcpc', '-n', '-t', '3', '-T', '2', '-i', 'eth0', '-s', '/usr/share/udhcpc/default.script'), calls)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
|||
Loading…
Reference in New Issue