From e1408da0fb0224e64037bc693d262374795bf9a4 Mon Sep 17 00:00:00 2001 From: Matus Marhefka Date: Wed, 20 Oct 2021 09:03:59 +0200 Subject: [PATCH] tests/install_vm.py: add timeouted wait for VM to shutdown Added timeout should prevent issues where a VM is still in the `running` state after `virsh console` disconnects and therefore subsequent `virsh start` fails, e.g.: ``` Starting Reboot... dracut Warning: Killing all remaining processes Rebooting. [ 522.430163] reboot: Restarting system error: Domain is already active ``` --- tests/install_vm.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/install_vm.py b/tests/install_vm.py index 6a51477a289..59ffc499587 100755 --- a/tests/install_vm.py +++ b/tests/install_vm.py @@ -4,6 +4,7 @@ import os import sys import subprocess +import time def parse_args(): @@ -110,6 +111,25 @@ def parse_args(): return parser.parse_args() +def wait_vm_not_running(domain): + timeout = 300 + + print("Waiting for {0} VM to shutdown (max. {1}s)".format(domain, timeout)) + end_time = time.time() + timeout + try: + while True: + time.sleep(5) + if subprocess.getoutput("virsh domstate {0}".format(domain)).rstrip() != "running": + return + if time.time() >= end_time: + print("Timeout reached: {0} VM failed to shutdown, cancelling wait." + .format(domain)) + return + except KeyboardInterrupt: + print("Interrupted, cancelling wait.") + return + + def main(): data = parse_args() username = "" @@ -210,6 +230,7 @@ def main(): os.system(command) if data.console: os.system("unbuffer virsh console {0}".format(data.domain)) + wait_vm_not_running(data.domain) os.system("virsh start {0}".format(data.domain)) print("\nTo determine the IP address of the {0} VM use:".format(data.domain))