Exemple #1
0
 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)
Exemple #2
0
    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)
Exemple #3
0
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()
            }
        if 'error' in e['data']:
            vm.shutdown()
            return {
                'error': 'block-job failed: ' + e['data']['error'],
                '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}