[PATCH v2 0/4] qemu: Introduce 'virDomainQemuMonitorCommandWithFiles'

I was doing some tests with 'add-fd' and 'getfd' and made this to help me. v2: - add API support for passing FDs back, but for now we don't have use for it Peter Krempa (4): virnetmessage: Introduce virNetMessageClearFDs lib: Introduce 'virDomainQemuMonitorCommandWithFiles' virsh: Implement support for virDomainQemuMonitorCommandWithFiles qemu: Implement qemuDomainQemuMonitorCommandWithFiles docs/manpages/virsh.rst | 6 +- include/libvirt/libvirt-qemu.h | 8 +++ src/driver-hypervisor.h | 10 ++++ src/libvirt-qemu.c | 89 +++++++++++++++++++++++++++++ src/libvirt_qemu.syms | 5 ++ src/libvirt_remote.syms | 1 + src/qemu/qemu_driver.c | 42 ++++++++++++-- src/qemu/qemu_monitor.c | 7 ++- src/qemu/qemu_monitor.h | 1 + src/qemu/qemu_monitor_json.c | 6 +- src/qemu/qemu_monitor_json.h | 2 + src/qemu/qemu_monitor_text.c | 8 +-- src/qemu_protocol-structs | 9 +++ src/remote/qemu_protocol.x | 20 ++++++- src/remote/remote_daemon_dispatch.c | 62 ++++++++++++++++++++ src/remote/remote_driver.c | 57 ++++++++++++++++++ src/rpc/virnetmessage.c | 9 ++- src/rpc/virnetmessage.h | 1 + tools/virsh-domain.c | 19 +++++- 19 files changed, 345 insertions(+), 17 deletions(-) -- 2.35.1

The helper splits out the clearing of the FDs transacted inside a virNetMessage. APIs transacting FDs both from and to the client at the same time will need to clear the FDs stored in virNetMessage as the structure is re-used for the reply and without clearing the list of FDs we'd return the FDs sent by the client in addition to the new FDs sent by the API.t Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- src/libvirt_remote.syms | 1 + src/rpc/virnetmessage.c | 9 ++++++++- src/rpc/virnetmessage.h | 1 + 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/libvirt_remote.syms b/src/libvirt_remote.syms index 2fa46e5e0c..f0f90815cf 100644 --- a/src/libvirt_remote.syms +++ b/src/libvirt_remote.syms @@ -102,6 +102,7 @@ virNetDaemonUpdateServices; # rpc/virnetmessage.h virNetMessageAddFD; virNetMessageClear; +virNetMessageClearFDs; virNetMessageClearPayload; virNetMessageDecodeHeader; virNetMessageDecodeLength; diff --git a/src/rpc/virnetmessage.c b/src/rpc/virnetmessage.c index ca11f1688e..221da7c59b 100644 --- a/src/rpc/virnetmessage.c +++ b/src/rpc/virnetmessage.c @@ -48,7 +48,7 @@ virNetMessage *virNetMessageNew(bool tracked) void -virNetMessageClearPayload(virNetMessage *msg) +virNetMessageClearFDs(virNetMessage *msg) { size_t i; @@ -58,6 +58,13 @@ virNetMessageClearPayload(virNetMessage *msg) msg->donefds = 0; msg->nfds = 0; VIR_FREE(msg->fds); +} + + +void +virNetMessageClearPayload(virNetMessage *msg) +{ + virNetMessageClearFDs(msg); msg->bufferOffset = 0; msg->bufferLength = 0; diff --git a/src/rpc/virnetmessage.h b/src/rpc/virnetmessage.h index aadf1b69b0..8f878962f8 100644 --- a/src/rpc/virnetmessage.h +++ b/src/rpc/virnetmessage.h @@ -49,6 +49,7 @@ struct _virNetMessage { virNetMessage *virNetMessageNew(bool tracked); +void virNetMessageClearFDs(virNetMessage *msg); void virNetMessageClearPayload(virNetMessage *msg); void virNetMessageClear(virNetMessage *); -- 2.35.1

