]> git.proxmox.com Git - mirror_lxc.git/blob - config/init/common/lxc-containers.in
Rework init scripts
[mirror_lxc.git] / config / init / common / lxc-containers.in
1 #!/bin/sh
2
3 sysconfdir="@SYSCONFDIR@"
4 distrosysconfdir="@LXC_DISTRO_SYSCONF@"
5 bindir="@BINDIR@"
6 localstatedir="@LOCALSTATEDIR@"
7
8 # These can be overridden in @LXC_DISTRO_SYSCONF@/lxc
9
10 # Autostart containers?
11 LXC_AUTO="true"
12
13 # BOOTGROUPS - What groups should start on bootup?
14 # Comma separated list of groups.
15 # Leading comma, trailing comma or embedded double
16 # comma indicates when the NULL group should be run.
17 # Example (default): boot the onboot group first then the NULL group
18 BOOTGROUPS="onboot,"
19
20 # SHUTDOWNDELAY - Wait time for a container to shut down.
21 # Container shutdown can result in lengthy system
22 # shutdown times. Even 5 seconds per container can be
23 # too long.
24 SHUTDOWNDELAY=5
25
26 # OPTIONS can be used for anything else.
27 # If you want to boot everything then
28 # options can be "-a" or "-a -A".
29 OPTIONS=
30
31 # STOPOPTS are stop options. The can be used for anything else to stop.
32 # If you want to kill containers fast, use -k
33 STOPOPTS="-a -A -s"
34
35 # Source function library.
36 test ! -r "$sysconfdir"/rc.d/init.d/functions ||
37 . "$sysconfdir"/rc.d/init.d/functions
38
39 # provide action() fallback
40 if ! type action >/dev/null 2>&1; then
41 # Real basic fallback for sysvinit "action" verbage.
42 action() {
43 echo -n "$1 "
44 "$@" && echo "OK" || echo "Failed"
45 }
46 fi
47
48 if [ -d "$localstatedir"/lock/subsys ]
49 then
50 lockdir="$localstatedir"/lock/subsys
51 else
52 lockdir="$localstatedir"/lock
53 fi
54
55 # Source any configurable options
56 [ ! -f "$distrosysconfdir"/lxc ] || . "$distrosysconfdir"/lxc
57
58 # Check for needed utility program
59 [ -x "$bindir"/lxc-autostart ] || exit 1
60
61 # If libvirtd is providing the bridge, it might not be
62 # immediately available, so wait a bit for it before starting
63 # up the containers or else any that use the bridge will fail
64 # to start
65 wait_for_bridge()
66 {
67 [ -f "$sysconfdir"/lxc/default.conf ] || { return 0; }
68
69 which ifconfig >/dev/null 2>&1
70 if [ $? = 0 ]; then
71 cmd="ifconfig -a"
72 else
73 which ip >/dev/null 2>&1
74 if [ $? = 0 ]; then
75 cmd="ip link list"
76 fi
77 fi
78 [ -n cmd ] || { return 0; }
79
80 BRNAME=`grep '^[ ]*lxc.network.link' "$sysconfdir"/lxc/default.conf | sed 's/^.*=[ ]*//'`
81 if [ -z "$BRNAME" ]; then
82 return 0
83 fi
84
85 for try in `seq 1 30`; do
86 eval $cmd |grep "^$BRNAME" >/dev/null 2>&1
87 if [ $? = 0 ]; then
88 return
89 fi
90 sleep 1
91 done
92 }
93
94 # See how we were called.
95 case "$1" in
96 start)
97 [ "x$LXC_AUTO" = "xtrue" ] || { exit 0; }
98
99 [ ! -f "$lockdir"/lxc ] || { exit 0; }
100
101 if [ -n "$BOOTGROUPS" ]; then
102 BOOTGROUPS="-g $BOOTGROUPS"
103 fi
104
105 # Start containers
106 wait_for_bridge
107
108 # Start autoboot containers first then the NULL group "onboot,".
109 action $"Starting LXC autoboot containers: " "$bindir"/lxc-autostart $OPTIONS $BOOTGROUPS
110 touch "$lockdir"/lxc
111 ;;
112
113 stop)
114 if [ -n "$SHUTDOWNDELAY" ]; then
115 SHUTDOWNDELAY="-t $SHUTDOWNDELAY"
116 fi
117
118 # The stop is serialized and can take excessive time. We need to avoid
119 # delaying the system shutdown / reboot as much as we can since it's not
120 # parallelized... Even 5 second timout may be too long.
121 action $"Stopping LXC containers: " "$bindir"/lxc-autostart $STOPOPTS $SHUTDOWNDELAY
122 rm -f "$lockdir"/lxc
123 ;;
124
125 restart|reload|force-reload)
126 $0 stop
127 $0 start
128 ;;
129
130 *)
131 echo "Usage: $0 {start|stop|restart|reload|force-reload}"
132 exit 2
133 ;;
134 esac
135
136 exit $?