]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virDomainDriverGenerateMachineName: Factor out embed path hashing
authorMichal Privoznik <mprivozn@redhat.com>
Fri, 20 Mar 2020 18:52:32 +0000 (19:52 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Tue, 7 Apr 2020 13:26:22 +0000 (15:26 +0200)
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@redhat.com>
Reviewed-by: Andrea Bolognani <abologna@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
src/hypervisor/domain_driver.c
src/hypervisor/domain_driver.h
src/libvirt_private.syms

index 7bf0fb3f9887831e80d63d492df08f4102531dc6..31821fc7120bf9939f3ad5dd6b33576d4f81e04b 100644 (file)
 
 #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);
index c52e37f0386378cfe6e95c9464a0fe069382081c..b66ae2d4219ef1a961f916e794eca53af0b17dbf 100644 (file)
 
 #include "domain_conf.h"
 
+char *
+virDomainDriverGenerateRootHash(const char *drivername,
+                                const char *root);
+
 char *
 virDomainDriverGenerateMachineName(const char *drivername,
                                    const char *root,
index b02b6380ed140ece9292ac1cd7223657f1a3f2fc..ec367653d5a824691d210da9b880c8680134a8a5 100644 (file)
@@ -1403,6 +1403,7 @@ virDomainCgroupSetupMemtune;
 
 # hypervisor/domain_driver.h
 virDomainDriverGenerateMachineName;
+virDomainDriverGenerateRootHash;
 virDomainDriverMergeBlkioDevice;
 virDomainDriverParseBlkioDeviceStr;
 virDomainDriverSetupPersistentDefBlkioParams;