utils/lxc: Add working autostart for OpenWrt 2494/head
authorDaniel Dickinson <openwrt@daniel.thecshore.com>
Sun, 13 Mar 2016 00:01:59 +0000 (19:01 -0500)
committerDaniel Dickinson <openwrt@daniel.thecshore.com>
Sun, 13 Mar 2016 00:01:59 +0000 (19:01 -0500)
Standard LXC autostart is currently not working in OpenWrt,
therefore add our own autostart mechanism for now.

Signed-off-by: Daniel Dickinson <openwrt@daniel.thecshore.com>
utils/lxc/files/lxc-auto.config [new file with mode: 0644]
utils/lxc/files/lxc-auto.init [new file with mode: 0755]

diff --git a/utils/lxc/files/lxc-auto.config b/utils/lxc/files/lxc-auto.config
new file mode 100644 (file)
index 0000000..b5a7ec9
--- /dev/null
@@ -0,0 +1,5 @@
+#config container
+       #option name container1
+       #option timeout 300
+       #list command '/bin/command --option'
+
diff --git a/utils/lxc/files/lxc-auto.init b/utils/lxc/files/lxc-auto.init
new file mode 100755 (executable)
index 0000000..937f082
--- /dev/null
@@ -0,0 +1,60 @@
+#!/bin/sh /etc/rc.common
+
+. /lib/functions.sh
+
+START=99
+STOP=00
+
+run_command() {
+       local command="$1"
+       $command
+}
+
+start_container() {
+       local cfg="$1"
+       local name
+
+       config_get name "$cfg" name
+       config_list_foreach "$cfg" command run_command
+       if [ -n "$name" ]; then
+               /usr/bin/lxc-start -n "$name"
+       fi
+}
+
+max_timeout=0
+
+stop_container() {
+       local cfg="$1"
+       local name timeout
+
+       config_get name "$cfg" name
+       config_get timeout "$cfg" timeout 300
+
+       if [ "$max_timeout" -lt "$timeout" ]; then
+               max_timeout=$timeout
+       fi
+
+       if [ -n "$name" ]; then
+               if [ "$timeout" = "0" ]; then
+                       /usr/bin/lxc-stop -n "$name" &
+               else
+                       /usr/bin/lxc-stop -n "$name" -t $timeout &
+               fi
+       fi
+}
+
+start() {
+       config_load lxc-auto
+       config_foreach start_container container
+}
+
+stop() {
+       config_load lxc-auto
+       config_foreach stop_container container
+       # ensure e.g. shutdown doesn't occur before maximum timeout on
+       # containers that are shutting down
+       if [ $max_timeout -gt 0 ]; then
+               sleep $max_timeout
+       fi
+}
+