[Kimchi-devel] [PATCH 1/4] support create/delete VMIface: update model

shaohef at linux.vnet.ibm.com shaohef at linux.vnet.ibm.com
Tue Jan 21 17:25:17 UTC 2014


From: ShaoHe Feng <shaohef at linux.vnet.ibm.com>

attach a network interface to vm in model
detach a network interface for vm in model

Signed-off-by: ShaoHe Feng <shaohef at linux.vnet.ibm.com>
---
 src/kimchi/model.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 50 insertions(+), 1 deletion(-)

diff --git a/src/kimchi/model.py b/src/kimchi/model.py
index 383509d..fbc920d 100644
--- a/src/kimchi/model.py
+++ b/src/kimchi/model.py
@@ -33,6 +33,7 @@ import logging
 import os
 import platform
 import psutil
+import random
 import re
 import shutil
 import subprocess
@@ -45,7 +46,7 @@ import uuid
 from cherrypy.process.plugins import BackgroundTask
 from cherrypy.process.plugins import SimplePlugin
 from collections import defaultdict
-from lxml import objectify
+from lxml import objectify, etree
 from xml.etree import ElementTree
 
 
@@ -968,6 +969,44 @@ class Model(object):
                     iface.destroy()
                 iface.undefine()
 
+    def vmifaces_create(self, vm, params):
+        def randomMAC():
+            mac = [0x52, 0x54, 0x00,
+                   random.randint(0x00, 0x7f),
+                   random.randint(0x00, 0xff),
+                   random.randint(0x00, 0xff)]
+            return ':'.join(map(lambda x: "%02x" % x, mac))
+
+        if (params["type"] == "network" and
+            params["network"] not in self.networks_get_list()):
+            raise InvalidParameter("%s is not an available network" %
+                                   params["network"])
+
+        dom = self._get_vm(vm)
+        macs = (iface.mac.get('address')
+                for iface in self._get_vmifaces(vm))
+
+        mac = randomMAC()
+        while True:
+            if mac not in macs:
+                break
+            mac = randomMAC()
+
+        E = objectify.E
+        children = [E.mac(address=mac)]
+        ("network" in params.keys() and
+         children.append(E.source(network=params['network'])))
+        ("model" in params.keys() and
+         children.append(E.model(type=params['model'])))
+        attrib = {"type": params["type"]}
+
+        xml =  etree.tostring(E.interface(*children, **attrib))
+
+        dom.attachDeviceFlags(xml,
+                              libvirt.VIR_DOMAIN_AFFECT_CURRENT)
+
+        return mac
+
     def _get_vmifaces(self, vm):
         dom = self._get_vm(vm)
         xml = dom.XMLDesc(0)
@@ -1003,6 +1042,16 @@ class Model(object):
 
         return info
 
+    def vmiface_delete(self, vm, mac):
+        dom = self._get_vm(vm)
+        iface = self._get_vmiface(vm, mac)
+
+        if iface is None:
+            raise NotFoundError('iface: "%s"' % mac)
+
+        dom.detachDeviceFlags(etree.tostring(iface),
+                              libvirt.VIR_DOMAIN_AFFECT_CURRENT)
+
     def add_task(self, target_uri, fn, opaque=None):
         id = self.next_taskid
         self.next_taskid = self.next_taskid + 1
-- 
1.8.4.2




More information about the Kimchi-devel mailing list