qemu.machine.machine module¶
QEMU machine module:
The machine module primarily provides the QEMUMachine class, which provides facilities for managing the lifetime of a QEMU VM.
- exception qemu.machine.machine.AbnormalShutdown[source]¶
Bases:
qemu.machine.machine.QEMUMachineError
Exception raised when a graceful shutdown was requested, but not performed.
- class qemu.machine.machine.QEMUMachine(binary: str, args: Sequence[str] = (), wrapper: Sequence[str] = (), name: Optional[str] = None, base_temp_dir: str = '/var/tmp', monitor_address: Optional[Union[Tuple[str, int], str]] = None, socket_scm_helper: Optional[str] = None, sock_dir: Optional[str] = None, drain_console: bool = False, console_log: Optional[str] = None, log_dir: Optional[str] = None, qmp_timer: Optional[float] = None)[source]¶
Bases:
object
A QEMU VM.
Use this object as a context manager to ensure the QEMU process terminates:
with VM(binary) as vm: ... # vm is guaranteed to be shut down here
- add_args(*args: str) → None[source]¶
Adds to the list of extra arguments to be given to the QEMU binary
- add_fd(fd: int, fdset: int, opaque: str, opts: str = '') → qemu.machine.machine._T[source]¶
Pass a file descriptor to the VM
- property args: List[str]¶
Returns the list of arguments given to the QEMU binary.
- command(cmd: str, conv_keys: bool = True, **args: Any) → object[source]¶
Invoke a QMP command. On success return the response dict. On failure raise an exception.
- property console_socket: socket.socket¶
Returns a socket connected to the console
- static event_match(event: Any, match: Optional[Any]) → bool[source]¶
Check if an event matches optional match criteria.
The match criteria takes the form of a matching subdict. The event is checked to be a superset of the subdict, recursively, with matching values whenever the subdict values are not None.
This has a limitation that you cannot explicitly check for None values.
- Examples, with the subdict queries on the left:
None matches any object.
{“foo”: None} matches {“foo”: {“bar”: 1}}
{“foo”: None} matches {“foo”: 5}
{“foo”: {“abc”: None}} does not match {“foo”: {“bar”: 1}}
{“foo”: {“rab”: 2}} matches {“foo”: {“bar”: 1, “rab”: 2}}
- event_wait(name: str, timeout: float = 60.0, match: Optional[Dict[str, Any]] = None) → Optional[Dict[str, Any]][source]¶
event_wait waits for and returns a named event from QMP with a timeout.
name: The event to wait for. timeout: QEMUMonitorProtocol.pull_event timeout parameter. match: Optional match criteria. See event_match for details.
- events_wait(events: Sequence[Tuple[str, Any]], timeout: float = 60.0) → Optional[Dict[str, Any]][source]¶
events_wait waits for and returns a single named event from QMP. In the case of multiple qualifying events, this function returns the first one.
- Parameters
events – A sequence of (name, match_criteria) tuples. The match criteria are optional and may be None. See event_match for details.
timeout – Optional timeout, in seconds. See QEMUMonitorProtocol.pull_event.
- Raises
QMPTimeoutError – If timeout was non-zero and no matching events were found.
- Returns
A QMP event matching the filter criteria. If timeout was 0 and no event matched, None.
- get_log() → Optional[str][source]¶
After self.shutdown or failed qemu execution, this returns the output of the qemu process.
- get_qmp_event(wait: bool = False) → Optional[Dict[str, Any]][source]¶
Poll for one queued QMP events and return it
- get_qmp_events(wait: bool = False) → List[Dict[str, Any]][source]¶
Poll for queued QMP events and return a list of dicts
- launch() → None[source]¶
Launch the VM and make sure we cleanup and expose the command line/output in case of exception
- property log_dir: str¶
Returns a directory to be used for writing logs
- qmp(cmd: str, args_dict: Optional[Dict[str, object]] = None, conv_keys: Optional[bool] = None, **args: Any) → Dict[str, Any][source]¶
Invoke a QMP command and return the response dict
- send_fd_scm(fd: Optional[int] = None, file_path: Optional[str] = None) → int[source]¶
Send an fd or file_path to socket_scm_helper.
Exactly one of fd and file_path must be given. If it is file_path, the helper will open that file and pass its own fd.
- set_console(device_type: Optional[str] = None, console_index: int = 0) → None[source]¶
Sets the device type for a console device
If set, the console device and a backing character device will be added to the base arguments of the resulting QEMU command line.
This is a convenience method that will either use the provided device type, or default to a “-serial chardev:console” command line argument.
The actual setting of command line arguments will be be done at machine launch time, as it depends on the temporary directory to be created.
- @param device_type: the device type, such as “isa-serial”. If
None is given (the default value) a “-serial chardev:console” command line argument will be used instead, resorting to the machine’s default device type.
- @param console_index: the index of the console device to use.
If not zero, the command line will create ‘index - 1’ consoles and connect them to the ‘null’ backing character device.
- set_machine(machine_type: str) → None[source]¶
Sets the machine type
If set, the machine type will be added to the base arguments of the resulting QEMU command line.
- set_qmp_monitor(enabled: bool = True) → None[source]¶
Set the QMP monitor.
- @param enabled: if False, qmp monitor options will be removed from
the base arguments of the resulting QEMU command line. Default is True.
Note
Call this function before launch().
- shutdown(has_quit: bool = False, hard: bool = False, timeout: Optional[int] = 30) → None[source]¶
Terminate the VM (gracefully if possible) and perform cleanup. Cleanup will always be performed.
If the VM has not yet been launched, or shutdown(), wait(), or kill() have already been called, this method does nothing.
- Parameters
has_quit – When true, do not attempt to issue ‘quit’ QMP command.
hard – When true, do not attempt graceful shutdown, and suppress the SIGKILL warning log message.
timeout – Optional timeout in seconds for graceful shutdown. Default 30 seconds, A
None
value is an infinite wait.
- property temp_dir: str¶
Returns a temporary directory to be used for this machine
- exception qemu.machine.machine.QEMUMachineAddDeviceError[source]¶
Bases:
qemu.machine.machine.QEMUMachineError
Exception raised when a request to add a device can not be fulfilled
The failures are caused by limitations, lack of information or conflicting requests on the QEMUMachine methods. This exception does not represent failures reported by the QEMU binary itself.