1 |
#!/bin/bash |
2 |
|
3 |
# based on Tony Risinger code from lxc-users |
4 |
# http://www.mail-archive.com/lxc-users@lists.sourceforge.net/msg00074.html |
5 |
|
6 |
which inotifywait >/dev/null || apt-get install inotify-tools |
7 |
|
8 |
name=$1 |
9 |
|
10 |
if [ ! -e /var/lib/lxc/$name/config ] ; then |
11 |
echo "Usage: $0 name" |
12 |
ls /var/lib/lxc/*/config | cut -d/ -f5 |
13 |
exit 1 |
14 |
fi |
15 |
|
16 |
rootfs=`grep lxc.rootfs /var/lib/lxc/$name/config | cut -d= -f2` |
17 |
echo "$name rootfs $rootfs" |
18 |
|
19 |
# cleanup init scripts which don't work in containers |
20 |
ls $rootfs/etc/rc?.d/*umountfs 2>/dev/null | xargs -i rm -v {} |
21 |
ls $rootfs/etc/rc?.d/*umountroot 2>/dev/null | xargs -i rm -v {} |
22 |
ls $rootfs/etc/rc?.d/*hwclock* 2>/dev/null | xargs -i rm -v {} |
23 |
|
24 |
lxc-info -n $name | grep RUNNING || lxc-start -d -n $name -o /tmp/${name}.log && echo "$name start" |
25 |
|
26 |
while true; do |
27 |
# time of 5 minutes on it JUST IN CASE... |
28 |
vps_utmp=${rootfs}/var/run/utmp |
29 |
inotifywait -qqt 300 ${vps_utmp} |
30 |
if [ $(wc -l < /cgroup/${name}/tasks) -eq 1 ]; then |
31 |
|
32 |
runlevel="$(runlevel ${vps_utmp})" |
33 |
echo "# $name runlevel $runlevel" |
34 |
|
35 |
case $runlevel in |
36 |
N*) |
37 |
# nothing for new boot state |
38 |
;; |
39 |
??0) |
40 |
echo "$name halt" |
41 |
lxc-stop -n "${name}" |
42 |
break |
43 |
;; |
44 |
??6) |
45 |
echo "$name reboot"; |
46 |
lxc-stop -n ${name} |
47 |
lxc-wait -n ${name} -s STOPPED |
48 |
mount /mnt/llin -o remount,rw |
49 |
lxc-start -d -n ${name} -o /tmp/${name}.log |
50 |
;; |
51 |
*) |
52 |
# make sure vps is still running |
53 |
state="$(lxc-info -n "${name}" | sed -e 's/.* is //')" |
54 |
[ "$state" = "RUNNING" ] || break |
55 |
;; |
56 |
esac |
57 |
fi |
58 |
done |
59 |
|