summaryrefslogtreecommitdiff
path: root/debian/openvswitch-switch.init
blob: 0907cdf7ca96120253b872f3a682a32f13537be9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#! /bin/sh
#
# /etc/init.d/openvswitch-switch
#
# Written by Miquel van Smoorenburg <miquels@cistron.nl>.
# Modified for Debian by Ian Murdock <imurdock@gnu.ai.mit.edu>.
# Further changes by Javier Fernandez-Sanguino <jfs@debian.org>
# Modified for openvswitch-switch.
#
# Version:	@(#)skeleton  1.9  26-Feb-2001  miquels@cistron.nl
#
### BEGIN INIT INFO
# Provides:          openvswitch-switch
# Required-Start:    $network $named $remote_fs $syslog
# Required-Stop:     $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Open vSwitch switch
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
ovs_vswitchd=/usr/sbin/ovs-vswitchd
ovsdb_server=/usr/bin/ovsdb-server

(test -x $ovsdb_server && test -x $ovs_vswitchd) || exit 0

DODTIME=1                   # Time to wait for the server to die, in seconds
                            # If this value is set too low you might not
                            # let some servers to die gracefully and
                            # 'restart' will not work

# Include ovs-openflowd defaults if available
unset OVSDB_SERVER_OPTS
unset OVS_VSWITCHD_OPTS
unset CORE_LIMIT
unset ENABLE_MONITOR
default=/etc/default/openvswitch-switch
if [ -f $default ] ; then
    . $default
fi

: ${ENABLE_MONITOR:=y}

set -e

