X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=resources%2Flibraries%2Fpython%2FQemuUtils.py;h=2123e42a4bbe5c1f95add56fdc18852014330f5b;hb=fe29ca4b7e6c412c69f1c56404513a33236aa617;hp=1e3a8aa6b6db24873dfe781b30f3192a252b68bc;hpb=eb38a257f3fbecee93a883ddfc62683e49c649a1;p=csit.git diff --git a/resources/libraries/python/QemuUtils.py b/resources/libraries/python/QemuUtils.py index 1e3a8aa6b6..2123e42a4b 100644 --- a/resources/libraries/python/QemuUtils.py +++ b/resources/libraries/python/QemuUtils.py @@ -18,7 +18,7 @@ import json from robot.api import logger -from resources.libraries.python.ssh import SSH +from resources.libraries.python.ssh import SSH, SSHTimeout from resources.libraries.python.constants import Constants from resources.libraries.python.topology import NodeType @@ -142,8 +142,7 @@ class QemuUtils(object): raise ValueError('Host CPU count must match Qemu Thread count') for qemu_cpu, host_cpu in zip(qemu_cpus, host_cpus): - cmd = 'taskset -p {0} {1}'.format(hex(1 << int(host_cpu)), - qemu_cpu['thread_id']) + cmd = 'taskset -pc {0} {1}'.format(host_cpu, qemu_cpu['thread_id']) (ret_code, _, stderr) = self._ssh.exec_command_sudo(cmd) if int(ret_code) != 0: logger.debug('Set affinity failed {0}'.format(stderr)) @@ -228,9 +227,10 @@ class QemuUtils(object): response will contain the "error" keyword instead of "return". """ # To enter command mode, the qmp_capabilities command must be issued. - qmp_cmd = 'echo "{ \\"execute\\": \\"qmp_capabilities\\" }' + \ - '{ \\"execute\\": \\"' + cmd + '\\" }" | sudo -S nc -U ' + \ - self.__QMP_SOCK + qmp_cmd = 'echo "{ \\"execute\\": \\"qmp_capabilities\\" }' \ + '{ \\"execute\\": \\"' + cmd + \ + '\\" }" | sudo -S socat - UNIX-CONNECT:' + self.__QMP_SOCK + (ret_code, stdout, stderr) = self._ssh.exec_command(qmp_cmd) if int(ret_code) != 0: logger.debug('QMP execute failed {0}'.format(stderr)) @@ -247,8 +247,9 @@ class QemuUtils(object): def _qemu_qga_flush(self): """Flush the QGA parser state """ - qga_cmd = 'printf "\xFF" | sudo -S nc ' \ - '-q 1 -U ' + self.__QGA_SOCK + qga_cmd = '(printf "\xFF"; sleep 1) | sudo -S socat - UNIX-CONNECT:' + \ + self.__QGA_SOCK + #TODO: probably need something else (ret_code, stdout, stderr) = self._ssh.exec_command(qga_cmd) if int(ret_code) != 0: logger.debug('QGA execute failed {0}'.format(stderr)) @@ -268,8 +269,10 @@ class QemuUtils(object): :param cmd: QGA command to execute. :type cmd: str """ - qga_cmd = 'echo "{ \\"execute\\": \\"' + cmd + '\\" }" | sudo -S nc ' \ - '-q 1 -U ' + self.__QGA_SOCK + qga_cmd = '(echo "{ \\"execute\\": \\"' + \ + cmd + \ + '\\" }"; sleep 1) | sudo -S socat - UNIX-CONNECT:' + \ + self.__QGA_SOCK (ret_code, stdout, stderr) = self._ssh.exec_command(qga_cmd) if int(ret_code) != 0: logger.debug('QGA execute failed {0}'.format(stderr)) @@ -280,21 +283,25 @@ class QemuUtils(object): return {} return json.loads(stdout.split('\n', 1)[0]) - def _wait_until_vm_boot(self, timeout=300): + def _wait_until_vm_boot(self, timeout=60): """Wait until QEMU VM is booted. Ping QEMU guest agent each 5s until VM booted or timeout. - :param timeout: Waiting timeout in seconds (optional, default 300s). + :param timeout: Waiting timeout in seconds (optional, default 60s). :type timeout: int """ start = time() - while 1: + while True: if time() - start > timeout: raise RuntimeError('timeout, VM {0} not booted on {1}'.format( self._qemu_opt['disk_image'], self._node['host'])) - self._qemu_qga_flush() - out = self._qemu_qga_exec('guest-ping') + out = None + try: + self._qemu_qga_flush() + out = self._qemu_qga_exec('guest-ping') + except ValueError: + logger.trace('QGA guest-ping unexpected output {}'.format(out)) # Empty output - VM not booted yet if not out: sleep(5) @@ -305,8 +312,10 @@ class QemuUtils(object): elif out.get('error') is not None: sleep(5) else: - raise RuntimeError('QGA guest-ping unexpected output {}'.format( - out)) + # If there is an unexpected output from QGA guest-info, try + # again until timeout. + logger.trace('QGA guest-ping unexpected output {}'.format(out)) + logger.trace('VM {0} booted on {1}'.format(self._qemu_opt['disk_image'], self._node['host'])) @@ -490,6 +499,9 @@ class QemuUtils(object): # If 'huge_allocate' is set to true try to allocate as well. self._huge_page_check(allocate=self._qemu_opt.get('huge_allocate')) + # Disk option + drive = '-drive file={},format=raw,cache=none,if=virtio'.format( + self._qemu_opt.get('disk_image')) # Setup QMP via unix socket qmp = '-qmp unix:{0},server,nowait'.format(self.__QMP_SOCK) # Setup serial console @@ -501,11 +513,12 @@ class QemuUtils(object): '-device isa-serial,chardev=qga0' # Graphic setup graphic = '-monitor none -display none -vga none' + # Run QEMU - cmd = '{0} {1} {2} {3} {4} -hda {5} {6} {7} {8} {9}'.format( + cmd = '{0} {1} {2} {3} {4} {5} {6} {7} {8} {9}'.format( self.__QEMU_BIN, self._qemu_opt.get('smp'), mem, ssh_fwd, self._qemu_opt.get('options'), - self._qemu_opt.get('disk_image'), qmp, serial, qga, graphic) + drive, qmp, serial, qga, graphic) (ret_code, _, stderr) = self._ssh.exec_command_sudo(cmd, timeout=300) if int(ret_code) != 0: logger.debug('QEMU start failed {0}'.format(stderr)) @@ -513,7 +526,12 @@ class QemuUtils(object): self._node['host'])) logger.trace('QEMU running') # Wait until VM boot - self._wait_until_vm_boot() + try: + self._wait_until_vm_boot() + except (RuntimeError, SSHTimeout): + self.qemu_kill() + self.qemu_clear_socks() + raise # Update interface names in VM node dict self._update_vm_interfaces() # Return VM node dict