<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
</head>
<body text="#000000" bgcolor="#ffffff">
<br>
What is the best method for gracefully shutting down LXC
containers in a production environment? <br>
<br>
By graceful, I mean that apps such as databases get a shutdown
signal, so they can save their data to disk, complete any pending
network ops, flush buffers, close filehandles, etc. without data
loss.<br>
<br>
Presently, the script /etc/init.d/lxc that ships for Ubuntu just
does an lxc-stop on any container listed in /etc/default/lxc. Since
that is like "pulling the power cord", that seems like an
irresponsible and dangerous thing to do. It also does not handle
LXC containers not listed in /etc/default/lxc. It needs to be
fixed.<br>
<br>
There is an RPM package for OpenSuse called rclxc at
<a class="moz-txt-link-freetext" href="http://download.opensuse.org/repositories/home:/aljex/">http://download.opensuse.org/repositories/home:/aljex/</a> which has an
init script for LXC. It uses the following technique:<br>
<br>
<tt>lxcstop () {<br>
typeset -i PID=0<br>
lxc-ps -- -C init -o pid |while read CN PID ;do<br>
[[ $PID -gt 1 ]] || continue<br>
[[ "${1:-$CN}" = "$CN" ]] || continue<br>
grep -q 'p0::powerfail:/sbin/init 0'
${LXC_SRV}/${CN}/etc/inittab || continue<br>
kill -PWR $PID<br>
done<br>
}<br>
</tt><br>
It sends a SIGPWR (after kindly checking .../etc/inittab to make
sure init will handle it). It uses lxc-ps to find the PID of the
init process first.<br>
<br>
The Python script posted yesterday has its own technique. It
searches /proc/CONTAINER_PIDs/exe for a link to "/sbin/init", and
then sends a SIGINT to those. That seems like a reasonable
approach, but all of the Ubuntu init scripts are /bin/sh shell
scripts, not Python scripts.<br>
<br>
There is also an init script at <a class="moz-txt-link-freetext" href="http://lxc.teegra.net/">http://lxc.teegra.net/</a> for Arch
Linux, but as the page says, "... this one is quite simplistic and
does not invoke <strong><tt>shutdown</tt></strong>/<strong><tt>halt</tt></strong>
or <strong><tt>init 0</tt></strong> in the containers. Also, it
might hang on waiting for a container to start." Like the Ubuntu
script, it just calls lxc-stop, i.e., pulls the power cable on your
containers. Not graceful.<br>
<br>
Several of the other scripts or tutorials I found are also
outdated or incomplete. For example, many still recommend running
the container using "screen", from before the lxc-start -d option
was available.<br>
<br>
As an alternate approach, what about running:<br>
<br>
lxc-attach -n CONTAINER shutdown -h now<br>
<br>
Is there any drawback to doing that, instead? The Python script
and the OpenSuse init script mentioned above both need root access,
but using lxc-attach (instead) would theoretically work once the
User Namespaces are fully implemented.<br>
<br>
Other considerations for a production-quality script:<br>
<br>
1. A watchdog timeout, so that if a process hangs during shutdown,
eventually lxc-stop would get called anyway. (A broken LXC process
should not prevent a host O.S. shutdown!) Could a timeout option be
added to lxc-wait for this feature?<br>
<br>
2. A method that does not require root, the way virsh does not
require root to start or stop a VM. (Maybe this needs to wait.)<br>
<br>
3. An "official" command name for graceful shutdowns from the host.
I propose lxc-shutdown. (There is an unofficial OpenSuse package
from rdannert that has a "lxc-shutdown-all" command, but I have not
seen the name "lxc-shutdown" used anywhere.)<br>
<br>
4. Which signal? SIGINT? SIGPWR? Both?<br>
<br>
<br>
I am looking to put some development and testing into this. If
readers would kindly post their own "best practices", I could create
a new lxc-shutdown command and an init script that uses it.<br>
<br>
<br>
Thank You,<br>
Derek Simkowiak<br>
<br>
P.S.> The last major discussion I found about this was from ~two
years ago:<br>
<a class="moz-txt-link-freetext" href="http://www.mail-archive.com/lxc-users@lists.sourceforge.net/msg00040.html">http://www.mail-archive.com/lxc-users@lists.sourceforge.net/msg00040.html</a><br>
<br>
<br>
</body>
</html>