[PATCH v2 04/11] virDomainDriverGenerateMachineName: Factor out embed path hashing

Michal Privoznik mprivozn at redhat.com
Thu Mar 26 15:15:08 UTC 2020


The code that generates "qemu-embed-$hash" is going to be useful
in more places. Separate it out into a function.

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
Reviewed-by: Andrea Bolognani <abologna at redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413 at gmail.com>
---
 src/hypervisor/domain_driver.c | 42 ++++++++++++++++++++++------------
 src/hypervisor/domain_driver.h |  4 ++++
 src/libvirt_private.syms       |  1 +
 3 files changed, 33 insertions(+), 14 deletions(-)

diff --git a/src/hypervisor/domain_driver.c b/src/hypervisor/domain_driver.c
index 7bf0fb3f98..31821fc712 100644
--- a/src/hypervisor/domain_driver.c
+++ b/src/hypervisor/domain_driver.c
@@ -28,6 +28,22 @@
 
 #define VIR_FROM_THIS VIR_FROM_DOMAIN
 
+char *
+virDomainDriverGenerateRootHash(const char *drivername,
+                                const char *root)
+{
+    g_autofree char *hash = NULL;
+
+    if (virCryptoHashString(VIR_CRYPTO_HASH_SHA256, root, &hash) < 0)
+        return NULL;
+
+    /* When two embed drivers start two domains with the same @name and @id
+     * we would generate a non-unique name. Include parts of hashed @root
+     * which guarantees uniqueness. The first 8 characters of SHA256 ought
+     * to be enough for anybody. */
+    return g_strdup_printf("%s-embed-%.8s", drivername, hash);
+}
+
 
 #define HOSTNAME_CHARS \
     "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-"
@@ -72,26 +88,24 @@ virDomainDriverGenerateMachineName(const char *drivername,
 {
     virBuffer buf = VIR_BUFFER_INITIALIZER;
 
-    virBufferAsprintf(&buf, "%s-", drivername);
-
     if (root) {
         g_autofree char *hash = NULL;
 
-        /* When two embed drivers start two domains with the same @name and @id
-         * we would generate a non-unique name. Include parts of hashed @root
-         * which guarantees uniqueness. The first 8 characters of SHA256 ought
-         * to be enough for anybody. */
-        if (virCryptoHashString(VIR_CRYPTO_HASH_SHA256, root, &hash) < 0)
+        if (!(hash = virDomainDriverGenerateRootHash(drivername, root)))
             return NULL;
 
-        virBufferAsprintf(&buf, "embed-%.8s-", hash);
-    } else if (!privileged) {
-        g_autofree char *username = NULL;
-        if (!(username = virGetUserName(geteuid()))) {
-            virBufferFreeAndReset(&buf);
-            return NULL;
+        virBufferAsprintf(&buf, "%s-", hash);
+    } else {
+        virBufferAsprintf(&buf, "%s-", drivername);
+        if (!privileged) {
+
+            g_autofree char *username = NULL;
+            if (!(username = virGetUserName(geteuid()))) {
+                virBufferFreeAndReset(&buf);
+                return NULL;
+            }
+            virBufferAsprintf(&buf, "%s-", username);
         }
-        virBufferAsprintf(&buf, "%s-", username);
     }
 
     virBufferAsprintf(&buf, "%d-", id);
diff --git a/src/hypervisor/domain_driver.h b/src/hypervisor/domain_driver.h
index c52e37f038..b66ae2d421 100644
--- a/src/hypervisor/domain_driver.h
+++ b/src/hypervisor/domain_driver.h
@@ -22,6 +22,10 @@
 
 #include "domain_conf.h"
 
+char *
+virDomainDriverGenerateRootHash(const char *drivername,
+                                const char *root);
+
 char *
 virDomainDriverGenerateMachineName(const char *drivername,
                                    const char *root,
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 69f278f6fb..58a895a593 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1403,6 +1403,7 @@ virDomainCgroupSetupMemtune;
 
 # hypervisor/domain_driver.h
 virDomainDriverGenerateMachineName;
+virDomainDriverGenerateRootHash;
 virDomainDriverMergeBlkioDevice;
 virDomainDriverParseBlkioDeviceStr;
 virDomainDriverSetupPersistentDefBlkioParams;
-- 
2.24.1




More information about the libvir-list mailing list