Commit 3a41efec authored by Alain Takoudjou's avatar Alain Takoudjou

updates

parent f3aa60e4
...@@ -69,13 +69,20 @@ def getInitialQemuResourceDict(pid_file): ...@@ -69,13 +69,20 @@ def getInitialQemuResourceDict(pid_file):
return None return None
with open(pid_file) as f: with open(pid_file) as f:
pid = int(f.read()) try:
pid = int(f.read())
except ValueError, e:
raise ValueError("%r is empty or doesn't contain a valid pid number: %s" % (
pid_file, str(e)))
try: try:
process = psutil.Process(pid) process = psutil.Process(pid)
except psutil.NoSuchProcess: except psutil.NoSuchProcess:
print 'No process with pid %s' % pid raise ValueError('No process with pid %s' % pid)
return None
if not process.name().startswith('qemu-system'):
raise ValueError("The pid %s is used by %r and not a qemu process." % (
pid, process.name()))
resource_dict = {'cpu': None, 'ram': None} resource_dict = {'cpu': None, 'ram': None}
cmd_list = process.cmdline() cmd_list = process.cmdline()
cpu_index = cmd_list.index('-smp') cpu_index = cmd_list.index('-smp')
...@@ -137,7 +144,10 @@ class QemuQMPWrapper(object): ...@@ -137,7 +144,10 @@ class QemuQMPWrapper(object):
data = self.sockf.readline() data = self.sockf.readline()
if not data: if not data:
return return
response = json.loads(data) try:
response = json.loads(data)
except ValueError:
raise ValueError('Wrong data: %s' % data)
if 'error' in response: if 'error' in response:
raise QmpCommandError(response["error"]["desc"]) raise QmpCommandError(response["error"]["desc"])
if 'event' in response: if 'event' in response:
...@@ -148,18 +158,17 @@ class QemuQMPWrapper(object): ...@@ -148,18 +158,17 @@ class QemuQMPWrapper(object):
return response return response
def _send(self, message): def _send(self, message, retry=0, sleep=0.5):
self.socket.sendall(json.dumps(message)) self.socket.sendall(json.dumps(message))
return self._readResponse() response = self._readResponse()
for i in range(0, retry):
def _sendRetry(self, message): if response is not None:
""" break
Send Qmp message and retry once if the result is None print "Retrying send command after %s second(s)..." % sleep
""" time.sleep(sleep)
result = self._send(message) self.socket.sendall(json.dumps(message))
if result is None: response = self._readResponse()
return self._send(message) return response
return result
def _getVMStatus(self): def _getVMStatus(self):
response = self._send({'execute': 'query-status'}) response = self._send({'execute': 'query-status'})
...@@ -317,9 +326,9 @@ class QemuQMPWrapper(object): ...@@ -317,9 +326,9 @@ class QemuQMPWrapper(object):
return some info about VM CPUs return some info about VM CPUs
""" """
cpu_info_dict = {'hotplugged': [], 'base': []} cpu_info_dict = {'hotplugged': [], 'base': []}
cpu_list = self._sendRetry({ cpu_list = self._send({
'execute': 'query-cpus' 'execute': 'query-cpus'
})['return'] }, retry=5)['return']
for cpu in cpu_list: for cpu in cpu_list:
if 'unattached' in cpu['qom_path']: if 'unattached' in cpu['qom_path']:
index = 'base' index = 'base'
...@@ -337,9 +346,9 @@ class QemuQMPWrapper(object): ...@@ -337,9 +346,9 @@ class QemuQMPWrapper(object):
return some info about VM Memory. Can only say info about hotplugged RAM return some info about VM Memory. Can only say info about hotplugged RAM
""" """
mem_info_dict = {'hotplugged': [], 'base': []} mem_info_dict = {'hotplugged': [], 'base': []}
memory_list = self._sendRetry({ memory_list = self._send({
'execute': 'query-memory-devices' 'execute': 'query-memory-devices'
})['return'] }, retry=5)['return']
for mem in memory_list: for mem in memory_list:
if mem['data']['hotplugged'] == True: if mem['data']['hotplugged'] == True:
mem_info_dict['hotplugged'].append(mem['data']) mem_info_dict['hotplugged'].append(mem['data'])
...@@ -386,6 +395,7 @@ class QemuQMPWrapper(object): ...@@ -386,6 +395,7 @@ class QemuQMPWrapper(object):
raise ValueError("Cannot remove device %s" % dev_id) raise ValueError("Cannot remove device %s" % dev_id)
# try soft reboot of the VM # try soft reboot of the VM
print "Powering down the VM..."
self.powerdown() self.powerdown()
system_exited = False system_exited = False
# wait for ~10 seconds if the system exit, else quit Qemu # wait for ~10 seconds if the system exit, else quit Qemu
...@@ -418,7 +428,7 @@ class QemuQMPWrapper(object): ...@@ -418,7 +428,7 @@ class QemuQMPWrapper(object):
unremovable_cpu = 0 unremovable_cpu = 0
cpu_hotplugable_list = self._send({ cpu_hotplugable_list = self._send({
'execute': 'query-hotpluggable-cpus' 'execute': 'query-hotpluggable-cpus'
})['return'] }, retry=5)['return']
cpu_hotplugable_list.reverse() cpu_hotplugable_list.reverse()
for cpu in cpu_hotplugable_list: for cpu in cpu_hotplugable_list:
if cpu.get('qom-path', '') == '': if cpu.get('qom-path', '') == '':
...@@ -508,8 +518,8 @@ class QemuQMPWrapper(object): ...@@ -508,8 +518,8 @@ class QemuQMPWrapper(object):
num_slot_used = 0 num_slot_used = 0
memory_id_list = [] # current hotplugged memory memory_id_list = [] # current hotplugged memory
cleanup_memdev_id_dict = {} cleanup_memdev_id_dict = {}
current_dimm_list = self._send({ "execute": "query-memory-devices" }) current_dimm_list = self._send({ "execute": "query-memory-devices" }, retry=5)
current_memdev_list = self._send({ "execute": "query-memdev" }) current_memdev_list = self._send({ "execute": "query-memdev" }, retry=5)
for memdev in current_memdev_list['return']: for memdev in current_memdev_list['return']:
cleanup_memdev_id_dict[memdev['id']] = '' cleanup_memdev_id_dict[memdev['id']] = ''
......
...@@ -76,7 +76,7 @@ class TestQemuQMPWrapper(unittest.TestCase): ...@@ -76,7 +76,7 @@ class TestQemuQMPWrapper(unittest.TestCase):
if message['arguments']['id'].startswith('cpu'): if message['arguments']['id'].startswith('cpu'):
self.setChange('cpu', -1) self.setChange('cpu', -1)
if self.fail: if self.fail:
return {"error": {"class": "CommandFailed", "message": ""}} return {"error": {"class": "CommandFailed", "desc": ""}}
return {"return": {}} return {"return": {}}
def fake_getEventList(self, timeout=0, cleanup=False): def fake_getEventList(self, timeout=0, cleanup=False):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment