render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
c480ed
From 4551f704f56b289735ed17f3b9ee0f9663183d09 Mon Sep 17 00:00:00 2001
c480ed
Message-Id: <4551f704f56b289735ed17f3b9ee0f9663183d09@dist-git>
c480ed
From: Pavel Hrdina <phrdina@redhat.com>
c480ed
Date: Mon, 1 Jul 2019 17:07:13 +0200
c480ed
Subject: [PATCH] vircgroup: introduce virCgroupV2Available
c480ed
MIME-Version: 1.0
c480ed
Content-Type: text/plain; charset=UTF-8
c480ed
Content-Transfer-Encoding: 8bit
c480ed
c480ed
We cannot detect only mount points to figure out whether cgroup v2
c480ed
is available because systemd uses cgroup v2 for process tracking and
c480ed
all controllers are mounted as cgroup v1 controllers.
c480ed
c480ed
To make sure that this is no the situation we need to check
c480ed
'cgroup.controllers' file if it's not empty to make sure that cgroup
c480ed
v2 is not mounted only for process tracking.
c480ed
c480ed
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c480ed
(cherry picked from commit 034ef217d73aa240340dd22590402bb9636648b5)
c480ed
c480ed
Conflicts:
c480ed
    src/util/vircgroupv2.c: missing commit c0abcca417
c480ed
c480ed
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1689297
c480ed
c480ed
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c480ed
Message-Id: <46bcb2f86e4ca5f029f67be5385770d725ef5c42.1561993100.git.phrdina@redhat.com>
c480ed
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c480ed
---
c480ed
 src/util/vircgroupv2.c | 51 ++++++++++++++++++++++++++++++++++++++++++
c480ed
 1 file changed, 51 insertions(+)
c480ed
c480ed
diff --git a/src/util/vircgroupv2.c b/src/util/vircgroupv2.c
c480ed
index 23bf81dae2..4ab9f2b792 100644
c480ed
--- a/src/util/vircgroupv2.c
c480ed
+++ b/src/util/vircgroupv2.c
c480ed
@@ -19,16 +19,23 @@
c480ed
  */
c480ed
 #include <config.h>
c480ed
 
c480ed
+#ifdef __linux__
c480ed
+# include <mntent.h>
c480ed
+#endif /* __linux__ */
c480ed
+
c480ed
 #include "internal.h"
c480ed
 
c480ed
 #define __VIR_CGROUP_ALLOW_INCLUDE_PRIV_H__
c480ed
 #include "vircgrouppriv.h"
c480ed
 #undef __VIR_CGROUP_ALLOW_INCLUDE_PRIV_H__
c480ed
 
c480ed
+#include "viralloc.h"
c480ed
 #include "vircgroup.h"
c480ed
 #include "vircgroupbackend.h"
c480ed
 #include "vircgroupv2.h"
c480ed
+#include "virfile.h"
c480ed
 #include "virlog.h"
c480ed
+#include "virstring.h"
c480ed
 
c480ed
 VIR_LOG_INIT("util.cgroup");
c480ed
 
c480ed
@@ -41,8 +48,52 @@ VIR_ENUM_IMPL(virCgroupV2Controller, VIR_CGROUP_CONTROLLER_LAST,
c480ed
 
c480ed
 #ifdef __linux__
c480ed
 
c480ed
+/* We're looking for one 'cgroup2' fs mount which has some
c480ed
+ * controllers enabled. */
c480ed
+static bool
c480ed
+virCgroupV2Available(void)
c480ed
+{
c480ed
+    bool ret = false;
c480ed
+    FILE *mounts = NULL;
c480ed
+    struct mntent entry;
c480ed
+    char buf[CGROUP_MAX_VAL];
c480ed
+
c480ed
+    if (!(mounts = fopen("/proc/mounts", "r")))
c480ed
+        return false;
c480ed
+
c480ed
+    while (getmntent_r(mounts, &entry, buf, sizeof(buf)) != NULL) {
c480ed
+        VIR_AUTOFREE(char *) contFile = NULL;
c480ed
+        VIR_AUTOFREE(char *) contStr = NULL;
c480ed
+
c480ed
+        if (STRNEQ(entry.mnt_type, "cgroup2"))
c480ed
+            continue;
c480ed
+
c480ed
+        /* Systemd uses cgroup v2 for process tracking but no controller is
c480ed
+         * available. We should consider this configuration as cgroup v2 is
c480ed
+         * not available. */
c480ed
+        if (virAsprintf(&contFile, "%s/cgroup.controllers", entry.mnt_dir) < 0)
c480ed
+            goto cleanup;
c480ed
+
c480ed
+        if (virFileReadAll(contFile, 1024 * 1024, &contStr) < 0)
c480ed
+            goto cleanup;
c480ed
+
c480ed
+        if (STREQ(contStr, ""))
c480ed
+            continue;
c480ed
+
c480ed
+        ret = true;
c480ed
+        break;
c480ed
+    }
c480ed
+
c480ed
+ cleanup:
c480ed
+    VIR_FORCE_FCLOSE(mounts);
c480ed
+    return ret;
c480ed
+}
c480ed
+
c480ed
+
c480ed
 virCgroupBackend virCgroupV2Backend = {
c480ed
     .type = VIR_CGROUP_BACKEND_TYPE_V2,
c480ed
+
c480ed
+    .available = virCgroupV2Available,
c480ed
 };
c480ed
 
c480ed
 
c480ed
-- 
c480ed
2.22.0
c480ed