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

shaohef at linux.vnet.ibm.com shaohef at linux.vnet.ibm.com
Tue Apr 22 12:48:54 UTC 2014


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

criterias are:
1. if there are interfaces already attached to a vm, use the same model
2. if "vmifaces" element in metatdata, get the model from metatdata
3. if "OS" element in metatdata, get the model from "OS" metatdata

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

diff --git a/src/kimchi/model/vmifaces.py b/src/kimchi/model/vmifaces.py
index 30e5373..da9721b 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,28 @@ class VMIfacesModel(object):
         vmiface = objectify.fromstring(iface_xml)
         return vmiface.attrib.get("model")
 
+    def _choose_model(self, vm):
+        # first 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:
+                return iface.model.get('type')
+
+        dom = VMModel.get_vm(vm, self.conn)
+        # then get model from vmiface metadata
+        model = self._vm_get_iface_metadata(dom)
+        if model is not None:
+            return model
+
+        # 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_updata_iface_metadata(
+                dom, {"model": entry['nic_model']})
+            return entry['nic_model']
+
+        return None
+
     def create(self, vm, params):
         def randomMAC():
             mac = [0x52, 0x54, 0x00,
@@ -76,8 +99,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