def boot(self, img, extra_args=[]): boot_dev = BOOT_DEVICE[self._config['boot_dev_type']] boot_params = boot_dev.format(img) args = self._args + boot_params.split(' ') args += self._data_args + extra_args + self._config['extra_args'] logging.debug("QEMU args: %s", " ".join(args)) qemu_path = get_qemu_path(self.arch, self._build_path) # Since console_log_path is only set when the user provides the # log_console option, we will set drain_console=True so the # console is always drained. guest = QEMUMachine(binary=qemu_path, args=args, console_log=self._console_log_path, drain_console=True) guest.set_machine(self._config['machine']) guest.set_console() try: guest.launch() except: logging.error("Failed to launch QEMU, command line:") logging.error(" ".join([qemu_path] + args)) logging.error("Log:") logging.error(guest.get_log()) logging.error("QEMU version >= 2.10 is required") raise atexit.register(self.shutdown) self._guest = guest # Init console so we can start consuming the chars. self.console_init() usernet_info = guest.qmp("human-monitor-command", command_line="info usernet").get("return") self.ssh_port = get_info_usernet_hostfwd_port(usernet_info) if not self.ssh_port: raise Exception("Cannot find ssh port from 'info usernet':\n%s" % \ usernet_info)
def boot(self, img, extra_args=[]): args = self._args + [ "-device", "VGA", "-drive", "file=%s,if=none,id=drive0,cache=writeback" % img, "-device", "virtio-blk,drive=drive0,bootindex=0" ] args += self._data_args + extra_args logging.debug("QEMU args: %s", " ".join(args)) qemu_bin = os.environ.get("QEMU", "qemu-system-" + self.arch) guest = QEMUMachine(binary=qemu_bin, args=args) guest.set_machine('pc') guest.set_console() try: guest.launch() except: logging.error("Failed to launch QEMU, command line:") logging.error(" ".join([qemu_bin] + args)) logging.error("Log:") logging.error(guest.get_log()) logging.error("QEMU version >= 2.10 is required") raise atexit.register(self.shutdown) self._guest = guest usernet_info = guest.qmp("human-monitor-command", command_line="info usernet") self.ssh_port = None for l in usernet_info["return"].splitlines(): fields = l.split() if "TCP[HOST_FORWARD]" in fields and "22" in fields: self.ssh_port = l.split()[3] if not self.ssh_port: raise Exception("Cannot find ssh port from 'info usernet':\n%s" % \ usernet_info)
def _new_vm(self, name, *args): self._sd = tempfile.TemporaryDirectory(prefix="avo_qemu_sock_") vm = QEMUMachine(self.qemu_bin, base_temp_dir=self.workdir, sock_dir=self._sd.name, log_dir=self.logdir) self.log.debug('QEMUMachine "%s" created', name) self.log.debug('QEMUMachine "%s" temp_dir: %s', name, vm.temp_dir) self.log.debug('QEMUMachine "%s" log_dir: %s', name, vm.log_dir) if args: vm.add_args(*args) return vm
def test_machine_types(self): # collect all machine types except 'none', 'isapc', 'microvm' with QEMUMachine(self.qemu_bin) as vm: vm.launch() machines = [m['name'] for m in vm.command('query-machines')] vm.shutdown() machines.remove('none') machines.remove('isapc') machines.remove('microvm') for dev_type in DEV_TYPES: # create the list of machine types and their parameters. mtypes = list() for m in machines: if self.seg_max_adjust_enabled(m): enabled = 'true' else: enabled = 'false' mtypes.append({ 'name': m, DEV_TYPES[dev_type]['seg_max_adjust']: enabled }) # test each machine type for a device type for mt in mtypes: self.check_mt(mt, dev_type)
def check_mt(self, mt, dev_type_name): with QEMUMachine(self.qemu_bin) as vm: vm.set_machine(mt["name"]) for s in VM_DEV_PARAMS[dev_type_name]: vm.add_args(s) vm.launch() query_ok, props, error = self.query_virtqueue(vm, dev_type_name) if not query_ok: self.fail('machine type {0}: {1}'.format(mt['name'], error)) for prop_name, prop_val in props.items(): expected_val = mt[prop_name] self.assertEqual(expected_val, prop_val)
def run_device(self, devtype, opts=None, machine='pc'): """ Run QEMU with `-device DEVTYPE`, return device info from `query-pci` """ with QEMUMachine(self.qemu_bin) as vm: vm.set_machine(machine) if opts: devtype += ',' + opts vm.add_args('-device', '%s,id=devfortest' % (devtype)) vm.add_args('-S') vm.launch() pcibuses = vm.command('query-pci') alldevs = [dev for bus in pcibuses for dev in bus['devices']] devfortest = [dev for dev in alldevs if dev['qdev_id'] == 'devfortest'] return devfortest[0], get_pci_interfaces(vm, devtype)
def check_mt(self, mt, dev_type_name): mt['device'] = dev_type_name # Only for the debug() call. logger = logging.getLogger('machine') logger.debug(mt) with QEMUMachine(self.qemu_bin) as vm: vm.set_machine(mt["name"]) vm.add_args('-nodefaults') for s in VM_DEV_PARAMS[dev_type_name]: vm.add_args(s) try: vm.launch() query_ok, props, error = self.query_virtqueue( vm, dev_type_name) except: query_ok = False error = sys.exc_info()[0] if not query_ok: self.fail('machine type {0}: {1}'.format(mt['name'], error)) for prop_name, prop_val in props.items(): expected_val = mt[prop_name] self.assertEqual(expected_val, prop_val)
def run(self, hardware, scenario, result_dir=os.getcwd()): abs_result_dir = os.path.join(result_dir, scenario._name) if self._transport == "tcp": uri = "tcp:%s:9000" % self._dst_host elif self._transport == "rdma": uri = "rdma:%s:9000" % self._dst_host elif self._transport == "unix": if self._dst_host != "localhost": raise Exception( "Running use unix migration transport for non-local host") uri = "unix:/var/tmp/qemu-migrate-%d.migrate" % os.getpid() try: os.remove(uri[5:]) os.remove(monaddr) except: pass if self._dst_host != "localhost": dstmonaddr = ("localhost", 9001) else: dstmonaddr = "/var/tmp/qemu-dst-%d-monitor.sock" % os.getpid() srcmonaddr = "/var/tmp/qemu-src-%d-monitor.sock" % os.getpid() src = QEMUMachine(self._binary, args=self._get_src_args(hardware), wrapper=self._get_src_wrapper(hardware), name="qemu-src-%d" % os.getpid(), monitor_address=srcmonaddr) dst = QEMUMachine(self._binary, args=self._get_dst_args(hardware, uri), wrapper=self._get_dst_wrapper(hardware), name="qemu-dst-%d" % os.getpid(), monitor_address=dstmonaddr) try: src.launch() dst.launch() ret = self._migrate(hardware, scenario, src, dst, uri) progress_history = ret[0] qemu_timings = ret[1] vcpu_timings = ret[2] if uri[0:5] == "unix:": os.remove(uri[5:]) if self._verbose: print("Finished migration") src.shutdown() dst.shutdown() return Report( hardware, scenario, progress_history, Timings(self._get_timings(src) + self._get_timings(dst)), Timings(qemu_timings), Timings(vcpu_timings), self._binary, self._dst_host, self._kernel, self._initrd, self._transport, self._sleep) except Exception as e: if self._debug: print("Failed: %s" % str(e)) try: src.shutdown() except: pass try: dst.shutdown() except: pass if self._debug: print(src.get_log()) print(dst.get_log()) raise
def _new_vm(self, *args): vm = QEMUMachine(self.qemu_bin) if args: vm.add_args(*args) return vm
def bench_block_job(cmd, cmd_args, qemu_args): """Benchmark block-job cmd -- qmp command to run block-job (like blockdev-backup) cmd_args -- dict of qmp command arguments qemu_args -- list of Qemu command line arguments, including path to Qemu binary Returns {'seconds': int} on success and {'error': str} on failure, dict may contain addional 'vm-log' field. Return value is compatible with simplebench lib. """ vm = QEMUMachine(qemu_args[0], args=qemu_args[1:]) try: vm.launch() except OSError as e: return {'error': 'popen failed: ' + str(e)} except (QMPConnectError, socket.timeout): return {'error': 'qemu failed: ' + str(vm.get_log())} try: res = vm.qmp(cmd, **cmd_args) if res != {'return': {}}: vm.shutdown() return {'error': '"{}" command failed: {}'.format(cmd, str(res))} e = vm.event_wait('JOB_STATUS_CHANGE') assert e['data']['status'] == 'created' start_ms = e['timestamp']['seconds'] * 1000000 + \ e['timestamp']['microseconds'] e = vm.events_wait( (('BLOCK_JOB_READY', None), ('BLOCK_JOB_COMPLETED', None), ('BLOCK_JOB_FAILED', None)), timeout=True) if e['event'] not in ('BLOCK_JOB_READY', 'BLOCK_JOB_COMPLETED'): vm.shutdown() return { 'error': 'block-job failed: ' + str(e), 'vm-log': vm.get_log() } end_ms = e['timestamp']['seconds'] * 1000000 + \ e['timestamp']['microseconds'] finally: vm.shutdown() return {'seconds': (end_ms - start_ms) / 1000000.0}
def _new_vm(self, *args): self._sd = tempfile.TemporaryDirectory(prefix="avo_qemu_sock_") vm = QEMUMachine(self.qemu_bin, sock_dir=self._sd.name) if args: vm.add_args(*args) return vm
def _new_vm(self, *args): vm = QEMUMachine(self.qemu_bin, sock_dir=tempfile.mkdtemp()) if args: vm.add_args(*args) return vm