[PATCH V2 0/2] add a smart way to get interface model

From: ShaoHe Feng <shaohef@linux.vnet.ibm.com> V1 -> V2 fist choose model from iface metadata. this patch depends on [PATCH V5 0/3] bug fix: get user and group when VM is running Also we should set the interface model into metadata when the user change the model of interface. not implement, for it depends on: [PATCH V2 1/4] vmiface update support: update API.md ShaoHe Feng (2): add two moethod to get/set vm iface model in metadata add a smart way to get interface model src/kimchi/model/vmifaces.py | 50 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) -- 1.9.0

From: ShaoHe Feng <shaohef@linux.vnet.ibm.com> We store the model in metadata. We can get it from metadata when we create a vm iface. Signed-off-by: ShaoHe Feng <shaohef@linux.vnet.ibm.com> --- src/kimchi/model/vmifaces.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/kimchi/model/vmifaces.py b/src/kimchi/model/vmifaces.py index 9bf110e..5b8845f 100644 --- a/src/kimchi/model/vmifaces.py +++ b/src/kimchi/model/vmifaces.py @@ -24,6 +24,8 @@ from lxml import etree, objectify from lxml.builder import E 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 from kimchi.model.vms import DOM_STATE_MAP, VMModel @@ -37,6 +39,12 @@ class VMIfacesModel(object): macs.append(iface.mac.get('address')) return macs + def _vm_get_iface_metadata(self, dom): + iface_xml = (get_vm_metadata_element(dom, "iface") or + """<iface></iface>""") + vmiface = objectify.fromstring(iface_xml) + return vmiface.attrib.get("model") + def create(self, vm, params): def randomMAC(): mac = [0x52, 0x54, 0x00, @@ -99,6 +107,14 @@ class VMIfaceModel(object): return iface return None + @staticmethod + def vm_update_iface_metadata(dom, params): + model = params.get("model") + if model is None: + return + iface = E.iface({"model": model}) + set_vm_metadata_element(dom, etree.tostring(iface)) + def lookup(self, vm, mac): info = {} -- 1.9.0

From: ShaoHe Feng <shaohef@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@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
participants (1)
-
shaohef@linux.vnet.ibm.com