# running_pid pid name
#
# Check if 'pid' is a process named 'name'
running_pid()
{
    local pid=$1 name=$2
    [ -z "$pid" ] && return 1 
    [ ! -d /proc/$pid ] &&  return 1
    cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1`
    # Is this the expected child?
    case $cmd in
        $name|*/$name)
            return 0
            ;;
        *)
            return 1
            ;;
    esac
}

# running name
#
# Checks for a running process named 'name' by looking for a pidfile
# named /var/run/openvswitch/${name}.pid
running()
{
    local name=$1
    local pidfile=/var/run/openvswitch/${name}.pid

    # No pidfile, probably no daemon present
    [ ! -f "$pidfile" ] && return 1

    # Obtain the pid and check it against the binary name
    pid=`cat $pidfile`
    running_pid $pid $name || return 1
    return 0
}

# force_stop name
#
# Checks for a running process named 'name', by looking for a pidfile
# named /var/run/openvswitch/${name}.pid, and then kills it and waits 
# for it to die.
force_stop() {
    local name=$1
    local pidfile=/var/run/openvswitch/${name}.pid

    [ ! -f "$pidfile" ] && return
    if running $name; then
        kill $pid
        [ -n "$DODTIME" ] && sleep "$DODTIME"
        if running $name; then
            kill -KILL $pid
            [ -n "$DODTIME" ] && sleep "$DODTIME"
            if running $name; then
                echo "Cannot kill $name (pid=$pid)!"
                exit 1
            fi
        fi
    fi
    rm -f $pidfile
    return 0
}

must_succeed() {
    echo -n "$1: "
    shift
    if "$@"; then
        echo "success."
    else
        echo " ERROR."
        exit 1
    fi
}

check_op() {
    echo -n "$1: "
    shift
    if "$@"; then
        echo "success."
    else
        echo " ERROR."
    fi
}

# is_module_loaded module
#
# Returns 0 if 'module' is loaded, 1 otherwise.

is_module_loaded() {
    local module=$1
    grep -q "^$module " /proc/modules
}

# load_module module
#
# Loads 'module' into the running kernel, if it is not already loaded.
load_module() {
    local module=$1
    echo -n "Loading $module: "
    if is_module_loaded $module; then
        echo "already loaded, nothing to do."
    elif modprobe $module; then
        echo "success."
    else
        echo "ERROR."
        echo "$module has probably not been built for this kernel."
        if ! test -d /usr/share/doc/openvswitch-datapath-source; then
            echo "Install the openvswitch-datapath-source package, then read"
            echo "/usr/share/doc/openvswitch-datapath-source/README.Debian"
        else
            echo "For instructions, read"
            echo "/usr/share/doc/openvswitch-datapath-source/README.Debian"
        fi
        exit 0
    fi
}

# unload_module module
#
# Unloads 'module' from the running kernel, if it is loaded.
unload_module() {
    local module=$1
    echo -n "Unloading $module: "
    if is_module_loaded $module; then
        if rmmod $module; then
            echo "success."
        else
            echo "ERROR."
            exit 1
        fi
    else
        echo "not loaded, nothing to do."
    fi
}

unload_modules() {
    if is_module_loaded openvswitch_mod; then
        for dp in $(ovs-dpctl dump-dps); do
            echo -n "Deleting datapath $dp: "
            if ovs-dpctl del-dp $dp; then
                echo "success."
            else
                echo "ERROR."
            fi
        done
    fi
    unload_module openvswitch_mod
}

case "$1" in
    start)
        load_module openvswitch_mod

        if test -n "$CORE_LIMIT"; then
            check_op "Setting core limit to $CORE_LIMIT" ulimit -c "$CORE_LIMIT"
        fi

        # Create an empty configuration database if it doesn't exist.
        if test ! -e /etc/openvswitch/conf.db; then
            install -d -m 755 -o root -g root /etc/openvswitch

            # Create configuration database.
            ovsdb-tool -vANY:console:emer \
                create /etc/openvswitch/conf.db \
                /usr/share/openvswitch/vswitch.ovsschema
        else
            # Upgrade or downgrade schema and compact database.
            ovsdb-tool -vANY:console:emer \
                convert /etc/openvswitch/conf.db \
                /usr/share/openvswitch/vswitch.ovsschema
        fi

        if test "$ENABLE_MONITOR" = y; then
            monitor_opt=--monitor
        else
            monitor_opt=
        fi

        if [ ! -d /var/run/openvswitch ]; then
            install -d -m 755 -o root -g root /var/run/openvswitch
        fi

        if [ ! -d /var/log/openvswitch ]; then
            install -d -m 755 -o root -g root /var/log/openvswitch
        fi

        if [ ! -d /var/log/openvswitch/cores ]; then
            install -d -m 755 -o root -g root /var/log/openvswitch/cores
        fi

        # Start ovsdb-server.
        set --
        set -- "$@" /etc/openvswitch/conf.db
        set -- "$@" --verbose=ANY:console:emer --verbose=ANY:syslog:err
        set -- "$@" --log-file=/var/log/openvswitch/ovsdb-server.log
        set -- "$@" --detach --no-chdir --pidfile $monitor_opt
        set -- "$@" --remote punix:/var/run/openvswitch/db.sock
        set -- "$@" --remote db:Open_vSwitch,managers
        set -- "$@" --private-key=db:SSL,private_key
        set -- "$@" --certificate=db:SSL,certificate
        set -- "$@" --bootstrap-ca-cert=db:SSL,ca_cert
        set -- "$@" $OVSDB_SERVER_OPTS
        echo -n "Starting ovsdb-server: "
        start-stop-daemon --start --quiet \
            --pidfile /var/run/openvswitch/ovsdb-server.pid \
            --chdir /var/log/openvswitch/cores              \
            --exec $ovsdb_server -- "$@"
        if running ovsdb-server; then
            echo "ovsdb-server."
        else
            echo " ERROR."
        fi

        ovs-vsctl --no-wait --timeout=5 init

        # Start ovs-vswitchd.
        set --
        set -- "$@" --verbose=ANY:console:emer --verbose=ANY:syslog:err
        set -- "$@" --log-file=/var/log/openvswitch/ovs-vswitchd.log
        set -- "$@" --detach --no-chdir --pidfile $monitor_opt
        set -- "$@" unix:/var/run/openvswitch/db.sock
        set -- "$@" $OVS_VSWITCHD_OPTS
        echo -n "Starting ovs-vswitchd: "
        start-stop-daemon --start --quiet \
            --pidfile /var/run/openvswitch/ovs-vswitchd.pid \
            --chdir /var/log/openvswitch/cores              \
            --exec $ovs_vswitchd -- "$@"
        if running ovs-vswitchd; then
            echo "ovs-vswitchd."
        else
            echo " ERROR."
        fi
        ;;
    stop)
        echo -n "Stopping ovs-vswitchd: "
        start-stop-daemon --stop --quiet --oknodo --retry 5 \
            --pidfile /var/run/openvswitch/ovs-vswitchd.pid \
            --chdir /var/log/openvswitch/cores              \
            --exec $ovs_vswitchd
        echo "ovs-vswitchd."

        echo -n "Stopping ovsdb-server: "
        start-stop-daemon --stop --quiet --oknodo --retry 5 \
            --pidfile /var/run/openvswitch/ovsdb-server.pid \
            --chdir /var/log/openvswitch/cores              \
            --exec $ovsdb_server
        echo "ovsdb-server."
        ;;
    force-stop)
        echo -n "Forcefully stopping ovs-vswitchd: "
        force_stop ovs-vswitchd
        if ! running ovs-vswitchd; then
            echo "ovs-vswitchd."
        else
            echo " ERROR."
        fi

        echo -n "Forcefully stopping ovsdb-server: "
        force_stop ovsdb-server
        if ! running ovsdb-server; then
            echo "ovsdb-server."
        else
            echo " ERROR."
        fi
        ;;
    unload)
        unload_modules
        ;;
    reload)
        ;;
    force-reload)
        # Nothing to do, since ovs-vswitchd automatically reloads
        # whenever its configuration changes, and ovsdb-server doesn't
        # have anything to reload.
        ;;
    restart)
        $0 stop || true
        $0 start
        ;;
    status)
        for daemon in ovs-vswitchd ovsdb-server; do
            echo -n "$daemon is "
            if running $daemon;  then
                echo "running"
            else
                echo " not running."
                exit 1
            fi
        done
        ;;
    *)
        N=/etc/init.d/openvswitch-switch
        echo "Usage: $N {start|stop|restart|force-reload|status|force-stop|unload}" >&2
        exit 1
        ;;
esac

exit 0