This API has the same semantics as 'virDomainQemuMonitorCommand' but accepts file descriptors which are then forwarded to qemu. Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- include/libvirt/libvirt-qemu.h | 8 +++ src/driver-hypervisor.h | 10 ++++ src/libvirt-qemu.c | 89 +++++++++++++++++++++++++++++ src/libvirt_qemu.syms | 5 ++ src/qemu_protocol-structs | 9 +++ src/remote/qemu_protocol.x | 20 ++++++- src/remote/remote_daemon_dispatch.c | 62 ++++++++++++++++++++ src/remote/remote_driver.c | 57 ++++++++++++++++++ 8 files changed, 259 insertions(+), 1 deletion(-) diff --git a/include/libvirt/libvirt-qemu.h b/include/libvirt/libvirt-qemu.h index 0cc2872821..98f2ac3823 100644 --- a/include/libvirt/libvirt-qemu.h +++ b/include/libvirt/libvirt-qemu.h @@ -37,6 +37,14 @@ typedef enum { int virDomainQemuMonitorCommand(virDomainPtr domain, const char *cmd, char **result, unsigned int flags); +int virDomainQemuMonitorCommandWithFiles(virDomainPtr domain, + const char *cmd, + unsigned int ninfiles, + int *infiles, + unsigned int *noutfiles, + int **outfiles, + char **result, + unsigned int flags); virDomainPtr virDomainQemuAttach(virConnectPtr domain, unsigned int pid_value, diff --git a/src/driver-hypervisor.h b/src/driver-hypervisor.h index c83fb648a2..4423eb0885 100644 --- a/src/driver-hypervisor.h +++ b/src/driver-hypervisor.h @@ -874,6 +874,15 @@ typedef int const char *cmd, char **result, unsigned int flags); +typedef int +(*virDrvDomainQemuMonitorCommandWithFiles)(virDomainPtr domain, + const char *cmd, + unsigned int ninfiles, + int *infiles, + unsigned int *noutfiles, + int **outfiles, + char **result, + unsigned int flags); typedef char * (*virDrvDomainQemuAgentCommand)(virDomainPtr domain, @@ -1597,6 +1606,7 @@ struct _virHypervisorDriver { virDrvDomainRevertToSnapshot domainRevertToSnapshot; virDrvDomainSnapshotDelete domainSnapshotDelete; virDrvDomainQemuMonitorCommand domainQemuMonitorCommand; + virDrvDomainQemuMonitorCommandWithFiles domainQemuMonitorCommandWithFiles; virDrvDomainQemuAttach domainQemuAttach; virDrvDomainQemuAgentCommand domainQemuAgentCommand; virDrvConnectDomainQemuMonitorEventRegister connectDomainQemuMonitorEventRegister; diff --git a/src/libvirt-qemu.c b/src/libvirt-qemu.c index 1afb5fe529..9e80577b56 100644 --- a/src/libvirt-qemu.c +++ b/src/libvirt-qemu.c @@ -96,6 +96,95 @@ virDomainQemuMonitorCommand(virDomainPtr domain, const char *cmd, } +/** + * virDomainQemuMonitorCommandWithFiles: + * @domain: a domain object + * @cmd: the qemu monitor command string + * @ninfiles: number of filedescriptors passed in @infiles + * @infiles: filedescriptors to be passed to qemu with the command + * @noutfiles: if non-NULL filled with number of returned file descriptors + * @outfiles: if non-NULL filled with an array of returned file descriptors + * @result: a string returned by @cmd + * @flags: bitwise-or of supported virDomainQemuMonitorCommandFlags + * + * This API is QEMU specific, so it will only work with hypervisor + * connections to the QEMU driver with local connections using the unix socket. + * + * Send an arbitrary monitor command @cmd with file descriptors @infiles to + * @domain through the qemu monitor and optionally return file descriptors via + * @outfiles. There are several requirements to safely and successfully use + * this API: + * + * - A @cmd that queries state without making any modifications is safe + * - A @cmd that alters state that is also tracked by libvirt is unsafe, + * and may cause libvirtd to crash + * - A @cmd that alters state not tracked by the current version of + * libvirt is possible as a means to test new qemu features before + * they have support in libvirt, but no guarantees are made to safety + * + * If VIR_DOMAIN_QEMU_MONITOR_COMMAND_HMP is set, the command is + * considered to be a human monitor command and libvirt will automatically + * convert it into QMP if needed. In that case the @result will also + * be converted back from QMP. + * + * If successful, @result will be filled with the string output of the + * @cmd, and the caller must free this string. + * + * Returns 0 in case of success, -1 in case of failure + */ +int +virDomainQemuMonitorCommandWithFiles(virDomainPtr domain, + const char *cmd, + unsigned int ninfiles, + int *infiles, + unsigned int *noutfiles, + int **outfiles, + char **result, + unsigned int flags) +{ + virConnectPtr conn; + + VIR_DOMAIN_DEBUG(domain, + "cmd=%s, ninfiles=%u, infiles=%p, noutfiles=%p, outfiles=%p, result=%p, flags=0x%x", + cmd, ninfiles, infiles, noutfiles, outfiles, result, flags); + + virResetLastError(); + + virCheckDomainReturn(domain, -1); + conn = domain->conn; + + if (ninfiles > 0 || outfiles) { + int rc; + if ((rc = VIR_DRV_SUPPORTS_FEATURE(conn->driver, conn, + VIR_DRV_FEATURE_FD_PASSING) <= 0)) { + if (rc == 0) + virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", + _("fd passing is not supported by this connection")); + goto error; + } + } + + virCheckNonNullArgGoto(result, error); + virCheckReadOnlyGoto(conn->flags, error); + + if (conn->driver->domainQemuMonitorCommandWithFiles) { + int ret; + ret = conn->driver->domainQemuMonitorCommandWithFiles(domain, cmd, + ninfiles, infiles, + noutfiles, outfiles, + result, flags); + if (ret < 0) + goto error; + return ret; + } + + virReportUnsupportedError(); + + error: + virDispatchError(conn); + return -1; +} + /** * virDomainQemuAttach: * @conn: pointer to a hypervisor connection diff --git a/src/libvirt_qemu.syms b/src/libvirt_qemu.syms index 3a297e3a2b..019e545101 100644 --- a/src/libvirt_qemu.syms +++ b/src/libvirt_qemu.syms @@ -30,3 +30,8 @@ LIBVIRT_QEMU_1.2.3 { virConnectDomainQemuMonitorEventDeregister; virConnectDomainQemuMonitorEventRegister; } LIBVIRT_QEMU_0.10.0; + +LIBVIRT_QEMU_8.1.0 { + global: + virDomainQemuMonitorCommandWithFiles; +} LIBVIRT_QEMU_1.2.3; diff --git a/src/qemu_protocol-structs b/src/qemu_protocol-structs index 8501543cd9..ea0854385f 100644 --- a/src/qemu_protocol-structs +++ b/src/qemu_protocol-structs @@ -47,6 +47,14 @@ struct qemu_domain_monitor_event_msg { u_int micros; remote_string details; }; +struct qemu_domain_monitor_command_with_files_args { + remote_nonnull_domain dom; + remote_nonnull_string cmd; + u_int flags; +}; +struct qemu_domain_monitor_command_with_files_ret { + remote_nonnull_string result; +}; enum qemu_procedure { QEMU_PROC_DOMAIN_MONITOR_COMMAND = 1, QEMU_PROC_DOMAIN_ATTACH = 2, @@ -54,4 +62,5 @@ enum qemu_procedure { QEMU_PROC_CONNECT_DOMAIN_MONITOR_EVENT_REGISTER = 4, QEMU_PROC_CONNECT_DOMAIN_MONITOR_EVENT_DEREGISTER = 5, QEMU_PROC_DOMAIN_MONITOR_EVENT = 6, + QEMU_PROC_DOMAIN_MONITOR_COMMAND_WITH_FILES = 7, }; diff --git a/src/remote/qemu_protocol.x b/src/remote/qemu_protocol.x index 8ff5dc8568..c7f3abfcbf 100644 --- a/src/remote/qemu_protocol.x +++ b/src/remote/qemu_protocol.x @@ -79,6 +79,17 @@ struct qemu_domain_monitor_event_msg { remote_string details; }; +struct qemu_domain_monitor_command_with_files_args { + remote_nonnull_domain dom; + remote_nonnull_string cmd; + unsigned int flags; +}; + +struct qemu_domain_monitor_command_with_files_ret { + remote_nonnull_string result; +}; + + /* Define the program number, protocol version and procedure numbers here. */ const QEMU_PROGRAM = 0x20008087; const QEMU_PROTOCOL_VERSION = 1; @@ -151,5 +162,12 @@ enum qemu_procedure { * @generate: both * @acl: none */ - QEMU_PROC_DOMAIN_MONITOR_EVENT = 6 + QEMU_PROC_DOMAIN_MONITOR_EVENT = 6, + + /** + * @generate: none + * @priority: low + * @acl: domain:write + */ + QEMU_PROC_DOMAIN_MONITOR_COMMAND_WITH_FILES = 7 }; diff --git a/src/remote/remote_daemon_dispatch.c b/src/remote/remote_daemon_dispatch.c index 587cc0b3a5..510856024c 100644 --- a/src/remote/remote_daemon_dispatch.c +++ b/src/remote/remote_daemon_dispatch.c @@ -4684,6 +4684,68 @@ qemuDispatchDomainMonitorCommand(virNetServer *server G_GNUC_UNUSED, } +static int +qemuDispatchDomainMonitorCommandWithFiles(virNetServer *server G_GNUC_UNUSED, + virNetServerClient *client, + virNetMessage *msg, + struct virNetMessageError *rerr, + qemu_domain_monitor_command_with_files_args *args, + qemu_domain_monitor_command_with_files_ret *ret) +{ + virDomainPtr dom = NULL; + int *infiles = NULL; + unsigned int ninfiles = 0; + int *outfiles = NULL; + unsigned int noutfiles = 0; + int rv = -1; + virConnectPtr conn = remoteGetHypervisorConn(client); + size_t i; + + if (!conn) + goto cleanup; + + if (!(dom = get_nonnull_domain(conn, args->dom))) + goto cleanup; + + infiles = g_new0(int, msg->nfds); + for (i = 0; i < msg->nfds; i++) { + if ((infiles[i] = virNetMessageDupFD(msg, i)) < 0) + goto cleanup; + ninfiles++; + } + + /* This API can both receive FDs from the client and send FDs back, but 'msg' + * is being reused. Thus we must clear the list of FDs in it to prevent + * us sending back the FDs client sent us. */ + virNetMessageClearFDs(msg); + + if (virDomainQemuMonitorCommandWithFiles(dom, args->cmd, ninfiles, infiles, + &noutfiles, &outfiles, + &ret->result, args->flags) < 0) + goto cleanup; + + for (i = 0; i < noutfiles; i++) { + if (virNetMessageAddFD(msg, outfiles[i]) < 0) + goto cleanup; + } + + /* return 1 here to let virNetServerProgramDispatchCall know we are passing fds */ + if (noutfiles > 0) + rv = 1; + else + rv = 0; + + cleanup: + for (i = 0; i < noutfiles; i++) + VIR_FORCE_CLOSE(outfiles[i]); + + if (rv < 0) + virNetMessageSaveError(rerr); + virObjectUnref(dom); + return rv; +} + + static int remoteDispatchDomainMigrateBegin3(virNetServer *server G_GNUC_UNUSED, virNetServerClient *client, diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index d721da6d2d..4babfbaa94 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -5931,6 +5931,62 @@ remoteDomainQemuMonitorCommand(virDomainPtr domain, const char *cmd, } +static int +remoteDomainQemuMonitorCommandWithFiles(virDomainPtr domain, + const char *cmd, + unsigned int ninfiles, + int *infiles, + unsigned int *noutfiles, + int **outfiles, + char **result, + unsigned int flags) +{ + int rv = -1; + qemu_domain_monitor_command_with_files_args args; + qemu_domain_monitor_command_with_files_ret ret; + struct private_data *priv = domain->conn->privateData; + size_t rpc_noutfiles = 0; + g_autofree int *rpc_outfiles = NULL; + size_t i; + + remoteDriverLock(priv); + + make_nonnull_domain(&args.dom, domain); + args.cmd = (char *)cmd; + args.flags = flags; + + memset(&ret, 0, sizeof(ret)); + if (callFull(domain->conn, priv, REMOTE_CALL_QEMU, + infiles, ninfiles, &rpc_outfiles, &rpc_noutfiles, + QEMU_PROC_DOMAIN_MONITOR_COMMAND_WITH_FILES, + (xdrproc_t) xdr_qemu_domain_monitor_command_with_files_args, (char *) &args, + (xdrproc_t) xdr_qemu_domain_monitor_command_with_files_ret, (char *) &ret) == -1) + goto done; + + if (outfiles) + *outfiles = g_steal_pointer(&rpc_outfiles); + + if (noutfiles) + *noutfiles = rpc_noutfiles; + + *result = g_strdup(ret.result); + + rv = 0; + + xdr_free((xdrproc_t) xdr_qemu_domain_monitor_command_with_files_ret, (char *) &ret); + + done: + if (rpc_outfiles) { + for (i = 0; rpc_noutfiles < i; i++) { + VIR_FORCE_CLOSE(rpc_outfiles[i]); + } + } + + remoteDriverUnlock(priv); + return rv; +} + + static char * remoteDomainMigrateBegin3(virDomainPtr domain, const char *xmlin, @@ -8506,6 +8562,7 @@ static virHypervisorDriver hypervisor_driver = { .domainSnapshotHasMetadata = remoteDomainSnapshotHasMetadata, /* 0.9.13 */ .domainSnapshotDelete = remoteDomainSnapshotDelete, /* 0.8.0 */ .domainQemuMonitorCommand = remoteDomainQemuMonitorCommand, /* 0.8.3 */ + .domainQemuMonitorCommandWithFiles = remoteDomainQemuMonitorCommandWithFiles, /* 8.1.0 */ .domainQemuAttach = remoteDomainQemuAttach, /* 0.9.4 */ .domainQemuAgentCommand = remoteDomainQemuAgentCommand, /* 0.10.0 */ .connectDomainQemuMonitorEventRegister = remoteConnectDomainQemuMonitorEventRegister, /* 1.2.3 */ -- 2.35.1

Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- docs/manpages/virsh.rst | 6 +++++- tools/virsh-domain.c | 19 ++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst index 429879d2dd..1e6914a438 100644 --- a/docs/manpages/virsh.rst +++ b/docs/manpages/virsh.rst @@ -7894,7 +7894,8 @@ qemu-monitor-command :: - qemu-monitor-command domain { [--hmp] | [--pretty] [--return-value] } command... + qemu-monitor-command domain { [--hmp] | [--pretty] [--return-value] } + [--pass-fds N,M,...] command... Send an arbitrary monitor command *command* to domain *domain* through the QEMU monitor. The results of the command will be printed on stdout. @@ -7927,6 +7928,9 @@ extracted rather than passing through the full reply from QEMU. If *--hmp* is passed, the command is considered to be a human monitor command and libvirt will automatically convert it into QMP and convert the result back. +If *--pass-fds* is specified, the argument is a comma separated list +of open file descriptors which should be passed on to qemu along with the +command. qemu-agent-command ------------------ diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 433ea674b5..1ebfb3a567 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -9721,6 +9721,11 @@ static const vshCmdOptDef opts_qemu_monitor_command[] = { .type = VSH_OT_BOOL, .help = N_("extract the value of the 'return' key from the returned string") }, + {.name = "pass-fds", + .type = VSH_OT_STRING, + .completer = virshCompleteEmpty, + .help = N_("pass file descriptors N,M,... along with the command") + }, {.name = "cmd", .type = VSH_OT_ARGV, .flags = VSH_OFLAG_REQ, @@ -9819,6 +9824,8 @@ cmdQemuMonitorCommand(vshControl *ctl, const vshCmd *cmd) bool returnval = vshCommandOptBool(cmd, "return-value"); virJSONValue *formatjson; g_autofree char *jsonstr = NULL; + g_autofree int *fds = NULL; + size_t nfds = 0; VSH_EXCLUSIVE_OPTIONS("hmp", "pretty"); VSH_EXCLUSIVE_OPTIONS("hmp", "return-value"); @@ -9838,9 +9845,19 @@ cmdQemuMonitorCommand(vshControl *ctl, const vshCmd *cmd) return NULL; } - if (virDomainQemuMonitorCommand(dom, monitor_cmd, &result, flags) < 0) + if (virshFetchPassFdsList(ctl, cmd, &nfds, &fds) < 0) return false; + if (fds) { + if (virDomainQemuMonitorCommandWithFiles(dom, monitor_cmd, nfds, fds, + NULL, NULL, + &result, flags) < 0) + return false; + } else { + if (virDomainQemuMonitorCommand(dom, monitor_cmd, &result, flags) < 0) + return false; + } + if (returnval || pretty) { resultjson = virJSONValueFromString(result); -- 2.35.1

Add support for sending one FD from the client along with a monitor command so that it's possible to use 'getfd' and 'add-fd' to use FDs passed from the client with other QMP commands. Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- src/qemu/qemu_driver.c | 42 ++++++++++++++++++++++++++++++++---- src/qemu/qemu_monitor.c | 7 +++--- src/qemu/qemu_monitor.h | 1 + src/qemu/qemu_monitor_json.c | 6 ++++-- src/qemu/qemu_monitor_json.h | 2 ++ src/qemu/qemu_monitor_text.c | 8 +++---- 6 files changed, 53 insertions(+), 13 deletions(-) diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index f1f708e511..706c47792c 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -13921,21 +13921,44 @@ qemuDomainBackupGetXMLDesc(virDomainPtr domain, } -static int qemuDomainQemuMonitorCommand(virDomainPtr domain, const char *cmd, - char **result, unsigned int flags) +static int +qemuDomainQemuMonitorCommandWithFiles(virDomainPtr domain, + const char *cmd, + unsigned int ninfds, + int *infds, + unsigned int *noutfds, + int **outfds, + char **result, + unsigned int flags) { virQEMUDriver *driver = domain->conn->privateData; virDomainObj *vm = NULL; int ret = -1; qemuDomainObjPrivate *priv; bool hmp; + int fd = -1; virCheckFlags(VIR_DOMAIN_QEMU_MONITOR_COMMAND_HMP, -1); + /* currently we don't pass back any fds */ + if (outfds) + *outfds = NULL; + if (noutfds) + *noutfds = 0; + + if (ninfds > 1) { + virReportError(VIR_ERR_INVALID_ARG, "%s", + _("at most 1 fd can be passed to qemu along with a command")); + return -1; + } + + if (ninfds == 1) + fd = infds[0]; + if (!(vm = qemuDomainObjFromDomain(domain))) goto cleanup; - if (virDomainQemuMonitorCommandEnsureACL(domain->conn, vm->def) < 0) + if (virDomainQemuMonitorCommandWithFilesEnsureACL(domain->conn, vm->def) < 0) goto cleanup; if (qemuDomainObjBeginJob(driver, vm, QEMU_JOB_QUERY) < 0) @@ -13951,7 +13974,7 @@ static int qemuDomainQemuMonitorCommand(virDomainPtr domain, const char *cmd, hmp = !!(flags & VIR_DOMAIN_QEMU_MONITOR_COMMAND_HMP); qemuDomainObjEnterMonitor(driver, vm); - ret = qemuMonitorArbitraryCommand(priv->mon, cmd, result, hmp); + ret = qemuMonitorArbitraryCommand(priv->mon, cmd, fd, result, hmp); qemuDomainObjExitMonitor(driver, vm); endjob: @@ -13963,6 +13986,16 @@ static int qemuDomainQemuMonitorCommand(virDomainPtr domain, const char *cmd, } +static int +qemuDomainQemuMonitorCommand(virDomainPtr domain, + const char *cmd, + char **result, + unsigned int flags) +{ + return qemuDomainQemuMonitorCommandWithFiles(domain, cmd, 0, NULL, NULL, NULL, result, flags); +} + + static int qemuDomainOpenConsole(virDomainPtr dom, const char *dev_name, @@ -20873,6 +20906,7 @@ static virHypervisorDriver qemuHypervisorDriver = { .domainRevertToSnapshot = qemuDomainRevertToSnapshot, /* 0.8.0 */ .domainSnapshotDelete = qemuDomainSnapshotDelete, /* 0.8.0 */ .domainQemuMonitorCommand = qemuDomainQemuMonitorCommand, /* 0.8.3 */ + .domainQemuMonitorCommandWithFiles = qemuDomainQemuMonitorCommandWithFiles, /* 8.1.0 */ .domainQemuAttach = NULL, /* 0.9.4 - 5.5.0 */ .domainQemuAgentCommand = qemuDomainQemuAgentCommand, /* 0.10.0 */ .connectDomainQemuMonitorEventRegister = qemuConnectDomainQemuMonitorEventRegister, /* 1.2.3 */ diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c index dc81e41783..4fcba588c7 100644 --- a/src/qemu/qemu_monitor.c +++ b/src/qemu/qemu_monitor.c @@ -3042,17 +3042,18 @@ qemuMonitorDrivePivot(qemuMonitor *mon, int qemuMonitorArbitraryCommand(qemuMonitor *mon, const char *cmd, + int fd, char **reply, bool hmp) { - VIR_DEBUG("cmd=%s, reply=%p, hmp=%d", cmd, reply, hmp); + VIR_DEBUG("cmd=%s, fd=%d, reply=%p, hmp=%d", cmd, fd, reply, hmp); QEMU_CHECK_MONITOR(mon); if (hmp) - return qemuMonitorJSONHumanCommand(mon, cmd, reply); + return qemuMonitorJSONHumanCommand(mon, cmd, fd, reply); else - return qemuMonitorJSONArbitraryCommand(mon, cmd, reply); + return qemuMonitorJSONArbitraryCommand(mon, cmd, fd, reply); } diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h index 1d21183d82..c8e8036f26 100644 --- a/src/qemu/qemu_monitor.h +++ b/src/qemu/qemu_monitor.h @@ -1088,6 +1088,7 @@ char *qemuMonitorDiskNameLookup(qemuMonitor *mon, int qemuMonitorArbitraryCommand(qemuMonitor *mon, const char *cmd, + int fd, char **reply, bool hmp); diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c index 1de932f638..ad040e22c1 100644 --- a/src/qemu/qemu_monitor_json.c +++ b/src/qemu/qemu_monitor_json.c @@ -1462,6 +1462,7 @@ qemuMonitorJSONHandleGuestCrashloaded(qemuMonitor *mon, int qemuMonitorJSONHumanCommand(qemuMonitor *mon, const char *cmd_str, + int fd, char **reply_str) { g_autoptr(virJSONValue) cmd = NULL; @@ -1473,7 +1474,7 @@ qemuMonitorJSONHumanCommand(qemuMonitor *mon, "s:command-line", cmd_str, NULL); - if (!cmd || qemuMonitorJSONCommand(mon, cmd, &reply) < 0) + if (!cmd || qemuMonitorJSONCommandWithFd(mon, cmd, fd, &reply) < 0) return -1; if (qemuMonitorJSONHasError(reply, "CommandNotFound")) { @@ -4495,6 +4496,7 @@ qemuMonitorJSONDiskNameLookup(qemuMonitor *mon, int qemuMonitorJSONArbitraryCommand(qemuMonitor *mon, const char *cmd_str, + int fd, char **reply_str) { g_autoptr(virJSONValue) cmd = NULL; @@ -4503,7 +4505,7 @@ int qemuMonitorJSONArbitraryCommand(qemuMonitor *mon, if (!(cmd = virJSONValueFromString(cmd_str))) return -1; - if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0) + if (qemuMonitorJSONCommandWithFd(mon, cmd, fd, &reply) < 0) return -1; if (!(*reply_str = virJSONValueToString(reply, false))) diff --git a/src/qemu/qemu_monitor_json.h b/src/qemu/qemu_monitor_json.h index eea3478af0..e34a30c889 100644 --- a/src/qemu/qemu_monitor_json.h +++ b/src/qemu/qemu_monitor_json.h @@ -43,6 +43,7 @@ qemuMonitorJSONIOProcess(qemuMonitor *mon, int qemuMonitorJSONHumanCommand(qemuMonitor *mon, const char *cmd, + int fd, char **reply); int @@ -369,6 +370,7 @@ qemuMonitorJSONDiskNameLookup(qemuMonitor *mon, int qemuMonitorJSONArbitraryCommand(qemuMonitor *mon, const char *cmd_str, + int fd, char **reply_str); int diff --git a/src/qemu/qemu_monitor_text.c b/src/qemu/qemu_monitor_text.c index 0ca7f5a470..86af1e4975 100644 --- a/src/qemu/qemu_monitor_text.c +++ b/src/qemu/qemu_monitor_text.c @@ -43,7 +43,7 @@ int qemuMonitorTextAddDrive(qemuMonitor *mon, * address required when attaching drives to a controller */ cmd = g_strdup_printf("drive_add dummy %s", drivestr); - if (qemuMonitorJSONHumanCommand(mon, cmd, &reply) < 0) + if (qemuMonitorJSONHumanCommand(mon, cmd, -1, &reply) < 0) return -1; if (strstr(reply, "unknown command:")) { @@ -93,7 +93,7 @@ int qemuMonitorTextDriveDel(qemuMonitor *mon, cmd = g_strdup_printf("drive_del %s", drivestr); - if (qemuMonitorJSONHumanCommand(mon, cmd, &reply) < 0) + if (qemuMonitorJSONHumanCommand(mon, cmd, -1, &reply) < 0) return -1; if (strstr(reply, "unknown command:")) { @@ -124,7 +124,7 @@ qemuMonitorTextCreateSnapshot(qemuMonitor *mon, cmd = g_strdup_printf("savevm \"%s\"", name); - if (qemuMonitorJSONHumanCommand(mon, cmd, &reply)) + if (qemuMonitorJSONHumanCommand(mon, cmd, -1, &reply)) return -1; if (strstr(reply, "Error while creating snapshot") || @@ -150,7 +150,7 @@ int qemuMonitorTextDeleteSnapshot(qemuMonitor *mon, const char *name) g_autofree char *reply = NULL; cmd = g_strdup_printf("delvm \"%s\"", name); - if (qemuMonitorJSONHumanCommand(mon, cmd, &reply)) + if (qemuMonitorJSONHumanCommand(mon, cmd, -1, &reply)) return -1; if (strstr(reply, "No block device supports snapshots")) { -- 2.35.1

On a Monday in 2022, Peter Krempa wrote:
I was doing some tests with 'add-fd' and 'getfd' and made this to help me.
v2: - add API support for passing FDs back, but for now we don't have use for it
Peter Krempa (4): virnetmessage: Introduce virNetMessageClearFDs lib: Introduce 'virDomainQemuMonitorCommandWithFiles' virsh: Implement support for virDomainQemuMonitorCommandWithFiles qemu: Implement qemuDomainQemuMonitorCommandWithFiles
docs/manpages/virsh.rst | 6 +- include/libvirt/libvirt-qemu.h | 8 +++ src/driver-hypervisor.h | 10 ++++ src/libvirt-qemu.c | 89 +++++++++++++++++++++++++++++ src/libvirt_qemu.syms | 5 ++ src/libvirt_remote.syms | 1 + src/qemu/qemu_driver.c | 42 ++++++++++++-- src/qemu/qemu_monitor.c | 7 ++- src/qemu/qemu_monitor.h | 1 + src/qemu/qemu_monitor_json.c | 6 +- src/qemu/qemu_monitor_json.h | 2 + src/qemu/qemu_monitor_text.c | 8 +-- src/qemu_protocol-structs | 9 +++ src/remote/qemu_protocol.x | 20 ++++++- src/remote/remote_daemon_dispatch.c | 62 ++++++++++++++++++++ src/remote/remote_driver.c | 57 ++++++++++++++++++ src/rpc/virnetmessage.c | 9 ++- src/rpc/virnetmessage.h | 1 + tools/virsh-domain.c | 19 +++++- 19 files changed, 345 insertions(+), 17 deletions(-)
Reviewed-by: Ján Tomko <jtomko@redhat.com> Jano
participants (2)
-
Ján Tomko
-
Peter Krempa