[lxc-devel] [PATCH] lxc-net: create a file to tell the lxc-containers script we are done (or failed)

Serge Hallyn serge.hallyn at ubuntu.com
Mon Jun 29 20:26:13 UTC 2015


Bc I can't think of a robust way to do this with systemd in a
non-racy way.

If someone wants to propose a cleaner patch I'm happy to drop
this, but this is working for me in Ubuntu wily.

Signed-off-by: Serge Hallyn <serge.hallyn at ubuntu.com>
---
 config/init/common/lxc-containers.in | 25 +++++++++++++++++++++++--
 config/init/common/lxc-net.in        | 13 ++++++++++++-
 2 files changed, 35 insertions(+), 3 deletions(-)

diff --git a/config/init/common/lxc-containers.in b/config/init/common/lxc-containers.in
index 73ccdba..a1a9c77 100644
--- a/config/init/common/lxc-containers.in
+++ b/config/init/common/lxc-containers.in
@@ -4,6 +4,7 @@ sysconfdir="@SYSCONFDIR@"
 distrosysconfdir="@LXC_DISTRO_SYSCONF@"
 bindir="@BINDIR@"
 localstatedir="@LOCALSTATEDIR@"
+varrun="@RUNTIME_PATH@/lxc"
 
 # These can be overridden in @LXC_DISTRO_SYSCONF@/lxc
 
@@ -68,6 +69,27 @@ wait_for_bridge()
     done
 }
 
+wait_for_lxcnet() {
+    count=1
+    while [ $count -lt 10 ]; do
+        if [ -f "${varrun}/network_failed" ]; then
+            echo "lxc-net failed!  Aborting lxc-containers job"
+            exit 1
+        elif [ -f "${varrun}/network_ran" ]; then
+            break
+        fi
+        echo "Waiting for lxc-net ($count)"
+        count=$((count+1))
+        sleep 1
+    done
+    if [ $count -eq 10 ]; then
+        echo "timed out waiting for lxc-net;  exiting"
+        exit 1
+    fi
+
+    wait_for_bridge
+}
+
 # See how we were called.
 case "$1" in
     start)
@@ -79,8 +101,7 @@ case "$1" in
             BOOTGROUPS="-g $BOOTGROUPS"
         fi
 
-        # Start containers
-        wait_for_bridge
+        wait_for_lxcnet
 
         # Start autoboot containers first then the NULL group "onboot,".
         "$bindir"/lxc-autostart $OPTIONS $BOOTGROUPS
diff --git a/config/init/common/lxc-net.in b/config/init/common/lxc-net.in
index efe64c1..91230d5 100644
--- a/config/init/common/lxc-net.in
+++ b/config/init/common/lxc-net.in
@@ -48,8 +48,16 @@ ifup() {
     ip link set dev $1 up
 }
 
+touchran() {
+    touch "${varrun}/network_ran"
+}
+
+touchfailed() {
+    touch "${varrun}/network_failed"
+}
+
 start() {
-    [ "x$USE_LXC_BRIDGE" = "xtrue" ] || { exit 0; }
+    [ "x$USE_LXC_BRIDGE" = "xtrue" ] || { touchran; exit 0; }
 
     [ ! -f "${varrun}/network_up" ] || { echo "lxc-net is already running"; exit 1; }
 
@@ -63,6 +71,7 @@ start() {
         set +e
         if [ "$FAILED" = "1" ]; then
             echo "Failed to setup lxc-net." >&2
+            touchfailed
             stop force
         fi
     }
@@ -127,10 +136,12 @@ start() {
     dnsmasq $LXC_DHCP_CONFILE_ARG $LXC_DOMAIN_ARG -u ${DNSMASQ_USER} --strict-order --bind-interfaces --pid-file="${varrun}"/dnsmasq.pid --listen-address ${LXC_ADDR} --dhcp-range ${LXC_DHCP_RANGE} --dhcp-lease-max=${LXC_DHCP_MAX} --dhcp-no-override --except-interface=lo --interface=${LXC_BRIDGE} --dhcp-leasefile="${varlib}"/misc/dnsmasq.${LXC_BRIDGE}.leases --dhcp-authoritative $LXC_IPV6_ARG || cleanup
 
     touch "${varrun}"/network_up
+    touchran
     FAILED=0
 }
 
 stop() {
+    rm -f "${varrun}/network_ran"
     [ "x$USE_LXC_BRIDGE" = "xtrue" ] || { exit 0; }
 
     [ -f "${varrun}/network_up" ] || [ "$1" = "force" ] || { echo "lxc-net isn't running"; exit 1; }
-- 
2.1.4



More information about the lxc-devel mailing list