xenserver: Only replace XenServer hosts scripts that we recognize.
[sliver-openvswitch.git] / xenserver / openvswitch-xen.spec
1 # Spec file for Open vSwitch.
2
3 # Copyright (C) 2009, 2010, 2011, 2012 Nicira Networks, Inc.
4 #
5 # Copying and distribution of this file, with or without modification,
6 # are permitted in any medium without royalty provided the copyright
7 # notice and this notice are preserved.  This file is offered as-is,
8 # without warranty of any kind.
9
10 # When building, the rpmbuild command line should define
11 # openvswitch_version, kernel_name, kernel_version, kernel_flavor,
12 # and build_number using -D arguments.
13 # for example:
14 #
15 #    rpmbuild -D "openvswitch_version 1.1.0+build123"
16 #      -D "kernel_name  NAME-xen"
17 #      -D "kernel_version 2.6.32.12-0.7.1.xs5.6.100.323.170596"
18 #      -D "kernel_flavor xen"
19 #      -D "build_number --with-build-number=123"
20 #      -bb /usr/src/redhat/SPECS/openvswitch-xen.spec
21
22 %define xen_version %{kernel_version}%{kernel_flavor}
23
24 # bump this when breaking compatibility with userspace
25 %define module_abi_version 0
26
27 # build-supplemental-pack.sh requires this naming for kernel module packages
28 %define module_package modules-%{kernel_flavor}-%{kernel_version}
29
30 Name: openvswitch
31 Summary: Open vSwitch daemon/database/utilities
32 Group: System Environment/Daemons
33 URL: http://www.openvswitch.org/
34 Vendor: Nicira Networks, Inc.
35 Version: %{openvswitch_version}
36
37 License: ASL 2.0
38 Release: 1
39 Source: openvswitch-%{openvswitch_version}.tar.gz
40 Buildroot: /tmp/openvswitch-xen-rpm
41 Requires: openvswitch_mod.ko.%{module_abi_version}
42
43 %description
44 Open vSwitch provides standard network bridging functions augmented with
45 support for the OpenFlow protocol for remote per-flow control of
46 traffic.
47
48 %package %{module_package}
49 Summary: Open vSwitch kernel module
50 Group: System Environment/Kernel
51 License: GPLv2
52 Provides: %{name}-modules-%{kernel_flavor} = %{kernel_version}, openvswitch_mod.ko.%{module_abi_version}
53 Requires: kernel-%{kernel_name} = %{kernel_version}
54
55 %description %{module_package}
56 Open vSwitch Linux kernel module compiled against kernel version
57 %{xen_version}.
58
59 %prep
60 %setup -q -n openvswitch-%{openvswitch_version}
61
62 %build
63 ./configure --prefix=/usr --sysconfdir=/etc --localstatedir=%{_localstatedir} --with-linux=/lib/modules/%{xen_version}/build --enable-ssl %{build_number}
64 make %{_smp_mflags}
65
66 %install
67 rm -rf $RPM_BUILD_ROOT
68 make install DESTDIR=$RPM_BUILD_ROOT
69 install -d -m 755 $RPM_BUILD_ROOT/etc
70 install -d -m 755 $RPM_BUILD_ROOT/etc/init.d
71 install -m 755 xenserver/etc_init.d_openvswitch \
72          $RPM_BUILD_ROOT/etc/init.d/openvswitch
73 install -m 755 xenserver/etc_init.d_openvswitch-xapi-update \
74          $RPM_BUILD_ROOT/etc/init.d/openvswitch-xapi-update
75 install -d -m 755 $RPM_BUILD_ROOT/etc/sysconfig
76 install -d -m 755 $RPM_BUILD_ROOT/etc/logrotate.d
77 install -m 755 xenserver/etc_logrotate.d_openvswitch \
78          $RPM_BUILD_ROOT/etc/logrotate.d/openvswitch
79 install -d -m 755 $RPM_BUILD_ROOT/etc/profile.d
80 install -m 755 xenserver/etc_profile.d_openvswitch.sh \
81          $RPM_BUILD_ROOT/etc/profile.d/openvswitch.sh
82 install -d -m 755 $RPM_BUILD_ROOT/etc/xapi.d/plugins
83 install -m 755 xenserver/etc_xapi.d_plugins_openvswitch-cfg-update \
84          $RPM_BUILD_ROOT/etc/xapi.d/plugins/openvswitch-cfg-update
85 install -d -m 755 $RPM_BUILD_ROOT/usr/share/openvswitch/scripts
86 install -m 755 xenserver/opt_xensource_libexec_interface-reconfigure \
87              $RPM_BUILD_ROOT/usr/share/openvswitch/scripts/interface-reconfigure
88 install -m 644 xenserver/opt_xensource_libexec_InterfaceReconfigure.py \
89              $RPM_BUILD_ROOT/usr/share/openvswitch/scripts/InterfaceReconfigure.py
90 install -m 644 xenserver/opt_xensource_libexec_InterfaceReconfigureBridge.py \
91              $RPM_BUILD_ROOT/usr/share/openvswitch/scripts/InterfaceReconfigureBridge.py
92 install -m 644 xenserver/opt_xensource_libexec_InterfaceReconfigureVswitch.py \
93              $RPM_BUILD_ROOT/usr/share/openvswitch/scripts/InterfaceReconfigureVswitch.py
94 install -m 755 xenserver/etc_xensource_scripts_vif \
95              $RPM_BUILD_ROOT/usr/share/openvswitch/scripts/vif
96 install -m 755 xenserver/usr_share_openvswitch_scripts_ovs-xapi-sync \
97                $RPM_BUILD_ROOT/usr/share/openvswitch/scripts/ovs-xapi-sync
98 install -m 755 xenserver/usr_share_openvswitch_scripts_sysconfig.template \
99          $RPM_BUILD_ROOT/usr/share/openvswitch/scripts/sysconfig.template
100 install -d -m 755 $RPM_BUILD_ROOT/usr/lib/xsconsole/plugins-base
101 install -m 644 \
102         xenserver/usr_lib_xsconsole_plugins-base_XSFeatureVSwitch.py \
103                $RPM_BUILD_ROOT/usr/lib/xsconsole/plugins-base/XSFeatureVSwitch.py
104
105 install -d -m 755 $RPM_BUILD_ROOT/lib/modules/%{xen_version}/extra/openvswitch
106 find datapath/linux -name *.ko -exec install -m 755  \{\} $RPM_BUILD_ROOT/lib/modules/%{xen_version}/extra/openvswitch \;
107 install python/compat/uuid.py $RPM_BUILD_ROOT/usr/share/openvswitch/python
108 install python/compat/argparse.py $RPM_BUILD_ROOT/usr/share/openvswitch/python
109
110 install -d -m 755 $RPM_BUILD_ROOT/etc/xensource/bugtool
111 mv $RPM_BUILD_ROOT/usr/share/openvswitch/bugtool-plugins/* $RPM_BUILD_ROOT/etc/xensource/bugtool
112
113 # Get rid of stuff we don't want to make RPM happy.
114 rm \
115     $RPM_BUILD_ROOT/usr/bin/ovs-benchmark \
116     $RPM_BUILD_ROOT/usr/sbin/ovs-bugtool \
117     $RPM_BUILD_ROOT/usr/bin/ovs-controller \
118     $RPM_BUILD_ROOT/usr/bin/ovs-pki \
119     $RPM_BUILD_ROOT/usr/bin/ovs-test \
120     $RPM_BUILD_ROOT/usr/share/man/man8/ovs-test.8 \
121     $RPM_BUILD_ROOT/usr/share/man/man1/ovs-benchmark.1 \
122     $RPM_BUILD_ROOT/usr/share/man/man8/ovs-bugtool.8 \
123     $RPM_BUILD_ROOT/usr/share/man/man8/ovs-controller.8 \
124     $RPM_BUILD_ROOT/usr/share/man/man8/ovs-pki.8
125
126 install -d -m 755 $RPM_BUILD_ROOT/var/lib/openvswitch
127
128 %clean
129 rm -rf $RPM_BUILD_ROOT
130
131 %post
132 # A list of Citrix XenServer scripts that we might need to replace
133 # with our own versions.
134 scripts="
135     /etc/xensource/scripts/vif
136     /opt/xensource/libexec/InterfaceReconfigure.py
137     /opt/xensource/libexec/InterfaceReconfigureBridge.py
138     /opt/xensource/libexec/InterfaceReconfigureVswitch.py
139     /opt/xensource/libexec/interface-reconfigure"
140
141 # Calculate into $md5sums a comma-separated set of md5sums of the
142 # Citrix XenServer scripts that we might need to replace.  We might be
143 # upgrading an older version of the package that moved the files out
144 # of the way, so we need to look for the files in those out-of-the-way
145 # locations first.
146 md5sums=
147 for script in $scripts; do
148     b=$(basename "$script")
149     if test -e /usr/lib/openvswitch/xs-saved/"$b"; then
150         f=/usr/lib/openvswitch/xs-saved/"$b"
151     elif test -e /usr/lib/openvswitch/xs-original/"$b"; then
152         f=/usr/lib/openvswitch/xs-original/"$b"
153     elif test -e "$script" && test ! -h "$script"; then
154         f=$script
155     else
156         printf "\n$script: not found\n"
157         f=/dev/null
158     fi
159     md5sums="$md5sums,$(md5sum $f | awk '{print $1}')"
160 done
161 md5sums=${md5sums#,}
162
163 # Now check the md5sums against the known sets of md5sums:
164 #
165 #   - If they are known to be a version of XenServer scripts that we should
166 #     replace, we replace them (by putting $scripts into $replace_files).
167 #
168 #   - Otherwise, we guess that it's better not to replace them, because the
169 #     improvements that our versions of the scripts provide are minimal, so
170 #     it's better to avoid possibly breaking any changes made upstream by
171 #     Citrix.
172 case $md5sums in
173     cf09a68d9f8b434e79a4c83b01a3bb4b,395866df1b0b20c12c4dd2f7de0ecdb4,9d493545ae81463239d3162cbc798852,862d0939b441de9264a900628e950fe9,21f85db25599d7f026cd489385d58aa6)
174         keep_files=
175         replace_files=$scripts
176         printf "\nVerified host scripts from XenServer 6.0.0.\n"
177         ;;
178         
179     c5f48246577a17cf1b971fb5ce4e920b,2e2c912f86f9c536c89adc34ff3c2b2b,28d3ff72d72bdec4f37d70699f5edb76,67e1d0af16fc1ddf10009c5c063ad2ba,24bae6906d182ba47668174f8e480cc6)
180         keep_files=
181         replace_files=$scripts
182         printf "\nVerified host scripts from XenServer 5.6-FP1.\n"
183         ;;
184
185     *)
186         keep_files=$scripts
187         replace_files=
188         cat <<EOF
189
190 The host scripts on this machine are not those of any supported
191 version of XenServer.  On XenServer earlier than 5.6-FP1, your Open
192 vSwitch installation will not work.  On XenServer 5.6-FP1 or later,
193 Open vSwitch is not verified to work, which could lead to unexpected
194 behavior.
195
196 EOF
197         ;;
198 esac
199
200 if grep -F net.ipv4.conf.all.arp_filter /etc/sysctl.conf >/dev/null 2>&1; then :; else
201     cat >>/etc/sysctl.conf <<EOF
202 # This works around an issue in xhad, which binds to a particular
203 # Ethernet device, which in turn causes ICMP port unreachable messages
204 # if packets are received are on the wrong interface, which in turn
205 # can happen if we send out ARP replies on every interface (as Linux
206 # does by default) instead of just on the interface that has the IP
207 # address being ARPed for, which this sysctl setting in turn works
208 # around.
209 #
210 # Bug #1378.
211 net.ipv4.conf.all.arp_filter = 1
212 EOF
213 fi
214
215 if test ! -e /etc/openvswitch/conf.db; then
216     install -d -m 755 -o root -g root /etc/openvswitch
217
218     # Create ovs-vswitchd config database
219     ovsdb-tool -vANY:console:off create /etc/openvswitch/conf.db \
220             /usr/share/openvswitch/vswitch.ovsschema
221
222     # Create initial table in config database
223     ovsdb-tool -vANY:console:off transact /etc/openvswitch/conf.db \
224             '[{"op": "insert", "table": "Open_vSwitch", "row": {}}]' \
225             > /dev/null
226 fi
227
228 # Create default or update existing /etc/sysconfig/openvswitch.
229 SYSCONFIG=/etc/sysconfig/openvswitch
230 TEMPLATE=/usr/share/openvswitch/scripts/sysconfig.template
231 if [ ! -e $SYSCONFIG ]; then
232     cp $TEMPLATE $SYSCONFIG
233 else
234     for var in $(awk -F'[ :]' '/^# [_A-Z0-9]+:/{print $2}' $TEMPLATE)
235     do
236         if ! grep $var $SYSCONFIG >/dev/null 2>&1; then
237             echo >> $SYSCONFIG
238             sed -n "/$var:/,/$var=/p" $TEMPLATE >> $SYSCONFIG
239         fi
240     done
241 fi
242
243 # Deliberately break %postun in broken OVS builds that revert original
244 # XenServer scripts during rpm -U by moving the directory where it thinks
245 # they are saved.
246 if [ -d /usr/lib/openvswitch/xs-original ]; then
247     mkdir -p /usr/lib/openvswitch/xs-saved
248     mv /usr/lib/openvswitch/xs-original/* /usr/lib/openvswitch/xs-saved/ &&
249         rmdir /usr/lib/openvswitch/xs-original
250 fi
251
252 # Replace XenServer files by our versions.
253 mkdir -p /usr/lib/openvswitch/xs-saved \
254     || printf "Could not create script backup directory.\n"
255 for f in $replace_files; do
256     s=$(basename "$f")
257     t=$(readlink "$f")
258     if [ -f "$f" ] && [ "$t" != "/usr/share/openvswitch/scripts/$s" ]; then
259         mv "$f" /usr/lib/openvswitch/xs-saved/ \
260             || printf "Could not save original XenServer $s script\n"
261         ln -s "/usr/share/openvswitch/scripts/$s" "$f" \
262             || printf "Could not link to Open vSwitch $s script\n"
263     fi
264 done
265
266 # Clean up dangling symlinks to removed OVS replacement scripts no longer
267 # provided by OVS. Any time a replacement script is removed from OVS,
268 # it should be added here to ensure correct reversion from old versions of
269 # OVS that don't clean up dangling symlinks during the uninstall phase.
270 for orig in /usr/sbin/brctl /usr/sbin/xen-bugtool $keep_files; do
271     saved=/usr/lib/openvswitch/xs-saved/$(basename "$orig")
272     [ -e "$saved" ] && mv -f "$saved" "$orig"
273 done
274
275 # Ensure all required services are set to run
276 for s in openvswitch openvswitch-xapi-update; do
277     if chkconfig --list $s >/dev/null 2>&1; then
278         chkconfig --del $s || printf "Could not remove $s init script."
279     fi
280     chkconfig --add $s || printf "Could not add $s init script."
281     chkconfig $s on || printf "Could not enable $s init script."
282 done
283
284 if [ "$1" = "1" ]; then    # $1 = 1 for install
285     # Configure system to use Open vSwitch
286     /opt/xensource/bin/xe-switch-network-backend vswitch
287 else    # $1 = 2 for upgrade
288
289     mode=$(cat /etc/xensource/network.conf)
290     if [ "$mode" != "vswitch" ] && [ "$mode" != "openvswitch" ]; then
291         printf "\nThe server is not configured to run Open vSwitch.  To run in\n"
292         printf "vswitch mode, you must run the following command:\n\n"
293         printf "\txe-switch-network-backend vswitch"
294     else
295         printf "\nTo use the new Open vSwitch install, you should reboot the\n"
296         printf "server now.  Failure to do so may result in incorrect operation."
297     fi
298
299     printf "\n\n"
300 fi
301
302 %posttrans %{module_package}
303 # Ensure that modprobe will find our modules.
304 #
305 # This has to be in %posttrans instead of %post because older versions
306 # installed modules into a different directory and "rpm -U" runs the
307 # new version's %post before removing the old version's files, so if
308 # we use %post then depmod may find the old versions that are about to
309 # be removed.
310 depmod %{xen_version}
311
312 %preun
313 if [ "$1" = "0" ]; then     # $1 = 0 for uninstall
314     # Configure system to use bridge
315     /opt/xensource/bin/xe-switch-network-backend bridge
316
317     # The "openvswitch" service should have been removed from
318     # "xe-switch-network-backend bridge".
319     for s in openvswitch openvswitch-xapi-update; do
320         if chkconfig --list $s >/dev/null 2>&1; then
321             chkconfig --del $s || printf "Could not remove $s init script."
322         fi
323     done
324 fi
325
326 %postun
327 # Restore original XenServer scripts if the OVS equivalent no longer exists.
328 # This works both in the upgrade and erase cases.
329 # This lists every file that every version of OVS has ever replaced. Never
330 # remove old files that OVS no longer replaces, or upgrades from old versions
331 # will fail to restore the XS originals, leaving the system in a broken state.
332 # Also be sure to add removed script paths to the %post scriptlet above to
333 # prevent the same problem when upgrading from old versions of OVS that lack
334 # this restore-on-upgrade logic.
335 for f in \
336     /etc/xensource/scripts/vif \
337     /usr/sbin/brctl \
338     /usr/sbin/xen-bugtool \
339     /opt/xensource/libexec/interface-reconfigure \
340     /opt/xensource/libexec/InterfaceReconfigure.py \
341     /opt/xensource/libexec/InterfaceReconfigureBridge.py \
342     /opt/xensource/libexec/InterfaceReconfigureVswitch.py
343 do
344     # Only revert dangling symlinks.
345     if [ -h "$f" ] && [ ! -e "$f" ]; then
346         s=$(basename "$f")
347         if [ ! -f "/usr/lib/openvswitch/xs-saved/$s" ]; then
348             printf "Original XenServer $s script not present in /usr/lib/openvswitch/xs-saved\n" >&2
349             printf "Could not restore original XenServer script.\n" >&2
350         else
351             (rm -f "$f" \
352                 && mv "/usr/lib/openvswitch/xs-saved/$s" "$f") \
353                 || printf "Could not restore original XenServer $s script.\n" >&2
354         fi
355     fi
356 done
357
358 if [ "$1" = "0" ]; then     # $1 = 0 for uninstall
359     rm -f /usr/lib/xsconsole/plugins-base/XSFeatureVSwitch.pyc \
360         /usr/lib/xsconsole/plugins-base/XSFeatureVSwitch.pyo
361
362     rm -f /usr/share/openvswitch/scripts/InterfaceReconfigure.pyc \
363         /usr/share/openvswitch/scripts/InterfaceReconfigure.pyo \
364         /usr/share/openvswitch/scripts/InterfaceReconfigureBridge.pyc \
365         /usr/share/openvswitch/scripts/InterfaceReconfigureBridge.pyo \
366         /usr/share/openvswitch/scripts/InterfaceReconfigureVSwitch.pyc \
367         /usr/share/openvswitch/scripts/InterfaceReconfigureVSwitch.pyo
368
369     # Remove all configuration files
370     rm -f /etc/openvswitch/conf.db
371     rm -f /etc/sysconfig/openvswitch
372     rm -f /etc/openvswitch/vswitchd.cacert
373
374     # Remove saved XenServer scripts directory, but only if it's empty
375     rmdir -p /usr/lib/openvswitch/xs-saved 2>/dev/null
376 fi
377
378 exit 0
379
380 %files
381 %defattr(-,root,root)
382 /etc/init.d/openvswitch
383 /etc/init.d/openvswitch-xapi-update
384 /etc/xapi.d/plugins/openvswitch-cfg-update
385 /etc/xensource/bugtool/*
386 /etc/logrotate.d/openvswitch
387 /etc/profile.d/openvswitch.sh
388 /usr/share/openvswitch/python/
389 /usr/share/openvswitch/scripts/ovs-xapi-sync
390 /usr/share/openvswitch/scripts/interface-reconfigure
391 /usr/share/openvswitch/scripts/InterfaceReconfigure.py
392 /usr/share/openvswitch/scripts/InterfaceReconfigureBridge.py
393 /usr/share/openvswitch/scripts/InterfaceReconfigureVswitch.py
394 /usr/share/openvswitch/scripts/vif
395 /usr/share/openvswitch/scripts/sysconfig.template
396 /usr/share/openvswitch/scripts/ovs-bugtool-*
397 /usr/share/openvswitch/scripts/ovs-save
398 /usr/share/openvswitch/scripts/ovs-ctl
399 /usr/share/openvswitch/scripts/ovs-lib
400 /usr/share/openvswitch/vswitch.ovsschema
401 /usr/sbin/ovs-vlan-bug-workaround
402 /usr/sbin/ovs-vswitchd
403 /usr/sbin/ovsdb-server
404 /usr/bin/ovs-appctl
405 /usr/bin/ovs-dpctl
406 /usr/bin/ovs-ofctl
407 /usr/bin/ovs-parse-leaks
408 /usr/bin/ovs-pcap
409 /usr/bin/ovs-tcpundump
410 /usr/bin/ovs-vlan-test
411 /usr/bin/ovs-vsctl
412 /usr/bin/ovsdb-client
413 /usr/bin/ovsdb-tool
414 /usr/lib/xsconsole/plugins-base/XSFeatureVSwitch.py
415 /usr/share/man/man1/ovsdb-client.1.gz
416 /usr/share/man/man1/ovsdb-server.1.gz
417 /usr/share/man/man1/ovsdb-tool.1.gz
418 /usr/share/man/man5/ovs-vswitchd.conf.db.5.gz
419 /usr/share/man/man8/ovs-appctl.8.gz
420 /usr/share/man/man8/ovs-ctl.8.gz
421 /usr/share/man/man8/ovs-dpctl.8.gz
422 /usr/share/man/man8/ovs-ofctl.8.gz
423 /usr/share/man/man8/ovs-parse-leaks.8.gz
424 /usr/share/man/man1/ovs-pcap.1.gz
425 /usr/share/man/man1/ovs-tcpundump.1.gz
426 /usr/share/man/man8/ovs-vlan-bug-workaround.8.gz
427 /usr/share/man/man8/ovs-vlan-test.8.gz
428 /usr/share/man/man8/ovs-vsctl.8.gz
429 /usr/share/man/man8/ovs-vswitchd.8.gz
430 /var/lib/openvswitch
431 %exclude /usr/lib/xsconsole/plugins-base/*.py[co]
432 %exclude /usr/sbin/ovs-brcompatd
433 %exclude /usr/share/man/man8/ovs-brcompatd.8.gz
434 %exclude /usr/share/openvswitch/scripts/*.py[co]
435 %exclude /usr/share/openvswitch/python/*.py[co]
436 %exclude /usr/share/openvswitch/python/ovs/*.py[co]
437 %exclude /usr/share/openvswitch/python/ovs/db/*.py[co]
438
439 %files %{module_package}
440 /lib/modules/%{xen_version}/extra/openvswitch/openvswitch_mod.ko
441 %exclude /lib/modules/%{xen_version}/extra/openvswitch/brcompat_mod.ko