[virt-tools-list] [PATCH 09/11] KvmHypervisor can now manage networks.

Darryl L. Pierce dpierce at redhat.com
Thu Apr 14 18:06:08 UTC 2011


It can define and undefine, as well as create and destroy, networks. It
can also retrieve a specific network and verify that a network exists.
---
 src/virtlib/hypervisors/kvm.py |   57 ++++++++++++++++++++++++++++++++++++++++
 1 files changed, 57 insertions(+), 0 deletions(-)

diff --git a/src/virtlib/hypervisors/kvm.py b/src/virtlib/hypervisors/kvm.py
index a69e151..ee0f29a 100644
--- a/src/virtlib/hypervisors/kvm.py
+++ b/src/virtlib/hypervisors/kvm.py
@@ -68,3 +68,60 @@ class KvmHypervisor(Hypervisor):
     def do_get_domain(self, name):
         return self.__conn.lookupByName(name)
 
+    def do_get_networks(self, defined, created):
+        result = []
+
+        if defined:
+            result.extend(self.__conn.listDefinedNetworks())
+        if created:
+            result.extend(self.__conn.listNetworks())
+
+        return result
+
+    def do_get_network(self, network):
+        return self.__conn.networkLookupByName(network)
+
+    def do_define_network(self, config):
+        name = config.name
+        ip = config.ipv4_address_raw
+        start = config.ipv4_start_address
+        end = config.ipv4_end_address
+        fw = config.physical_device
+
+        xml = "<network>" + \
+              "  <name>%s</name>\n" % name
+        if not config.public_ipv4_network:
+            if fw is not "":
+                xml += "  <forward dev='%s'/>\n" % fw
+            else:
+                xml += "  <forward/>\n"
+
+        xml += "  <ip address='%s' netmask='%s'>\n" % (str(ip[1]), str(ip.netmask()))
+        xml += "    <dhcp>\n"
+        xml += "      <range start='%s' end='%s'/>\n" % (str(start), str(end))
+        xml += "    </dhcp>\n"
+        xml += "  </ip>\n"
+        xml += "</network>\n"
+
+        print "xml:\n%s" % xml
+
+        self.__conn.networkDefineXML(xml)
+
+    def do_undefine_network(self, network):
+        _network = self.get_network(network);
+        _network.undefine()
+
+    def do_create_network(self, network):
+        _network = self.get_network(network)
+
+        if _network is not None:
+            if not _network.isActive():
+                _network.create()
+
+    def do_destroy_network(self, network):
+        _network = self.get_network(network)
+
+        if _network is not None:
+            if _network.isActive():
+                _network.destroy()
+
-- 
1.7.4.2




More information about the virt-tools-list mailing list