I have been playing with Linux containers for a while, and finally I decided to take a plunge and migrate one of my servers from OpenVZ to lxc. It worked quite well for testing until I noticed lack of support for automatic startup or shutdown. lxc-users mailing list was helpful in providing useful hints and I found 5 OpenSUSE scripts but decided it's too complicated for task at hand.
I wanted single script, for easy deployment on any Debian box, with following features:
- reboot and halt from inside container should work as expected
- cleanly shutdown or reboot container from host (as opposed to lxc-stop which is equivalent to turning power off)
- init script to start and stop containers on host boot and shutdown
Result is lxc-watchdog.sh. It can start containers (and remember to start it on next host reboot), reboot or halt containers from host (using signals to trigger container's init) and automatically configure your container when you start it for the first time. Here is quick overview:
root@prod:/srv# ./lxc-watchdog.sh status koha-240 RUNNING boot /virtual/koha-240 koha-241 STOPPED boot /virtual/koha-241 koha-242 STOPPED /virtual/koha-242 root@prod:/srv# ./lxc-watchdog.sh start # start koha-240 'koha-240' is RUNNING # start koha-241 2010-03-16T23:44:16 koha-241 start 'koha-241' is RUNNING # skip start koha-242 root@prod:/srv# ./lxc-watchdog.sh status koha-240 RUNNING boot /virtual/koha-240 koha-241 RUNNING boot /virtual/koha-241 koha-242 STOPPED /virtual/koha-242 root@prod:/srv# ls -al /var/lib/lxc/*/on_boot -rw-r--r-- 1 root root 9 2010-03-16 21:40 /var/lib/lxc/koha-240/on_boot -rw-r--r-- 1 root root 9 2010-03-16 21:40 /var/lib/lxc/koha-241/on_boot -rw-r--r-- 1 root root 0 2010-03-16 22:58 /var/lib/lxc/koha-242/on_bootAs you can see, I used file /var/lib/lxc/name/on_boot to record which machines to bring up. When container is started for the first time, it will have boot enabled (just in case this is production application which you will reboot in 6 months and then wonder why it doesn't work). You can change boot status using:
root@prod:/srv# ./lxc-watchdog.sh boot koha-242 # boot koha-242 root@prod:/srv# ./lxc-watchdog.sh status koha-240 RUNNING boot /virtual/koha-240 koha-241 RUNNING boot /virtual/koha-241 koha-242 STOPPED boot /virtual/koha-242 root@prod:/srv# ./lxc-watchdog.sh disable koha-242 # disable koha-242Installation as init script /etc/init.d/lxc-watchdog is easy:
root@prod:/srv# ln -s /srv/lxc-watchdog.sh /etc/init.d/lxc-watchdog root@prod:/srv# update-rc.d lxc-watchdog defaults update-rc.d: using dependency based boot sequencingAnd finally, it can also be used to manually start, halt or reboot containers:
root@prod:/srv# /etc/init.d/lxc-watchdog start koha-242 # start koha-242 2010-03-16T23:47:46 koha-242 start 'koha-242' is RUNNING root@prod:/srv# /etc/init.d/lxc-watchdog status koha-240 RUNNING boot /virtual/koha-240 koha-241 RUNNING boot /virtual/koha-241 koha-242 RUNNING /virtual/koha-242 root@prod:/srv# /etc/init.d/lxc-watchdog restart koha-242 # restart koha-242 2010-03-16T23:48:46 koha-242 kill -SIGINT 24838 root@prod:/srv# /etc/init.d/lxc-watchdog status koha-240 RUNNING boot /virtual/koha-240 koha-241 RUNNING boot /virtual/koha-241 koha-242 RUNNING /virtual/koha-242 root@prod:/srv# /etc/init.d/lxc-watchdog stop koha-242 # stop koha-242 2010-03-16T23:49:55 koha-242 stop 2010-03-16T23:49:55 koha-242 kill -SIGPWR 26086 2010-03-16T23:50:11 koha-242 stopedIn fact, you can use halt or reboot if you don't like stop and restart, just to keep one mapping less in your brain when working with it.
Log files are created for each container in /tmp/name.log. They include lxc-start output with boot messages and any output that started scripts might create which is useful for debugging container installations. Output from watchdog monitoring /var/run/utmp in container is also included, and it reports number of tasks (processes) in container, and here is example of stopping container:
root@prod:/srv# tail -5 /tmp/koha-242.log 2010-03-16T23:49:56 koha-242 66 tasks 2010-03-16T23:50:04 koha-242 22 tasks 2010-03-16T23:50:11 koha-242 runlevel 2 0 2010-03-16T23:50:11 koha-242 halt 2010-03-16T23:50:12 koha-242 watchdog exitedHopefully this will make your switch to Linux Containers and recent kernels easier...