From 1e8bd7a37d94b32d0e7da15b4a685be742b04a3b Mon Sep 17 00:00:00 2001 From: gryf Date: Thu, 6 May 2021 20:44:32 +0200 Subject: [PATCH] Do not relay on vboxmanage output. There is a function which retrieve information about VM by looking on the 'vboxmanage vminfo' command. The output wasn't consistent, yet it was fixed recently so that amount of memory was all wrong. Currently the only thing which is searched for is the VM configuration path, and all the rest is taken out from the XML VM definition. --- box.py | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/box.py b/box.py index 8de0d68..af175dd 100755 --- a/box.py +++ b/box.py @@ -377,34 +377,23 @@ class VBoxManage: if line.startswith('Config file:'): self.vm_info['config_file'] = line.split('Config ' 'file:')[1].strip() - continue - - if line.startswith('Memory size'): - mem = line.split('Memory size')[1].strip() - if mem.isnumeric(): - self.vm_info['memory'] = mem - else: - # 12288MB - self.vm_info['memory'] = mem[:-2] - continue - - if line.startswith('Number of CPUs:'): - self.vm_info['cpus'] = line.split('Number of CPUs:')[1].strip() - continue - - if line.startswith('UUID:'): - self.vm_info['uuid'] = line.split('UUID:')[1].strip() + break dom = xml.dom.minidom.parse(self.vm_info['config_file']) + gebtn = dom.getElementsByTagName - for extradata in dom.getElementsByTagName('ExtraDataItem'): + self.vm_info['cpus'] = gebtn('CPU')[0].getAttribute('count') + self.vm_info['uuid'] = gebtn('Machine')[0].getAttribute('uuid')[1:-1] + self.vm_info['memory'] = gebtn('Memory')[0].getAttribute('RAMSize') + + for extradata in gebtn('ExtraDataItem'): key = extradata.getAttribute('name') val = extradata.getAttribute('value') self.vm_info[key] = val - if len(dom.getElementsByTagName('Forwarding')): - fw = dom.getElementsByTagName('Forwarding')[0] - self.vm_info['port'] = fw.getAttribute('hostport') + if len(gebtn('Forwarding')): + fw = gebtn('Forwarding')[0].getAttribute('hostport') + self.vm_info['port'] = fw return self.vm_info