xenserver: Make ovsdb-server look for remotes in manager_options column.
[sliver-openvswitch.git] / xenserver / etc_init.d_openvswitch
index c972988..aff491d 100755 (executable)
@@ -5,7 +5,7 @@
 # chkconfig: 2345 09 91
 # description: Manage Open vSwitch kernel modules and user-space daemons
 
-# Copyright (C) 2009, 2010 Nicira Networks, Inc.
+# Copyright (C) 2009, 2010, 2011 Nicira Networks, Inc.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
+### BEGIN INIT INFO
+# Provides:          openvswitch-switch
+# Required-Start:
+# Required-Stop:
+# Default-Start:     2 3 4 5
+# Default-Stop:      0 1 6
+# Short-Description: Open vSwitch switch
+### END INIT INFO
+
+# source function library
+if [ -f /etc/init.d/functions ]; then
+    . /etc/init.d/functions
+elif [ -f /etc/rc.d/init.d/functions ]; then
+    . /etc/rc.d/init.d/functions
+elif [ -f /lib/lsb/init-functions ]; then
+    . /lib/lsb/init-functions
+else
+    echo "$0: missing LSB shell function library" >&2
+    exit 1
+fi
 
-. /etc/init.d/functions
+if type action >/dev/null 2>&1; then
+    :
+else
+    # SUSE lacks action
+    action() {
+       STRING=$1
+       shift
+       "$@"
+       rc=$?
+       if [ $rc -eq 0 ] ; then
+              log_success_msg $"$STRING "
+       else
+              log_failure_msg $"$STRING "
+       fi
+       return $rc
+    }
+fi
 
-. /etc/xensource-inventory
+test -e /etc/xensource-inventory && . /etc/xensource-inventory
 test -e /etc/sysconfig/openvswitch && . /etc/sysconfig/openvswitch
+if test -e /etc/xensource/network.conf; then
+    NETWORK_MODE=$(cat /etc/xensource/network.conf)
+fi
+
+case ${NETWORK_MODE:=openvswitch} in
+    vswitch|openvswitch)
+        ;;
+    bridge)
+        exit 0
+        ;;
+    *)
+        echo "Open vSwitch disabled (/etc/xensource/network.conf is invalid)" >&2
+        exit 0
+        ;;
+esac
 
 # General config variables in /etc/sysconfig/openvswitch
-if test "$PRODUCT_VERSION" = "5.5.0"; then
-    # XenServer 5.5.0 needs ovs-brcompatd and /proc/net simulation.
+if [ -f /etc/xensource-inventory ]; then
+    if test "$PRODUCT_VERSION" = "5.5.0"; then
+        # XenServer 5.5.0 needs ovs-brcompatd and /proc/net simulation.
+        : ${ENABLE_BRCOMPAT:=y}
+        : ${ENABLE_FAKE_PROC_NET:=y}
+    else
+        # Later versions don't need them.
+        : ${ENABLE_BRCOMPAT:=n}
+        : ${ENABLE_FAKE_PROC_NET:=n}
+    fi
+else
     : ${ENABLE_BRCOMPAT:=y}
     : ${ENABLE_FAKE_PROC_NET:=y}
-else
-    # Later versions don't need them.
-    : ${ENABLE_BRCOMPAT:=n}
-    : ${ENABLE_FAKE_PROC_NET:=n}
 fi
 : ${ENABLE_MONITOR:=y}
 : ${FORCE_COREFILES:=y}
 
 # Config variables specific to ovsdb-server
-: ${OVSDB_SERVER_REMOTES:=punix:/var/run/openvswitch/db.sock db:Open_vSwitch,managers}
+: ${OVSDB_SERVER_REMOTES:=punix:/var/run/openvswitch/db.sock db:Open_vSwitch,managers db:Open_vSwitch,manager_options}
 : ${OVSDB_SERVER_DB:=/etc/openvswitch/conf.db}
 : ${OVSDB_SERVER_PIDFILE:=/var/run/openvswitch/ovsdb-server.pid}
 : ${OVSDB_SERVER_RUN_DIR:=/var/xen/openvswitch}
@@ -81,25 +137,11 @@ fi
 : ${BRCOMPATD_VALGRIND_LOG:=}
 : ${BRCOMPATD_VALGRIND_OPT:=}
 
