[Kimchi-devel] [PATCH V3 2/3] VM Edit CPU/Memory: (Backend) Changes VM control and model

Rodrigo Trujillo rodrigo.trujillo at linux.vnet.ibm.com
Fri Apr 11 18:38:07 UTC 2014


This patch changes vm control file in order to allow user to change cpus
and memory. It also changes vms model in order to change xml properly
and test if vm is running.

Signed-off-by: Rodrigo Trujillo <rodrigo.trujillo at linux.vnet.ibm.com>
---
 src/kimchi/control/vms.py |  4 ++--
 src/kimchi/model/vms.py   | 43 +++++++++++++++++++++++++++----------------
 2 files changed, 29 insertions(+), 18 deletions(-)

diff --git a/src/kimchi/control/vms.py b/src/kimchi/control/vms.py
index e75b27f..0964fb7 100644
--- a/src/kimchi/control/vms.py
+++ b/src/kimchi/control/vms.py
@@ -32,7 +32,7 @@ class VMs(Collection):
 class VM(Resource):
     def __init__(self, model, ident):
         super(VM, self).__init__(model, ident)
-        self.update_params = ["name"]
+        self.update_params = ["name", "cpus", "memory"]
         self.screenshot = VMScreenShot(model, ident)
         self.uri_fmt = '/vms/%s'
         for ident, node in sub_nodes.items():
@@ -43,7 +43,7 @@ class VM(Resource):
 
     @property
     def data(self):
-        return {'name': self.ident,
+        return {'name': self.info['name'],
                 'uuid': self.info['uuid'],
                 'stats': self.info['stats'],
                 'memory': self.info['memory'],
diff --git a/src/kimchi/model/vms.py b/src/kimchi/model/vms.py
index 2425a43..35fbf3e 100644
--- a/src/kimchi/model/vms.py
+++ b/src/kimchi/model/vms.py
@@ -47,7 +47,9 @@ DOM_STATE_MAP = {0: 'nostate',
                  6: 'crashed'}
 
 GUESTS_STATS_INTERVAL = 5
-VM_STATIC_UPDATE_PARAMS = {'name': './name'}
+VM_STATIC_UPDATE_PARAMS = {'name': './name',
+                           'cpus': './vcpu',
+                           'memory': './memory'}
 VM_LIVE_UPDATE_PARAMS = {}
 
 stats = {}
@@ -235,33 +237,41 @@ class VMModel(object):
 
     def update(self, name, params):
         dom = self.get_vm(name, self.conn)
+        # Change memory unit from MB to KB
         dom = self._static_vm_update(dom, params)
         self._live_vm_update(dom, params)
         return dom.name().decode('utf-8')
 
     def _static_vm_update(self, dom, params):
         state = DOM_STATE_MAP[dom.info()[0]]
-
         old_xml = new_xml = dom.XMLDesc(0)
 
         for key, val in params.items():
             if key in VM_STATIC_UPDATE_PARAMS:
+                # Machine must be powered off
+                if state == 'running':
+                    raise InvalidOperation("KCHVM0022E")
                 xpath = VM_STATIC_UPDATE_PARAMS[key]
+                if key == 'memory':
+                    val = val * 1024
+                if type(val) == int:
+                    val = str(val)
                 new_xml = xmlutils.xml_item_update(new_xml, xpath, val)
 
-        try:
-            if 'name' in params:
-                if state == 'running':
-                    msg_args = {'name': dom.name(), 'new_name': params['name']}
-                    raise InvalidParameter("KCHVM0003E", msg_args)
-                else:
-                    dom.undefine()
-            conn = self.conn.get()
-            dom = conn.defineXML(new_xml)
-        except libvirt.libvirtError as e:
-            dom = conn.defineXML(old_xml)
-            raise OperationFailed("KCHVM0008E", {'name': dom.name(),
-                                                 'err': e.get_error_message()})
+        if not new_xml == old_xml:
+            # Remove currentMemory element
+            root = ElementTree.fromstring(new_xml)
+            root.remove(root.find('.currentMemory'))
+            new_xml = ElementTree.tostring(root, encoding="utf-8")
+            try:
+                conn = self.conn.get()
+                dom.undefine()
+                dom = conn.defineXML(new_xml)
+            except libvirt.libvirtError as e:
+                dom = conn.defineXML(old_xml)
+                raise OperationFailed("KCHVM0008E",
+                                      {'name': dom.name(),
+                                       'err': e.get_error_message()})
         return dom
 
     def _live_vm_update(self, dom, params):
@@ -304,7 +314,8 @@ class VMModel(object):
         res['io_throughput'] = vm_stats.get('disk_io', 0)
         res['io_throughput_peak'] = vm_stats.get('max_disk_io', 100)
 
-        return {'state': state,
+        return {'name': name,
+                'state': state,
                 'stats': res,
                 'uuid': dom.UUIDString(),
                 'memory': info[2] >> 10,
-- 
1.8.5.3




More information about the Kimchi-devel mailing list