ovsdb: Implement garbage collection.
[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:     $remote_fs
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 openvswitch-switch defaults if available
33 unset OVSDB_SERVER_OPTS
34 unset OVS_VSWITCHD_OPTS
35 unset CORE_LIMIT
36 unset ENABLE_MONITOR
37 default=/etc/default/openvswitch-switch
38 if [ -f $default ] ; then
39     . $default
40 fi
41
42 : ${ENABLE_MONITOR:=y}
43
44 set -e
45
46 # running_pid pid name
47 #
48 # Check if 'pid' is a process named 'name'
49 running_pid()
50 {
51     local pid=$1 name=$2
52     [ -z "$pid" ] && return 1 
53     [ ! -d /proc/$pid ] &&  return 1
54     cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1`
55     # Is this the expected child?
56     case $cmd in
57         $name|*/$name)
58             return 0
59             ;;
60         *)
61             return 1
62             ;;
63     esac
64 }
65
66 # running name
67 #
68 # Checks for a running process named 'name' by looking for a pidfile
69 # named /var/run/openvswitch/${name}.pid
70 running()
71 {
72     local name=$1
73     local pidfile=/var/run/openvswitch/${name}.pid
74
75     # No pidfile, probably no daemon present
76     [ ! -f "$pidfile" ] && return 1
77
78     # Obtain the pid and check it against the binary name
79     pid=`cat $pidfile`
80     running_pid $pid $name || return 1
81     return 0
82 }
83
84 # force_stop name
85 #
86 # Checks for a running process named 'name', by looking for a pidfile
87 # named /var/run/openvswitch/${name}.pid, and then kills it and waits 
88 # for it to die.
89 force_stop() {
90     local name=$1
91     local pidfile=/var/run/openvswitch/${name}.pid
92
93     [ ! -f "$pidfile" ] && return
94     if running $name; then
95         kill $pid
96         [ -n "$DODTIME" ] && sleep "$DODTIME"
97         if running $name; then
98             kill -KILL $pid
99             [ -n "$DODTIME" ] && sleep "$DODTIME"
100             if running $name; then
101                 echo "Cannot kill $name (pid=$pid)!"
102                 exit 1
103             fi
104         fi
105     fi
106     rm -f $pidfile
107     return 0
108 }
109
110 must_succeed() {
111     echo -n "$1: "
112     shift
113     if "$@"; then
114         echo "success."
115     else
116         echo " ERROR."
117         exit 1
118     fi
119 }
120
121 check_op() {
122     echo -n "$1: "
123     shift
124     if "$@"; then
125         echo "success."
126     else
127         echo " ERROR."
128     fi
129 }
130
131 # is_module_loaded module
132 #
133 # Returns 0 if 'module' is loaded, 1 otherwise.
134
135 is_module_loaded() {
136     local module=$1
137     grep -q "^$module " /proc/modules
138 }
139
140 # load_module module
141 #
142 # Loads 'module' into the running kernel, if it is not already loaded.
143 load_module() {
144     local module=$1
145     echo -n "Loading $module: "
146     if is_module_loaded $module; then
147         echo "already loaded, nothing to do."
148     elif modprobe $module; then
149         echo "success."
150     else
151         echo "ERROR."
152         echo "$module has probably not been built for this kernel."
153         if ! test -d /usr/share/doc/openvswitch-datapath-source; then
154             echo "Install the openvswitch-datapath-source package, then read"
155             echo "/usr/share/doc/openvswitch-datapath-source/README.Debian"
156         else
157             echo "For instructions, read"
158             echo "/usr/share/doc/openvswitch-datapath-source/README.Debian"
159         fi
160         exit 0
161     fi
162 }
163
164 # unload_module module
165 #
166 # Unloads 'module' from the running kernel, if it is loaded.
167 unload_module() {
168     local module=$1
169     echo -n "Unloading $module: "
170     if is_module_loaded $module; then
171         if rmmod $module; then
172             echo "success."
173         else
174             echo "ERROR."
175             exit 1
176         fi
177     else
178         echo "not loaded, nothing to do."
179     fi
180 }
181
182 unload_modules() {
183     if is_module_loaded openvswitch_mod; then
184         for dp in $(ovs-dpctl dump-dps); do
185             echo -n "Deleting datapath $dp: "
186             if ovs-dpctl del-dp $dp; then
187                 echo "success."
188             else
189                 echo "ERROR."
190             fi
191         done
192     fi
193     unload_module openvswitch_mod
194 }
195
196 set_system_info() {
197     ovs_version=`ovs-vswitchd --version | sed 's/.*) //;1q'`
198     ovs-vsctl --no-wait --timeout=5 set Open_vSwitch . \
199         ovs-version="$ovs_version"
200
201     if (lsb_release --id) >/dev/null 2>&1; then
202         system_type=`lsb_release --id -s`
203         system_release=`lsb_release --release -s`
204         system_codename=`lsb_release --codename -s`
205         system_version="${system_release}-${system_codename}"
206
207         ovs-vsctl --no-wait --timeout=5 set Open_vSwitch . \
208             system-type="$system_type" \
209             system-version="$system_version"
210     fi
211 }
212
213 case "$1" in
214     start)
215         conf_file=/etc/openvswitch/conf.db
216         schema_file=/usr/share/openvswitch/vswitch.ovsschema
217         schema_ver=`ovsdb-tool schema-version "$schema_file"`
218
219         load_module openvswitch_mod
220
221         if test -n "$CORE_LIMIT"; then
222             check_op "Setting core limit to $CORE_LIMIT" ulimit -c "$CORE_LIMIT"
223         fi
224
225         # Create an empty configuration database if it doesn't exist.
226         if test ! -e $conf_file; then
227             # Create configuration database.
228             ovsdb-tool -vANY:console:emer create $conf_file $schema_file
229         elif test "X`ovsdb-tool needs-conversion $conf_file $schema_file`" != Xno; then
230             # Back up the old version.
231             version=`ovsdb-tool db-version "$conf_file"`
232             cksum=`ovsdb-tool db-cksum "$conf_file" | awk '{print $1}'`
233             cp "$conf_file" "$conf_file.backup$version-$cksum"
234             
235             # Compact database.  This is important if the old schema did not
236             # enable garbage collection (i.e. if it did not have any tables
237             # with "isRoot": true) but the new schema does.  In that situation
238             # the old database may contain a transaction that creates a record
239             # followed by a transaction that creates the first use of the
240             # record.  Replaying that series of transactions against the new
241             # database schema (as "convert" does) would cause the record to be
242             # dropped by the first transaction, then the second transaction
243             # would cause a referential integrity failure (for a strong
244             # reference).
245             ovsdb-tool -vANY:console:emer compact $conf_file
246
247             # Upgrade or downgrade schema and compact database.
248             ovsdb-tool -vANY:console:emer convert $conf_file $schema_file
249         fi
250
251         if test "$ENABLE_MONITOR" = y; then
252             monitor_opt=--monitor
253         else
254             monitor_opt=
255         fi
256
257         if [ ! -d /var/run/openvswitch ]; then
258             install -d -m 755 -o root -g root /var/run/openvswitch
259         fi
260
261         if [ ! -d /var/log/openvswitch ]; then
262             install -d -m 755 -o root -g root /var/log/openvswitch
263         fi
264
265         if [ ! -d /var/log/openvswitch/cores ]; then
266             install -d -m 755 -o root -g root /var/log/openvswitch/cores
267         fi
268
269         # Start ovsdb-server.
270         set --
271         set -- "$@" $conf_file
272         set -- "$@" --verbose=ANY:console:emer --verbose=ANY:syslog:err
273         set -- "$@" --log-file=/var/log/openvswitch/ovsdb-server.log
274         set -- "$@" --detach --no-chdir --pidfile $monitor_opt
275         set -- "$@" --remote punix:/var/run/openvswitch/db.sock
276         set -- "$@" --remote db:Open_vSwitch,manager_options
277         set -- "$@" --private-key=db:SSL,private_key
278         set -- "$@" --certificate=db:SSL,certificate
279         set -- "$@" --bootstrap-ca-cert=db:SSL,ca_cert
280         set -- "$@" $OVSDB_SERVER_OPTS
281         echo -n "Starting ovsdb-server: "
282         start-stop-daemon --start --quiet --oknodo \
283             --pidfile /var/run/openvswitch/ovsdb-server.pid \
284             --chdir /var/log/openvswitch/cores              \
285             --exec $ovsdb_server -- "$@"
286         if running ovsdb-server; then
287             echo "ovsdb-server."
288         else
289             echo " ERROR."
290         fi
291
292         ovs-vsctl --no-wait --timeout=5 init -- set Open_vSwitch . db-version="$schema_ver"
293
294         set_system_info
295
296         # Start ovs-vswitchd.
297         set --
298         set -- "$@" --verbose=ANY:console:emer --verbose=ANY:syslog:err
299         set -- "$@" --log-file=/var/log/openvswitch/ovs-vswitchd.log
300         set -- "$@" --detach --no-chdir --pidfile $monitor_opt
301         set -- "$@" unix:/var/run/openvswitch/db.sock
302         set -- "$@" $OVS_VSWITCHD_OPTS
303         echo -n "Starting ovs-vswitchd: "
304         start-stop-daemon --start --quiet --oknodo \
305             --pidfile /var/run/openvswitch/ovs-vswitchd.pid \
306             --chdir /var/log/openvswitch/cores              \
307             --exec $ovs_vswitchd -- "$@"
308         if running ovs-vswitchd; then
309             echo "ovs-vswitchd."
310         else
311             echo " ERROR."
312         fi
313         ;;
314     stop)
315         echo -n "Stopping ovs-vswitchd: "
316         start-stop-daemon --stop --quiet --oknodo --retry 5 \
317             --pidfile /var/run/openvswitch/ovs-vswitchd.pid \
318             --chdir /var/log/openvswitch/cores              \
319             --exec $ovs_vswitchd
320         echo "ovs-vswitchd."
321
322         echo -n "Stopping ovsdb-server: "
323         start-stop-daemon --stop --quiet --oknodo --retry 5 \
324             --pidfile /var/run/openvswitch/ovsdb-server.pid \
325             --chdir /var/log/openvswitch/cores              \
326             --exec $ovsdb_server
327         echo "ovsdb-server."
328         ;;
329     force-stop)
330         echo -n "Forcefully stopping ovs-vswitchd: "
331         force_stop ovs-vswitchd
332         if ! running ovs-vswitchd; then
333             echo "ovs-vswitchd."
334         else
335             echo " ERROR."
336         fi
337
338         echo -n "Forcefully stopping ovsdb-server: "
339         force_stop ovsdb-server
340         if ! running ovsdb-server; then
341             echo "ovsdb-server."
342         else
343             echo " ERROR."
344         fi
345         ;;
346     unload)
347         unload_modules
348         ;;
349     reload)
350         ;;
351     force-reload)
352         # Nothing to do, since ovs-vswitchd automatically reloads
353         # whenever its configuration changes, and ovsdb-server doesn't
354         # have anything to reload.
355         ;;
356     restart)
357         $0 stop || true
358         $0 start
359         ;;
360     status)
361         for daemon in ovs-vswitchd ovsdb-server; do
362             echo -n "$daemon is "
363             if running $daemon;  then
364                 echo "running"
365             else
366                 echo "not running."
367                 exit 1
368             fi
369         done
370         ;;
371     *)
372         N=/etc/init.d/openvswitch-switch
373         echo "Usage: $N {start|stop|restart|force-reload|status|force-stop|unload}" >&2
374         exit 1
375         ;;
376 esac
377
378 exit 0