From c12327cbd12fa60d157bbcdba81c8e733a17236b Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Sat, 13 Jul 2013 23:07:01 -0400 Subject: [PATCH] Guest: Switch memory units to KB to match default XML --- tests/utils.py | 8 ++++---- tests/xmlparse.py | 6 +++--- virtManager/create.py | 6 +++--- virtManager/details.py | 2 +- virtManager/domain.py | 8 ++++---- virtinst/Guest.py | 28 +++++++--------------------- virtinst/cli.py | 4 ++-- virtinst/xmlbuilder.py | 6 +++--- 8 files changed, 27 insertions(+), 41 deletions(-) diff --git a/tests/utils.py b/tests/utils.py index e88da6aeb..4357e8fe0 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -170,8 +170,8 @@ def diff_compare(actual_out, filename=None, expect_out=None): def get_basic_paravirt_guest(installer=None): g = virtinst.Guest(_conn, type="xen") g.name = "TestGuest" - g.memory = int(200) - g.maxmemory = int(400) + g.memory = int(200 * 1024) + g.maxmemory = int(400 * 1024) g.uuid = "12345678-1234-1234-1234-123456789012" g.add_device(VirtualGraphics(_conn, "vnc", keymap="ja")) g.vcpus = 5 @@ -190,8 +190,8 @@ def get_basic_paravirt_guest(installer=None): def get_basic_fullyvirt_guest(typ="xen", installer=None): g = virtinst.Guest(_conn, type=typ) g.name = "TestGuest" - g.memory = int(200) - g.maxmemory = int(400) + g.memory = int(200 * 1024) + g.maxmemory = int(400 * 1024) g.uuid = "12345678-1234-1234-1234-123456789012" g.cdrom = "/dev/loop0" g.add_device(VirtualGraphics(_conn, "sdl")) diff --git a/tests/xmlparse.py b/tests/xmlparse.py index c110661ad..1f931baf7 100644 --- a/tests/xmlparse.py +++ b/tests/xmlparse.py @@ -106,9 +106,9 @@ class XMLParseTest(unittest.TestCase): check("maxvcpus", 5, 12) check("vcpus", 12, 10) check("cpuset", "1-3", "1-8,^6", "1-5,15") - check("maxmemory", 400, 500) - check("memory", 200, 1000) - check("maxmemory", 1000, 2000) + check("maxmemory", 409600, 512000) + check("memory", 204800, 1024000) + check("maxmemory", 1024000, 2048000) check("uuid", "12345678-1234-1234-1234-123456789012", "11111111-2222-3333-4444-555555555555") check("emulator", "/usr/lib/xen/bin/qemu-dm", "/usr/binnnn/fooemu") diff --git a/virtManager/create.py b/virtManager/create.py index ce42c95b5..f3460ac08 100644 --- a/virtManager/create.py +++ b/virtManager/create.py @@ -821,7 +821,7 @@ class vmmCreate(vmmGObjectUI): def populate_summary(self): distro, version, dlabel, vlabel = self.get_config_os_info() - mem = self.pretty_memory(int(self.guest.memory) * 1024) + mem = self.pretty_memory(int(self.guest.memory)) cpu = str(int(self.guest.vcpus)) instmethod = self.get_config_install_page() @@ -1611,8 +1611,8 @@ class vmmCreate(vmmGObjectUI): # Memory try: - self.guest.memory = int(mem) - self.guest.maxmemory = int(mem) + self.guest.memory = int(mem) * 1024 + self.guest.maxmemory = int(mem) * 1024 except Exception, e: return self.err.val_err(_("Error setting guest memory."), e) diff --git a/virtManager/details.py b/virtManager/details.py index 64137fd58..73acc8073 100644 --- a/virtManager/details.py +++ b/virtManager/details.py @@ -1823,7 +1823,7 @@ class vmmDetails(vmmGObjectUI): maxadj.set_range(mem, upper) def generate_cpuset(self): - mem = int(self.vm.get_memory()) / 1024 / 1024 + mem = int(self.vm.get_memory()) / 1024 return virtinst.Guest.generate_cpuset(self.conn.get_backend(), mem) # VCPUS diff --git a/virtManager/domain.py b/virtManager/domain.py index 8d30b4eed..88111f58c 100644 --- a/virtManager/domain.py +++ b/virtManager/domain.py @@ -538,8 +538,8 @@ class vmmDomain(vmmLibvirtObject): def define_both_mem(self, memory, maxmem): def change(guest): - guest.memory = int(int(memory) / 1024) - guest.maxmemory = int(int(maxmem) / 1024) + guest.memory = int(memory) + guest.maxmemory = int(maxmem) return self._redefine_guest(change) # Security define methods @@ -997,9 +997,9 @@ class vmmDomain(vmmLibvirtObject): return self._get_guest(inactive=True).description def get_memory(self): - return int(self._get_guest().memory * 1024) + return int(self._get_guest().memory) def maximum_memory(self): - return int(self._get_guest().maxmemory * 1024) + return int(self._get_guest().maxmemory) def vcpu_count(self): return int(self._get_guest().vcpus) diff --git a/virtinst/Guest.py b/virtinst/Guest.py index a6c6a9e87..234f1959c 100644 --- a/virtinst/Guest.py +++ b/virtinst/Guest.py @@ -139,7 +139,6 @@ class Guest(XMLBuilder): cell_mem = conn.getCellsFreeMemory(0, len(cells)) cell_id = -1 - mem = mem * 1024 for i in range(len(cells)): if cell_mem[i] < mem: # Cell doesn't have enough mem to fit, skip it @@ -288,34 +287,21 @@ class Guest(XMLBuilder): def get_memory(self): return self._memory def set_memory(self, val): - if not isinstance(val, int) or val <= 0: - raise ValueError(_("Memory value must be an integer greater " - "than 0")) self._memory = val if self.maxmemory is None or self.maxmemory < val: self.maxmemory = val - def _xml_memory_value(self): - return int(self.memory) * 1024 - memory = XMLProperty(get_memory, set_memory, - xpath="./currentMemory", - get_converter=lambda s, x: int(x) / 1024, - set_converter=lambda s, x: int(x) * 1024) + memory = XMLProperty(get_memory, set_memory, is_int=True, + xpath="./currentMemory") # Memory allocated to the guest. Should be given in MB def get_maxmemory(self): return self._maxmemory def set_maxmemory(self, val): - if not isinstance(val, int) or val <= 0: - raise ValueError(_("Max Memory value must be an integer greater " - "than 0")) self._maxmemory = val - def _xml_maxmemory_value(self): - return int(self.maxmemory) * 1024 - maxmemory = XMLProperty(get_maxmemory, set_maxmemory, - xpath="./memory", - get_converter=lambda s, x: int(x) / 1024, - set_converter=lambda s, x: int(x) * 1024) + maxmemory = XMLProperty(get_maxmemory, set_maxmemory, is_int=True, + xpath="./memory") + def get_hugepage(self): return self._hugepage def set_hugepage(self, val): @@ -927,8 +913,8 @@ class Guest(XMLBuilder): xml = add(" %s" % self.name) xml = add(" %s" % self.uuid) xml = add(desc_xml) - xml = add(" %s" % (self.maxmemory * 1024)) - xml = add(" %s" % (self.memory * 1024)) + xml = add(" %s" % self.maxmemory) + xml = add(" %s" % self.memory) # # diff --git a/virtinst/cli.py b/virtinst/cli.py index a2a7c863f..5e5374742 100644 --- a/virtinst/cli.py +++ b/virtinst/cli.py @@ -636,10 +636,10 @@ def get_memory(memory, guest, image_memory=None): if mem < MIN_RAM: raise ValueError(_("Installs currently require %d megs " "of RAM.") % MIN_RAM) - guest.memory = mem + guest.memory = mem * 1024 if memory is None and image_memory is not None: - memory = int(image_memory) / 1024 + memory = int(image_memory) prompt_loop(prompt_txt, err_txt, memory, guest, "memory", func=check_memory) diff --git a/virtinst/xmlbuilder.py b/virtinst/xmlbuilder.py index 453a5b818..5ac8a4b2c 100644 --- a/virtinst/xmlbuilder.py +++ b/virtinst/xmlbuilder.py @@ -248,9 +248,9 @@ class XMLProperty(property): @param get_converter: @param set_converter: optional function for converting the property value from the virtinst API to the guest XML. For example, - the Guest.memory API is in MB, but the libvirt domain memory API - is in KB. So, if xpath is specified, on a 'get' operation we need - to convert the XML value with int(val) / 1024. + the Guest.memory API was once in MB, but the libvirt domain + memory API is in KB. So, if xpath is specified, on a 'get' + operation we convert the XML value with int(val) / 1024. @param xml_get_xpath: @param xml_set_xpath: Not all props map cleanly to a static xpath. This allows passing functions which generate an xpath for getting