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 |
|
|
59 |
$rootfs/etc/rc?.d/*umountfs \ |
$rootfs/etc/rc?.d/*umountfs \ |
60 |
$rootfs/etc/rc?.d/*umountroot \ |
$rootfs/etc/rc?.d/*umountroot \ |
61 |
$rootfs/etc/rc?.d/*hwclock* \ |
$rootfs/etc/rc?.d/*hwclock* \ |
62 |
|
$rootfs/etc/rc?.d/*udev* \ |
63 |
2>/dev/null | xargs -i rm -v {} |
2>/dev/null | xargs -i rm -v {} |
64 |
|
|
65 |
|
echo $1 > $rootfs/etc/hostname |
66 |
} |
} |
67 |
|
|
68 |
|
|
169 |
} |
} |
170 |
|
|
171 |
|
|
172 |
|
usage() { |
173 |
|
echo "Usage: $0 {start|stop|restart|status|boot|disable} [name name ... ]" >&2 |
174 |
|
exit 3 |
175 |
|
} |
176 |
|
|
177 |
command_on_lxc() { |
command_on_lxc() { |
178 |
command=$1 |
command=$1 |
179 |
shift |
shift |
185 |
start) |
start) |
186 |
lxc_exists $1 |
lxc_exists $1 |
187 |
cleanup_init_scripts $1 |
cleanup_init_scripts $1 |
188 |
setup_inittab $1 ::power "p0::powerfail:/sbin/init 0" |
setup_inittab $1 ":respawn:/sbin/getty.*tty1" "c1:12345:respawn:/sbin/getty 38400 tty1 linux" |
189 |
setup_inittab $1 ::ctrlaltdel "p6::ctrlaltdel:/sbin/init 6" |
setup_inittab $1 "::power" "p0::powerfail:/sbin/init 0" |
190 |
|
setup_inittab $1 "::ctrlaltdel" "p6::ctrlaltdel:/sbin/init 6" |
191 |
lxc_start $1 |
lxc_start $1 |
192 |
# give container 5 seconds to start more than one process |
# give container 5 seconds to start more than one process |
193 |
( sleep 5 ; nohup $0 watchdog $1 >> /tmp/$1.log 2>/dev/null ) & |
( sleep 5 ; nohup $0 watchdog $1 >> /tmp/$1.log 2>/dev/null ) & |
209 |
echo -n > /var/lib/lxc/$1/on_boot |
echo -n > /var/lib/lxc/$1/on_boot |
210 |
;; |
;; |
211 |
*) |
*) |
212 |
echo "Usage: $0 {start|stop|restart|status}" >&2 |
usage |
|
exit 3 |
|
213 |
;; |
;; |
214 |
|
|
215 |
esac |
esac |
217 |
} |
} |
218 |
|
|
219 |
command=$1 |
command=$1 |
220 |
shift |
test -z "$command" && usage |
|
|
|
221 |
test "$command" = "status" && lxc_status && exit |
test "$command" = "status" && lxc_status && exit |
222 |
|
shift |
223 |
|
|
224 |
if [ -z "$1" ] ; then |
if [ -z "$1" ] ; then |
225 |
ls /var/lib/lxc/*/on_boot | while read path ; do |
ls /var/lib/lxc/*/on_boot | while read path ; do |