]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
conf: Don't generate clashing machine names for embed driver
authorMichal Privoznik <mprivozn@redhat.com>
Wed, 11 Mar 2020 16:56:12 +0000 (17:56 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Wed, 18 Mar 2020 14:52:08 +0000 (15:52 +0100)
So far, when using the qemu:///embed driver, management
applications can't chose whether they want to register their
domains in machined or not. While having that option is certainly
desired, it will require more work. What we can do meanwhile is
to generate names that include part of hash of the root
directory. This is to ensure that if two applications using
different roots but the same domain name (and ID) start the
domain no clashing name for machined is generated.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Andrea Bolognani <abologna@redhat.com>
src/conf/domain_conf.c
src/conf/domain_conf.h
src/lxc/lxc_domain.c
src/qemu/qemu_domain.c
tests/virsystemdtest.c

index 9c56cdbebdf959e95a1ec910a77c0613b25abefa..e0432fc47d726a53485c295a74c7c59879158ad7 100644 (file)
@@ -62,6 +62,7 @@
 #include "virdomainsnapshotobjlist.h"
 #include "virdomaincheckpointobjlist.h"
 #include "virutil.h"
+#include "vircrypto.h"
 
 #define VIR_FROM_THIS VIR_FROM_DOMAIN
 
@@ -31076,21 +31077,33 @@ virDomainMachineNameAppendValid(virBufferPtr buf,
 
 char *
 virDomainGenerateMachineName(const char *drivername,
+                             const char *root,
                              int id,
                              const char *name,
                              bool privileged)
 {
     virBuffer buf = VIR_BUFFER_INITIALIZER;
 
-    if (privileged) {
-        virBufferAsprintf(&buf, "%s-", drivername);
-    } else {
+    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)
+            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-%s-", username, drivername);
+        virBufferAsprintf(&buf, "%s-", username);
     }
 
     virBufferAsprintf(&buf, "%d-", id);
index 91b776c28a128e92ffbe8df389f67604ef746273..73bd097cf84b97cf1a73e16508456d4f5f90c3fa 100644 (file)
@@ -3649,6 +3649,7 @@ int virDomainDiskSetBlockIOTune(virDomainDiskDefPtr disk,
 
 char *
 virDomainGenerateMachineName(const char *drivername,
+                             const char *root,
                              int id,
                              const char *name,
                              bool privileged);
index 03d0f46b249d75a9aae1c643ef6f0a4d43501f7b..ebd2c2b56eb6fb431aa018237e4427bd1b9b8bf4 100644 (file)
@@ -406,7 +406,7 @@ virLXCDomainGetMachineName(virDomainDefPtr def, pid_t pid)
     }
 
     if (!ret)
-        ret = virDomainGenerateMachineName("lxc", def->id, def->name, true);
+        ret = virDomainGenerateMachineName("lxc", NULL, def->id, def->name, true);
 
     return ret;
 }
index edc8ba2ddb7672043bbbcba62fc85936a4f1b6a4..0e2252f6cf2c4a5cef97b07c52263ac33372b4ba 100644 (file)
@@ -16424,6 +16424,7 @@ qemuDomainGetMachineName(virDomainObjPtr vm)
 {
     qemuDomainObjPrivatePtr priv = vm->privateData;
     virQEMUDriverPtr driver = priv->driver;
+    g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
     char *ret = NULL;
 
     if (vm->pid > 0) {
@@ -16433,7 +16434,8 @@ qemuDomainGetMachineName(virDomainObjPtr vm)
     }
 
     if (!ret)
-        ret = virDomainGenerateMachineName("qemu", vm->def->id, vm->def->name,
+        ret = virDomainGenerateMachineName("qemu", cfg->root,
+                                           vm->def->id, vm->def->name,
                                            virQEMUDriverIsPrivileged(driver));
 
     return ret;
index fa0980c845e84fc39d2e4cb9af8021a9da9b9bdc..050941dce88dca2d17f8997e9af3c85b773fab56 100644 (file)
@@ -379,6 +379,7 @@ testGetMachineName(const void *opaque G_GNUC_UNUSED)
 struct testNameData {
     const char *name;
     const char *expected;
+    const char *root;
     int id;
     bool legacy;
 };
@@ -413,8 +414,8 @@ testMachineName(const void *opaque)
     int ret = -1;
     char *actual = NULL;
 
-    if (!(actual = virDomainGenerateMachineName("qemu", data->id,
-                                                data->name, true)))
+    if (!(actual = virDomainGenerateMachineName("qemu", data->root,
+                                                data->id, data->name, true)))
         goto cleanup;
 
     if (STRNEQ(actual, data->expected)) {
@@ -724,30 +725,34 @@ mymain(void)
 
     TEST_SCOPE_NEW("qemu-3-demo", "machine-qemu\\x2d3\\x2ddemo.scope");
 
-# define TEST_MACHINE(_name, _id, machinename) \
+# define TEST_MACHINE(_name, _root, _id, machinename) \
     do { \
         struct testNameData data = { \
-            .name = _name, .expected = machinename, .id = _id, \
+            .name = _name, .expected = machinename, .root = _root, .id = _id, \
         }; \
         if (virTestRun("Test scopename", testMachineName, &data) < 0) \
             ret = -1; \
     } while (0)
 
-    TEST_MACHINE("demo", 1, "qemu-1-demo");
-    TEST_MACHINE("demo-name", 2, "qemu-2-demo-name");
-    TEST_MACHINE("demo!name", 3, "qemu-3-demoname");
-    TEST_MACHINE(".demo", 4, "qemu-4-demo");
-    TEST_MACHINE("bull\U0001f4a9", 5, "qemu-5-bull");
-    TEST_MACHINE("demo..name", 6, "qemu-6-demo.name");
-    TEST_MACHINE("12345678901234567890123456789012345678901234567890123456789", 7,
+    TEST_MACHINE("demo", NULL, 1, "qemu-1-demo");
+    TEST_MACHINE("demo-name", NULL, 2, "qemu-2-demo-name");
+    TEST_MACHINE("demo!name", NULL, 3, "qemu-3-demoname");
+    TEST_MACHINE(".demo", NULL, 4, "qemu-4-demo");
+    TEST_MACHINE("bull\U0001f4a9", NULL, 5, "qemu-5-bull");
+    TEST_MACHINE("demo..name", NULL, 6, "qemu-6-demo.name");
+    TEST_MACHINE("12345678901234567890123456789012345678901234567890123456789", NULL, 7,
                  "qemu-7-123456789012345678901234567890123456789012345678901234567");
-    TEST_MACHINE("123456789012345678901234567890123456789012345678901234567890", 8,
+    TEST_MACHINE("123456789012345678901234567890123456789012345678901234567890", NULL, 8,
                  "qemu-8-123456789012345678901234567890123456789012345678901234567");
-    TEST_MACHINE("kstest-network-device-default-httpks_(c9eed63e-981e-48ec-acdc-56b3f8c5f678)", 100,
+    TEST_MACHINE("kstest-network-device-default-httpks_(c9eed63e-981e-48ec-acdc-56b3f8c5f678)",
+                 NULL, 100,
                  "qemu-100-kstest-network-device-default-httpksc9eed63e-981e-48ec");
-    TEST_MACHINE("kstest-network-device-default-httpks_(c9eed63e-981e-48ec--cdc-56b3f8c5f678)", 10,
+    TEST_MACHINE("kstest-network-device-default-httpks_(c9eed63e-981e-48ec--cdc-56b3f8c5f678)",
+                 NULL, 10,
                  "qemu-10-kstest-network-device-default-httpksc9eed63e-981e-48ec-c");
-    TEST_MACHINE("demo.-.test.", 11, "qemu-11-demo.test");
+    TEST_MACHINE("demo.-.test.", NULL, 11, "qemu-11-demo.test");
+    TEST_MACHINE("demo", "/tmp/root1", 1, "qemu-embed-0991f456-1-demo");
+    TEST_MACHINE("demo", "/tmp/root2", 1, "qemu-embed-95d47ff5-1-demo");
 
 # define TESTS_PM_SUPPORT_HELPER(name, function) \
     do { \