To support vlan tagged virtual network, kimchi needs create the underlying
vlan and bridge interface. Libvirt's interface driver can do it with
a given XML definition. This patch targets to generate the XML according
to the interface and vlan id.
Signed-off-by: Mark Wu <wudxw(a)linux.vnet.ibm.com>
---
Changes:
v4->v5:
Add missing dependency of python-lxml
contrib/DEBIAN/control.in | 1 +
contrib/kimchi.spec.fedora.in | 1 +
contrib/kimchi.spec.suse.in | 1 +
docs/README.md | 6 +++---
src/kimchi/networkxml.py | 19 +++++++++++++++++++
tests/test_networkxml.py | 22 ++++++++++++++++++++++
tests/utils.py | 6 ++++++
7 files changed, 53 insertions(+), 3 deletions(-)
diff --git a/contrib/DEBIAN/control.in b/contrib/DEBIAN/control.in
index eecfb27..271bee4 100644
--- a/contrib/DEBIAN/control.in
+++ b/contrib/DEBIAN/control.in
@@ -18,6 +18,7 @@ Depends: python-cherrypy3 (>= 3.2.0),
python-ethtool,
sosreport,
python-ipaddr,
+ python-lxml,
open-iscsi
Build-Depends:
Maintainer: Aline Manera <alinefm(a)br.ibm.com>
diff --git a/contrib/kimchi.spec.fedora.in b/contrib/kimchi.spec.fedora.in
index 38c72f1..cc8c298 100644
--- a/contrib/kimchi.spec.fedora.in
+++ b/contrib/kimchi.spec.fedora.in
@@ -24,6 +24,7 @@ Requires: python-jsonschema >= 1.3.0
Requires: python-ethtool
Requires: sos
Requires: python-ipaddr
+Requires: python-lxml
Requires: nfs-utils
Requires: iscsi-initiator-utils
diff --git a/contrib/kimchi.spec.suse.in b/contrib/kimchi.spec.suse.in
index 7be4984..d1aec7b 100644
--- a/contrib/kimchi.spec.suse.in
+++ b/contrib/kimchi.spec.suse.in
@@ -19,6 +19,7 @@ Requires: python-psutil >= 0.6.0
Requires: python-jsonschema >= 1.3.0
Requires: python-ethtool
Requires: python-ipaddr
+Requires: python-lxml
Requires: nfs-client
Requires: iscsi-initiator-utils
diff --git a/docs/README.md b/docs/README.md
index e7599ec..1ccea01 100644
--- a/docs/README.md
+++ b/docs/README.md
@@ -37,7 +37,7 @@ Install Dependencies
libvirt libxml2-python python-imaging \
PyPAM m2crypto python-jsonschema rpm-build \
qemu-kvm python-psutil python-ethtool sos \
- python-ipaddr nfs-utils
+ python-ipaddr python-lxml nfs-utils
# If using RHEL6, install the following additional packages:
$ sudo yum install python-unittest2 python-ordereddict
# Restart libvirt to allow configuration changes to take effect
@@ -58,7 +58,7 @@ for more information on how to configure your system to access this
repository.
libvirt-bin python-libxml2 python-imaging \
python-pam python-m2crypto python-jsonschema \
qemu-kvm libtool python-psutil python-ethtool \
- sosreport python-ipaddr nfs-common
+ sosreport python-ipaddr python-lxml nfs-common
Packages version requirement:
python-jsonschema >= 1.3.0
@@ -71,7 +71,7 @@ for more information on how to configure your system to access this
repository.
libvirt python-libxml2 python-imaging \
python-pam python-M2Crypto python-jsonschema \
rpm-build kvm python-psutil python-ethtool \
- python-ipaddr nfs-client
+ python-ipaddr python-lxml nfs-client
Packages version requirement:
python-psutil >= 0.6.0
diff --git a/src/kimchi/networkxml.py b/src/kimchi/networkxml.py
index 786cb69..63cb210 100644
--- a/src/kimchi/networkxml.py
+++ b/src/kimchi/networkxml.py
@@ -21,6 +21,10 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import ipaddr
+import lxml.etree as ET
+
+
+from lxml.builder import E
# FIXME, do not support ipv6
@@ -109,3 +113,18 @@ def to_network_xml(**kwargs):
</network>
""" % params
return xml
+
+
+def create_vlan_tagged_bridge_xml(bridge, interface, vlan_id):
+ vlan = E.vlan(E.interface(name=interface))
+ vlan.set('tag', vlan_id)
+ m = E.interface(
+ E.start(mode='onboot'),
+ E.bridge(
+ E.interface(
+ vlan,
+ type='vlan',
+ name='.'.join([interface, vlan_id]))),
+ type='bridge',
+ name=bridge)
+ return ET.tostring(m)
diff --git a/tests/test_networkxml.py b/tests/test_networkxml.py
index 3073bce..42b3ea9 100644
--- a/tests/test_networkxml.py
+++ b/tests/test_networkxml.py
@@ -25,6 +25,9 @@ import unittest
import kimchi.networkxml as nxml
+import utils
+
+
from kimchi.xmlutils import xpath_get_text
@@ -151,3 +154,22 @@ class NetworkXmlTests(unittest.TestCase):
netmask = xpath_get_text(xml, "/network/ip/@netmask")[0]
self.assertEquals(netmask,
str(ipaddr.IPNetwork(params["net"]).netmask))
+
+
+class InterfaceXmlTests(unittest.TestCase):
+
+ def test_vlan_tagged_bridge_no_ip(self):
+ expected_xml = """
+ <interface type='bridge' name='br10'>
+ <start mode='onboot'/>
+ <bridge>
+ <interface type='vlan' name='em1.10'>
+ <vlan tag='10'>
+ <interface name='em1'/>
+ </vlan>
+ </interface>
+ </bridge>
+ </interface>
+ """
+ actual_xml = nxml.create_vlan_tagged_bridge_xml('br10', 'em1',
'10')
+ self.assertEquals(actual_xml, utils.normalize_xml(expected_xml))
diff --git a/tests/utils.py b/tests/utils.py
index 008f668..79fc2e2 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -32,6 +32,7 @@ import unittest
from contextlib import closing
+from lxml import etree
import kimchi.server
@@ -159,3 +160,8 @@ def patch_auth():
import kimchi.auth
kimchi.auth.authenticate = _authenticate
+
+
+def normalize_xml(xml_str):
+ return etree.tostring(etree.fromstring(xml_str,
+ etree.XMLParser(remove_blank_text=True)))
--
1.8.4.2