[Lxc-users] lxc-start-ephemeral + upstart = pain?

Dan Kegel dank at kegel.com
Fri Oct 5 16:06:08 UTC 2012


On Fri, Oct 5, 2012 at 7:21 AM, Serge Hallyn <serge.hallyn at canonical.com> wrote:
>> Might be able to work around this by wrapping lxc-start-ephemeral in a
>> program which forks off the script, then sits waiting for signals
>> (SIGCHILD or SIGINT), and tell upstart to send its signals to the
>> wrapper.
>> I wonder if the Python version of lxc-start-ephemeral might help here.
>>  Or is sh able to do what I need with the proper use of & and friends?
>
> Just thinking out loud - what about creating a wrapper script which
> traps SIGUSR1, having it start your ephemeral container and then lxc-wait
> on it to be first RUNNING then STOPPED, have the upstart instance job
> leave command running int he foreground, have it 'kill signal SIGUSR1',
> and then have the script lxc-stop when it gets SIGUSR1?

I think the showstopper with doing this in a shell script is
that they can't quite wait for both a child process to finish
and listen for signals at the same time.
Unless you turn on job control, you can't trap SIGCHLD (and even
then you get a trap for each process that exits).

The next most universal language is Perl, but if lxc-start-ephemeral
is now written in Python, the most logical thing to do is build
this SIGTERM listening right into that script.

But I'm ok with the coward's way out for now, letting lxc-start-ephemeral
run in the foreground, and stopping it via lxc-stop in a pre-stop stanza.
- Dan

p.s. Here's a broken draft that can't work.

#!/bin/sh
# Upstart wrapper for lxc-start-ephemeral
#
# Upstart requires that jobs it starts listen for signals on the original
# process started ( http://upstart.ubuntu.com/cookbook/#expect ).
# Signals used are:
#  SIGTERM - orderly shutdown (
http://upstart.ubuntu.com/cookbook/#kill-signal )
#  SIGKILL - if the process ignores SIGTERM too long
(http://upstart.ubuntu.com/cookbook/#kill-timeout)
#  SIGHUP  - reload configuration (
http://upstart.ubuntu.com/cookbook/#initctl-reload )
#
# lxc-start-ephemeral does lots of forking, and doesn't listen for
signals, hence this wrapper.

do_shutdown() {
    # SIGTERM recieved, shut down container
    sudo lxc-stop -n $LXC_NAME
}

do_done() {
    # container exited, break out of main loop
    exit
}

do_start() {
    lxc-start-ephemeral "$@"
}

trap do_shutdown 15  # SIGTERM
trap do_done 17  # SIGCHLD    -- fails unless job control turned on!

do_start &

while sleep 1
do
    # Every time sleep returns, the shell will check for signals,
    # and will execute do_shutdown if SIGTERM is received.
    # This adds one second of latency to shutdown.
    # Other languages may be better at this.
    :
done




More information about the lxc-users mailing list