Commit 20f47925 authored by Iain Lane's avatar Iain Lane Committed by Martin Pitt

lxc, lxd: Wait for containers to get a default network route

We execute `apt-get update' more or less as soon as the container is
started. In some situations this is too early: it can be before network
is fully working.

When building lxc//lxd containers or using autopkgtest-virt-lxc/-lxd,
wait until we have a default route before proceeding.

Modified-By: Martin Pitt <mpitt@debian.org>:
 - Add the same approach to LXC
 - Some somplifications/robustifications

LP: #1749736
parent 9c8f6445
......@@ -107,16 +107,18 @@ setup() {
fi
lxc-start --daemon --name=$1
# wait until it is booted: lxc-attach works and we get a numeric runlevel
# wait until it is booted: lxc-attach works, we get a numeric runlevel, and networking is up
timeout=60
while [ $timeout -ge 0 ]; do
timeout=$((timeout - 1))
sleep 1
O=`lxc-attach --name=$1 runlevel 2>/dev/null` || continue
[ "$O" = "${O%[0-9]}" ] || break
if [ "$O" != "${O%[0-9]}" ] && lxc-attach --name=$1 -- sh -ec 'test -n "$(ip route show to 0/0)"'; then
break
fi
done
[ $timeout -ge 0 ] || {
echo "Timed out waiting for container to boot" >&2
echo "Timed out waiting for container to boot and get networking" >&2
lxc-stop --kill --name=$1 || true
lxc-destroy --name=$1 || true
exit 1
......
......@@ -68,16 +68,18 @@ setup() {
lxc exec "$CONTAINER" -- chmod 644 /etc/apt/apt.conf.d/01proxy
fi
# wait until it is booted: lxc exec works and we get a numeric runlevel
# wait until it is booted: lxc exec works, we get a numeric runlevel, and networking is up
timeout=60
while [ $timeout -ge 0 ]; do
timeout=$((timeout - 1))
sleep 1
O=`lxc exec "$CONTAINER" runlevel 2>/dev/null </dev/null` || continue
[ "$O" = "${O%[0-9]}" ] || break
if [ "$O" != "${O%[0-9]}" ] && lxc exec "$CONTAINER" -- sh -ec 'test -n "$(ip route show to 0/0)"'; then
break
fi
done
[ $timeout -ge 0 ] || {
echo "Timed out waiting for container to boot" >&2
echo "Timed out waiting for container to boot and get networking" >&2
exit 1
}
......
......@@ -110,7 +110,7 @@ def wait_booted(lxc_name):
'''Wait until the container has sufficiently booted to interact with it
Do this by checking that the runlevel is someting numeric, i. e. not
"unknown" or "S".
"unknown" or "S", and it has a default route.
'''
timeout = 60
while timeout > 0:
......@@ -124,12 +124,21 @@ def wait_booted(lxc_name):
continue
out = out.strip()
if out.split()[-1].isdigit():
return
break
adtlog.debug('wait_booted: runlevel "%s", retrying...' % out)
VirtSubproc.bomb('timed out waiting for container %s to start; '
'last runlevel "%s"' % (lxc_name, out))
adtlog.debug('waiting for network')
try:
VirtSubproc.check_exec(sudoify(['lxc-attach', '--name', lxc_name, '--', 'sh', '-ec',
'''while true; do
if test -n "$(ip route show to 0/0)"; then
break;
fi;
sleep 1;
done''']), timeout=60)
except VirtSubproc.Timeout:
VirtSubproc.bomb('timed out waiting for container %s to start and get networking' % lxc_name)
def determine_normal_user(lxc_name):
......
......@@ -86,7 +86,7 @@ def wait_booted():
'''Wait until the container has sufficiently booted to interact with it
Do this by checking that the runlevel is someting numeric, i. e. not
"unknown" or "S".
"unknown" or "S", and it has a default route.
'''
timeout = 60
while timeout > 0:
......@@ -100,12 +100,21 @@ def wait_booted():
continue
out = out.strip()
if out.split()[-1].isdigit():
return
break
adtlog.debug('wait_booted: runlevel "%s", retrying...' % out)
VirtSubproc.bomb('timed out waiting for container %s to start; '
'last runlevel "%s"' % (container_name, out))
adtlog.debug('waiting for network')
try:
VirtSubproc.check_exec(['lxc', 'exec', container_name, '--', 'sh', '-ec',
'''while true; do
if test -n "$(ip route show to 0/0)"; then
break;
fi;
sleep 1;
done'''], timeout=60)
except VirtSubproc.Timeout:
VirtSubproc.bomb('timed out waiting for container %s to start and get networking' % container_name)
def determine_normal_user():
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment