diff --git a/selftests/unit/test_qemu_devices.py b/selftests/unit/test_qemu_devices.py index 5de5ddd0a4..6fa75a6b7e 100644 --- a/selftests/unit/test_qemu_devices.py +++ b/selftests/unit/test_qemu_devices.py @@ -566,18 +566,20 @@ def setUp(self): def tearDown(self): self.god.unstub_all() - def create_qdev(self, vm_name='vm1', strict_mode="no", + def create_qdev(self, vm_name='vm1', machine_type='pc', strict_mode="no", allow_hotplugged_vm="yes"): """ :return: Initialized qcontainer.DevContainer object """ qemu_cmd = '/usr/bin/qemu_kvm' - qcontainer.process.system_output.expect_call('%s -help' % qemu_cmd, + qcontainer.process.system_output.expect_call('%s -M %s -help' % + (qemu_cmd, machine_type), timeout=10, ignore_status=True, shell=True, verbose=False ).and_return(QEMU_HELP) - qcontainer.process.system_output.expect_call("%s -device \? 2>&1" - % qemu_cmd, timeout=10, + qcontainer.process.system_output.expect_call("%s -M %s -device \? 2>&1" + % (qemu_cmd, machine_type), + timeout=10, ignore_status=True, shell=True, verbose=False @@ -588,7 +590,8 @@ def create_qdev(self, vm_name='vm1', strict_mode="no", shell=True, verbose=False ).and_return(QEMU_MACHINE) - cmd = "echo -e 'help\nquit' | %s -monitor stdio -vnc none" % qemu_cmd + cmd = "echo -e 'help\nquit' | %s -M %s -monitor stdio -vnc none" % \ + (qemu_cmd, machine_type) qcontainer.process.system_output.expect_call(cmd, timeout=10, ignore_status=True, shell=True, @@ -597,8 +600,8 @@ def create_qdev(self, vm_name='vm1', strict_mode="no", cmd = ('echo -e \'{ "execute": "qmp_capabilities" }\n' '{ "execute": "query-commands", "id": "RAND91" }\n' '{ "execute": "quit" }\'' - '| %s -qmp stdio -vnc none | grep return |' - ' grep RAND91' % qemu_cmd) + ' | %s -M %s -qmp stdio -vnc none | grep return | grep RAND91' + % (qemu_cmd, machine_type)) qcontainer.process.system_output.expect_call(cmd, timeout=10, ignore_status=True, shell=True, @@ -608,15 +611,15 @@ def create_qdev(self, vm_name='vm1', strict_mode="no", cmd = ('echo -e \'{ "execute": "qmp_capabilities" }\n' '{ "execute": "query-commands", "id": "RAND91" }\n' '{ "execute": "quit" }\' | (sleep 1; cat )' - '| %s -qmp stdio -vnc none | grep return |' - ' grep RAND91' % qemu_cmd) + ' | %s -M %s -qmp stdio -vnc none | grep return | grep RAND91' + % (qemu_cmd, machine_type)) qcontainer.process.system_output.expect_call(cmd, timeout=10, ignore_status=True, shell=True, verbose=False ).and_return(QEMU_QMP) - qdev = qcontainer.DevContainer(qemu_cmd, vm_name, strict_mode, 'no', + qdev = qcontainer.DevContainer(qemu_cmd, vm_name, machine_type, strict_mode, 'no', allow_hotplugged_vm) self.god.check_playback() @@ -825,7 +828,7 @@ def test_qdev_functional(self): def test_qdev_hotplug(self): """ Test the hotplug/unplug functionality """ - qdev = self.create_qdev('vm1', False, True) + qdev = self.create_qdev('vm1', 'pc', False, True) devs = qdev.machine_by_params(ParamsDict({'machine_type': 'pc'})) for dev in devs: qdev.insert(dev) diff --git a/virttest/qemu_devices/qcontainer.py b/virttest/qemu_devices/qcontainer.py index 6857cb7416..55cb996572 100644 --- a/virttest/qemu_devices/qcontainer.py +++ b/virttest/qemu_devices/qcontainer.py @@ -42,19 +42,22 @@ class DevContainer(object): """ # General methods - def __init__(self, qemu_binary, vmname, strict_mode="no", + def __init__(self, qemu_binary, vmname, machine_type, strict_mode="no", workaround_qemu_qmp_crash="no", allow_hotplugged_vm="yes"): """ :param qemu_binary: qemu binary :param vm: related VM + :param machine_type: VM machine type :param strict_mode: Use strict mode (set optional params) """ - def get_hmp_cmds(qemu_binary): + def get_hmp_cmds(): """ :return: list of human monitor commands """ - _ = decode_to_text(process.system_output("echo -e 'help\nquit' | %s -monitor " - "stdio -vnc none" % qemu_binary, - timeout=10, ignore_status=True, - shell=True, verbose=False)) + c = "echo -e 'help\nquit' | %s -M %s -monitor stdio -vnc none" % \ + (self.__qemu_binary, self.__machine_type) + _ = decode_to_text(process.system_output(c, timeout=10, + ignore_status=True, + shell=True, + verbose=False)) _ = re.findall(r'^([^()\|\[\sA-Z]+\|?\w+)', _, re.M) hmp_cmds = [] for cmd in _: @@ -65,26 +68,26 @@ def get_hmp_cmds(qemu_binary): hmp_cmds.extend(cmd.split('|')) return hmp_cmds - def get_qmp_cmds(qemu_binary, workaround_qemu_qmp_crash=False): + def get_qmp_cmds(workaround_qemu_qmp_crash=False): """ :return: list of qmp commands """ + c1 = ('echo -e \'{ "execute": "qmp_capabilities" }\n' + '{ "execute": "query-commands", "id": "RAND91" }\n' + '{ "execute": "quit" }\'') + c2 = '(sleep 1; cat )' + c3 = '%s -M %s -qmp stdio -vnc none | grep return | grep RAND91' % \ + (self.__qemu_binary, self.__machine_type) + cmds = None if not workaround_qemu_qmp_crash: - cmds = decode_to_text(process.system_output('echo -e \'' - '{ "execute": "qmp_capabilities" }\n' - '{ "execute": "query-commands", "id": "RAND91" }\n' - '{ "execute": "quit" }\'' - '| %s -qmp stdio -vnc none | grep return |' - ' grep RAND91' % qemu_binary, timeout=10, - ignore_status=True, shell=True, + cmds = decode_to_text(process.system_output("%s | %s" % (c1, c3), + timeout=10, + ignore_status=True, + shell=True, verbose=False)).splitlines() if not cmds: # Some qemu versions crashes when qmp used too early; add sleep - cmds = decode_to_text(process.system_output('echo -e \'' - '{ "execute": "qmp_capabilities" }\n' - '{ "execute": "query-commands", "id": "RAND91" }\n' - '{ "execute": "quit" }\' | (sleep 1; cat )' - '| %s -qmp stdio -vnc none | grep return |' - ' grep RAND91' % qemu_binary, timeout=10, + cmds = decode_to_text(process.system_output("%s | %s | %s" % (c1, c2, c3), + timeout=10, ignore_status=True, shell=True, verbose=False)).splitlines() if cmds: @@ -93,34 +96,18 @@ def get_qmp_cmds(qemu_binary, workaround_qemu_qmp_crash=False): return cmds self.__state = -1 # -1 synchronized, 0 synchronized after hotplug + self.__machine_type = machine_type self.__qemu_binary = qemu_binary self.__execute_qemu_last = None self.__execute_qemu_out = "" - # Check whether we need to add machine_type - cmd = "%s -device \? 2>&1" % qemu_binary - result = process.run(cmd, timeout=10, - ignore_status=True, - shell=True, - verbose=False) - # Some architectures (arm) require machine type to be always set - if result.exit_status and b"machine specified" in result.stdout: - self.__workaround_machine_type = True - basic_qemu_cmd = "%s -machine virt" % qemu_binary - else: - self.__workaround_machine_type = False - basic_qemu_cmd = qemu_binary + self.__qemu_help = self.execute_qemu("-help", 10) # escape the '?' otherwise it will fail if we have a single-char # filename in cwd self.__device_help = self.execute_qemu("-device \? 2>&1", 10) - self.__machine_types = decode_to_text(process.system_output("%s -M \?" % qemu_binary, - timeout=10, - ignore_status=True, - shell=True, - verbose=False)) - self.__hmp_cmds = get_hmp_cmds(basic_qemu_cmd) - self.__qmp_cmds = get_qmp_cmds(basic_qemu_cmd, - workaround_qemu_qmp_crash == 'always') + self.__machine_types = decode_to_text(self.execute_qemu("-M \?", 10)) + self.__hmp_cmds = get_hmp_cmds() + self.__qmp_cmds = get_qmp_cmds(workaround_qemu_qmp_crash == 'always') self.vmname = vmname self.strict_mode = strict_mode == 'yes' self.__devices = [] @@ -430,6 +417,9 @@ def has_qmp_cmd(self, cmd): def execute_qemu(self, options, timeout=5): """ Execute this qemu and return the stdout+stderr output. + + The function adds -M option to qemu command line if the options + argument doesn't contain it. :param options: additional qemu options :type options: string :param timeout: execution timeout @@ -438,11 +428,11 @@ def execute_qemu(self, options, timeout=5): :rtype: string """ if self.__execute_qemu_last != options: - if self.__workaround_machine_type: - cmd = "%s -machine virt %s 2>&1" % (self.__qemu_binary, - options) + if "-M" in options or "-machine" in options: + cmd = "%s %s" % (self.__qemu_binary, options) else: - cmd = "%s %s 2>&1" % (self.__qemu_binary, options) + cmd = "%s -M %s %s" % (self.__qemu_binary, + self.__machine_type, options) result = process.run(cmd, timeout=timeout, ignore_status=True, shell=True, diff --git a/virttest/qemu_vm.py b/virttest/qemu_vm.py index e622dee0a4..4cb9178d34 100644 --- a/virttest/qemu_vm.py +++ b/virttest/qemu_vm.py @@ -1493,7 +1493,8 @@ def sort_key(dev): cmd += "numactl -m %s " % n # Start constructing devices representation - devices = qcontainer.DevContainer(qemu_binary, self.name, + machine_type = params.get('machine_type').split(':', 1)[-1] + devices = qcontainer.DevContainer(qemu_binary, self.name, machine_type, params.get('strict_mode'), params.get( 'workaround_qemu_qmp_crash'),