From ac9634f0af8d2cf8259a3cf7d7d6e19cc0d67457 Mon Sep 17 00:00:00 2001
From: Ben Pfaff <blp@nicira.com>
Date: Fri, 11 Sep 2009 14:41:39 -0700
Subject: [PATCH] xenserver: Make RPM install work again.

The RPM install was generating a database cache in Python pickle format in
/etc/ovs-vswitchd.conf, but interface-reconfigure was looking for it in
XML format in /var/lib/openvswitch/dbcache.  This fixes the problem, by
adding an init-dbcache command to interface-reconfigure and then using that
at RPM install time.

This moves the database cache creation from %pre to %post.  This is
necessary so that interface-reconfigure is available from the install
script.
---
 ...pt_xensource_libexec_interface-reconfigure |  6 +++
 xenserver/vswitch-xen.spec                    | 37 +++----------------
 2 files changed, 12 insertions(+), 31 deletions(-)

diff --git a/xenserver/opt_xensource_libexec_interface-reconfigure b/xenserver/opt_xensource_libexec_interface-reconfigure
index 5681d02d2..70749b8a3 100755
--- a/xenserver/opt_xensource_libexec_interface-reconfigure
+++ b/xenserver/opt_xensource_libexec_interface-reconfigure
@@ -8,6 +8,7 @@
     %(command-name)s --session <SESSION-REF> --pif <PIF-REF> [up|down|rewrite]
     %(command-name)s --force <BRIDGE> [up|down|rewrite <CONFIG>]
     %(command-name)s --force all down
+    %(command-name)s init-dbcache
 
     where,
           <CONFIG> = --device=<INTERFACE> --mode=dhcp
@@ -1441,6 +1442,9 @@ def main(argv=None):
         if len(force_rewrite_config) and not (force_interface and action == "rewrite"):
             raise Usage("\"--force rewrite\" needed for --device, --mode, --ip, --netmask, and --gateway")
 
+        if action == "init-dbcache" and arglist:
+            raise Usage("\"init-dbcache\" action does not accept any options")
+
         global db
         if force_interface:
             log("Force interface %s %s" % (force_interface, action))
@@ -1458,6 +1462,8 @@ def main(argv=None):
                     action_down(pif)
                 else:
                     raise Usage("Unknown action %s"  % action)
+        elif action == "init-dbcache":
+            DatabaseCache().save(dbcache_file)
         else:
             db = DatabaseCache(session_ref=session)
 
diff --git a/xenserver/vswitch-xen.spec b/xenserver/vswitch-xen.spec
index 886834f50..6a218ab66 100644
--- a/xenserver/vswitch-xen.spec
+++ b/xenserver/vswitch-xen.spec
@@ -106,10 +106,12 @@ rm -rf \
     $RPM_BUILD_ROOT/root/vswitch/share/man/man8/ovs-pki.8 \
     $RPM_BUILD_ROOT/root/vswitch/share/openvswitch
 
+install -d -m 755 $RPM_BUILD_ROOT/var/lib/openvswitch
+
 %clean
 rm -rf $RPM_BUILD_ROOT
 
-%pre
+%post
 if [ ! -f /etc/xensource-inventory ]; then
     printf "XenSource inventory not present in /etc/xensource-inventory"
     exit 1
@@ -146,7 +148,7 @@ EOF
     fi
 fi
 
-if test ! -e /etc/ovs-vswitch.dbcache; then
+if test ! -e /var/lib/openvswitch/dbcache; then
     if test "$1" = 1; then
         printf "Creating xapi database cache...  "
     else
@@ -155,34 +157,7 @@ if test ! -e /etc/ovs-vswitch.dbcache; then
         printf "Re-creating xapi database cache...  "
     fi
 
-    source /etc/xensource-inventory
-    if python - "$INSTALLATION_UUID" <<EOF
-import XenAPI
-import pickle
-import sys
-
-session = XenAPI.xapi_local()
-try:
-    session.xenapi.login_with_password("root", "")
-
-    vlans = session.xenapi.VLAN.get_all_records()
-    bonds = session.xenapi.Bond.get_all_records()
-    pifs = session.xenapi.PIF.get_all_records()
-    networks = session.xenapi.network.get_all_records()
-    host = session.xenapi.host.get_by_uuid(sys.argv[1])
-finally:
-    session.xenapi.session.logout()
-
-dbcache_file = "/etc/ovs-vswitch.dbcache"
-f = open(dbcache_file, 'w')
-pickle.dump({'vlans': vlans,
-             'bonds': bonds,
-             'pifs': pifs,
-             'networks': networks}, f)
-pickle.dump({'host': host}, f)
-f.close()
-EOF
-    then
+    if /root/vswitch/scripts/interface-reconfigure init-dbcache; then
         printf "done.\n"
     else
         printf "FAILED\n"
@@ -193,7 +168,6 @@ EOF
     fi
 fi
 
-%post
 if grep -F net.ipv4.conf.all.arp_filter /etc/sysctl.conf >/dev/null 2>&1; then :; else
     cat >>/etc/sysctl.conf <<EOF
 # This works around an issue in xhad, which binds to a particular
@@ -357,3 +331,4 @@ fi
 /root/vswitch/share/man/man8/ovs-dpctl.8
 /root/vswitch/share/man/man8/ovs-ofctl.8
 /root/vswitch/share/man/man8/ovs-vswitchd.8
+/var/lib/openvswitch
-- 
2.47.0