-# Config variables specific to ovs-xenserverd
-: ${XENSERVERD_PIDFILE:=/var/run/openvswitch/ovs-xenserverd.pid}
-: ${XENSERVERD_RUN_DIR:=/var/xen/openvswitch}
-: ${XENSERVERD_PRIORITY:=}
-: ${XENSERVERD_LOGFILE:=}
-: ${XENSERVERD_FILE_LOGLEVEL:=}
-: ${XENSERVERD_SYSLOG_LOGLEVEL:=}
-: ${XENSERVERD_MEMLEAK_LOGFILE:=}
-: ${XENSERVERD_STRACE_LOG:=}
-: ${XENSERVERD_STRACE_OPT:=}
-: ${XENSERVERD_VALGRIND_LOG:=}
-: ${XENSERVERD_VALGRIND_OPT:=}
-
 # Full paths to executables & modules
 ovsdb_server="/usr/sbin/ovsdb-server"
 ovsdb_tool="/usr/bin/ovsdb-tool"
 vswitchd="/usr/sbin/ovs-vswitchd"
 brcompatd="/usr/sbin/ovs-brcompatd"
-xenserverd="/usr/sbin/ovs-xenserverd"
 dpctl="/usr/bin/ovs-dpctl"
 appctl="/usr/bin/ovs-appctl"
 ofctl="/usr/bin/ovs-ofctl"
@@ -118,8 +160,10 @@ else
     monitor_opt=
 fi
 
