From nobody Sat May 18 12:12:34 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1647427676558356.041663674348; Wed, 16 Mar 2022 03:47:56 -0700 (PDT) Received: from localhost ([::1]:36534 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nURC8-0006ML-8r for importer@patchew.org; Wed, 16 Mar 2022 06:47:56 -0400 Received: from eggs.gnu.org ([209.51.188.92]:44952) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nUQNI-0002U9-5p for qemu-devel@nongnu.org; Wed, 16 Mar 2022 05:55:24 -0400 Received: from beetle.greensocs.com ([5.135.226.135]:37366) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nUQNF-000871-FS for qemu-devel@nongnu.org; Wed, 16 Mar 2022 05:55:23 -0400 Received: from crumble.bar.greensocs.com (unknown [172.17.10.6]) by beetle.greensocs.com (Postfix) with ESMTPS id B1E2C2077C; Wed, 16 Mar 2022 09:55:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=greensocs.com; s=mail; t=1647424517; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=xDgmgKOc2H7XHHBYrssQUDnFNttHwY3wGJ50JVV6H3s=; b=Iu0E5JpnzDdOpk3EM8tUGPsJotml+24ehVsoKUuk1/dwR5C8dFjocpYCRJB9EZfqp7+nDD R3y58JVGj6LXCTFTc0lb4PrbQUhosTc6s+z/SFvBg9xtyd3sSYL7gtMvX6Qs/j95gFnAjc oGFWjBIG4BycpudAARzqi28H/cvfzwQ= From: Damien Hedde To: qemu-devel@nongnu.org Subject: [RFC PATCH] python: add qmp-send program to send raw qmp commands to qemu Date: Wed, 16 Mar 2022 10:54:55 +0100 Message-Id: <20220316095455.6473-1-damien.hedde@greensocs.com> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=5.135.226.135; envelope-from=damien.hedde@greensocs.com; helo=beetle.greensocs.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Damien Hedde , berrange@redhat.com, Beraldo Leal , Markus Armbruster , Cleber Rosa , John Snow Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1647427680351100001 Content-Type: text/plain; charset="utf-8" It takes an input file containing raw qmp commands (concatenated json dicts) and send all commands one by one to a qmp server. When one command fails, it exits. As a convenience, it can also wrap the qemu process to avoid having to start qemu in background. When wrapping qemu, the program returns only when the qemu process terminates. Signed-off-by: Damien Hedde --- Hi all, Following our discussion, I've started this. What do you think ? I tried to follow Daniel's qmp-shell-wrap. I think it is better to have similar options (eg: logging). There is also room for factorizing code if we want to keep them aligned and ease maintenance. There are still some pylint issues (too many branches in main and it does not like my context manager if else line). But it's kind of a mess to fix theses so I think it's enough for a first version. I name that qmp-send as Daniel proposed, maybe qmp-test matches better what I'm doing there ? Thanks, Damien --- python/qemu/aqmp/qmp_send.py | 229 +++++++++++++++++++++++++++++++++++ scripts/qmp/qmp-send | 11 ++ 2 files changed, 240 insertions(+) create mode 100644 python/qemu/aqmp/qmp_send.py create mode 100755 scripts/qmp/qmp-send diff --git a/python/qemu/aqmp/qmp_send.py b/python/qemu/aqmp/qmp_send.py new file mode 100644 index 0000000000..cbca1d0205 --- /dev/null +++ b/python/qemu/aqmp/qmp_send.py @@ -0,0 +1,229 @@ +# +# Copyright (C) 2022 Greensocs +# +# This work is licensed under the terms of the GNU GPL, version 2 or +# later. See the COPYING file in the top-level directory. +# + +""" +usage: qmp-send [-h] [-f FILE] [-s SOCKET] [-v] [-p] [--wrap ...] + +Send raw qmp commands to qemu as long as they succeed. It either connects = to a +remote qmp server using the provided socket or wrap the qemu process. It s= tops +sending the provided commands when a command fails (disconnection or error +response). + +optional arguments: + -h, --help show this help message and exit + -f FILE, --file FILE Input file containing the commands + -s SOCKET, --socket SOCKET + < UNIX socket path | TCP address:port > + -v, --verbose Verbose (echo commands sent and received) + -p, --pretty Pretty-print JSON + --wrap ... QEMU command line to invoke + +When qemu wrap option is used, this script waits for qemu to terminate but +never send any quit or kill command. This needs to be done manually. +""" + +import argparse +import contextlib +import json +import logging +import os +from subprocess import Popen +import sys +from typing import List, TextIO + +from qemu.aqmp import ConnectError, QMPError, SocketAddrT +from qemu.aqmp.legacy import ( + QEMUMonitorProtocol, + QMPBadPortError, + QMPMessage, +) + + +LOG =3D logging.getLogger(__name__) + + +class QmpRawDecodeError(Exception): + """ + Exception for raw qmp decoding + + msg: exception message + lineno: input line of the error + colno: input column of the error + """ + def __init__(self, msg: str, lineno: int, colno: int): + self.msg =3D msg + self.lineno =3D lineno + self.colno =3D colno + super().__init__(f"{msg}: line {lineno} column {colno}") + + +class QMPSendError(QMPError): + """ + QMP Send Base error class. + """ + + +class QMPSend(QEMUMonitorProtocol): + """ + QMP Send class. + """ + def __init__(self, address: SocketAddrT, + pretty: bool =3D False, + verbose: bool =3D False, + server: bool =3D False): + super().__init__(address, server=3Dserver) + self._verbose =3D verbose + self._pretty =3D pretty + self._server =3D server + + def setup_connection(self) -> None: + """Setup the connetion with the remote client/server.""" + if self._server: + self.accept() + else: + self.connect() + + def _print(self, qmp_message: object) -> None: + jsobj =3D json.dumps(qmp_message, + indent=3D4 if self._pretty else None, + sort_keys=3Dself._pretty) + print(str(jsobj)) + + def execute_cmd(self, cmd: QMPMessage) -> None: + """Execute a qmp command.""" + if self._verbose: + self._print(cmd) + resp =3D self.cmd_obj(cmd) + if resp is None: + raise QMPSendError("Disconnected") + if self._verbose: + self._print(resp) + if 'error' in resp: + raise QMPSendError(f"Command failed: {resp['error']}") + + +def raw_load(file: TextIO) -> List[QMPMessage]: + """parse a raw qmp command file. + + JSON formatted commands can expand on several lines but must + be separated by an end-of-line (two commands can not share the + same line). + File must not end with empty lines. + """ + cmds: List[QMPMessage] =3D [] + linecnt =3D 0 + while True: + buf =3D file.readline() + if not buf: + return cmds + prev_err_pos =3D None + buf_linecnt =3D 1 + while True: + try: + cmds.append(json.loads(buf)) + break + except json.JSONDecodeError as err: + if prev_err_pos =3D=3D err.pos: + # adding a line made no progress so + # + either we're at EOF and json data is truncated + # + or the parsing error is before + raise QmpRawDecodeError(err.msg, linecnt + err.lineno, + err.colno) from err + prev_err_pos =3D err.pos + buf +=3D file.readline() + buf_linecnt +=3D 1 + linecnt +=3D buf_linecnt + + +def report_error(msg: str) -> None: + """Write an error to stderr.""" + sys.stderr.write('ERROR: %s\n' % msg) + + +def main() -> None: + """ + qmp-send entry point: parse command line arguments and start the REPL. + """ + parser =3D argparse.ArgumentParser( + description=3D""" + Send raw qmp commands to qemu as long as they succeed. It eith= er + connects to a remote qmp server using the provided socket or w= rap + the qemu process. It stops sending the provided commands when a + command fails (disconnection or error response). + """, + epilog=3D""" + When qemu wrap option is used, this script waits for qemu + to terminate but never send any quit or kill command. This + needs to be done manually. + """) + + parser.add_argument('-f', '--file', action=3D'store', + help=3D'Input file containing the commands') + parser.add_argument('-s', '--socket', action=3D'store', + help=3D'< UNIX socket path | TCP address:port >') + parser.add_argument('-v', '--verbose', action=3D'store_true', + help=3D'Verbose (echo commands sent and received)') + parser.add_argument('-p', '--pretty', action=3D'store_true', + help=3D'Pretty-print JSON') + + parser.add_argument('--wrap', nargs=3Dargparse.REMAINDER, + help=3D'QEMU command line to invoke') + + args =3D parser.parse_args() + + socket =3D args.socket + wrap_qemu =3D args.wrap is not None + + if wrap_qemu: + if len(args.wrap) !=3D 0: + qemu_cmdline =3D args.wrap + else: + qemu_cmdline =3D ["qemu-system-x86_64"] + if socket is None: + socket =3D "qmp-send-wrap-%d" % os.getpid() + qemu_cmdline +=3D ["-qmp", "unix:%s" % socket] + + try: + address =3D QMPSend.parse_address(socket) + except QMPBadPortError: + parser.error(f"Bad port number: {socket}") + return # pycharm doesn't know error() is noreturn + + try: + with open(args.file, mode=3D'rt', encoding=3D'utf8') as file: + qmp_cmds =3D raw_load(file) + except QmpRawDecodeError as err: + report_error(str(err)) + sys.exit(1) + + try: + with QMPSend(address, args.pretty, args.verbose, + server=3Dwrap_qemu) as qmp: + # starting with python 3.7 we could use contextlib.nullcontext + qemu =3D Popen(qemu_cmdline) if wrap_qemu else contextlib.supp= ress() + with qemu: + try: + qmp.setup_connection() + except ConnectError as err: + if isinstance(err.exc, OSError): + report_error(f"Couldn't connect to {socket}: {err!= s}") + else: + report_error(str(err)) + sys.exit(1) + try: + for cmd in qmp_cmds: + qmp.execute_cmd(cmd) + except QMPError as err: + report_error(str(err)) + sys.exit(1) + finally: + if wrap_qemu: + os.unlink(socket) + + +if __name__ =3D=3D '__main__': + main() diff --git a/scripts/qmp/qmp-send b/scripts/qmp/qmp-send new file mode 100755 index 0000000000..8d3063797c --- /dev/null +++ b/scripts/qmp/qmp-send @@ -0,0 +1,11 @@ +#!/usr/bin/env python3 + +import os +import sys + +sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'pytho= n')) +from qemu.aqmp import qmp_send + + +if __name__ =3D=3D '__main__': + qmp_send.main() --=20 2.35.1