[Lxc-users] How to make a container init DIE after finishing runlevel 0

Daniel Lezcano daniel.lezcano at free.fr
Mon Jan 25 23:42:13 UTC 2010


Michael H. Warfield wrote:
> On Mon, 2010-01-25 at 21:50 +0100, Daniel Lezcano wrote:
>
>   
>>> apologies for the length, but how is everyone else handling this?
>>> this is the last thing i need to solve before i actually start running
>>> all my services on this setup.
>>>   
>>>       
>> I was wondering if the kernel shouldn't send a signal to the init's 
>> parent when sys_reboot is called.
>>     
>
> Which still leaves open the question of telling the difference between a
> halt and a reboot.  My trick of using the final runlevel
> in /var/run/utmp ran a foul over a gotcha in the Debian containers that
> they seem to default to mounting tmpfs over /var/run and /var/lock so
> you loose that information.  I had to disable "RAMRUN" and "RAMLOCK"
> in /etc/default/rcS in the debian images to get around that but I'm not
> sure I'm happy doing that.  The alternative examining /var/log/wtmp
> didn't work out as reliable.  OpenVZ has a similar problem and it writes
> a "reboot" file that can be read but that seems inelegant as well.  I
> don't think anything works if someone does a "reboot -f" but I need to
> test that condition yet.
>
> To also answer the OP's question.  Here's what I use.  I have a script
> that runs periodically in the host.  If the number of processes in a
> running container is 1, then I check for the runlevel in
> ${rootfs}/var/run/utmp.  If that's "? 0" then it's a halt and I run
> lxc-stop.  If it's "? 6" then it's a reboot and I stop the container and
> then restart it.  I run it every 5 minutes or so or manually.  It runs
> fast and could be run more often.  Just sucks polling things like that,
> though.  That script, lxc-check, is attached.
>   

I trick I just found:

 while $(true); do
    inotifywait /var/lib/lxc/debian/rootfs/var/run/utmp;
    if [ "$(wc -l /cgroup/debian/tasks | awk '{ print $1 }')" = "1" ]; then
        lxc-stop -n debian
    fi;
done

This command can stay always there and it will trigger a lxc-stop when 
the container remains with 1 process.
No polling and immediate :)
At the first glance, it seems to work well, but of course not compatible 
with upstart.

linux-owop:~ # lxc-start -n debian && reset; echo 
"######################## exited ##########################"
SELinux:  Could not open policy file <= 
/etc/selinux/targeted/policy/policy.24:
 No such file or directory
INIT: version 2.86 booting
Activating swap...done.
Cleaning up ifupdown....
Loading kernel modules...FATAL: Could not load 
/lib/modules/2.6.32-mcr-3.18/modu
les.dep: No such file or directory
Checking file systems...fsck 1.41.3 (12-Oct-2008)
done.
Setting kernel variables (/etc/sysctl.conf)...done.
Mounting local filesystems...done.
Activating swapfile swap...done.
Setting up networking....
Configuring network interfaces...Internet Systems Consortium DHCP Client 
V3.1.1
Copyright 2004-2008 Internet Systems Consortium.
All rights reserved.
For info, please visit http://www.isc.org/sw/dhcp/

Listening on LPF/eth0/d6:c8:78:f7:09:12
Sending on   LPF/eth0/d6:c8:78:f7:09:12
Sending on   Socket/fallback
DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 7
DHCPOFFER from 172.20.0.1
DHCPREQUEST on eth0 to 255.255.255.255 port 67
DHCPACK from 172.20.0.1
bound to 172.20.0.10 -- renewal in 34492 seconds.
done.
INIT: Entering runlevel: 3
Starting OpenBSD Secure Shell server: sshd.

Debian GNU/Linux 5.0 debian console

debian login: root
Last login: Mon Jan 25 23:28:34 UTC 2010 on console
Linux debian 2.6.32-mcr-3.18 #19 Mon Jan 25 11:19:47 CET 2010 x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
debian:~# poweroff

Broadcast message from root at debian (console) (Mon Jan 25 23:32:07 2010):

The system is going down for system halt NOW!
INIT: Switching to runlevel: 0
INIT: Sending processes the TERM signal
debian:~# Asking all remaining processes to terminate...done.
Killing all remaining processes...failed.
Deconfiguring network interfaces...There is already a pid file 
/var/run/dhclient.eth0.pid with pid 187
removed stale PID file
Internet Systems Consortium DHCP Client V3.1.1
Copyright 2004-2008 Internet Systems Consortium.
All rights reserved.
For info, please visit http://www.isc.org/sw/dhcp/

Listening on LPF/eth0/d6:c8:78:f7:09:12
Sending on   LPF/eth0/d6:c8:78:f7:09:12
Sending on   Socket/fallback
DHCPRELEASE on eth0 to 172.20.0.1 port 67
done.
Cleaning up ifupdown....
mount: / is busy
Will now halt.
INIT: no more processes left in this runlevel
######################## exited ##########################
linux-owop:~ #












More information about the lxc-users mailing list