-function dp_list {
-    "$dpctl" show | grep '^dp[0-9]\+:' | cut -d':' -f 1
+function hup_monitor_external_ids {
+    if [ -e /var/run/openvswitch/ovs-external-ids.pid ]; then
+        action "Configuring Open vSwitch external IDs" kill -HUP `cat /var/run/openvswitch/ovs-external-ids.pid`
+    fi
 }
 
 function turn_on_corefiles {
@@ -127,12 +171,20 @@ function turn_on_corefiles {
 }
 
 function remove_all_dp {
-    for dp in $(dp_list); do
+    for dp in $($dpctl dump-dps); do
         action "Removing datapath: $dp" "$dpctl" del-dp "$dp"
     done
 }
 
 function insert_modules_if_required {
+    if test -e /sys/module/bridge; then
+       bridges=`echo /sys/class/net/*/bridge | sed 's,/sys/class/net/,,g;s,/bridge,,g'`
+       if test "$bridges" != "*"; then
+            log_warning_msg "not removing bridge module because bridges exist ($bridges)"
+        else
+            action "removing bridge module" rmmod bridge
+        fi
+    fi
     if ! lsmod | grep -q "openvswitch_mod"; then
         action "Inserting llc module" modprobe llc
         action "Inserting openvswitch module" modprobe openvswitch_mod
@@ -140,9 +192,6 @@ function insert_modules_if_required {
     if [ "$ENABLE_BRCOMPAT" = "y" ] && [ -n "$BRCOMPATD_PIDFILE" ] && ! lsmod | grep -q "brcompat_mod"; then
         action "Inserting brcompat module" modprobe brcompat_mod
     fi
-    if [ -f "/lib/modules/`uname -r`/kernel/extra/openvswitch/veth_mod.ko" ] && ! lsmod | grep -q "veth_mod"; then
-        action "Inserting veth module" modprobe veth_mod
-    fi
 }
 
 function remove_modules {
@@ -195,15 +244,15 @@ function start_daemon {
         exit 1
     elif test -n "$strace_log"; then
         local mode=strace
-        set -- $nice strace -o "$strace_log" $strace_opt "$@"
+        set -- strace -o "$strace_log" $strace_opt "$@"
     elif test -n "$valgrind_log"; then
         local mode=valgrind
-        set -- $nice valgrind --log-file="$valgrind_log" $valgrind_opt "$@"
+        set -- valgrind --log-file="$valgrind_log" $valgrind_opt "$@"
     else
         local mode=production
         eval local pidfile=\$${DAEMON}_PIDFILE
-        install -d -m 755 -o root -g root `dirname $VSWITCHD_PIDFILE`
-        set -- $nice "$@" --pidfile="$pidfile" --detach $monitor_opt --no-chdir
+        install -d -m 755 -o root -g root `dirname $pidfile`
+        set -- "$@" --pidfile="$pidfile" --detach $monitor_opt --no-chdir
     fi
 
     # Configure niceness.
@@ -216,7 +265,7 @@ function start_daemon {
         action "Starting `basename $BINARY`" "$@"
     else
         # Start in background and force a "success" message
-        action "Starting ovs-vswitchd with $mode debugging" true
+        action "Starting `basename $BINARY` with $mode debugging" true
         ("$@") &
     fi
 }
@@ -251,16 +300,6 @@ function start_brcompatd {
                  "$VSWITCHD_OVSDB_SERVER"
 }
 
-function start_xenserverd {
-    if [ ! -d "$XENSERVERD_RUN_DIR" ]; then
-        install -d -m 755 -o root -g root "$XENSERVERD_RUN_DIR"
-    fi
-    cd "$XENSERVERD_RUN_DIR"
-
-    install -d -m 755 -o root -g root `dirname $XENSERVERD_PIDFILE`
-    action "Starting ovs-xenserverd" "$xenserverd" --no-chdir --pidfile=$XENSERVERD_PIDFILE --detach $monitor_opt -vANY:CONSOLE:EMER
-}
-
 function stop_daemon {
     local DAEMON=$1
     local BINARY=$2
@@ -268,6 +307,13 @@ function stop_daemon {
     if test -f "$pidfile"; then
         local pid=$(cat "$pidfile")
         action "Killing `basename $BINARY` ($pid)" kill $pid
+        for delay in .1 .25 .65 1 1 1 1; do
+            if kill -0 $pid >/dev/null 2>&1; then
+                sleep $delay
+            else
+                break
+            fi
+        done
         rm -f "$pidfile"
     fi
 }
@@ -285,7 +331,7 @@ Restarting Open vSwitch on a live server is not guaranteed to work.  It is
 provided as a convenience for those situations in which it does work.
 
 EOF
-    read -s -r -n 1 -p "Countinue with restart (y/N): " response
+    read -s -r -n 1 -p "Continue with restart (y/N): " response
     printf "\n"
     case "$response" in
         y|Y)
@@ -297,12 +343,28 @@ EOF
     esac
 }
 
-function set_system_uuid {
-    if test -n "$INSTALLATION_UUID"; then
-        action "Configuring Open vSwitch system UUID" true
-        $vsctl --no-wait set Open_vSwitch . external-ids:system-uuid="$INSTALLATION_UUID"
+function set_system_ids {
+    if [ -f /etc/xensource-inventory ]; then
+        OVS_VERSION=`ovs-vswitchd --version | sed 's/.*) //;1q'`
+        action "Configuring Open vSwitch system IDs" true
+        $vsctl --no-wait --timeout=5 set Open_vSwitch . \
+            ovs-version="$OVS_VERSION" \
+            system-type="$PRODUCT_BRAND" \
+            system-version="$PRODUCT_VERSION-$BUILD_NUMBER" \
+            external-ids:system-id="$INSTALLATION_UUID" \
+            external-ids:xs-system-uuid="$INSTALLATION_UUID"
     else
-        action "Configuring Open vSwitch system UUID" false
+       if test -f /etc/openvswitch/install_uuid.conf; then
+            . /etc/openvswitch/install_uuid.conf
+       elif INSTALLATION_UUID=`uuidgen`; then
+           echo "INSTALLATION_UUID=$INSTALLATION_UUID" > /etc/openvswitch/install_uuid.conf
+       else
+           log_failure_msg "missing uuidgen, could not generate system UUID"
+           return
+        fi
+        $vsctl --no-wait --timeout=5 set Open_vSwitch . \
+            external-ids:system-id="$INSTALLATION_UUID"
+        action "Configuring Open vSwitch system IDs" true
     fi
 }
 
@@ -320,6 +382,7 @@ function start {
     # Allow GRE traffic.
     iptables -I INPUT -p gre -j ACCEPT
 
+    schemaver=`$ovsdb_tool schema-version "$VSWITCHD_OVSDB_SCHEMA"`
     if [ ! -e "$OVSDB_SERVER_DB" ]; then
         warning "$OVSDB_SERVER_DB does not exist"
         install -d -m 755 -o root -g root `dirname $OVSDB_SERVER_DB`
@@ -327,34 +390,51 @@ function start {
         action "Creating empty database $OVSDB_SERVER_DB" true
         $ovsdb_tool -vANY:console:emer create "$OVSDB_SERVER_DB" "$VSWITCHD_OVSDB_SCHEMA"
     else
+        # If schema version changed, then back up the old version.
+        oldver=`$ovsdb_tool db-version "$OVSDB_SERVER_DB"`
+        if test "X$oldver" != "X$schemaver"; then
+            backup=$OVSDB_SERVER_DB.backup$oldver
+            action "Backing up $OVSDB_SERVER_DB in $backup before converting from schema version \"$oldver\" to \"$schemaver\"" true
+            cp "$OVSDB_SERVER_DB" "$backup"
+        fi
+
         # Upgrade or downgrade schema and compact database.
         $ovsdb_tool -vANY:console:emer convert "$OVSDB_SERVER_DB" "$VSWITCHD_OVSDB_SCHEMA"
     fi
 
     start_ovsdb_server
-    $vsctl --no-wait init
+    $vsctl --no-wait --timeout=5 init -- set Open_vSwitch . db-version="$schemaver"
     if [ ! -e /var/run/openvswitch.booted ]; then
         touch /var/run/openvswitch.booted
         for bridge in $($vsctl list-br); do
-            $vsctl --no-wait del-br $bridge
+            $vsctl --no-wait --timeout=5 del-br $bridge
         done
     fi
 
-    set_system_uuid
+    set_system_ids
 
     start_vswitchd
     if [ "${ENABLE_BRCOMPAT}" = "y" ] ; then
         start_brcompatd
     fi
-    start_xenserverd
+
+    if [ -f /etc/xensource-inventory ]; then
+        # Start daemon to monitor external ids
+        PYTHONPATH=/usr/share/openvswitch/python \
+                   /usr/share/openvswitch/scripts/ovs-external-ids \
+                   --pidfile --detach $monitor_opt "$VSWITCHD_OVSDB_SERVER"
+    fi
+
     touch /var/lock/subsys/openvswitch
 }
 
 function stop {
-    stop_daemon XENSERVERD "$xenserverd"
     stop_daemon BRCOMPATD "$brcompatd"
     stop_daemon VSWITCHD "$vswitchd"
     stop_daemon OVSDB_SERVER "$ovsdb_server"
+    if [ -e /var/run/openvswitch/ovs-external-ids.pid ]; then
+        kill `cat /var/run/openvswitch/ovs-external-ids.pid`
+    fi
     rm -f /var/lock/subsys/openvswitch
 }
 
@@ -375,6 +455,12 @@ case "$1" in
     restart)
         restart
         ;;
+    reload|force-reload)
+        # Nothing to do to ovs-vswitchd and ovsdb-server as they keep their
+        # configuration up-to-date all the time.  HUP ovs-external-ids so it
+        # re-runs.
+        hup_monitor_external_ids
+       ;;
     strace-vswitchd)
         shift
         strace -p $(cat "$VSWITCHD_PIDFILE") "$@"
@@ -387,17 +473,15 @@ case "$1" in
         status -p "$OVSDB_SERVER_PIDFILE" ovsdb-server &&
         status -p "$VSWITCHD_PIDFILE" ovs-vswitchd &&
         (test "$ENABLE_BRCOMPAT" != "y" || 
-            status -p "$BRCOMPATD_PIDFILE" ovs-brcompatd) &&
-        status -p "$XENSERVERD_PIDFILE" ovs-xenserverd
+            status -p "$BRCOMPATD_PIDFILE" ovs-brcompatd)
         ;;
     version)
         /usr/sbin/ovsdb-server -V
         /usr/sbin/ovs-vswitchd -V
         /usr/sbin/ovs-brcompatd -V
-        /usr/sbin/ovs-xenserverd -V
         ;;
     help)
-        printf "openvswitch [start|stop|restart|unload|status|version]\n"
+        printf "openvswitch [start|stop|restart|reload|force-reload|status|version]\n"
         ;;
     *)
         printf "Unknown command: $1\n"