2018-12-18 02:08:53 +01:00
|
|
|
#!/usr/bin/env bash
|
2018-02-09 01:14:38 +01:00
|
|
|
set -e
|
|
|
|
|
|
|
|
usage() {
|
|
|
|
echo "usage: lxc-wait -n CONTAINER" >&2
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
args="$(getopt -o +n: --long help,name: -- "$@")"
|
|
|
|
eval "set -- $args"
|
|
|
|
while true; do
|
|
|
|
case "$1" in
|
2020-10-15 04:55:57 +02:00
|
|
|
--help) usage ;;
|
|
|
|
-n | --name)
|
|
|
|
CONTAINER_NAME="$2"
|
|
|
|
shift
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--)
|
|
|
|
shift
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
*) usage ;;
|
2018-02-09 01:14:38 +01:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ -z "$CONTAINER_NAME" ] || [ "$#" -gt 0 ]; then
|
|
|
|
usage
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$EUID" -ne 0 ]; then
|
|
|
|
echo "error: this script must be run as root" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# We poll.
|
2020-06-23 21:32:00 +02:00
|
|
|
poll_runlevel() {
|
|
|
|
for _ in {1..60}; do
|
|
|
|
echo "lxc-wait: $CONTAINER_NAME: polling for boot..." >&2
|
|
|
|
runlevel="$(lxc-attach --clear-env -n "$CONTAINER_NAME" -- runlevel 2>/dev/null)" \
|
2020-10-15 04:55:57 +02:00
|
|
|
|| {
|
|
|
|
sleep 1
|
|
|
|
continue
|
|
|
|
}
|
2020-06-23 21:32:00 +02:00
|
|
|
if [ "$runlevel" != "${0%[0-9]}" ]; then
|
|
|
|
echo "lxc-wait: $CONTAINER_NAME: booted!" >&2
|
|
|
|
poll_network
|
|
|
|
fi
|
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
echo "error: timeout waiting for container to boot" >&2
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
poll_network() {
|
|
|
|
for _ in {1..60}; do
|
|
|
|
echo "lxc-wait: $CONTAINER_NAME: polling for network..." >&2
|
|
|
|
# New hosts don't have `host` or `nslookup`
|
|
|
|
lxc-attach --clear-env -n "$CONTAINER_NAME" -- \
|
2020-10-15 04:55:57 +02:00
|
|
|
ping -q -c 1 archive.ubuntu.com 2>/dev/null >/dev/null \
|
|
|
|
|| {
|
|
|
|
sleep 1
|
|
|
|
continue
|
|
|
|
}
|
2020-06-23 21:32:00 +02:00
|
|
|
echo "lxc-wait: $CONTAINER_NAME: network is up!" >&2
|
2018-02-09 01:14:38 +01:00
|
|
|
exit 0
|
2020-06-23 21:32:00 +02:00
|
|
|
done
|
|
|
|
echo "error: timeout waiting for container to get network" >&2
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
poll_runlevel
|