+function wait_device () {
+ tapname=$1; shift
+ timeout=$1; shift
+
+ expire=$(($(date +%s) + $timeout))
+
+ while ! ip link show up | egrep -q "^[0-9]+: +$tapname:"; do
+ echo "Waiting for $tapname to come UP...$(($expire - $(date +%s)))s left" >&2
+ sleep 1
+ [ $(date +%s) -ge $expire ] && return 1
+ done
+ return 0
+}
+
+######################################## startup
+function start_db () {
+ get_params "" "$@"
+
+ ## init conf
+ conf_dir=$(dirname $DB_CONF_FILE)
+ [ -d $conf_dir ] || mkdir -p $conf_dir
+ [ -f $DB_CONF_FILE ] || ovsdb-tool create $DB_CONF_FILE $DB_SCHEMA
+
+ ## init run
+ [ -d $RUN_DIR ] || mkdir -p $RUN_DIR
+
+ ## check
+ [ -f $DB_CONF_FILE ] || { echo "Could not initialize $DB_CONF_FILE - exiting" ; exit 1 ; }
+ [ -d $RUN_DIR ] || { echo "Could not initialize $RUN_DIR - exiting" ; exit 1 ; }
+
+ ## run the stuff
+ if [ ! -f "$DB_PID_FILE" ]; then
+ ovsdb-server --remote=punix:$DB_SOCKET \
+ --remote=db:Open_vSwitch,manager_options \
+ --private-key=db:SSL,private_key \
+ --certificate=db:SSL,certificate \
+ --bootstrap-ca-cert=db:SSL,ca_cert \
+ --pidfile=$DB_PID_FILE \
+ --log-file=$DB_LOG \
+ --unixctl=$DB_CTRL_SOCKET \
+ --detach >& /dev/null
+ else
+ echo 'ovsdb-server appears to be running already, *not* starting'
+ fi
+ wait_server $DB_PID_FILE ovsdb-server 30
+ echo $DB_PID_FILE
+}
+
+function start_switch () {
+ get_params "" "$@"
+
+ # ensure ovsdb-server is running
+ is_db_running || { echo "ovsdb-server not running" >&2 ; exit 1 ; }
+
+ if [ ! -f "$SWITCH_PID_FILE" ] ; then
+ ovs-vswitchd \
+ --pidfile=$SWITCH_PID_FILE \
+ --log-file=$SWITCH_LOG \
+ --unixctl=$SWITCH_SOCKET \
+ --detach \
+ unix:$DB_SOCKET >& /dev/null
+ else
+ echo 'ovs-vswitchd appears to be running already, *not* starting'
+ fi
+ wait_server $SWITCH_PID_FILE ovs-vswitchd 30
+}
+
+# first dumb stab just read "pkill ovsdb-server" and "pkill ovs-vswitchd"
+# quick and dirty : we locate the control file through a search in /var/run
+# caller should be requested to remember and provide this pid instead
+function stop_db () {
+ controlfile=$(ls $RUN_DIR/$DB_CTL_PATTERN)
+ [ -f $controlfile ] && ovs-appctl --target=$controlfile exit
+}
+
+function stop_switch () {
+ ovs-appctl --target=$SWITCH_SOCKET exit || :
+}
+
+function status () {
+ pids=$(pgrep '^ovs')
+ [ -n "$pids" ] && ps $pids
+}
+
+function start () {
+ start_db
+ start_switch
+}