[libvirt] [PATCH v2] systemd: Modernize machine naming

Martin Kletzander mkletzan at redhat.com
Wed Feb 3 14:27:27 UTC 2016


So, systemd-machined has this philosophy that machine names are like
hostnames and hence should follow the same rules.  But we always allowed
international characters in domain names.  Thus we need to modify the
machine name we are passing to systemd.

In order to change some machinenames that we will be passing to systemd,
we also need to call TerminateMachine at the end of a lifetime of a
domain.  Even for domains that were started with older libvirt.  That
can be achieved thanks to virSystemdGetMachineNameByPID().  And because
we can change machine names, we can get rid of the inconsistent and
pointless escaping of domain names when creating machine names.

So this patch modifies the naming in the following way.  It creates the
name as <drivername>-<id>-<name> where invalid hostname characters are
stripped out of the name and if the resulting name is longer, it
truncates it to 64 characters.  That way we can start domains we
couldn't start before.  Well, at least on systemd.

To make it work all together, the machineName (which is needed only for
systemd) is saved in domain's private data.  That way the generation is
moved to the driver and we don't need to pass various unnecessary
arguments to cgroup functions.

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1282846

Signed-off-by: Martin Kletzander <mkletzan at redhat.com>
---
 src/lxc/lxc_cgroup.c    | 13 ++++++++--
 src/lxc/lxc_domain.c    |  1 +
 src/lxc/lxc_domain.h    |  1 +
 src/lxc/lxc_process.c   | 19 +++++++++-----
 src/qemu/qemu_cgroup.c  | 27 ++++++++++++++------
 src/qemu/qemu_cgroup.h  |  2 +-
 src/qemu/qemu_domain.c  |  1 +
 src/qemu/qemu_domain.h  |  1 +
 src/qemu/qemu_process.c |  4 +--
 src/util/vircgroup.c    | 25 +++++++++---------
 src/util/vircgroup.h    | 12 ++++-----
 src/util/virsystemd.c   | 67 ++++++++++++++++++++++++++++++++++---------------
 src/util/virsystemd.h   | 11 ++++----
 tests/virsystemdtest.c  | 45 +++++++++++++++++----------------
 14 files changed, 145 insertions(+), 84 deletions(-)

diff --git a/src/lxc/lxc_cgroup.c b/src/lxc/lxc_cgroup.c
index ad254e4934fc..31489466cfbf 100644
--- a/src/lxc/lxc_cgroup.c
+++ b/src/lxc/lxc_cgroup.c
@@ -29,6 +29,7 @@
 #include "viralloc.h"
 #include "vircgroup.h"
 #include "virstring.h"
+#include "virsystemd.h"

 #define VIR_FROM_THIS VIR_FROM_LXC

