From nobody Fri Oct 18 06:19:43 2024 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 172068441914896.54371472655237; Thu, 11 Jul 2024 00:53:39 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 15194A92; Thu, 11 Jul 2024 03:53:37 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id CA617E32; Thu, 11 Jul 2024 03:53:17 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id C095DD56; Thu, 11 Jul 2024 03:53:13 -0400 (EDT) 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 AAFD412DF for ; Thu, 11 Jul 2024 03:53:11 -0400 (EDT) Received: from mx-prod-mc-04.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-589-x-hSc_5oNYWZcP-QPVJd7w-1; Thu, 11 Jul 2024 03:53:05 -0400 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-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 8667A1958B15 for ; Thu, 11 Jul 2024 07:53:04 +0000 (UTC) Received: from maggie.brq.redhat.com (unknown [10.43.3.102]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id C3ED53000182 for ; Thu, 11 Jul 2024 07:53:03 +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_H4,RCVD_IN_MSPIKE_WL,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=1720684391; 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=FwqC64nKfaJmYj2tgtiNUXo+WTCjzat7P/dVLQ7o/SI=; b=GNikazM8+ZOdssVzq9he+i32zP3Ezm9/xln8VpaM8yfXY/xP3wVvJD7+CIEWgJJ3BQXZGg mFyd1NiTgNVC0jYObBNOnVzsJDkIfoBIja0Lao/2caLl8PavGDqZ/kX1+cavZsq747DLcR mfucoxTWY7mhu+RflHcAYOrLvSqOUes= X-MC-Unique: x-hSc_5oNYWZcP-QPVJd7w-1 From: Michal Privoznik To: devel@lists.libvirt.org Subject: [PATCH] virt-host-validate: Rework calling of driver validation Date: Thu, 11 Jul 2024 09:53:01 +0200 Message-ID: <441da73ed75958294f226883f384ef239499db5b.1720684381.git.mprivozn@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-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: LJOZG5GEAHZ6S5JXZCJXOKIKYDKYAOTM X-Message-ID-Hash: LJOZG5GEAHZ6S5JXZCJXOKIKYDKYAOTM X-MailFrom: mprivozn@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: 1720684421238116600 Content-Type: text/plain; charset="utf-8"; x-default="true" It all started with me looking at the --help output which also printed "bhyve" as supported hypervisor type. Well, it's not on my Linux machine. To resolve this, I'm just creating a static array of { "$driver", callback() } pairs and iterating over it. The array is then initialized at compile time with supported drivers. Signed-off-by: Michal Privoznik --- tools/virt-host-validate.c | 80 +++++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 35 deletions(-) diff --git a/tools/virt-host-validate.c b/tools/virt-host-validate.c index 426648a5d3..365b8acd92 100644 --- a/tools/virt-host-validate.c +++ b/tools/virt-host-validate.c @@ -29,6 +29,7 @@ #include "internal.h" #include "virerror.h" #include "virgettext.h" +#include "virglibutil.h" =20 #include "virt-host-validate-common.h" #if WITH_QEMU @@ -44,26 +45,58 @@ # include "virt-host-validate-ch.h" #endif =20 +typedef struct _virValidateCallbacks virValidateCallbacks; +struct _virValidateCallbacks { + const char *name; + int (*callback)(void); +}; + +static virValidateCallbacks validateCallbacks[] =3D { +#if WITH_QEMU + { "qemu", virHostValidateQEMU }, +#endif +#if WITH_LXC + { "lxc", virHostValidateLXC }, +#endif +#if WITH_BHYVE + { "bhyve", virHostValidateBhyve }, +#endif +#if WITH_CH + { "ch", virHostValidateCh }, +#endif +}; + static void show_help(FILE *out, const char *argv0) { + g_autofree char *hvs =3D NULL; + char *hvs_list[G_N_ELEMENTS(validateCallbacks) + 1] =3D { }; + size_t i; + + for (i =3D 0; i < G_N_ELEMENTS(validateCallbacks); i++) { + hvs_list[i] =3D g_strdup_printf(" - %1$s", validateCallbacks[i].= name); + } + + hvs =3D g_strjoinv("\n", hvs_list); + + for (i =3D 0; i < G_N_ELEMENTS(validateCallbacks); i++) { + g_free(hvs_list[i]); + } + fprintf(out, _("\n" "syntax: %1$s [OPTIONS] [HVTYPE]\n" "\n" " Hypervisor types:\n" "\n" - " - qemu\n" - " - lxc\n" - " - bhyve\n" - " - ch\n" + "%2$s\n" "\n" " Options:\n" " -h, --help Display command line help\n" " -v, --version Display command version\n" " -q, --quiet Don't display progress information\n" "\n"), - argv0); + argv0, hvs); } =20 static void @@ -87,6 +120,7 @@ main(int argc, char **argv) int ret =3D EXIT_SUCCESS; bool quiet =3D false; bool usedHvname =3D false; + size_t i; =20 if (virGettextInitialize() < 0 || virErrorInitialize() < 0) { @@ -126,37 +160,13 @@ main(int argc, char **argv) =20 virValidateSetQuiet(quiet); =20 -#if WITH_QEMU - if (!hvname || STREQ(hvname, "qemu")) { - usedHvname =3D true; - if (virHostValidateQEMU() < 0) - ret =3D EXIT_FAILURE; + for (i =3D 0; i < G_N_ELEMENTS(validateCallbacks); i++) { + if (!hvname || STREQ(hvname, validateCallbacks[i].name)) { + usedHvname =3D true; + if (validateCallbacks[i].callback() < 0) + ret =3D EXIT_FAILURE; + } } -#endif - -#if WITH_LXC - if (!hvname || STREQ(hvname, "lxc")) { - usedHvname =3D true; - if (virHostValidateLXC() < 0) - ret =3D EXIT_FAILURE; - } -#endif - -#if WITH_BHYVE - if (!hvname || STREQ(hvname, "bhyve")) { - usedHvname =3D true; - if (virHostValidateBhyve() < 0) - ret =3D EXIT_FAILURE; - } -#endif - -#if WITH_CH - if (!hvname || STREQ(hvname, "ch")) { - usedHvname =3D true; - if (virHostValidateCh() < 0) - ret =3D EXIT_FAILURE; - } -#endif =20 if (hvname && !usedHvname) { fprintf(stderr, _("%1$s: unsupported hypervisor name %2$s\n"), --=20 2.44.2