On 04/22/2014 09:48 AM, shaohef(a)linux.vnet.ibm.com wrote:
From: ShaoHe Feng <shaohef(a)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
What is the difference between getting the model from "vmifaces" and
"OS" metadata?
From my view, the "vmifaces" element was set based on OS during VM
creation so it will be the same
if we get "OS" element and find the iface model on osinfo.py dict
I'd say the criterias are:
1) "vmifaces" metadata
2) if "vmifaces" metadata does not exist AND there are interfaces, use
the same model
3) if "vmifaces" metadata does not exist AND there is NOT interface,
leave model empty to libvirt fill it properly
Signed-off-by: ShaoHe Feng <shaohef(a)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))