Line
Link Here
|
0 |
-- /usr/portage/app-emulation/lxc/files/lxc.initd.3 |
0 |
++ /etc/init.d/lxc |
Lines 1-137
Link Here
|
1 |
#!/sbin/runscript |
1 |
#!/sbin/runscript |
2 |
# Copyright 1999-2014 Gentoo Foundation |
2 |
# Copyright 1999-2014 Gentoo Foundation |
3 |
# Distributed under the terms of the GNU General Public License v2 |
3 |
# Distributed under the terms of the GNU General Public License v2 |
4 |
# $Header: /var/cvsroot/gentoo-x86/app-emulation/lxc/files/lxc.initd.3,v 1.1 2014/10/18 17:00:37 hwoarang Exp $ |
4 |
# $Header: $ |
5 |
|
5 |
|
|
|
6 |
#_name=${SVCNAME#*.} |
6 |
CONTAINER=${SVCNAME#*.} |
7 |
CONTAINER=${SVCNAME#*.} |
7 |
|
8 |
name="Linux Container ${CONTAINER}" |
8 |
lxc_get_configfile() { |
9 |
extra_commands="info halt unfreeze" |
9 |
if [ -f "/etc/lxc/${CONTAINER}.conf" ]; then |
10 |
extra_started_commands="freeze attach" |
10 |
echo "/etc/lxc/${CONTAINER}.conf" |
11 |
|
11 |
elif [ -f "/etc/lxc/${CONTAINER}/config" ]; then |
12 |
description="Service to start Linux Container" |
12 |
echo "/etc/lxc/${CONTAINER}/config" |
13 |
description_info="Get info about container" |
13 |
else |
14 |
description_halt="Forse to killing container" |
14 |
eerror "Unable to find a suitable configuration file." |
15 |
description_freeze="Freeze running container" |
15 |
eerror "If you set up the container in a non-standard" |
16 |
description_unfreeze="Unfreeze freezed container" |
16 |
eerror "location, please set the CONFIGFILE variable." |
17 |
description_attach="Start a shell inside a running container" |
17 |
return 1 |
18 |
|
18 |
fi |
19 |
## if necessary, you can manually set the value of these variables in the /etc/rc.conf or /etc/conf.d/lxc |
19 |
} |
20 |
## be sure when you change this variables |
20 |
|
21 |
: ${rc_lxc__cgrouppath:=/sys/fs/cgroup/*/lxc} |
21 |
[ $CONTAINER != $SVCNAME ] && CONFIGFILE=${CONFIGFILE:-$(lxc_get_configfile)} |
22 |
# to avoid situation like as this https://bugs.gentoo.org/show_bug.cgi?id=416643 |
22 |
|
23 |
: ${rc_lxc__lxcpath:=$(lxc-config lxc.lxcpath)} |
23 |
lxc_get_var() { |
24 |
## https://bugs.gentoo.org/show_bug.cgi?id=525956 |
24 |
awk 'BEGIN { FS="[ \t]*=[ \t]*" } $1 == "'$1'" { print $2; exit }' ${CONFIGFILE} |
25 |
[ -f "${rc_lxc__lxcpath}/${CONTAINER}.conf" ] && \ |
25 |
} |
26 |
CONFIGFILE="${rc_lxc__lxcpath}/${CONTAINER}.conf" || \ |
|
|
27 |
CONFIGFILE="${rc_lxc__lxcpath}/${CONTAINER}/config" |
28 |
|
29 |
eval pidfile="\${rc_${SVCNAME//./_}__pidfile:-/run/lxc/\${CONTAINER}.pid}" |
30 |
eval logfile="\${rc_${SVCNAME//./_}__logfile:-/var/log/lxc/\${CONTAINER}.log}" |
31 |
eval logpriority="\${rc_${SVCNAME//./_}__logpriority:-\${rc_lxc__logpriority:-WARN}}" |
32 |
eval CONSOLELOG="\$rc_${SVCNAME//./_}__consolelog" |
33 |
|
34 |
command="/usr/sbin/lxc-start" |
35 |
command_args="--name '${CONTAINER}' --rcfile '${CONFIGFILE}' --logfile '${logfile}' --logpriority '${logpriority}' --pidfile '${pidfile}' --daemon${CONSOLELOG:+ --console-log '${CONSOLELOG}'}" |
36 |
required_files=${CONFIGFILE} |
26 |
|
37 |
|
27 |
lxc_get_net_link_type() { |
38 |
lxc_get_net_link_type() { |
28 |
awk 'BEGIN { FS="[ \t]*=[ \t]*"; _link=""; _type="" } |
39 |
awk 'BEGIN { FS="[ \t]*=[ \t]*"; _link=""; _type="" } |
29 |
$1 == "lxc.network.type" {_type=$2;} |
40 |
$1 == "lxc.network.type" {_type=$2;} |
30 |
$1 == "lxc.network.link" {_link=$2;} |
41 |
$1 == "lxc.network.link" {_link=$2;} |
31 |
{if(_link != "" && _type != ""){ |
42 |
{if(_link != "" && _type != ""){ |
32 |
printf("%s:%s\n", _link, _type ); |
43 |
printf("%s:%s\n", _link, _type ); |
33 |
_link=""; _type=""; |
44 |
_link=""; _type=""; |
34 |
}; }' <${CONFIGFILE} |
45 |
}; }' < "${CONFIGFILE}" |
35 |
} |
46 |
} |
36 |
|
47 |
|
37 |
checkconfig() { |
48 |
checkconfig() { |
38 |
if [ ${CONTAINER} = ${SVCNAME} ]; then |
49 |
if [[ "${SVCNAME}" = "${CONTAINER}" ]]; then |
39 |
eerror "You have to create an init script for each container:" |
50 |
eerror "You have to create an init script for each container:" |
40 |
eerror " ln -s lxc /etc/init.d/lxc.container" |
51 |
eerror " ln -s lxc /etc/init.d/lxc.container" |
41 |
return 1 |
52 |
return 1 |
42 |
fi |
53 |
fi |
43 |
|
54 |
local _ret=0 |
44 |
# no need to output anything, the function takes care of that. |
55 |
## protection against bad SVCNAME |
45 |
[ -z "${CONFIGFILE}" ] && return 1 |
56 |
## Since ${SVCNAME//./_} may be used in some rc_.. environment, names must be correct |
46 |
|
57 |
if [[ -n "${SVCNAME//[0-9A-Za-z.]/}" ]]; then |
47 |
utsname=$(lxc_get_var lxc.utsname) |
58 |
eerror "Bad service name '${SVCNAME}'" |
48 |
if [ ${CONTAINER} != ${utsname} ]; then |
59 |
eerror "You can not use dashes in service name" |
49 |
eerror "You should use the same name for the service and the" |
60 |
_ret=1 |
50 |
eerror "container. Right now the container is called ${utsname}" |
61 |
fi |
51 |
return 1 |
62 |
if [[ "${SVCNAME//_/}" != "${SVCNAME}" ]]; then |
52 |
fi |
63 |
ewarn "This is a bad idea to use underline in the name of container service" |
|
|
64 |
ewarn "Some environments may conflict with services like as '${SVCNAME//_/.}'" |
65 |
fi |
66 |
return $_ret |
53 |
} |
67 |
} |
54 |
|
68 |
|
55 |
depend() { |
69 |
depend() { |
56 |
# be quiet, since we have to run depend() also for the |
70 |
checkconfig &>/dev/null || return 0 |
57 |
# non-muxed init script, unfortunately. |
71 |
config "${CONFIGFILE}" |
58 |
checkconfig 2>/dev/null || return 0 |
72 |
need localmount |
59 |
|
73 |
local _x _if |
60 |
config ${CONFIGFILE} |
74 |
for _x in $(lxc_get_net_link_type); do |
61 |
need localmount |
75 |
_if=${_x%:*} |
62 |
|
76 |
case "${_x##*:}" in |
63 |
local _x _if |
77 |
# when the network type is set to phys, we can make use of a |
64 |
for _x in $(lxc_get_net_link_type); do |
78 |
# network service (for instance to set it up before we disable |
65 |
_if=${_x%:*} |
79 |
# the net_admin capability), but we might also not set it up |
66 |
case "${_x##*:}" in |
80 |
# at all on the host and leave the net_admin capable service |
67 |
# when the network type is set to phys, we can make use of a |
81 |
# to take care of it. |
68 |
# network service (for instance to set it up before we disable |
82 |
phys) use net.${_if} ;; |
69 |
# the net_admin capability), but we might also not set it up |
83 |
*) need net.${_if} ;; |
70 |
# at all on the host and leave the net_admin capable service |
84 |
esac |
71 |
# to take care of it. |
85 |
done |
72 |
phys) use net.${_if} ;; |
86 |
} |
73 |
*) need net.${_if} ;; |
87 |
|
74 |
esac |
88 |
start_pre() { |
75 |
done |
89 |
checkconfig || return 1 |
76 |
} |
90 |
if lxc-info -n "${CONTAINER}" 2>/dev/null| grep -qiE '^state:\s+stopped$'; then |
77 |
|
91 |
local _c _p= |
78 |
start() { |
92 |
for _c in ${rc_lxc__cgrouppath}/"${CONTAINER}"; do |
79 |
checkconfig || return 1 |
93 |
[ -e "$_c" ] && \ |
80 |
rm /var/log/lxc/${CONTAINER}.log |
94 |
_p="$_p '$_c'" && \ |
81 |
|
95 |
eerror "Found '$_c'. Perhaps the last time the container '${CONTAINER}' was completed with an error." |
82 |
rootpath=$(lxc_get_var lxc.rootfs) |
96 |
done |
83 |
|
97 |
[ -z "$_p" ] && \ |
84 |
# Check the format of our init and the chroot's init, to see |
98 |
checkpath -q -d "$(dirname "${pidfile}")" && \ |
85 |
# if we have to use linux32 or linux64; always use setarch |
99 |
return 0 |
86 |
# when required, as that makes it easier to deal with |
100 |
ewarn "Possibly for you can help comand: rmdir${_p}" |
87 |
# x32-based containers. |
101 |
return 1 |
88 |
case $(scanelf -BF '%a#f' ${rootpath}/sbin/init) in |
102 |
else |
89 |
EM_X86_64) setarch=linux64;; |
103 |
if lxc-info -n "${CONTAINER}" 2>/dev/null | grep -qie '^state:'; then |
90 |
EM_386) setarch=linux32;; |
104 |
eerror "Can't start ${name}. It's not sopped." |
91 |
esac |
105 |
else |
92 |
|
106 |
eerror "Can't start ${name}. It's not found." |
93 |
ebegin "Starting ${CONTAINER}" |
107 |
fi |
94 |
env -i ${setarch} $(type -p lxc-start) -l WARN -n ${CONTAINER} -f ${CONFIGFILE} -d -o /var/log/lxc/${CONTAINER}.log |
108 |
fi |
95 |
sleep 0.5 |
109 |
} |
96 |
|
110 |
|
97 |
# lxc-start -d will _always_ report a correct startup, even if it |
111 |
start_post() { |
98 |
# failed, so rather than trust that, check that the cgroup exists. |
112 |
ebegin "Try to make sure ${name} started correctly" |
99 |
[ -d /sys/fs/cgroup/cpuset/lxc/${CONTAINER} ] |
113 |
ewaitfile 15 ${rc_lxc__cgrouppath}/"${CONTAINER}" |
100 |
eend $? |
114 |
eend $? |
|
|
115 |
} |
116 |
|
117 |
_halt(){ |
118 |
eval local HALT_TIMEOUT=\${rc_${SVCNAME//./_}__halt_timeout-${rc_lxc__halt_timeout-15}} |
119 |
ebegin "Halting ${name}" |
120 |
( lxc-stop --name "${CONTAINER}" --kill --nowait & ) &>/dev/null |
121 |
lxc-wait --name "${CONTAINER}" -s STOPPED ${HALT_TIMEOUT:+-t ${HALT_TIMEOUT}} |
122 |
if eend $? "Some error on halting ${name}"; then |
123 |
mark_service_stopped "${SVCNAME}" |
124 |
else |
125 |
mark_service_stopping "${SVCNAME}" |
126 |
return 1 |
127 |
fi |
128 |
} |
129 |
|
130 |
halt() { |
131 |
service_stopped "${SVCNAME}" && ewarn "WARNING: ${SVCNAME} is already marked as stopped, but I still try to kill ${name}" |
132 |
_halt |
101 |
} |
133 |
} |
102 |
|
134 |
|
103 |
stop() { |
135 |
stop() { |
104 |
checkconfig || return 1 |
136 |
eval local STOP_PWR_TIMEOUT=\${rc_${SVCNAME//./_}__stop_power_timeout-${rc_lxc__stop_power_timeout-15}} |
|
|
137 |
eval local STOP_NOKILL_TIMEOUT=\${rc_${SVCNAME//./_}__stop_nokill_timeout-${rc_lxc__stop_nokill_timeout-15}} |
138 |
if ! service_wasinactive "${SVCNAME}"; then |
139 |
if [ -n "${STOP_PWR_TIMEOUT}" ]; then |
140 |
ebegin "Sending signal to stop ${name}" |
141 |
kill -s SIGPWR "$(lxc-info --pid --no-humanize --name "${CONTAINER}")" &>/dev/null |
142 |
lxc-wait --name "${CONTAINER}" -s STOPPED -t ${STOP_PWR_TIMEOUT} |
143 |
eend $? && return 0 |
144 |
fi |
145 |
if [ -n "${STOP_NOKILL_TIMEOUT}" ]; then |
146 |
ebegin "Try to request a clean shutdown ${name}" |
147 |
( lxc-stop --name "${CONTAINER}" --nokill & ) &>/dev/null |
148 |
lxc-wait --name "${CONTAINER}" -s STOPPED -t ${STOP_NOKILL_TIMEOUT} |
149 |
eend $? && return 0 |
150 |
fi |
151 |
fi |
152 |
_halt |
153 |
} |
154 |
|
155 |
freeze() { |
156 |
ebegin "Freeze all the processes of the container ${CONTAINER}" |
157 |
lxc-freeze --name "${CONTAINER}" |
158 |
eend $? "Problem whith freezing container ${CONTAINER}" && mark_service_inactive "${SVCNAME}" |
159 |
} |
160 |
|
161 |
unfreeze() { |
162 |
if service_inactive "${SVCNAME}"; then |
163 |
ebegin "Unfreeze container ${CONTAINER}" |
164 |
lxc-unfreeze --name "${CONTAINER}" |
165 |
eend $? "Problem whith unfreezing container ${CONTAINER}" && mark_service_started "${SVCNAME}" |
166 |
else |
167 |
eerror "${SVCNAME}: cannot \`unfreeze' as it has not been started" |
168 |
fi |
169 |
} |
105 |
|
170 |
|
|
|
171 |
info() { |
172 |
lxc-info --name "${CONTAINER}" |
173 |
} |
106 |
|
174 |
|
107 |
if ! [ -d /sys/fs/cgroup/cpuset/lxc/${CONTAINER} ]; then |
175 |
attach() { |
108 |
ewarn "${CONTAINER} doesn't seem to be started." |
176 |
lxc-attach --name "${CONTAINER}" |
109 |
return 0 |
|
|
110 |
fi |
111 |
|
112 |
init_pid=$(lxc-info -n ${CONTAINER} --pid | awk '{ print $2 }') |
113 |
|
114 |
if [ -z "${init_pid}" ]; then |
115 |
ewarn "${CONTAINER} doesn't seem to be running." |
116 |
return 0 |
117 |
fi |
118 |
|
119 |
ebegin "Shutting down system in ${CONTAINER}" |
120 |
kill -PWR ${init_pid} |
121 |
eend $? |
122 |
|
123 |
TIMEOUT=${TIMEOUT:-30} |
124 |
i=0 |
125 |
while [ -n "$(pgrep -P ${init_pid})" -a $i -lt ${TIMEOUT} ]; do |
126 |
sleep 1 |
127 |
i=$(expr $i + 1) |
128 |
done |
129 |
|
130 |
if [ -n "${missingprocs}" ]; then |
131 |
ewarn "Something failed to properly shut down in ${CONTAINER}" |
132 |
fi |
133 |
|
134 |
ebegin "Stopping ${CONTAINER}" |
135 |
lxc-stop -n ${CONTAINER} |
136 |
eend $? |
137 |
} |
177 |
} |