debian: Create the config database if it doesn't exist on startup
[sliver-openvswitch.git] / debian / openvswitch-switch.init
1 #! /bin/sh
2 #
3 # /etc/init.d/openvswitch-switch
4 #
5 # Written by Miquel van Smoorenburg <miquels@cistron.nl>.
6 # Modified for Debian by Ian Murdock <imurdock@gnu.ai.mit.edu>.
7 # Further changes by Javier Fernandez-Sanguino <jfs@debian.org>
8 # Modified for openvswitch-switch.
9 #
10 # Version:      @(#)skeleton  1.9  26-Feb-2001  miquels@cistron.nl
11 #
12 ### BEGIN INIT INFO
13 # Provides:          openvswitch-switch
14 # Required-Start:    $network $named $remote_fs $syslog
15 # Required-Stop:
16 # Default-Start:     2 3 4 5
17 # Default-Stop:      0 1 6
18 # Short-Description: Open vSwitch switch
19 ### END INIT INFO
20
21 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
22 ovs_vswitchd=/usr/sbin/ovs-vswitchd
23 ovsdb_server=/usr/bin/ovsdb-server
24
25 (test -x $ovsdb_server && test -x $ovs_vswitchd) || exit 0
26
27 DODTIME=1                   # Time to wait for the server to die, in seconds
28                             # If this value is set too low you might not
29                             # let some servers to die gracefully and
30                             # 'restart' will not work
31
32 # Include ovs-openflowd defaults if available
33 unset OVSDB_SERVER_OPTS
34 unset OVS_VSWITCHD_OPTS
35 unset CORE_LIMIT
36 default=/etc/default/openvswitch-switch
37 if [ -f $default ] ; then
38         . $default
39 fi
40
41 set -e
42
43 # running_pid pid name
44 #
45 # Check if 'pid' is a process named 'name'
46 running_pid()
47 {
48     local pid=$1 name=$2
49     [ -z "$pid" ] && return 1 
50     [ ! -d /proc/$pid ] &&  return 1
51     cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1`
52     # Is this the expected child?
53     case $cmd in
54         $name|*/$name)
55             return 0
56             ;;
57         *)
58             return 1
59             ;;
60     esac
61 }
62
63 # running name
64 #
65 # Checks for a running process named 'name' by looking for a pidfile
66 # named /var/run/${name}.pid
67 running()
68 {
69     local name=$1
70     local pidfile=/var/run/${name}.pid
71
72     # No pidfile, probably no daemon present
73     [ ! -f "$pidfile" ] && return 1
74
75     # Obtain the pid and check it against the binary name
76     pid=`cat $pidfile`
77     running_pid $pid $name || return 1
78     return 0
79 }
80
81 # force_stop name
82 #
83 # Checks for a running process named 'name', by looking for a pidfile
84 # named /var/run/${name}.pid, and then kills it and waits for it to
85 # die.
86 force_stop() {
87     local name=$1
88     local pidfile=/var/run/${name}.pid
89
90     [ ! -f "$pidfile" ] && return
91     if running $name; then
92         kill $pid
93         [ -n "$DODTIME" ] && sleep "$DODTIME"s
94         if running $name; then
95             kill -KILL $pid
96             [ -n "$DODTIME" ] && sleep "$DODTIME"s
97             if running $name; then
98                 echo "Cannot kill $name (pid=$pid)!"
99                 exit 1
100             fi
101         fi
102     fi
103     rm -f $pidfile
104     return 0
105 }
106
107 must_succeed() {
108     echo -n "$1: "
109     shift
110     if "$@"; then
111         echo "success."
112     else
113         echo " ERROR."
114         exit 1
115     fi
116 }
117
118 check_op() {
119     echo -n "$1: "
120     shift
121     if "$@"; then
122         echo "success."
123     else
124         echo " ERROR."
125     fi
126 }
127
128 # is_module_loaded module
129 #
130 # Returns 0 if 'module' is loaded, 1 otherwise.
131
132 is_module_loaded() {
133     local module=$1
134     grep -q "^$module " /proc/modules
135 }
136
137 # load_module module
138 #
139 # Loads 'module' into the running kernel, if it is not already loaded.
140 load_module() {
141     local module=$1
142     echo -n "Loading $module: "
143     if is_module_loaded $module; then
144         echo "already loaded, nothing to do."
145     elif modprobe $module; then
146         echo "success."
147     else
148         echo "ERROR."
149         echo "$module has probably not been built for this kernel."
150         if ! test -d /usr/share/doc/openvswitch-datapath-source; then
151             echo "Install the openvswitch-datapath-source package, then read"
152             echo "/usr/share/doc/openvswitch-datapath-source/README.Debian"
153         else
154             echo "For instructions, read"
155             echo "/usr/share/doc/openvswitch-datapath-source/README.Debian"
156         fi
157         exit 1
158     fi
159 }
160
161 # unload_module module
162 #
163 # Unloads 'module' from the running kernel, if it is loaded.
164 unload_module() {
165     local module=$1
166     echo -n "Unloading $module: "
167     if is_module_loaded $module; then
168         if rmmod $module; then
169             echo "success."
170         else
171             echo "ERROR."
172             exit 1
173         fi
174     else
175         echo "not loaded, nothing to do."
176     fi
177 }
178
179 unload_modules() {
180     if is_module_loaded openvswitch_mod; then
181         for dp in $(ovs-dpctl dump-dps); do
182             echo -n "Deleting datapath $dp: "
183             if ovs-dpctl del-dp $dp; then
184                 echo "success."
185             else
186                 echo "ERROR."
187             fi
188         done
189     fi
190     unload_module openvswitch_mod
191     unload_module ip_gre_mod
192 }
193
194 case "$1" in
195     start)
196         load_module openvswitch_mod
197         unload_module ip_gre
198         load_module ip_gre_mod
199
200         if test -n "$CORE_LIMIT"; then
201             check_op "Setting core limit to $CORE_LIMIT" ulimit -c "$CORE_LIMIT"
202         fi
203
204         # Create an empty configuration database if it doesn't exist.
205         if test ! -e /etc/openvswitch-switch/conf; then
206             # Create configuration database.
207             ovsdb-tool -vANY:console:emer \
208                 create /etc/openvswitch-switch/conf \
209                 /usr/share/openvswitch/vswitch-idl.ovsschema
210         fi
211
212         # Start ovsdb-server.
213         set --
214         set -- "$@" --verbose=ANY:console:emer --verbose=ANY:syslog:err
215         set -- "$@" --log-file
216         set -- "$@" --detach --pidfile
217         set -- "$@" --remote punix:/var/run/ovsdb-server
218         set -- "$@" /etc/openvswitch-switch/conf
219         set -- "$@" $OVSDB_SERVER_OPTS
220         echo -n "Starting ovsdb-server: "
221         start-stop-daemon --start --quiet --pidfile /var/run/ovsdb-server.pid \
222             --exec $ovsdb_server -- "$@"
223         if running ovsdb-server; then
224             echo "ovsdb-server."
225         else
226             echo " ERROR."
227         fi
228
229         ovs-vsctl --no-wait init
230
231         # Start ovs-vswitchd.
232         set --
233         set -- "$@" --verbose=ANY:console:emer --verbose=ANY:syslog:err
234         set -- "$@" --log-file
235         set -- "$@" --detach --pidfile
236         set -- "$@" unix:/var/run/ovsdb-server
237         set -- "$@" $OVS_VSWITCHD_OPTS
238         echo -n "Starting ovs-vswitchd: "
239         start-stop-daemon --start --quiet --pidfile /var/run/ovs-vswitchd.pid \
240             --exec $ovs_vswitchd -- "$@"
241         if running ovs-vswitchd; then
242             echo "ovs-vswitchd."
243         else
244             echo " ERROR."
245         fi
246         ;;
247     stop)
248         echo -n "Stopping ovs-vswitchd: "
249         start-stop-daemon --stop --quiet --oknodo \
250             --pidfile /var/run/ovs-vswitchd.pid \
251             --exec $ovs_vswitchd
252         echo "ovs-vswitchd."
253
254         echo -n "Stopping ovsdb-server: "
255         start-stop-daemon --stop --quiet --oknodo \
256             --pidfile /var/run/ovsdb-server.pid \
257             --exec $ovsdb_server
258         echo "ovsdb-server."
259         ;;
260     force-stop)
261         echo -n "Forcefully stopping ovs-vswitchd: "
262         force_stop ovs-vswitchd
263         if ! running ovs-vswitchd; then
264             echo "ovs-vswitchd."
265         else
266             echo " ERROR."
267         fi
268
269         echo -n "Forcefully stopping ovsdb-server: "
270         force_stop ovsdb-server
271         if ! running ovsdb-server; then
272             echo "ovsdb-server."
273         else
274             echo " ERROR."
275         fi
276         ;;
277     unload)
278         unload_modules
279         ;;
280     reload)
281         ;;
282     force-reload)
283         # Nothing to do, since ovs-vswitchd automatically reloads
284         # whenever its configuration changes, and ovsdb-server doesn't
285         # have anything to reload.
286         ;;
287     restart)
288         $0 stop || true
289         $0 start
290         ;;
291     status)
292         for daemon in ovs-vswitchd ovsdb-server; do
293             echo -n "$daemon is "
294             if running $daemon;  then
295                 echo "running"
296             else
297                 echo " not running."
298                 exit 1
299             fi
300         done
301         ;;
302     *)
303         N=/etc/init.d/$NAME
304         echo "Usage: $N {start|stop|restart|force-reload|status|force-stop|unload}" >&2
305         exit 1
306         ;;
307 esac
308
309 exit 0