| 1 |
#!/sbin/runscript |
| 2 |
# Copyright 1999-2012 Gentoo Foundation |
| 3 |
# Distributed under the terms of the GNU General Public License v2 |
| 4 |
# $Header: /var/cvsroot/gentoo-x86/app-emulation/lxc/files/lxc.initd.2,v 1.5 2012/07/21 05:07:15 flameeyes Exp $ |
| 5 |
|
| 6 |
CONTAINER=${SVCNAME#*.} |
| 7 |
|
| 8 |
lxc_get_configfile() { |
| 9 |
if [ -f "/etc/lxc/${CONTAINER}.conf" ]; then |
| 10 |
echo "/etc/lxc/${CONTAINER}.conf" |
| 11 |
elif [ -f "/etc/lxc/${CONTAINER}/config" ]; then |
| 12 |
echo "/etc/lxc/${CONTAINER}/config" |
| 13 |
else |
| 14 |
eerror "Unable to find a suitable configuration file." |
| 15 |
eerror "If you set up the container in a non-standard" |
| 16 |
eerror "location, please set the CONFIGFILE variable." |
| 17 |
return 1 |
| 18 |
fi |
| 19 |
} |
| 20 |
|
| 21 |
[ $CONTAINER != $SVCNAME ] && CONFIGFILE=${CONFIGFILE:-$(lxc_get_configfile)} |
| 22 |
|
| 23 |
lxc_get_var() { |
| 24 |
awk 'BEGIN { FS="[ \t]*=[ \t]*" } $1 == "'$1'" { print $2; exit }' ${CONFIGFILE} |
| 25 |
} |
| 26 |
|
| 27 |
checkconfig() { |
| 28 |
if [ ${CONTAINER} = ${SVCNAME} ]; then |
| 29 |
eerror "You have to create an init script for each container:" |
| 30 |
eerror " ln -s lxc /etc/init.d/lxc.container" |
| 31 |
return 1 |
| 32 |
fi |
| 33 |
|
| 34 |
# no need to output anything, the function takes care of that. |
| 35 |
[ -z "${CONFIGFILE}" ] && return 1 |
| 36 |
|
| 37 |
utsname=$(lxc_get_var lxc.utsname) |
| 38 |
if [ ${CONTAINER} != ${utsname} ]; then |
| 39 |
eerror "You should use the same name for the service and the" |
| 40 |
eerror "container. Right now the container is called ${utsname}" |
| 41 |
return 1 |
| 42 |
fi |
| 43 |
} |
| 44 |
|
| 45 |
depend() { |
| 46 |
# be quiet, since we have to run depend() also for the |
| 47 |
# non-muxed init script, unfortunately. |
| 48 |
checkconfig 2>/dev/null || return 0 |
| 49 |
|
| 50 |
config ${CONFIGFILE} |
| 51 |
need localmount |
| 52 |
|
| 53 |
# find out which network interface the container is linked to, |
| 54 |
# and then require that to be enabled, so that the |
| 55 |
# dependencies are correct. |
| 56 |
netif=$(lxc_get_var lxc.network.link) |
| 57 |
|
| 58 |
# when the network type is set to phys, we can make use of a |
| 59 |
# network service (for instance to set it up before we disable |
| 60 |
# the net_admin capability), but we might also not set it up |
| 61 |
# at all on the host and leave the net_admin capable service |
| 62 |
# to take care of it. |
| 63 |
nettype=$(lxc_get_var lxc.network.type) |
| 64 |
|
| 65 |
if [ -n "${netif}" ]; then |
| 66 |
case "${nettype}" in |
| 67 |
phys) use net.${netif} ;; |
| 68 |
*) need net.${netif} ;; |
| 69 |
esac |
| 70 |
fi |
| 71 |
} |
| 72 |
|
| 73 |
start() { |
| 74 |
checkconfig || return 1 |
| 75 |
rm /var/log/lxc/${CONTAINER}.log |
| 76 |
|
| 77 |
rootpath=$(lxc_get_var lxc.rootfs) |
| 78 |
|
| 79 |
# Check the format of our init and the chroot's init, to see |
| 80 |
# if we have to use linux32 or linux64; always use setarch |
| 81 |
# when required, as that makes it easier to deal with |
| 82 |
# x32-based containers. |
| 83 |
case $(scanelf -BF '%a#f' ${rootpath}/sbin/init) in |
| 84 |
EM_X86_64) setarch=linux64;; |
| 85 |
EM_386) setarch=linux32;; |
| 86 |
esac |
| 87 |
|
| 88 |
ebegin "Starting ${CONTAINER}" |
| 89 |
env -i ${setarch} $(type -p lxc-start) -l WARN -n ${CONTAINER} -f ${CONFIGFILE} -d -o /var/log/lxc/${CONTAINER}.log |
| 90 |
sleep 0.5 |
| 91 |
|
| 92 |
# lxc-start -d will _always_ report a correct startup, even if it |
| 93 |
# failed, so rather than trust that, check that the cgroup exists. |
| 94 |
[ -d /sys/fs/cgroup/cpuset/lxc/${CONTAINER} ] |
| 95 |
eend $? |
| 96 |
} |
| 97 |
|
| 98 |
stop() { |
| 99 |
checkconfig || return 1 |
| 100 |
|
| 101 |
|
| 102 |
if ! [ -d /sys/fs/cgroup/cpuset/lxc/${CONTAINER} ]; then |
| 103 |
ewarn "${CONTAINER} doesn't seem to be started." |
| 104 |
return 0 |
| 105 |
fi |
| 106 |
|
| 107 |
init_pid=$(lxc-info -n ${CONTAINER} --pid | awk '{ print $2 }') |
| 108 |
|
| 109 |
if [ "${init_pid}" = "-1" ]; then |
| 110 |
ewarn "${CONTAINER} doesn't seem to be running." |
| 111 |
return 0 |
| 112 |
fi |
| 113 |
|
| 114 |
ebegin "Shutting down system in ${CONTAINER}" |
| 115 |
kill -PWR ${init_pid} |
| 116 |
eend $? |
| 117 |
|
| 118 |
TIMEOUT=${TIMEOUT:-30} |
| 119 |
i=0 |
| 120 |
while [ -n "$(pgrep -P ${init_pid})" -a $i -lt ${TIMEOUT} ]; do |
| 121 |
sleep 1 |
| 122 |
i=$(expr $i + 1) |
| 123 |
done |
| 124 |
|
| 125 |
if [ -n "${missingprocs}" ]; then |
| 126 |
ewarn "Something failed to properly shut down in ${CONTAINER}" |
| 127 |
fi |
| 128 |
|
| 129 |
ebegin "Stopping ${CONTAINER}" |
| 130 |
lxc-stop -n ${CONTAINER} |
| 131 |
eend $? |
| 132 |
} |