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