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

shaohef at linux.vnet.ibm.com shaohef at linux.vnet.ibm.com
Wed Jan 22 17:09:31 UTC 2014


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

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

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

diff --git a/src/kimchi/model.py b/src/kimchi/model.py
index 4acc5e8..64212a4 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
@@ -46,7 +47,8 @@ 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 etree, objectify
+from lxml.builder import E
 from xml.etree import ElementTree
 
 
@@ -971,6 +973,46 @@ 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)
+        if Model.dom_state_map[dom.info()[0]] != "shutoff":
+            raise InvalidOperation("do not support hot plugging attach "
+                                   "guest interface")
+
+        macs = (iface.mac.get('address')
+                for iface in self._get_vmifaces(vm))
+
+        mac = randomMAC()
+        while True:
+            if mac not in macs:
+                break
+            mac = randomMAC()
+
+        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)
@@ -1006,6 +1048,19 @@ class Model(object):
 
         return info
 
+    def vmiface_delete(self, vm, mac):
+        dom = self._get_vm(vm)
+        iface = self._get_vmiface(vm, mac)
+
+        if Model.dom_state_map[dom.info()[0]] != "shutoff":
+            raise InvalidOperation("do not support hot plugging detach "
+                                   "guest interface")
+        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