16 |
# |
# |
17 |
# based on Tony Risinger post to lxc-users mailing list |
# based on Tony Risinger post to lxc-users mailing list |
18 |
# http://www.mail-archive.com/lxc-users@lists.sourceforge.net/msg00074.html |
# http://www.mail-archive.com/lxc-users@lists.sourceforge.net/msg00074.html |
19 |
|
# |
20 |
|
# Install with: |
21 |
|
# ln -sf /srv/sysadmin-cookbook/recepies/lxc/lxc-watchdog.sh /etc/init.d/lxc-watchdog |
22 |
|
# update-rc.d lxc-watchdog defaults |
23 |
|
|
24 |
|
|
25 |
which inotifywait >/dev/null || apt-get install inotify-tools |
which inotifywait >/dev/null || apt-get install inotify-tools |
37 |
|
|
38 |
|
|
39 |
lxc_rootfs() { |
lxc_rootfs() { |
40 |
grep lxc.rootfs "/var/lib/lxc/$1/config" | cut -d= -f2 | sed 's/^ *//' |
grep '^ *lxc\.rootfs *=' "/var/lib/lxc/$1/config" | cut -d= -f2 | sed 's/^ *//' |
41 |
} |
} |
42 |
|
|
43 |
|
|
44 |
lxc_status() { |
lxc_status() { |
45 |
lxc-ls -1 | sort -u | xargs -i lxc-info -n {} | sed "s/'//g" | while read name is status ; do |
( find /var/lib/lxc/ -name "config" | cut -d/ -f5 | sort -u | xargs -i lxc-info -n {} | sed "s/'//g" | while read name is status ; do |
46 |
boot=" " |
boot="-" |
47 |
|
hostname=`cat $(lxc_rootfs $name)/etc/hostname` |
48 |
|
ip=`grep address $(lxc_rootfs $name)/etc/network/interfaces | sed 's/.*address //'` |
49 |
test -s /var/lib/lxc/$name/on_boot && boot="boot" |
test -s /var/lib/lxc/$name/on_boot && boot="boot" |
50 |
echo "$name $status $boot $(lxc_rootfs $name)" |
echo "$name $status $boot $(lxc_rootfs $name) $ip $hostname" |
51 |
done |
done ) | column -t |
52 |
} |
} |
53 |
|
|
54 |
|
|
166 |
} |
} |
167 |
|
|
168 |
|
|
169 |
|
usage() { |
170 |
|
echo "Usage: $0 {start|stop|restart|status|boot|disable} [name name ... ]" >&2 |
171 |
|
exit 3 |
172 |
|
} |
173 |
|
|
174 |
command_on_lxc() { |
command_on_lxc() { |
175 |
command=$1 |
command=$1 |
176 |
shift |
shift |
182 |
start) |
start) |
183 |
lxc_exists $1 |
lxc_exists $1 |
184 |
cleanup_init_scripts $1 |
cleanup_init_scripts $1 |
185 |
setup_inittab $1 ::power "p0::powerfail:/sbin/init 0" |
setup_inittab $1 ":respawn:/sbin/getty.*tty1" "c1:12345:respawn:/sbin/getty 38400 tty1 linux" |
186 |
setup_inittab $1 ::ctrlaltdel "p6::ctrlaltdel:/sbin/init 6" |
setup_inittab $1 "::power" "p0::powerfail:/sbin/init 0" |
187 |
|
setup_inittab $1 "::ctrlaltdel" "p6::ctrlaltdel:/sbin/init 6" |
188 |
lxc_start $1 |
lxc_start $1 |
189 |
# give container 5 seconds to start more than one process |
# give container 5 seconds to start more than one process |
190 |
( sleep 5 ; nohup $0 watchdog $1 >> /tmp/$1.log 2>/dev/null ) & |
( sleep 5 ; nohup $0 watchdog $1 >> /tmp/$1.log 2>/dev/null ) & |
206 |
echo -n > /var/lib/lxc/$1/on_boot |
echo -n > /var/lib/lxc/$1/on_boot |
207 |
;; |
;; |
208 |
*) |
*) |
209 |
echo "Usage: $0 {start|stop|restart|status}" >&2 |
usage |
|
exit 3 |
|
210 |
;; |
;; |
211 |
|
|
212 |
esac |
esac |
214 |
} |
} |
215 |
|
|
216 |
command=$1 |
command=$1 |
217 |
shift |
test -z "$command" && usage |
|
|
|
218 |
test "$command" = "status" && lxc_status && exit |
test "$command" = "status" && lxc_status && exit |
219 |
|
shift |
220 |
|
|
221 |
if [ -z "$1" ] ; then |
if [ -z "$1" ] ; then |
222 |
ls /var/lib/lxc/*/on_boot | while read path ; do |
ls /var/lib/lxc/*/on_boot | while read path ; do |