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