From nobody Tue Sep 9 19:12:41 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1738073663358469.40363646532614; Tue, 28 Jan 2025 06:14:23 -0800 (PST) Received: by lists.libvirt.org (Postfix, from userid 996) id AF3CE17D1; Tue, 28 Jan 2025 09:14:22 -0500 (EST) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 6E007178B; Tue, 28 Jan 2025 09:13:56 -0500 (EST) Received: by lists.libvirt.org (Postfix, from userid 996) id 09DE2B73; Tue, 28 Jan 2025 09:13:53 -0500 (EST) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 5A747A6D for ; Tue, 28 Jan 2025 09:13:52 -0500 (EST) Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-404-u2yux_7oMYOSp7DVZQ-4hQ-1; Tue, 28 Jan 2025 09:13:50 -0500 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id EC0F71955DC9 for ; Tue, 28 Jan 2025 14:13:49 +0000 (UTC) Received: from ajulis-thinkpadt14gen4.remote.csb (unknown [10.43.2.183]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 4108C3003FD2 for ; Tue, 28 Jan 2025 14:13:49 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1738073632; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=YkO27LnjcP9uIxZTdIhY81strT5rgBZBRxRy0L06zTQ=; b=ZsQzj7lxrya/Fi4Lgz9pXY/DWBCxa0wkJxY/7O+iWe9JKYRPdFuusOSJaQN9898EZDxO8h Qo0Q/4IKGNSU72thKkqRI7SpJrZZFPSY7GZB9nDr9VFDB9VxMpaH5JNh19PSf1W63DKTY1 EkOOcdOKxMRLfNQbqf6E9qrnGhrIOaM= X-MC-Unique: u2yux_7oMYOSp7DVZQ-4hQ-1 X-Mimecast-MFC-AGG-ID: u2yux_7oMYOSp7DVZQ-4hQ From: Adam Julis To: devel@lists.libvirt.org Subject: [PATCH] qemu: introduce load qemu.conf for "virt-qemu-run" Date: Tue, 28 Jan 2025 15:13:04 +0100 Message-ID: <745e2d24f1095984ba0012176ccb0ec597757566.1738073281.git.ajulis@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: Y5Apo94xpnQEWTFJc2-xnG_3NfVQOyHvnLd3YFFGlno_1738073630 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: DGYCTERVTZIATNYG5BO2N3UDC6MJ6ZCJ X-Message-ID-Hash: DGYCTERVTZIATNYG5BO2N3UDC6MJ6ZCJ X-MailFrom: ajulis@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1738073665203019000 Content-Type: text/plain; charset="utf-8"; x-default="true" Adding a new option --config (or -c) for specifying a custom qemu.conf file. Previously, virt-qemu-run loaded default configuration values for QEMU via qemuStateInitialize(). The configuration was loaded from a temporary ../etc/ directory using virQEMUDriverConfigLoadFile(), and any qemu.conf file present in that directory was also loaded automatically. This patch allows users to specify a custom configuration file, which is copied into the temporary directory (or a permanent folder if the -r option is used) before loading the configuration. If an existing qemu.conf is present, it is properly backed up and restored in case of a permanent folder. The custom qemu.conf is always removed when the program exits. Resolves: https://gitlab.com/libvirt/libvirt/-/issues/723 Signed-off-by: Adam Julis --- docs/manpages/virt-qemu-run.rst | 9 +++ src/qemu/qemu_shim.c | 121 +++++++++++++++++++++++++++++++- 2 files changed, 129 insertions(+), 1 deletion(-) diff --git a/docs/manpages/virt-qemu-run.rst b/docs/manpages/virt-qemu-run.= rst index 4d546ff8cc..ba1c90b52a 100644 --- a/docs/manpages/virt-qemu-run.rst +++ b/docs/manpages/virt-qemu-run.rst @@ -72,6 +72,15 @@ whose UUID should match a secret referenced in the guest= domain XML. =20 Display verbose information about startup. =20 +``-c`` *QEMU-CONF-FILE*, +``--config``\ =3D\ *QEMU-CONF-FILE* + +Specify the QEMU configuration file to be used for starting the VM. +*QEMU-CONF-FILE* is the full path to the QEMU configuration file. + +If this parameter is omitted, the default configuration values will +be used. + ``-h``, ``--help`` =20 Display the command line help. diff --git a/src/qemu/qemu_shim.c b/src/qemu/qemu_shim.c index 7fdd69b538..64c87ab264 100644 --- a/src/qemu/qemu_shim.c +++ b/src/qemu/qemu_shim.c @@ -23,6 +23,7 @@ #include #include #include +#include =20 #include "virfile.h" #include "virgettext.h" @@ -132,6 +133,100 @@ qemuShimQuench(void *userData G_GNUC_UNUSED, { } =20 +/* Load specific QEMU config file, is the -c option is used */ +static int +qemuAddConfigFile(const char *source_file, const char *root, bool *config_= to_delete, long long deltams) +{ + int ret =3D -1; + struct stat st; + VIR_AUTOCLOSE srcFD =3D -1; + VIR_AUTOCLOSE dstFD =3D -1; + g_autofree char *config_dir =3D NULL; + g_autofree char *source_file_real =3D NULL; + g_autofree char *config_path_file =3D NULL; + g_autofree char *config_path_file_real =3D NULL; + + if ((srcFD =3D open(source_file, O_RDONLY)) < 0) { + g_printerr("Couldn't open specific config file\n"); + goto cleanup; + } + + if (fstat(srcFD, &st) !=3D 0) { + g_printerr("Specific config file does not exist\n"); + goto cleanup; + } + if (!S_ISREG(st.st_mode)) { + g_printerr("Specific config is not a regular file\n"); + goto cleanup; + } + + /* Since source file exists, make the destination path or + * validate that it already exists */ + config_dir =3D g_strdup_printf("%s/etc/", root); + + if (g_mkdir_with_parents(config_dir, 0777) < 0) { + g_printerr("Couldn't make the directory for specific config file\n= "); + goto cleanup; + } + + config_path_file =3D g_strdup_printf("%sqemu.conf", config_dir); + + /* If the source file is same as the destination file, no action neede= d */ + if ((source_file_real =3D realpath(source_file, NULL)) && + (config_path_file_real =3D realpath(config_path_file, NULL))) { + if (STREQ(source_file_real, config_path_file_real)) { + ret =3D 0; + goto cleanup; + } + } + + /* Check already existing qemu.conf in the subfolder, if so, renamed + * (appended via deltams constant - should be unique). Final cleanup + * at main() will revert this change */ + if (access(config_path_file, R_OK) =3D=3D 0) { + if (rename(config_path_file, g_strdup_printf("%sqemu_old_%lld.conf= ", + config_dir, deltams))= !=3D 0) { + g_printerr("Couldn't rename old config file, try delete it\n"); + goto cleanup; + } + } + + if ((dstFD =3D open(config_path_file, O_WRONLY | O_CREAT | O_TRUNC, 06= 44)) < 0) { + g_printerr("Couldn't open file for define specific config\n"); + goto cleanup; + } + + /* Set the flag for deleting the new/modified config in main() cleanup, + * for ensure that behaviour without the user config will be always + * consistent and take default value */ + *config_to_delete =3D true; + + do { + char buffer[1024]; + ssize_t readed =3D 0; + + readed =3D saferead(srcFD, buffer, 1024); + + if (readed < 0) { + g_printerr("Couldn't read from specific config\n"); + goto cleanup; + } else if (readed =3D=3D 0) { + break; + } else { + ssize_t writed =3D safewrite(dstFD, buffer, readed); + if (writed !=3D readed) { + g_printerr("Couldn't write to file for define specific con= fig\n"); + goto cleanup; + } + } + } while (1); + + ret =3D 0; + + cleanup: + return ret; +} + int main(int argc, char **argv) { g_autoptr(virIdentity) sysident =3D NULL; @@ -142,8 +237,11 @@ int main(int argc, char **argv) g_autofree char *uri =3D NULL; g_autofree char *suri =3D NULL; const char *root =3D NULL; + const char *config =3D NULL; + long long delta_ms =3D 0; g_autofree char *escaped =3D NULL; bool tmproot =3D false; + bool config_to_delete =3D false; int ret =3D 1; g_autoptr(GError) error =3D NULL; g_auto(GStrv) secrets =3D NULL; @@ -156,6 +254,7 @@ int main(int argc, char **argv) { "root", 'r', 0, G_OPTION_ARG_STRING, &root, "Root directory", "D= IR" }, { "debug", 'd', 0, G_OPTION_ARG_NONE, &debug, "Debug output", NULL= }, { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Verbose output"= , NULL }, + { "config", 'c', 0, G_OPTION_ARG_STRING, &config, "Load specific Q= EMU configuration file", "QEMU-CONF-FILE"}, { 0 } }; int quitfd[2] =3D {-1, -1}; @@ -172,7 +271,7 @@ int main(int argc, char **argv) return 1; } =20 - if (argc !=3D 2) { + if (argc !=3D 2 && argc !=3D 3) { g_autofree char *help =3D g_option_context_get_help(ctx, TRUE, NUL= L); g_printerr("%s", help); return 1; @@ -234,6 +333,10 @@ int main(int argc, char **argv) goto cleanup; } =20 + delta_ms =3D deltams(); + if (config && (qemuAddConfigFile(config, root, &config_to_delete, delt= a_ms) < 0)) + g_printerr("Specific config file was not loaded, default was used\= n"); + escaped =3D g_uri_escape_string(root, NULL, true); =20 virFileActivateDirOverrideForProg(argv[0]); @@ -402,6 +505,22 @@ int main(int argc, char **argv) VIR_FORCE_CLOSE(quitfd[0]); VIR_FORCE_CLOSE(quitfd[1]); =20 + if (config_to_delete) { + g_autofree char *possible_old_file =3D g_strdup_printf("%s/etc/qem= u_old_%lld.conf", + root, delta_m= s); + g_autofree char *to_delete =3D g_strdup_printf("%s/etc/qemu.conf",= root); + + if (remove(to_delete) !=3D 0) + g_printerr("Deleting specific config failed, located in: %s\n", + to_delete); + + if (access(possible_old_file, R_OK) =3D=3D 0) { + if (rename(possible_old_file, to_delete) !=3D 0) + g_printerr("Renaming your old qemu.conf failed, ups, locat= ed in %s\n", + possible_old_file); + } + } + if (dom !=3D NULL) virDomainFree(dom); if (sconn !=3D NULL) --=20 2.47.1