[Kimchi-devel] [PATCH V2 2/2] add a smart way to get interface model

shaohef at linux.vnet.ibm.com shaohef at linux.vnet.ibm.com
Wed Apr 23 15:06:59 UTC 2014


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

criterias are:
1. if "ifaces" element in metatdata, get the model from metatdata
2. if "ifaces" metadata does not exist AND there are interfaces, use
the same model
3. if no "ifaces" metadata, no existing interfaces model,
AND there is "OS" element in metatdata, get the model from "OS" metatdata
4. let libvirt choose model

Signed-off-by: ShaoHe Feng <shaohef at linux.vnet.ibm.com>
---
 src/kimchi/model/vmifaces.py | 34 ++++++++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)

diff --git a/src/kimchi/model/vmifaces.py b/src/kimchi/model/vmifaces.py
index 5b8845f..226694b 100644
--- a/src/kimchi/model/vmifaces.py
+++ b/src/kimchi/model/vmifaces.py
@@ -23,6 +23,7 @@ import libvirt
 from lxml import etree, objectify
 from lxml.builder import E
 
+from kimchi import osinfo
 from kimchi.exception import InvalidOperation, InvalidParameter, NotFoundError
 from kimchi.model.utils import get_vm_metadata_element
 from kimchi.model.utils import set_vm_metadata_element
@@ -45,6 +46,31 @@ class VMIfacesModel(object):
         vmiface = objectify.fromstring(iface_xml)
         return vmiface.attrib.get("model")
 
+    def _choose_model(self, vm):
+        dom = VMModel.get_vm(vm, self.conn)
+        # first get model from vmiface metadata
+        model = self._vm_get_iface_metadata(dom)
+        if model is not None:
+            return model
+
+        # then find a model from the exist iface of vm
+        for iface in VMIfacesModel.get_vmifaces(vm, self.conn):
+            if iface.find("model") is not None:
+                VMIfaceModel.vm_update_iface_metadata(
+                    dom, {"model": iface.model.get('type')})
+                return iface.model.get('type')
+
+        # at last from OS metadata
+        distro, version = VMModel.vm_get_os_metadata(dom)
+        if distro is not None:
+            entry = osinfo.lookup(distro, version)
+            VMIfaceModel.vm_update_iface_metadata(
+                dom, {"model": entry['nic_model']})
+            return entry['nic_model']
+
+        # None, let libvirt to choose model
+        return None
+
     def create(self, vm, params):
         def randomMAC():
             mac = [0x52, 0x54, 0x00,
@@ -76,8 +102,12 @@ class VMIfacesModel(object):
         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'])))
+
+        model = params.get("model")
+        if "network" in params and model is None:
+            model = self._choose_model(vm)
+        model is not None and children.append(E.model({"type": model}))
+
         attrib = {"type": params["type"]}
 
         xml = etree.tostring(E.interface(*children, **attrib))
-- 
1.9.0




More information about the Kimchi-devel mailing list