#!/sbin/runscript # Copyright 1999-2012 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 # $Header: /var/cvsroot/gentoo-x86/app-emulation/lxc/files/lxc.initd.2,v 1.5 2012/07/21 05:07:15 flameeyes Exp $ CONTAINER=${SVCNAME#*.} lxc_get_configfile() { if [ -f "/etc/lxc/${CONTAINER}.conf" ]; then echo "/etc/lxc/${CONTAINER}.conf" elif [ -f "/etc/lxc/${CONTAINER}/config" ]; then echo "/etc/lxc/${CONTAINER}/config" else eerror "Unable to find a suitable configuration file." eerror "If you set up the container in a non-standard" eerror "location, please set the CONFIGFILE variable." return 1 fi } [ $CONTAINER != $SVCNAME ] && CONFIGFILE=${CONFIGFILE:-$(lxc_get_configfile)} lxc_get_var() { awk 'BEGIN { FS="[ \t]*=[ \t]*" } $1 == "'$1'" { print $2; exit }' ${CONFIGFILE} } lxc_try_script() { #Try to execute {pre,post}{up/down} script SCRIPT_TYPE=$1 SCRIPT_NAME="" if [ -f "/etc/lxc/${CONTAINER}.${SCRIPT_TYPE}" ]; then SCRIPT_NAME="/etc/lxc/${CONTAINER}.${SCRIPT_TYPE}" elif [ -f "/etc/lxc/${CONTAINER}/${SCRIPT_TYPE}" ]; then SCRIPT_NAME="/etc/lxc/${CONTAINER}/${SCRIPT_TYPE}" fi if [ "${SCRIPT_NAME}" != "" ] then ebegin "Starting ${SCRIPT_NAME}" ${SCRIPT_NAME} eend $? fi } checkconfig() { if [ ${CONTAINER} = ${SVCNAME} ]; then eerror "You have to create an init script for each container:" eerror " ln -s lxc /etc/init.d/lxc.container" return 1 fi # no need to output anything, the function takes care of that. [ -z "${CONFIGFILE}" ] && return 1 utsname=$(lxc_get_var lxc.utsname) if [ ${CONTAINER} != ${utsname} ]; then eerror "You should use the same name for the service and the" eerror "container. Right now the container is called ${utsname}" return 1 fi } depend() { # be quiet, since we have to run depend() also for the # non-muxed init script, unfortunately. checkconfig 2>/dev/null || return 0 config ${CONFIGFILE} need localmount # find out which network interface the container is linked to, # and then require that to be enabled, so that the # dependencies are correct. netif=$(lxc_get_var lxc.network.link) # when the network type is set to phys, we can make use of a # network service (for instance to set it up before we disable # the net_admin capability), but we might also not set it up # at all on the host and leave the net_admin capable service # to take care of it. nettype=$(lxc_get_var lxc.network.type) if [ -n "${netif}" ]; then case "${nettype}" in phys) use net.${netif} ;; *) need net.${netif} ;; esac fi } start() { checkconfig || return 1 rm /var/log/lxc/${CONTAINER}.log rootpath=$(lxc_get_var lxc.rootfs) # Check the format of our init and the chroot's init, to see # if we have to use linux32 or linux64; always use setarch # when required, as that makes it easier to deal with # x32-based containers. case $(scanelf -BF '%a#f' ${rootpath}/sbin/init) in EM_X86_64) setarch=linux64;; EM_I386) setarch=linux32;; esac lxc_try_script preup ebegin "Starting ${CONTAINER}" env -i ${setarch} $(type -p lxc-start) -l WARN -n ${CONTAINER} -f ${CONFIGFILE} -d -o /var/log/lxc/${CONTAINER}.log sleep 0.5 # lxc-start -d will _always_ report a correct startup, even if it # failed, so rather than trust that, check that the cgroup exists. [ -d /sys/fs/cgroup/cpuset/lxc/${CONTAINER} ] started=$? eend ${started} [[ ${started} == 0 ]] && lxc_try_script postup } stop() { checkconfig || return 1 if ! [ -d /sys/fs/cgroup/cpuset/lxc/${CONTAINER} ]; then ewarn "${CONTAINER} doesn't seem to be started." return 0 fi init_pid=$(lxc-info -n ${CONTAINER} --pid | awk '{ print $2 }') if [ "${init_pid}" = "-1" ]; then ewarn "${CONTAINER} doesn't seem to be running." return 0 fi lxc_try_script predown ebegin "Shutting down system in ${CONTAINER}" kill -PWR ${init_pid} eend $? TIMEOUT=${TIMEOUT:-30} i=0 while [ -n "$(pgrep -P ${init_pid})" -a $i -lt ${TIMEOUT} ]; do sleep 1 i=$(expr $i + 1) done if [ -n "${missingprocs}" ]; then ewarn "Something failed to properly shut down in ${CONTAINER}" fi ebegin "Stopping ${CONTAINER}" lxc-stop -n ${CONTAINER} stopped=$? [[ ${stopped} == 0 ]] && lxc_try_script postdown eend ${stopped} }