@@ -483,6 +484,13 @@ virCgroupPtr virLXCCgroupCreate(virDomainDefPtr def,
                                 int *nicindexes)
 {
     virCgroupPtr cgroup = NULL;
+    char *machineName = virSystemdMakeMachineName("lxc",
+                                                  def->id,
+                                                  def->name,
+                                                  true);
+
+    if (!machineName)
+        goto cleanup;

     if (def->resource->partition[0] != '/') {
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
@@ -491,9 +499,8 @@ virCgroupPtr virLXCCgroupCreate(virDomainDefPtr def,
         goto cleanup;
     }

-    if (virCgroupNewMachine(def->name,
+    if (virCgroupNewMachine(machineName,
                             "lxc",
-                            true,
                             def->uuid,
                             NULL,
                             initpid,
@@ -517,6 +524,8 @@ virCgroupPtr virLXCCgroupCreate(virDomainDefPtr def,
     }

  cleanup:
+    VIR_FREE(machineName);
+
     return cgroup;
 }

diff --git a/src/lxc/lxc_domain.c b/src/lxc/lxc_domain.c
index c3f7a564b36b..6732e934453f 100644
--- a/src/lxc/lxc_domain.c
+++ b/src/lxc/lxc_domain.c
@@ -28,6 +28,7 @@
 #include "virerror.h"
 #include <libxml/xpathInternals.h>
 #include "virstring.h"
+#include "vircgroup.h"
 #include "virutil.h"
 #include "virfile.h"

diff --git a/src/lxc/lxc_domain.h b/src/lxc/lxc_domain.h
index 2119c7899007..39c6e7def9ce 100644
--- a/src/lxc/lxc_domain.h
+++ b/src/lxc/lxc_domain.h
@@ -64,6 +64,7 @@ struct _virLXCDomainObjPrivate {
     pid_t initpid;

     virCgroupPtr cgroup;
+    char *machineName;
 };

 extern virDomainXMLNamespace virLXCDriverDomainXMLNamespace;
diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c
index f7e2b810b74b..b14b69da4a16 100644
--- a/src/lxc/lxc_process.c
+++ b/src/lxc/lxc_process.c
@@ -233,8 +233,7 @@ static void virLXCProcessCleanup(virLXCDriverPtr driver,
      * properly. See https://bugs.freedesktop.org/show_bug.cgi?id=68370 for
      * the bug we are working around here.
      */
-    virSystemdTerminateMachine(vm->def->name, "lxc", true);
-
+    virCgroupTerminateMachine(priv->machineName);

     /* The "release" hook cleans up additional resources */
     if (virHookPresent(VIR_HOOK_DRIVER_LXC)) {
@@ -1494,8 +1493,8 @@ int virLXCProcessStart(virConnectPtr conn,
      * point so lets detect that first, since it gives us a
      * more reliable way to kill everything off if something
      * goes wrong from here onwards ... */
-    if (virCgroupNewDetectMachine(vm->def->name, "lxc", vm->pid,
-                                  -1, &priv->cgroup) < 0)
+    if (virCgroupNewDetectMachine(vm->def->name, vm->def->uuid, "lxc",
+                                  vm->pid, -1, &priv->cgroup) < 0)
         goto cleanup;

     if (!priv->cgroup) {
@@ -1505,6 +1504,11 @@ int virLXCProcessStart(virConnectPtr conn,
         goto cleanup;
     }

+    /* Get the machine name so we can properly delete it through
+     * systemd later */
+    if (!(priv->machineName = virSystemdGetMachineNameByPID(vm->pid)))
+        virResetLastError();
+
     /* And we can get the first monitor connection now too */
     if (!(priv->monitor = virLXCProcessConnectMonitor(driver, vm))) {
         /* Intentionally overwrite the real monitor error message,
@@ -1677,8 +1681,8 @@ virLXCProcessReconnectDomain(virDomainObjPtr vm,
         if (!(priv->monitor = virLXCProcessConnectMonitor(driver, vm)))
             goto error;

-        if (virCgroupNewDetectMachine(vm->def->name, "lxc", vm->pid,
-                                      -1, &priv->cgroup) < 0)
+        if (virCgroupNewDetectMachine(vm->def->name, vm->def->uuid, "lxc",
+                                      vm->pid, -1, &priv->cgroup) < 0)
             goto error;

         if (!priv->cgroup) {
@@ -1688,6 +1692,9 @@ virLXCProcessReconnectDomain(virDomainObjPtr vm,
             goto error;
         }

+        if (!(priv->machineName = virSystemdGetMachineNameByPID(vm->pid)))
+            virResetLastError();
+
         if (virLXCUpdateActiveUSBHostdevs(driver, vm->def) < 0)
             goto error;

diff --git a/src/qemu/qemu_cgroup.c b/src/qemu/qemu_cgroup.c
index e41f4617c455..705594d93dc5 100644
--- a/src/qemu/qemu_cgroup.c
+++ b/src/qemu/qemu_cgroup.c
@@ -36,6 +36,7 @@
 #include "virfile.h"
 #include "virtypedparam.h"
 #include "virnuma.h"
+#include "virsystemd.h"

 #define VIR_FROM_THIS VIR_FROM_QEMU

@@ -772,9 +773,19 @@ qemuInitCgroup(virQEMUDriverPtr driver,
         goto cleanup;
     }

-    if (virCgroupNewMachine(vm->def->name,
+    /*
+     * We need to do this because of systemd-machined, because
+     * CreateMachine requires the name to be a valid hostname.
+     */
+    priv->machineName = virSystemdMakeMachineName("qemu",
+                                                  vm->def->id,
+                                                  vm->def->name,
+                                                  virQEMUDriverIsPrivileged(driver));
+    if (!priv->machineName)
+        goto cleanup;
+
+    if (virCgroupNewMachine(priv->machineName,
                             "qemu",
-                            true,
                             vm->def->uuid,
                             NULL,
                             vm->pid,
@@ -887,12 +898,17 @@ qemuConnectCgroup(virQEMUDriverPtr driver,
     virCgroupFree(&priv->cgroup);

     if (virCgroupNewDetectMachine(vm->def->name,
+                                  vm->def->uuid,
                                   "qemu",
                                   vm->pid,
                                   cfg->cgroupControllers,
                                   &priv->cgroup) < 0)
         goto cleanup;

+    priv->machineName = virSystemdGetMachineNameByPID(vm->pid);
+    if (!priv->machineName)
+        virResetLastError();
+
     qemuRestoreCgroupState(vm);

  done:
@@ -1264,17 +1280,14 @@ qemuSetupCgroupForIOThreads(virDomainObjPtr vm)
 }

 int
-qemuRemoveCgroup(virQEMUDriverPtr driver,
-                 virDomainObjPtr vm)
+qemuRemoveCgroup(virDomainObjPtr vm)
 {
     qemuDomainObjPrivatePtr priv = vm->privateData;

     if (priv->cgroup == NULL)
         return 0; /* Not supported, so claim success */

-    if (virCgroupTerminateMachine(vm->def->name,
-                                  "qemu",
-                                  virQEMUDriverIsPrivileged(driver)) < 0) {
+    if (virCgroupTerminateMachine(priv->machineName) < 0) {
         if (!virCgroupNewIgnoreError())
             VIR_DEBUG("Failed to terminate cgroup for %s", vm->def->name);
     }
diff --git a/src/qemu/qemu_cgroup.h b/src/qemu/qemu_cgroup.h
index 2bcf071d3792..347d126f7394 100644
--- a/src/qemu/qemu_cgroup.h
+++ b/src/qemu/qemu_cgroup.h
@@ -56,7 +56,7 @@ int qemuSetupCgroupCpusetCpus(virCgroupPtr cgroup, virBitmapPtr cpumask);
 int qemuSetupCgroupForVcpu(virDomainObjPtr vm);
 int qemuSetupCgroupForIOThreads(virDomainObjPtr vm);
 int qemuSetupCgroupForEmulator(virDomainObjPtr vm);
-int qemuRemoveCgroup(virQEMUDriverPtr driver, virDomainObjPtr vm);
+int qemuRemoveCgroup(virDomainObjPtr vm);
 int qemuAddToCgroup(virDomainObjPtr vm);

 #endif /* __QEMU_CGROUP_H__ */
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index 189552045177..09ad2f75916f 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -39,6 +39,7 @@
 #include "virtime.h"
 #include "virstoragefile.h"
 #include "virstring.h"
+#include "virsystemd.h"
 #include "virthreadjob.h"
 #include "viratomic.h"
 #include "virprocess.h"
diff --git a/src/qemu/qemu_domain.h b/src/qemu/qemu_domain.h
index 6a8cf705d8af..ab798faf0fb0 100644
--- a/src/qemu/qemu_domain.h
+++ b/src/qemu/qemu_domain.h
@@ -203,6 +203,7 @@ struct _qemuDomainObjPrivate {

     bool signalIOError; /* true if the domain condition should be signalled on
                            I/O error */
+    char *machineName;
 };

 # define QEMU_DOMAIN_DISK_PRIVATE(disk)	\
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index 7b09ba73896b..b3f8c93d1a93 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -4610,7 +4610,7 @@ qemuProcessLaunch(virConnectPtr conn,
     /* Ensure no historical cgroup for this VM is lying around bogus
      * settings */
     VIR_DEBUG("Ensuring no historical cgroup is lying around");
-    qemuRemoveCgroup(driver, vm);
+    qemuRemoveCgroup(vm);

     VIR_DEBUG("Setting up ports for graphics");
     if (qemuProcessSetupGraphics(driver, vm) < 0)
@@ -5405,7 +5405,7 @@ void qemuProcessStop(virQEMUDriverPtr driver,
     }

  retry:
-    if ((ret = qemuRemoveCgroup(driver, vm)) < 0) {
+    if ((ret = qemuRemoveCgroup(vm)) < 0) {
         if (ret == -EBUSY && (retries++ < 5)) {
             usleep(200*1000);
             goto retry;
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
index da0df7ae7a5e..4aed5d20b818 100644
--- a/src/util/vircgroup.c
+++ b/src/util/vircgroup.c
@@ -43,6 +43,7 @@
 #include "vircgrouppriv.h"

 #include "virutil.h"
+#include "viruuid.h"
 #include "viralloc.h"
 #include "virerror.h"
 #include "virlog.h"
@@ -243,12 +244,14 @@ static bool
 virCgroupValidateMachineGroup(virCgroupPtr group,
                               const char *name,
                               const char *drivername,
+                              const unsigned char *uuid,
                               bool stripEmulatorSuffix)
 {
     size_t i;
     bool valid = false;
     char *partname;
     char *scopename;
+    char uuidstr[VIR_UUID_STRING_BUFLEN];

     if (virAsprintf(&partname, "%s.libvirt-%s",
                     name, drivername) < 0)
@@ -263,6 +266,8 @@ virCgroupValidateMachineGroup(virCgroupPtr group,
     if (virCgroupPartitionEscape(&scopename) < 0)
         goto cleanup;

+    virUUIDFormat(uuid, uuidstr);
+
     for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
         char *tmp;

@@ -290,6 +295,7 @@ virCgroupValidateMachineGroup(virCgroupPtr group,
         tmp++;

         if (STRNEQ(tmp, name) &&
+            STRNEQ(tmp, uuidstr) &&
             STRNEQ(tmp, partname) &&
             STRNEQ(tmp, scopename)) {
             VIR_DEBUG("Name '%s' for controller '%s' does not match "
@@ -1554,6 +1560,7 @@ virCgroupNewDetect(pid_t pid,
  */
 int
 virCgroupNewDetectMachine(const char *name,
+                          const unsigned char *uuid,
                           const char *drivername,
                           pid_t pid,
                           int controllers,
@@ -1565,7 +1572,7 @@ virCgroupNewDetectMachine(const char *name,
         return -1;
     }

-    if (!virCgroupValidateMachineGroup(*group, name, drivername, true)) {
+    if (!virCgroupValidateMachineGroup(*group, name, drivername, uuid, true)) {
         VIR_DEBUG("Failed to validate machine name for '%s' driver '%s'",
                   name, drivername);
         virCgroupFree(group);
@@ -1582,7 +1589,6 @@ virCgroupNewDetectMachine(const char *name,
 static int
 virCgroupNewMachineSystemd(const char *name,
                            const char *drivername,
-                           bool privileged,
                            const unsigned char *uuid,
                            const char *rootdir,
                            pid_t pidleader,
@@ -1602,7 +1608,6 @@ virCgroupNewMachineSystemd(const char *name,
     VIR_DEBUG("Trying to setup machine '%s' via systemd", name);
     if ((rv = virSystemdCreateMachine(name,
                                       drivername,
-                                      privileged,
                                       uuid,
                                       rootdir,
                                       pidleader,
@@ -1690,11 +1695,9 @@ virCgroupNewMachineSystemd(const char *name,
 /*
  * Returns 0 on success, -1 on fatal error
  */
-int virCgroupTerminateMachine(const char *name,
-                              const char *drivername,
-                              bool privileged)
+int virCgroupTerminateMachine(const char *name)
 {
-    return virSystemdTerminateMachine(name, drivername, privileged);
+    return virSystemdTerminateMachine(name);
 }


@@ -1749,7 +1752,6 @@ virCgroupNewMachineManual(const char *name,
 int
 virCgroupNewMachine(const char *name,
                     const char *drivername,
-                    bool privileged,
                     const unsigned char *uuid,
                     const char *rootdir,
                     pid_t pidleader,
@@ -1766,7 +1768,6 @@ virCgroupNewMachine(const char *name,

     if ((rv = virCgroupNewMachineSystemd(name,
                                          drivername,
-                                         privileged,
                                          uuid,
                                          rootdir,
                                          pidleader,
@@ -4240,6 +4241,7 @@ virCgroupNewDetect(pid_t pid ATTRIBUTE_UNUSED,

 int
 virCgroupNewDetectMachine(const char *name ATTRIBUTE_UNUSED,
+                          const unsigned char *uuid ATTRIBUTE_UNUSED,
                           const char *drivername ATTRIBUTE_UNUSED,
                           pid_t pid ATTRIBUTE_UNUSED,
                           int controllers ATTRIBUTE_UNUSED,
@@ -4251,9 +4253,7 @@ virCgroupNewDetectMachine(const char *name ATTRIBUTE_UNUSED,
 }


-int virCgroupTerminateMachine(const char *name ATTRIBUTE_UNUSED,
-                              const char *drivername ATTRIBUTE_UNUSED,
-                              bool privileged ATTRIBUTE_UNUSED)
+int virCgroupTerminateMachine(const char *name ATTRIBUTE_UNUSED)
 {
     virReportSystemError(ENXIO, "%s",
                          _("Control groups not supported on this platform"));
@@ -4264,7 +4264,6 @@ int virCgroupTerminateMachine(const char *name ATTRIBUTE_UNUSED,
 int
 virCgroupNewMachine(const char *name ATTRIBUTE_UNUSED,
                     const char *drivername ATTRIBUTE_UNUSED,
-                    bool privileged ATTRIBUTE_UNUSED,
                     const unsigned char *uuid ATTRIBUTE_UNUSED,
                     const char *rootdir ATTRIBUTE_UNUSED,
                     pid_t pidleader ATTRIBUTE_UNUSED,
diff --git a/src/util/vircgroup.h b/src/util/vircgroup.h
index d754b1f3bd7a..dd4cb719f1f1 100644
--- a/src/util/vircgroup.h
+++ b/src/util/vircgroup.h
@@ -90,14 +90,16 @@ int virCgroupNewDetect(pid_t pid,
                        virCgroupPtr *group);

 int virCgroupNewDetectMachine(const char *name,
+                              const unsigned char *uuid,
                               const char *drivername,
                               pid_t pid,
                               int controllers,
-                              virCgroupPtr *group);
+                              virCgroupPtr *group)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
+    ATTRIBUTE_NONNULL(3);

 int virCgroupNewMachine(const char *name,
                         const char *drivername,
-                        bool privileged,
                         const unsigned char *uuid,
                         const char *rootdir,
                         pid_t pidleader,
@@ -110,10 +112,8 @@ int virCgroupNewMachine(const char *name,
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
     ATTRIBUTE_NONNULL(4);

-int virCgroupTerminateMachine(const char *name,
-                              const char *drivername,
-                              bool privileged)
-    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
+int virCgroupTerminateMachine(const char *name)
+    ATTRIBUTE_NONNULL(1);

 bool virCgroupNewIgnoreError(void);

diff --git a/src/util/virsystemd.c b/src/util/virsystemd.c
index 9e85e24b2c4b..5c6cd872bee0 100644
--- a/src/util/virsystemd.c
+++ b/src/util/virsystemd.c
@@ -25,6 +25,8 @@
 # include <systemd/sd-daemon.h>
 #endif

+#include "internal.h"
+
 #include "virsystemd.h"
 #include "viratomic.h"
 #include "virdbus.h"
@@ -33,6 +35,7 @@
 #include "virutil.h"
 #include "virlog.h"
 #include "virerror.h"
+#include "viruuid.h"

 #define VIR_FROM_THIS VIR_FROM_SYSTEMD

@@ -113,10 +116,42 @@ char *virSystemdMakeSliceName(const char *partition)
     return virBufferContentAndReset(&buf);
 }

+#define HOSTNAME_CHARS                                                  \
+    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-"

-char *virSystemdMakeMachineName(const char *name,
-                                const char *drivername,
-                                bool privileged)
+static void
+virSystemdAppendValidMachineName(virBufferPtr buf,
+                                 const char *name)
+{
+    bool skip_dot = false;
+
+    for (; *name; name++) {
+        if (strlen(virBufferCurrentContent(buf)) >= 64)
+            break;
+
+        if (*name == '.') {
+            if (!skip_dot)
+                virBufferAddChar(buf, *name);
+            skip_dot = true;
+            continue;
+        }
+
+        skip_dot = false;
+
+        if (!strchr(HOSTNAME_CHARS, *name))
+            continue;
+
+        virBufferAddChar(buf, *name);
+    }
+}
+
+#undef HOSTNAME_CHARS
+
+char *
+virSystemdMakeMachineName(const char *drivername,
+                          int id,
+                          const char *name,
+                          bool privileged)
 {
     char *machinename = NULL;
     char *username = NULL;
@@ -131,7 +166,8 @@ char *virSystemdMakeMachineName(const char *name,
         virBufferAsprintf(&buf, "%s-%s-", username, drivername);
     }

-    virSystemdEscapeName(&buf, name);
+    virBufferAsprintf(&buf, "%d-", id);
+    virSystemdAppendValidMachineName(&buf, name);

     machinename = virBufferContentAndReset(&buf);
  cleanup:
@@ -212,7 +248,6 @@ virSystemdGetMachineNameByPID(pid_t pid)
  */
 int virSystemdCreateMachine(const char *name,
                             const char *drivername,
-                            bool privileged,
                             const unsigned char *uuid,
                             const char *rootdir,
                             pid_t pidleader,
@@ -223,7 +258,6 @@ int virSystemdCreateMachine(const char *name,
 {
     int ret;
     DBusConnection *conn;
-    char *machinename = NULL;
     char *creatorname = NULL;
     char *slicename = NULL;
     static int hasCreateWithNetwork = 1;
@@ -239,8 +273,6 @@ int virSystemdCreateMachine(const char *name,
         return -1;

     ret = -1;
-    if (!(machinename = virSystemdMakeMachineName(name, drivername, privileged)))
-        goto cleanup;

     if (virAsprintf(&creatorname, "libvirt-%s", drivername) < 0)
         goto cleanup;
@@ -318,7 +350,7 @@ int virSystemdCreateMachine(const char *name,
                               "org.freedesktop.machine1.Manager",
                               "CreateMachineWithNetwork",
                               "sayssusa&ia(sv)",
-                              machinename,
+                              name,
                               16,
                               uuid[0], uuid[1], uuid[2], uuid[3],
                               uuid[4], uuid[5], uuid[6], uuid[7],
@@ -360,7 +392,7 @@ int virSystemdCreateMachine(const char *name,
                               "org.freedesktop.machine1.Manager",
                               "CreateMachine",
                               "sayssusa(sv)",
-                              machinename,
+                              name,
                               16,
                               uuid[0], uuid[1], uuid[2], uuid[3],
                               uuid[4], uuid[5], uuid[6], uuid[7],
@@ -381,20 +413,19 @@ int virSystemdCreateMachine(const char *name,

  cleanup:
     VIR_FREE(creatorname);
-    VIR_FREE(machinename);
     VIR_FREE(slicename);
     return ret;
 }

-int virSystemdTerminateMachine(const char *name,
-                               const char *drivername,
-                               bool privileged)
+int virSystemdTerminateMachine(const char *name)
 {
     int ret;
     DBusConnection *conn;
-    char *machinename = NULL;
     virError error;

+    if (!name)
+        return 0;
+
     memset(&error, 0, sizeof(error));

     ret = virDBusIsServiceEnabled("org.freedesktop.machine1");
@@ -409,9 +440,6 @@ int virSystemdTerminateMachine(const char *name,
     if (!(conn = virDBusGetSystemBus()))
         goto cleanup;

-    if (!(machinename = virSystemdMakeMachineName(name, drivername, privileged)))
-        goto cleanup;
-
     /*
      * The systemd DBus API we're invoking has the
      * following signature
@@ -431,7 +459,7 @@ int virSystemdTerminateMachine(const char *name,
                           "org.freedesktop.machine1.Manager",
                           "TerminateMachine",
                           "s",
-                          machinename) < 0)
+                          name) < 0)
         goto cleanup;

     if (error.code == VIR_ERR_ERROR &&
@@ -446,7 +474,6 @@ int virSystemdTerminateMachine(const char *name,
  cleanup:
     virResetError(&error);

-    VIR_FREE(machinename);
     return ret;
 }

diff --git a/src/util/virsystemd.h b/src/util/virsystemd.h
index a13a4c0c48be..9042b5f31263 100644
--- a/src/util/virsystemd.h
+++ b/src/util/virsystemd.h
@@ -23,18 +23,19 @@
 # define __VIR_SYSTEMD_H__

 # include "internal.h"
+# include "virbuffer.h"

 char *virSystemdMakeScopeName(const char *name,
                               const char *drivername);
 char *virSystemdMakeSliceName(const char *partition);

-char *virSystemdMakeMachineName(const char *name,
-                                const char *drivername,
+char *virSystemdMakeMachineName(const char *drivername,
+                                int id,
+                                const char *name,
                                 bool privileged);

 int virSystemdCreateMachine(const char *name,
                             const char *drivername,
-                            bool privileged,
                             const unsigned char *uuid,
                             const char *rootdir,
                             pid_t pidleader,
@@ -43,9 +44,7 @@ int virSystemdCreateMachine(const char *name,
                             int *nicindexes,
                             const char *partition);

-int virSystemdTerminateMachine(const char *name,
-                               const char *drivername,
-                               bool privileged);
+int virSystemdTerminateMachine(const char *name);

 void virSystemdNotifyStartup(void);

diff --git a/tests/virsystemdtest.c b/tests/virsystemdtest.c
index 3a3cd9968032..d7fccf92bc24 100644
--- a/tests/virsystemdtest.c
+++ b/tests/virsystemdtest.c
@@ -166,7 +166,6 @@ static int testCreateContainer(const void *opaque ATTRIBUTE_UNUSED)
     };
     if (virSystemdCreateMachine("demo",
                                 "lxc",
-                                true,
                                 uuid,
                                 "/proc/123/root",
                                 123,
@@ -182,9 +181,7 @@ static int testCreateContainer(const void *opaque ATTRIBUTE_UNUSED)

 static int testTerminateContainer(const void *opaque ATTRIBUTE_UNUSED)
 {
-    if (virSystemdTerminateMachine("demo",
-                                   "lxc",
-                                   true) < 0) {
+    if (virSystemdTerminateMachine("lxc-demo") < 0) {
         fprintf(stderr, "%s", "Failed to terminate LXC machine\n");
         return -1;
     }
@@ -202,7 +199,6 @@ static int testCreateMachine(const void *opaque ATTRIBUTE_UNUSED)
     };
     if (virSystemdCreateMachine("demo",
                                 "qemu",
-                                false,
                                 uuid,
                                 NULL,
                                 123,
@@ -218,9 +214,7 @@ static int testCreateMachine(const void *opaque ATTRIBUTE_UNUSED)

 static int testTerminateMachine(const void *opaque ATTRIBUTE_UNUSED)
 {
-    if (virSystemdTerminateMachine("demo",
-                                   "qemu",
-                                   false) < 0) {
+    if (virSystemdTerminateMachine("test-qemu-demo") < 0) {
         fprintf(stderr, "%s", "Failed to terminate KVM machine\n");
         return -1;
     }
@@ -242,7 +236,6 @@ static int testCreateNoSystemd(const void *opaque ATTRIBUTE_UNUSED)

     if ((rv = virSystemdCreateMachine("demo",
                                       "qemu",
-                                      true,
                                       uuid,
                                       NULL,
                                       123,
@@ -277,7 +270,6 @@ static int testCreateSystemdNotRunning(const void *opaque ATTRIBUTE_UNUSED)

     if ((rv = virSystemdCreateMachine("demo",
                                       "qemu",
-                                      true,
                                       uuid,
                                       NULL,
                                       123,
@@ -312,7 +304,6 @@ static int testCreateBadSystemd(const void *opaque ATTRIBUTE_UNUSED)

     if ((rv = virSystemdCreateMachine("demo",
                                       "qemu",
-                                      true,
                                       uuid,
                                       NULL,
                                       123,
@@ -348,7 +339,6 @@ static int testCreateNetwork(const void *opaque ATTRIBUTE_UNUSED)
     size_t nnicindexes = ARRAY_CARDINALITY(nicindexes);
     if (virSystemdCreateMachine("demo",
                                 "lxc",
-                                true,
                                 uuid,
                                 "/proc/123/root",
                                 123,
@@ -385,6 +375,7 @@ testGetMachineName(const void *opaque ATTRIBUTE_UNUSED)
 struct testNameData {
     const char *name;
     const char *expected;
+    int id;
 };

 static int
@@ -417,7 +408,8 @@ testMachineName(const void *opaque)
     int ret = -1;
     char *actual = NULL;

-    if (!(actual = virSystemdMakeMachineName(data->name, "qemu", true)))
+    if (!(actual = virSystemdMakeMachineName("qemu", data->id,
+                                             data->name, true)))
         goto cleanup;

     if (STRNEQ(actual, data->expected)) {
@@ -518,6 +510,12 @@ mymain(void)
 {
     int ret = 0;

+    unsigned char uuid[VIR_UUID_BUFLEN];
+
+    /* The one we use in tests quite often */
+    if (virUUIDParse("c7a5fdbd-edaf-9455-926a-d65c16db1809", uuid) < 0)
+        return EXIT_FAILURE;
+
     if (virtTestRun("Test create container ", testCreateContainer, NULL) < 0)
         ret = -1;
     if (virtTestRun("Test terminate container ", testTerminateContainer, NULL) < 0)
@@ -541,7 +539,7 @@ mymain(void)
 # define TEST_SCOPE(name, unitname)                                     \
     do {                                                                \
         struct testNameData data = {                                    \
-            name, unitname                                              \
+            name, unitname, 0,                                          \
         };                                                              \
         if (virtTestRun("Test scopename", testScopeName, &data) < 0)    \
             ret = -1;                                                   \
@@ -553,20 +551,25 @@ mymain(void)
     TEST_SCOPE(".demo", "machine-lxc\\x2d\\x2edemo.scope");
     TEST_SCOPE("bull💩", "machine-lxc\\x2dbull\\xf0\\x9f\\x92\\xa9.scope");

-# define TEST_MACHINE(name, machinename)                                \
+# define TEST_MACHINE(name, id, machinename)                            \
     do {                                                                \
         struct testNameData data = {                                    \
-            name, machinename                                           \
+            name, machinename, id,                                      \
         };                                                              \
         if (virtTestRun("Test scopename", testMachineName, &data) < 0)  \
             ret = -1;                                                   \
     } while (0)

-    TEST_MACHINE("demo", "qemu-demo");
-    TEST_MACHINE("demo-name", "qemu-demo\\x2dname");
-    TEST_MACHINE("demo!name", "qemu-demo\\x21name");
-    TEST_MACHINE(".demo", "qemu-\\x2edemo");
-    TEST_MACHINE("bull\U0001f4a9", "qemu-bull\\xf0\\x9f\\x92\\xa9");
+    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,
+                 "qemu-7-123456789012345678901234567890123456789012345678901234567");
+    TEST_MACHINE("123456789012345678901234567890123456789012345678901234567890", 8,
+                 "qemu-8-123456789012345678901234567890123456789012345678901234567");

 # define TESTS_PM_SUPPORT_HELPER(name, function)                        \
     do {                                                                \
-- 
2.7.0




More information about the libvir-list mailing list