From nobody Sun Feb 8 17:22:17 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) client-ip=207.211.31.81; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1585671908; cv=none; d=zohomail.com; s=zohoarc; b=U2fb40DOM3mz+kQUxoiJdSgDvjjKZyFTyzEkSnLI2l9ZCYqYbHiTLjloAUFxzvGeYe+Hc3la8y1HKAyYcXZGan5Vxytj4NEILbZT2ele45ddT874yXNxYEbME67Tzyv8MwPzlGEFuNsAvhx/157TgPb3ztEzr7Wwd65Y7yy13dQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585671908; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=5/E2+lEjzuGWX0g4fLiifEVMYGnWHMU0PGDe1DIZrdY=; b=gQEn2FLLg3MyigKy9DDkfWKTIRH1AUw0TviMj4hPiMrmCYfMzO4g0NKT431YN5kOhwe3YzNce+Mnk6nT2yOrX/O8hfllHJ24L95Av0XSSxVcX0fTMzs06m4Yh79B8Kx5BNCBJ8gTrNfs7k1TKv5Wq2F0N/rlpVwh0kQITyZOaWk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [207.211.31.81]) by mx.zohomail.com with SMTPS id 1585671908949872.4994777191605; Tue, 31 Mar 2020 09:25:08 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-105-a9egWSNtM-yrJFNGy9bv6w-1; Tue, 31 Mar 2020 12:25:05 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 477CF106BBDE; Tue, 31 Mar 2020 16:24:58 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1D34510002B5; Tue, 31 Mar 2020 16:24:58 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id BE4AF1803C41; Tue, 31 Mar 2020 16:24:57 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 02VGOeZe023066 for ; Tue, 31 Mar 2020 12:24:40 -0400 Received: by smtp.corp.redhat.com (Postfix) id 38F6F5D9E2; Tue, 31 Mar 2020 16:24:40 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.195.155]) by smtp.corp.redhat.com (Postfix) with ESMTP id A9E0C5D9C5 for ; Tue, 31 Mar 2020 16:24:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1585671907; h=from:from:sender:sender: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:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=5/E2+lEjzuGWX0g4fLiifEVMYGnWHMU0PGDe1DIZrdY=; b=BdKWuIaGFrcYkXi3coznBuPlRwwCL/0Lv89yEdGOMN4B7QtvJM/Dp01vRnXUbg2QWvAgGx nB3Kmrfxq7zgPeSNzhGlRjJ/IPzkvDUxcOXNIkFTP6L8QlgVef7ZFzi2sCc8YzSDWtMSFi iDUKwvcBfKuRB68wZ3VIXnf+W4/Txt4= X-MC-Unique: a9egWSNtM-yrJFNGy9bv6w-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v3 04/10] conf: Move virDomainGenerateMachineName to hypervisor/ Date: Tue, 31 Mar 2020 18:24:16 +0200 Message-Id: <26958b0c7e09be68f55c00156bbb4d3549f0aedb.1585671623.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" The virDomainGenerateMachineName() function doesn't belong in src/conf/ really, because it has nothing to do with domain XML parsing. It landed there because of lack of better place in the past. But now that we have src/hypervisor/ the function should live there. At the same time, the function name is changed to match new location. Signed-off-by: Michal Privoznik Reviewed-by: Andrea Bolognani Reviewed-by: Daniel Henrique Barboza Reviewed-by: Daniel P. Berrang=C3=A9 --- src/conf/domain_conf.c | 72 --------------------------------- src/conf/domain_conf.h | 7 ---- src/hypervisor/domain_driver.c | 74 ++++++++++++++++++++++++++++++++++ src/hypervisor/domain_driver.h | 7 ++++ src/libvirt_private.syms | 2 +- src/lxc/lxc_domain.c | 3 +- src/qemu/qemu_domain.c | 7 ++-- tests/virsystemdtest.c | 5 ++- 8 files changed, 91 insertions(+), 86 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 914e03c705..efb9a61243 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -62,7 +62,6 @@ #include "virdomainsnapshotobjlist.h" #include "virdomaincheckpointobjlist.h" #include "virutil.h" -#include "vircrypto.h" =20 #define VIR_FROM_THIS VIR_FROM_DOMAIN =20 @@ -31037,77 +31036,6 @@ virDomainDiskSetBlockIOTune(virDomainDiskDefPtr di= sk, return 0; } =20 -#define HOSTNAME_CHARS \ - "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-" - -static void -virDomainMachineNameAppendValid(virBufferPtr buf, - const char *name) -{ - bool skip =3D true; - - for (; *name; name++) { - if (strlen(virBufferCurrentContent(buf)) >=3D 64) - break; - - if (*name =3D=3D '.' || *name =3D=3D '-') { - if (!skip) - virBufferAddChar(buf, *name); - skip =3D true; - continue; - } - - skip =3D false; - - if (!strchr(HOSTNAME_CHARS, *name)) - continue; - - virBufferAddChar(buf, *name); - } - - /* trailing dashes or dots are not allowed */ - virBufferTrimChars(buf, "-."); -} - -#undef HOSTNAME_CHARS - -char * -virDomainGenerateMachineName(const char *drivername, - const char *root, - int id, - const char *name, - bool privileged) -{ - virBuffer buf =3D VIR_BUFFER_INITIALIZER; - - virBufferAsprintf(&buf, "%s-", drivername); - - if (root) { - g_autofree char *hash =3D NULL; - - /* When two embed drivers start two domains with the same @name an= d @id - * we would generate a non-unique name. Include parts of hashed @r= oot - * which guarantees uniqueness. The first 8 characters of SHA256 o= ught - * to be enough for anybody. */ - if (virCryptoHashString(VIR_CRYPTO_HASH_SHA256, root, &hash) < 0) - return NULL; - - virBufferAsprintf(&buf, "embed-%.8s-", hash); - } else if (!privileged) { - g_autofree char *username =3D NULL; - if (!(username =3D virGetUserName(geteuid()))) { - virBufferFreeAndReset(&buf); - return NULL; - } - virBufferAsprintf(&buf, "%s-", username); - } - - virBufferAsprintf(&buf, "%d-", id); - virDomainMachineNameAppendValid(&buf, name); - - return virBufferContentAndReset(&buf); -} - =20 /** * virDomainNetTypeSharesHostView: diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index 33875d942f..575290a6ac 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -3649,13 +3649,6 @@ virDomainGetBlkioParametersAssignFromDef(virDomainDe= fPtr def, int virDomainDiskSetBlockIOTune(virDomainDiskDefPtr disk, virDomainBlockIoTuneInfo *info); =20 -char * -virDomainGenerateMachineName(const char *drivername, - const char *root, - int id, - const char *name, - bool privileged); - bool virDomainNetTypeSharesHostView(const virDomainNetDef *net); =20 diff --git a/src/hypervisor/domain_driver.c b/src/hypervisor/domain_driver.c index fc5b6eeefe..7bf0fb3f98 100644 --- a/src/hypervisor/domain_driver.c +++ b/src/hypervisor/domain_driver.c @@ -23,10 +23,84 @@ #include "domain_driver.h" #include "viralloc.h" #include "virstring.h" +#include "vircrypto.h" +#include "virutil.h" =20 #define VIR_FROM_THIS VIR_FROM_DOMAIN =20 =20 +#define HOSTNAME_CHARS \ + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-" + +static void +virDomainMachineNameAppendValid(virBufferPtr buf, + const char *name) +{ + bool skip =3D true; + + for (; *name; name++) { + if (strlen(virBufferCurrentContent(buf)) >=3D 64) + break; + + if (*name =3D=3D '.' || *name =3D=3D '-') { + if (!skip) + virBufferAddChar(buf, *name); + skip =3D true; + continue; + } + + skip =3D false; + + if (!strchr(HOSTNAME_CHARS, *name)) + continue; + + virBufferAddChar(buf, *name); + } + + /* trailing dashes or dots are not allowed */ + virBufferTrimChars(buf, "-."); +} + +#undef HOSTNAME_CHARS + +char * +virDomainDriverGenerateMachineName(const char *drivername, + const char *root, + int id, + const char *name, + bool privileged) +{ + virBuffer buf =3D VIR_BUFFER_INITIALIZER; + + virBufferAsprintf(&buf, "%s-", drivername); + + if (root) { + g_autofree char *hash =3D NULL; + + /* When two embed drivers start two domains with the same @name an= d @id + * we would generate a non-unique name. Include parts of hashed @r= oot + * which guarantees uniqueness. The first 8 characters of SHA256 o= ught + * to be enough for anybody. */ + if (virCryptoHashString(VIR_CRYPTO_HASH_SHA256, root, &hash) < 0) + return NULL; + + virBufferAsprintf(&buf, "embed-%.8s-", hash); + } else if (!privileged) { + g_autofree char *username =3D NULL; + if (!(username =3D virGetUserName(geteuid()))) { + virBufferFreeAndReset(&buf); + return NULL; + } + virBufferAsprintf(&buf, "%s-", username); + } + + virBufferAsprintf(&buf, "%d-", id); + virDomainMachineNameAppendValid(&buf, name); + + return virBufferContentAndReset(&buf); +} + + /* Modify dest_array to reflect all blkio device changes described in * src_array. */ int diff --git a/src/hypervisor/domain_driver.h b/src/hypervisor/domain_driver.h index b6d5e66bba..c52e37f038 100644 --- a/src/hypervisor/domain_driver.h +++ b/src/hypervisor/domain_driver.h @@ -22,6 +22,13 @@ =20 #include "domain_conf.h" =20 +char * +virDomainDriverGenerateMachineName(const char *drivername, + const char *root, + int id, + const char *name, + bool privileged); + int virDomainDriverMergeBlkioDevice(virBlkioDevicePtr *dest_array, size_t *dest_size, virBlkioDevicePtr src_array, diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index e276f55bb1..b02b6380ed 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -406,7 +406,6 @@ virDomainFSTypeFromString; virDomainFSTypeToString; virDomainFSWrpolicyTypeFromString; virDomainFSWrpolicyTypeToString; -virDomainGenerateMachineName; virDomainGetBlkioParametersAssignFromDef; virDomainGetFilesystemForTarget; virDomainGraphicsAuthConnectedTypeFromString; @@ -1403,6 +1402,7 @@ virDomainCgroupSetupMemtune; =20 =20 # hypervisor/domain_driver.h +virDomainDriverGenerateMachineName; virDomainDriverMergeBlkioDevice; virDomainDriverParseBlkioDeviceStr; virDomainDriverSetupPersistentDefBlkioParams; diff --git a/src/lxc/lxc_domain.c b/src/lxc/lxc_domain.c index ebd2c2b56e..59f803837a 100644 --- a/src/lxc/lxc_domain.c +++ b/src/lxc/lxc_domain.c @@ -31,6 +31,7 @@ #include "virtime.h" #include "virsystemd.h" #include "virinitctl.h" +#include "domain_driver.h" =20 #define VIR_FROM_THIS VIR_FROM_LXC =20 @@ -406,7 +407,7 @@ virLXCDomainGetMachineName(virDomainDefPtr def, pid_t p= id) } =20 if (!ret) - ret =3D virDomainGenerateMachineName("lxc", NULL, def->id, def->na= me, true); + ret =3D virDomainDriverGenerateMachineName("lxc", NULL, def->id, d= ef->name, true); =20 return ret; } diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index e54eeae58f..25ee46fe34 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -44,6 +44,7 @@ #include "virfile.h" #include "domain_addr.h" #include "domain_capabilities.h" +#include "domain_driver.h" #include "domain_event.h" #include "virtime.h" #include "virnetdevopenvswitch.h" @@ -16544,9 +16545,9 @@ qemuDomainGetMachineName(virDomainObjPtr vm) } =20 if (!ret) - ret =3D virDomainGenerateMachineName("qemu", cfg->root, - vm->def->id, vm->def->name, - driver->privileged); + ret =3D virDomainDriverGenerateMachineName("qemu", cfg->root, + vm->def->id, vm->def->nam= e, + driver->privileged); =20 return ret; } diff --git a/tests/virsystemdtest.c b/tests/virsystemdtest.c index 050941dce8..e7dcdea8e9 100644 --- a/tests/virsystemdtest.c +++ b/tests/virsystemdtest.c @@ -34,6 +34,7 @@ # include "virlog.h" # include "virmock.h" # include "rpc/virnetsocket.h" +# include "domain_driver.h" # define VIR_FROM_THIS VIR_FROM_NONE =20 VIR_LOG_INIT("tests.systemdtest"); @@ -414,8 +415,8 @@ testMachineName(const void *opaque) int ret =3D -1; char *actual =3D NULL; =20 - if (!(actual =3D virDomainGenerateMachineName("qemu", data->root, - data->id, data->name, true= ))) + if (!(actual =3D virDomainDriverGenerateMachineName("qemu", data->root, + data->id, data->name= , true))) goto cleanup; =20 if (STRNEQ(actual, data->expected)) { --=20 2.24.1