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