From c86328181fbf3ceaa7c4565d060ec8b006642a65 Mon Sep 17 00:00:00 2001 From: Roberto Hueso Gómez Date: Tue, 28 Apr 2020 11:32:05 +0200 Subject: Poweroff VM before some operations This patch includes changes to: - Poweroff VM before running operations that require access to virtual disks. - Poweroff VM before host system poweroff. --- src/virtual/ogOperations.py | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) (limited to 'src/virtual') diff --git a/src/virtual/ogOperations.py b/src/virtual/ogOperations.py index 1856ac0..940cda7 100644 --- a/src/virtual/ogOperations.py +++ b/src/virtual/ogOperations.py @@ -22,6 +22,7 @@ import sys class OgQMP: QMP_TIMEOUT = 5 + QMP_POWEROFF_TIMEOUT = 300 def __init__(self, ip, port): self.ip = ip @@ -106,19 +107,29 @@ class OgVirtualOperations: if not os.path.exists(self.OG_PARTITIONS_PATH): os.mkdir(self.OG_PARTITIONS_PATH, mode=0o755) - def poweroff(self): + def poweroff_guest(self): try: qmp = OgQMP(self.IP, self.VIRTUAL_PORT) - qmp.talk(str({"execute": "qmp_capabilities"})) - qmp.talk(str({"execute": "system_powerdown"})) - qmp.disconnect() except: - pass + return + + qmp.talk(str({"execute": "system_powerdown"})) + out = qmp.recv() + assert(out['event'] == 'POWERDOWN') + out = qmp.recv(timeout=OgQMP.QMP_POWEROFF_TIMEOUT) + assert(out['event'] == 'SHUTDOWN') + qmp.disconnect() + + def poweroff_host(self): + subprocess.run(['/sbin/poweroff']) + + def poweroff(self): + self.poweroff_guest() + self.poweroff_host() def reboot(self): try: qmp = OgQMP(self.IP, self.VIRTUAL_PORT) - qmp.talk(str({"execute": "qmp_capabilities"})) qmp.talk(str({"execute": "system_reset"})) qmp.disconnect() except: @@ -233,6 +244,7 @@ class OgVirtualOperations: def setup(self, request, ogRest): path = f'{self.OG_PATH}/partitions.json' + self.poweroff_guest() self.refresh(ogRest) part_setup = request.getPartitionSetup() @@ -279,13 +291,7 @@ class OgVirtualOperations: repo = request.getRepo() samba_config = ogRest.samba_config - # Check if VM is running. - try: - qmp = OgQMP(self.IP, self.VIRTUAL_PORT) - qmp.disconnect() - return None - except: - pass + self.poweroff_guest() self.refresh(ogRest) @@ -317,14 +323,7 @@ class OgVirtualOperations: cid = request.getId() samba_config = ogRest.samba_config - # Check if VM is running. - try: - qmp = OgQMP(self.IP, self.VIRTUAL_PORT) - qmp.disconnect() - return None - except: - pass - + self.poweroff_guest() self.refresh(ogRest) drive_path = f'{self.OG_PARTITIONS_PATH}/disk{disk}_part{partition}.qcow2' @@ -441,7 +440,6 @@ class OgVirtualOperations: def hardware(self, path, ogRest): try: qmp = OgQMP(self.IP, self.VIRTUAL_PORT) - qmp.talk(str({"execute": "qmp_capabilities"})) pci_data = qmp.talk(str({"execute": "query-pci"})) mem_data = qmp.talk(str({"execute": "query-memory-size-summary"})) cpu_data = qmp.talk(str({"execute": "query-cpus-fast"})) -- cgit v1.2.3-18-g5258