--- /usr/portage/app-emulation/lxc/files/lxc.initd.3 2015-06-14 14:42:16.479272827 +0000 +++ lxc.initd.4 2015-01-11 15:55:42.000000000 +0000 @@ -1,139 +1,177 @@ #!/sbin/runscript -# Copyright 1999-2015 Gentoo Foundation +# Copyright 1999-2014 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 -# $Header: /var/cvsroot/gentoo-x86/app-emulation/lxc/files/lxc.initd.3,v 1.3 2015/06/13 11:54:25 hwoarang Exp $ +# $Header: $ +#_name=${SVCNAME#*.} CONTAINER=${SVCNAME#*.} - -LXC_PATH=`lxc-config lxc.lxcpath` - -lxc_get_configfile() { - if [ -f "${LXC_PATH}/${CONTAINER}.conf" ]; then - echo "${LXC_PATH}/${CONTAINER}.conf" - elif [ -f "${LXC_PATH}/${CONTAINER}/config" ]; then - echo "${LXC_PATH}/${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} -} +name="Linux Container ${CONTAINER}" +extra_commands="info halt unfreeze" +extra_started_commands="freeze attach" + +description="Service to start Linux Container" +description_info="Get info about container" +description_halt="Forse to killing container" +description_freeze="Freeze running container" +description_unfreeze="Unfreeze freezed container" +description_attach="Start a shell inside a running container" + +## if necessary, you can manually set the value of these variables in the /etc/rc.conf or /etc/conf.d/lxc +## be sure when you change this variables +: ${rc_lxc__cgrouppath:=/sys/fs/cgroup/*/lxc} +# to avoid situation like as this https://bugs.gentoo.org/show_bug.cgi?id=416643 +: ${rc_lxc__lxcpath:=$(lxc-config lxc.lxcpath)} +## https://bugs.gentoo.org/show_bug.cgi?id=525956 +[ -f "${rc_lxc__lxcpath}/${CONTAINER}.conf" ] && \ + CONFIGFILE="${rc_lxc__lxcpath}/${CONTAINER}.conf" || \ + CONFIGFILE="${rc_lxc__lxcpath}/${CONTAINER}/config" + +eval pidfile="\${rc_${SVCNAME//./_}__pidfile:-/run/lxc/\${CONTAINER}.pid}" +eval logfile="\${rc_${SVCNAME//./_}__logfile:-/var/log/lxc/\${CONTAINER}.log}" +eval logpriority="\${rc_${SVCNAME//./_}__logpriority:-\${rc_lxc__logpriority:-WARN}}" +eval CONSOLELOG="\$rc_${SVCNAME//./_}__consolelog" + +command="/usr/sbin/lxc-start" +command_args="--name '${CONTAINER}' --rcfile '${CONFIGFILE}' --logfile '${logfile}' --logpriority '${logpriority}' --pidfile '${pidfile}' --daemon${CONSOLELOG:+ --console-log '${CONSOLELOG}'}" +required_files=${CONFIGFILE} lxc_get_net_link_type() { - awk 'BEGIN { FS="[ \t]*=[ \t]*"; _link=""; _type="" } - $1 == "lxc.network.type" {_type=$2;} - $1 == "lxc.network.link" {_link=$2;} - {if(_link != "" && _type != ""){ - printf("%s:%s\n", _link, _type ); - _link=""; _type=""; - }; }' <${CONFIGFILE} + awk 'BEGIN { FS="[ \t]*=[ \t]*"; _link=""; _type="" } + $1 == "lxc.network.type" {_type=$2;} + $1 == "lxc.network.link" {_link=$2;} + {if(_link != "" && _type != ""){ + printf("%s:%s\n", _link, _type ); + _link=""; _type=""; + }; }' < "${CONFIGFILE}" } 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 + if [[ "${SVCNAME}" = "${CONTAINER}" ]]; then + eerror "You have to create an init script for each container:" + eerror " ln -s lxc /etc/init.d/lxc.container" + return 1 + fi + local _ret=0 + ## protection against bad SVCNAME + ## Since ${SVCNAME//./_} may be used in some rc_.. environment, names must be correct + if [[ -n "${SVCNAME//[0-9A-Za-z.]/}" ]]; then + eerror "Bad service name '${SVCNAME}'" + eerror "You can not use dashes in service name" + _ret=1 + fi + if [[ "${SVCNAME//_/}" != "${SVCNAME}" ]]; then + ewarn "This is a bad idea to use underline in the name of container service" + ewarn "Some environments may conflict with services like as '${SVCNAME//_/.}'" + fi + return $_ret } 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 - - local _x _if - for _x in $(lxc_get_net_link_type); do - _if=${_x%:*} - case "${_x##*:}" in - # 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. - phys) use net.${_if} ;; - *) need net.${_if} ;; - esac - done -} - -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_386) setarch=linux32;; - esac - - 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} ] - eend $? + checkconfig &>/dev/null || return 0 + config "${CONFIGFILE}" + need localmount + local _x _if + for _x in $(lxc_get_net_link_type); do + _if=${_x%:*} + case "${_x##*:}" in + # 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. + phys) use net.${_if} ;; + *) need net.${_if} ;; + esac + done +} + +start_pre() { + checkconfig || return 1 + if lxc-info -n "${CONTAINER}" 2>/dev/null| grep -qiE '^state:\s+stopped$'; then + local _c _p= + for _c in ${rc_lxc__cgrouppath}/"${CONTAINER}"; do + [ -e "$_c" ] && \ + _p="$_p '$_c'" && \ + eerror "Found '$_c'. Perhaps the last time the container '${CONTAINER}' was completed with an error." + done + [ -z "$_p" ] && \ + checkpath -q -d "$(dirname "${pidfile}")" && \ + return 0 + ewarn "Possibly for you can help comand: rmdir${_p}" + return 1 + else + if lxc-info -n "${CONTAINER}" 2>/dev/null | grep -qie '^state:'; then + eerror "Can't start ${name}. It's not sopped." + else + eerror "Can't start ${name}. It's not found." + fi + fi +} + +start_post() { + ebegin "Try to make sure ${name} started correctly" + ewaitfile 15 ${rc_lxc__cgrouppath}/"${CONTAINER}" + eend $? +} + +_halt(){ + eval local HALT_TIMEOUT=\${rc_${SVCNAME//./_}__halt_timeout-${rc_lxc__halt_timeout-15}} + ebegin "Halting ${name}" + ( lxc-stop --name "${CONTAINER}" --kill --nowait & ) &>/dev/null + lxc-wait --name "${CONTAINER}" -s STOPPED ${HALT_TIMEOUT:+-t ${HALT_TIMEOUT}} + if eend $? "Some error on halting ${name}"; then + mark_service_stopped "${SVCNAME}" + else + mark_service_stopping "${SVCNAME}" + return 1 + fi +} + +halt() { + service_stopped "${SVCNAME}" && ewarn "WARNING: ${SVCNAME} is already marked as stopped, but I still try to kill ${name}" + _halt } stop() { - checkconfig || return 1 + eval local STOP_PWR_TIMEOUT=\${rc_${SVCNAME//./_}__stop_power_timeout-${rc_lxc__stop_power_timeout-15}} + eval local STOP_NOKILL_TIMEOUT=\${rc_${SVCNAME//./_}__stop_nokill_timeout-${rc_lxc__stop_nokill_timeout-15}} + if ! service_wasinactive "${SVCNAME}"; then + if [ -n "${STOP_PWR_TIMEOUT}" ]; then + ebegin "Sending signal to stop ${name}" + kill -s SIGPWR "$(lxc-info --pid --no-humanize --name "${CONTAINER}")" &>/dev/null + lxc-wait --name "${CONTAINER}" -s STOPPED -t ${STOP_PWR_TIMEOUT} + eend $? && return 0 + fi + if [ -n "${STOP_NOKILL_TIMEOUT}" ]; then + ebegin "Try to request a clean shutdown ${name}" + ( lxc-stop --name "${CONTAINER}" --nokill & ) &>/dev/null + lxc-wait --name "${CONTAINER}" -s STOPPED -t ${STOP_NOKILL_TIMEOUT} + eend $? && return 0 + fi + fi + _halt +} + +freeze() { + ebegin "Freeze all the processes of the container ${CONTAINER}" + lxc-freeze --name "${CONTAINER}" + eend $? "Problem whith freezing container ${CONTAINER}" && mark_service_inactive "${SVCNAME}" +} + +unfreeze() { + if service_inactive "${SVCNAME}"; then + ebegin "Unfreeze container ${CONTAINER}" + lxc-unfreeze --name "${CONTAINER}" + eend $? "Problem whith unfreezing container ${CONTAINER}" && mark_service_started "${SVCNAME}" + else + eerror "${SVCNAME}: cannot \`unfreeze' as it has not been started" + fi +} +info() { + lxc-info --name "${CONTAINER}" +} - 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 [ -z "${init_pid}" ]; then - ewarn "${CONTAINER} doesn't seem to be running." - return 0 - fi - - 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} - eend $? +attach() { + lxc-attach --name "${CONTAINER}" }