Просмотр исходного кода

utils/lxc: Add working autostart for OpenWrt

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>
Daniel Dickinson 8 лет назад
Родитель
Сommit
b7b0f59e1c
2 измененных файлов: 65 добавлений и 0 удалений
  1. 5
    0
      utils/lxc/files/lxc-auto.config
  2. 60
    0
      utils/lxc/files/lxc-auto.init

+ 5
- 0
utils/lxc/files/lxc-auto.config Просмотреть файл

@@ -0,0 +1,5 @@
1
+#config container
2
+	#option name container1
3
+	#option timeout 300
4
+	#list command '/bin/command --option'
5
+

+ 60
- 0
utils/lxc/files/lxc-auto.init Просмотреть файл

@@ -0,0 +1,60 @@
1
+#!/bin/sh /etc/rc.common
2
+
3
+. /lib/functions.sh
4
+
5
+START=99
6
+STOP=00
7
+
8
+run_command() {
9
+	local command="$1"
10
+	$command
11
+}
12
+
13
+start_container() {
14
+	local cfg="$1"
15
+	local name
16
+
17
+	config_get name "$cfg" name
18
+	config_list_foreach "$cfg" command run_command
19
+	if [ -n "$name" ]; then
20
+		/usr/bin/lxc-start -n "$name"
21
+	fi
22
+}
23
+
24
+max_timeout=0
25
+
26
+stop_container() {
27
+	local cfg="$1"
28
+	local name timeout
29
+
30
+	config_get name "$cfg" name
31
+	config_get timeout "$cfg" timeout 300
32
+
33
+	if [ "$max_timeout" -lt "$timeout" ]; then
34
+		max_timeout=$timeout
35
+	fi
36
+
37
+	if [ -n "$name" ]; then
38
+		if [ "$timeout" = "0" ]; then
39
+			/usr/bin/lxc-stop -n "$name" &
40
+		else
41
+			/usr/bin/lxc-stop -n "$name" -t $timeout &
42
+		fi
43
+	fi
44
+}
45
+
46
+start() {
47
+	config_load lxc-auto
48
+	config_foreach start_container container
49
+}
50
+
51
+stop() {
52
+	config_load lxc-auto
53
+	config_foreach stop_container container
54
+	# ensure e.g. shutdown doesn't occur before maximum timeout on
55
+	# containers that are shutting down
56
+	if [ $max_timeout -gt 0 ]; then
57
+		sleep $max_timeout
58
+	fi
59
+}
60
+