-
Notifications
You must be signed in to change notification settings - Fork 0
/
lxc-shutdown-agent
executable file
·76 lines (67 loc) · 2.38 KB
/
lxc-shutdown-agent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/sh -
# lxc-shutdown-agent
# Monitor a container's runlevel and tasklist and perform lxc-stop/start
# on the containers behalf to emulate hardware shutdown or reboot.
# Perform lxc-stop if container goes to runlevel 0
# Perform lxc-stop;lxc-start if container goes to runlevel 6
# When doing lxc-stop, wait for task list to reach less than 2 first.
#
# Based on:
# http://www.mail-archive.com/[email protected]/msg00074.html
#
# run from /etc/init.d/lxc
# When the rc start script starts a container, it also runs this in the
# background to monitor that container.
#
# Requires intotifywait, which is neither installed by default nor even
# available in the standard oss non-oss repos. You have to get the package
# "inotify-tools" from the "Education" or "home:aljex" repos.
#
# 20100212 - Brian K. White - [email protected]
LXC_ETC=/etc/lxc
LXC_LIB=/var/lib/lxc
CGROUP_MOUNT=/cgroup
LXC_CONF=${LXC_ETC}/lxc.conf
[[ -s $LXC_CONF ]] && . $LXC_CONF
self=$0
VM=$1
VM_CFG=${LXC_LIB}/${VM}/config
abrt () { echo "${self}: $@" ; exit 1 ; }
stop_vm () {
/usr/bin/logger -t LXC-AGENT "[${VM}] waiting for processes to end..."
# we don' need no steenkeeng `wc -l ...`
t=${CGROUP_MOUNT}/${VM}/tasks
n=2
while [[ $n -gt 1 ]] ; do
n=0
[[ -s $t ]] || break
while read ;do ((n++)) ;done <$t ;inotifywait $t
done
/usr/bin/logger -t LXC-AGENT "[${VM}] Stopping container."
lxc-stop -n $VM
/usr/bin/logger -t LXC-AGENT "[${VM}] waiting for container to be stopped."
lxc-wait -n $VM -s STOPPED
/usr/bin/logger -t LXC-AGENT "[${VM}] container is stopped."
}
start_vm () {
/usr/bin/logger -t LXC-AGENT "[${VM}] Starting container."
nohup /usr/bin/lxc-start-vm -n $VM -a >/dev/null 2>&1 &
}
VM_STATE=`lxc-info -n $VM` VM_STATE=${VM_STATE##* }
[[ "$VM_STATE" = "RUNNING" ]] || abrt "Container \"$VM\" is not running."
unset VM_ROOTFS
while read a b c ;do [[ "$a" = "lxc.rootfs" ]] && VM_ROOTFS="$c" ;done <$VM_CFG
[ -f "$VM_ROOTFS" ] || VM_ROOTFS=$( readlink -e ${LXC_LIB}/${VM}/rootfs/rootfs )
[[ "$VM_ROOTFS" ]] || abrt "Could not determine rootfs for container \"$VM\"."
VM_UTMP=${VM_ROOTFS}/var/run/utmp
sleep 1
while inotifywait -qq $VM_UTMP ; do
RUNLEVEL=`/sbin/runlevel $VM_UTMP`
/usr/bin/logger -t LXC-AGENT "[${VM}] runlevel changed to ${RUNLEVEL}"
case "${RUNLEVEL}" in
N??) : ;;
??0) stop_vm ; break ;;
??6) stop_vm ; start_vm ; sleep 1;;
esac
done
/usr/bin/logger -t LXC-AGENT "[${VM}] exit."