1 |
flameeyes 10/08/01 21:13:46 |
2 |
|
3 |
Modified: lxc.initd |
4 |
Log: |
5 |
New init script, with more thorough verification of the config, support for mounting the cgroup pseudo-fs, proper network dependencies, improved start-up (verifies that the container actually started up) and stop (won't waste time if the container was externally stopped). |
6 |
(Portage version: 2.2_rc67/cvs/Linux x86_64) |
7 |
|
8 |
Revision Changes Path |
9 |
1.2 app-emulation/lxc/files/lxc.initd |
10 |
|
11 |
file : http://sources.gentoo.org/viewvc.cgi/gentoo-x86/app-emulation/lxc/files/lxc.initd?rev=1.2&view=markup |
12 |
plain: http://sources.gentoo.org/viewvc.cgi/gentoo-x86/app-emulation/lxc/files/lxc.initd?rev=1.2&content-type=text/plain |
13 |
diff : http://sources.gentoo.org/viewvc.cgi/gentoo-x86/app-emulation/lxc/files/lxc.initd?r1=1.1&r2=1.2 |
14 |
|
15 |
Index: lxc.initd |
16 |
=================================================================== |
17 |
RCS file: /var/cvsroot/gentoo-x86/app-emulation/lxc/files/lxc.initd,v |
18 |
retrieving revision 1.1 |
19 |
retrieving revision 1.2 |
20 |
diff -u -r1.1 -r1.2 |
21 |
--- lxc.initd 1 Aug 2010 03:17:51 -0000 1.1 |
22 |
+++ lxc.initd 1 Aug 2010 21:13:46 -0000 1.2 |
23 |
@@ -1,13 +1,17 @@ |
24 |
#!/sbin/runscript |
25 |
-# Copyright 1999-2010 Gentoo Foundation |
26 |
+# Copyright 2010-2010 Gentoo Foundation |
27 |
# Distributed under the terms of the GNU General Public License v2 |
28 |
-# $Header: /var/cvsroot/gentoo-x86/app-emulation/lxc/files/lxc.initd,v 1.1 2010/08/01 03:17:51 flameeyes Exp $ |
29 |
+# $Header: /var/cvsroot/gentoo-x86/app-emulation/lxc/files/lxc.initd,v 1.2 2010/08/01 21:13:46 flameeyes Exp $ |
30 |
|
31 |
CONTAINER=${SVCNAME#*.} |
32 |
CONFIGFILE=${CONFIGFILE:-/etc/lxc/${CONTAINER}.conf} |
33 |
|
34 |
-depend() { |
35 |
- need net.lo localmount |
36 |
+lxc_get_var() { |
37 |
+ awk 'BEGIN { FS="[ \t]*=[ \t]*" } $1 == "'$1'" { print $2; exit }' ${CONFIGFILE} |
38 |
+} |
39 |
+ |
40 |
+cgroup_get_mount() { |
41 |
+ mount | awk '$5 == "cgroup" { print $3; exit }' |
42 |
} |
43 |
|
44 |
checkconfig() { |
45 |
@@ -16,37 +20,81 @@ |
46 |
eerror " ln -s lxc /etc/init.d/lxc.container" |
47 |
return 1 |
48 |
fi |
49 |
+ |
50 |
+ utsname=$(lxc_get_var lxc.utsname) |
51 |
+ if [ ${CONTAINER} != ${utsname} ]; then |
52 |
+ eerror "You should use the same name for the service and the" |
53 |
+ eerror "container. Right now the container is called ${utsname}" |
54 |
+ return 1 |
55 |
+ fi |
56 |
} |
57 |
|
58 |
-rootpath() { |
59 |
- sed -n -e 's:^[ \t]*lxc.rootfs[ \t]*=[ \t]*\(.*\)$:\1:p' ${CONFIGFILE} |
60 |
+depend() { |
61 |
+ # be quiet, since we have to run depend() also for the |
62 |
+ # non-muxed init script, unfortunately. |
63 |
+ checkconfig 2>/dev/null || return 0 |
64 |
+ |
65 |
+ config ${CONFIGFILE} |
66 |
+ need localmount |
67 |
+ |
68 |
+ # find out which network interface the container is linked to, |
69 |
+ # and then require that to be enabled, so that the |
70 |
+ # dependencies are correct. |
71 |
+ netif=$(lxc_get_var lxc.network.link) |
72 |
+ [ -n "${netif}" ] && need net.${netif} |
73 |
} |
74 |
|
75 |
start() { |
76 |
checkconfig || return 1 |
77 |
|
78 |
+ # make sure that cgroup is mounted if it isn't already, this |
79 |
+ # ensures that we can actually proceed! |
80 |
+ cgroupmount=$(cgroup_get_mount) |
81 |
+ if [ -z ${cgroupmount} ]; then |
82 |
+ mkdir -p /cgroup |
83 |
+ |
84 |
+ if ! mount -t cgroup cgroup /cgroup; then |
85 |
+ eerror "Unable to mount cgroup pseudo-filesystem on /cgroup" |
86 |
+ return 1 |
87 |
+ fi |
88 |
+ |
89 |
+ cgroupmount=/cgroup |
90 |
+ fi |
91 |
+ |
92 |
rm /var/log/lxc/${CONTAINER}.log |
93 |
|
94 |
+ rootpath=$(lxc_get_var lxc.rootfs) |
95 |
+ |
96 |
# Check the format of our init and the chroot's init, to see if we |
97 |
# have to use linux32 or linux64… |
98 |
- case $(scanelf -BF '%M#f' /sbin/init $(rootpath)/sbin/init | tr '\n' ':') in |
99 |
+ case $(scanelf -BF '%M#f' /sbin/init ${rootpath}/sbin/init | tr '\n' ':') in |
100 |
ELFCLASS64:ELFCLASS64:) setarch=;; |
101 |
ELFCLASS32:ELFCLASS32:) setarch=;; |
102 |
ELFCLASS32:ELFCLASS64:) setarch=linux64;; |
103 |
ELFCLASS64:ELFCLASS32:) setarch=linux32;; |
104 |
esac |
105 |
|
106 |
- mkdir -p /var/log/lxc |
107 |
- |
108 |
ebegin "Starting ${CONTAINER}" |
109 |
${setarch} lxc-start -n ${CONTAINER} -f ${CONFIGFILE} -d -o /var/log/lxc/${CONTAINER}.log |
110 |
+ sleep 0.5 |
111 |
+ |
112 |
+ # lxc-start -d will _always_ report a correct startup, even if it |
113 |
+ # failed, so rather than trust that, check that the cgroup exists. |
114 |
+ [ -d ${cgroupmount}/${CONTAINER} ] |
115 |
eend $? |
116 |
} |
117 |
|
118 |
stop() { |
119 |
checkconfig || return 1 |
120 |
|
121 |
- local init_pid=$(head -n1 /cgroup/${CONTAINER}/tasks) |
122 |
+ cgroupmount=$(cgroup_get_mount) |
123 |
+ |
124 |
+ if ! [ -d ${cgroupmount}/${CONTAINER} ]; then |
125 |
+ ewarn "${CONTAINER} doesn't seem to be started." |
126 |
+ return 0 |
127 |
+ fi |
128 |
+ |
129 |
+ init_pid=$(head -n1 ${cgroupmount}/${CONTAINER}/tasks) |
130 |
|
131 |
ebegin "Shutting down system in ${CONTAINER}" |
132 |
kill -INT ${init_pid} |
133 |
@@ -64,4 +112,3 @@ |
134 |
lxc-stop -n ${CONTAINER} |
135 |
eend $? |
136 |
} |
137 |
- |