From nobody Tue Sep 9 19:00:37 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 1738329107154434.5865248391053; Fri, 31 Jan 2025 05:11:47 -0800 (PST) Received: by lists.libvirt.org (Postfix, from userid 996) id C884D1BD9; Fri, 31 Jan 2025 08:11:46 -0500 (EST) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 301C31BE5; Fri, 31 Jan 2025 08:11:17 -0500 (EST) Received: by lists.libvirt.org (Postfix, from userid 996) id 67F5D19D5; Fri, 31 Jan 2025 08:11:14 -0500 (EST) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 075AD1ABC for ; Fri, 31 Jan 2025 08:11:12 -0500 (EST) Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-689-MHa5yIELMRe3L0ctKBVpyw-1; Fri, 31 Jan 2025 08:11:10 -0500 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (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-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 544E91801F17 for ; Fri, 31 Jan 2025 13:11:09 +0000 (UTC) Received: from ajulis-thinkpadt14gen4.remote.csb (unknown [10.43.2.183]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 9F85D18008F0 for ; Fri, 31 Jan 2025 13:11:08 +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_H5,RCVD_IN_MSPIKE_WL,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_HELO_NONE 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=1738329071; 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: in-reply-to:in-reply-to:references:references; bh=AgzWHBXPB0JR13gV/6nl5vhElfgLgCwjPGPjldYa12U=; b=gBhZb5g1dxixOrrrDMVMGa1z8UKeAOKClkHSfS9q77T8Is+pM99mBlPdm2gQA3koJTHKl2 uB6S2uV1GQRMlNBs7h8JshtUA8gDgc/MdnghUTyhCevzuIypKVs8l1/vUPoK8Rbnkmn7oU +O5qDYLgB0uMdqOVeA9QjuhBsx6dtn0= X-MC-Unique: MHa5yIELMRe3L0ctKBVpyw-1 X-Mimecast-MFC-AGG-ID: MHa5yIELMRe3L0ctKBVpyw From: Adam Julis To: devel@lists.libvirt.org Subject: [PATCH v2] qemu: introduce load qemu.conf for "virt-qemu-run" Date: Fri, 31 Jan 2025 14:11:05 +0100 Message-ID: <1b2ae5ef89640ab51ffd263cc0665148cdbf168e.1738328895.git.ajulis@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 7iWQ88m7h8ahNXi1Vn3GiINFgQv8Ep5lsoRHvRk9WnA_1738329069 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: 24HOB66SAX5OA5RYRAMEOKQKLHULSKDC X-Message-ID-Hash: 24HOB66SAX5OA5RYRAMEOKQKLHULSKDC 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: 1738329112059019000 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 --- Changes to v1: - formatting - g_strdup - deleted redundant variables, cleanup - naming - error in loading function caused interrupting of program docs/manpages/virt-qemu-run.rst | 9 +++ src/qemu/qemu_shim.c | 120 +++++++++++++++++++++++++++++++- 2 files changed, 128 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..9672b85183 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,101 @@ qemuShimQuench(void *userData G_GNUC_UNUSED, { } =20 +/* Load specific QEMU config file for the -c option */ +static int +qemuAddConfigFile(const char *source_file, + const char *root, + char **old_config_file, + char **saved_config_file, + long long deltams) +{ + struct stat st; + VIR_AUTOCLOSE srcFD =3D -1; + VIR_AUTOCLOSE dstFD =3D -1; + g_autofree char *config_dir =3D NULL; + g_autofree char *config_dir_file =3D NULL; + g_autofree char *config_dir_file_real =3D NULL; + g_autofree char *source_file_real =3D NULL; + + if (source_file =3D=3D NULL) + return 0; + + if ((srcFD =3D open(source_file, O_RDONLY)) < 0) { + g_printerr("Couldn't open specific config file\n"); + return -1; + } + + if (fstat(srcFD, &st) !=3D 0) { + g_printerr("Specific config file does not exist\n"); + return -1; + } + if (!S_ISREG(st.st_mode)) { + g_printerr("Specific config is not a regular file\n"); + return -1; + } + + /* 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= "); + return -1; + } + + config_dir_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_dir_file_real =3D realpath(config_dir_file, NULL))) { + if (STREQ(source_file_real, config_dir_file_real)) { + return 0; + } + } + + /* 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_dir_file, R_OK) =3D=3D 0) { + *old_config_file =3D g_strdup_printf("%sqemu_old_%lld.conf", + config_dir, deltams); + + if (rename(config_dir_file, *old_config_file) !=3D 0) { + g_printerr("Couldn't rename old config file, try delete it\n"); + return -1; + } + } + + if ((dstFD =3D open(config_dir_file, O_WRONLY | O_CREAT | O_TRUNC, 064= 4)) < 0) { + g_printerr("Couldn't open file for define specific config\n"); + return -1; + } + + *saved_config_file =3D g_steal_pointer(&config_dir_file); + + do { + char buffer[1024]; + ssize_t nread =3D 0; + + nread =3D saferead(srcFD, buffer, 1024); + + if (nread < 0) { + g_printerr("Couldn't read from specific config\n"); + return -1; + } else if (nread =3D=3D 0) { + break; + } else { + ssize_t nwrite =3D safewrite(dstFD, buffer, nread); + if (nwrite !=3D nread) { + g_printerr("Couldn't write to file for define specific con= fig\n"); + return -1; + } + } + } while (1); + + return 0; +} + int main(int argc, char **argv) { g_autoptr(virIdentity) sysident =3D NULL; @@ -141,7 +237,10 @@ int main(int argc, char **argv) g_autofree char *xml =3D NULL; g_autofree char *uri =3D NULL; g_autofree char *suri =3D NULL; + g_autofree char *old_config_file =3D NULL; + g_autofree char *saved_config_file =3D NULL; const char *root =3D NULL; + const char *config =3D NULL; g_autofree char *escaped =3D NULL; bool tmproot =3D false; int ret =3D 1; @@ -156,6 +255,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 +272,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 +334,12 @@ int main(int argc, char **argv) goto cleanup; } =20 + if (config && qemuAddConfigFile(config, root, &old_config_file, + &saved_config_file, deltams()) < 0) { + g_printerr("Specific config file was not loaded, process interupte= d\n"); + goto cleanup; + } + escaped =3D g_uri_escape_string(root, NULL, true); =20 virFileActivateDirOverrideForProg(argv[0]); @@ -402,6 +508,18 @@ int main(int argc, char **argv) VIR_FORCE_CLOSE(quitfd[0]); VIR_FORCE_CLOSE(quitfd[1]); =20 + if (saved_config_file) { + if (remove(saved_config_file) !=3D 0) + g_printerr("Deleting specific config failed, located in: %s\n", + saved_config_file); + + if (old_config_file && access(old_config_file, R_OK) =3D=3D 0) { + if (rename(old_config_file, saved_config_file) !=3D 0) + g_printerr("Renaming your old qemu.conf failed, ups, locat= ed in %s\n", + old_config_file); + } + } + if (dom !=3D NULL) virDomainFree(dom); if (sconn !=3D NULL) --=20 2.47.1