Blame SOURCES/libvirt-virt-host-validate-rewrite-cgroup-detection-to-use-util-vircgroup.patch

c313de
From 0ede215415509022d94092b75ef075495c91a5d5 Mon Sep 17 00:00:00 2001
c313de
Message-Id: <0ede215415509022d94092b75ef075495c91a5d5@dist-git>
c313de
From: Pavel Hrdina <phrdina@redhat.com>
c313de
Date: Mon, 1 Jul 2019 17:08:03 +0200
c313de
Subject: [PATCH] virt-host-validate: rewrite cgroup detection to use
c313de
 util/vircgroup
c313de
MIME-Version: 1.0
c313de
Content-Type: text/plain; charset=UTF-8
c313de
Content-Transfer-Encoding: 8bit
c313de
c313de
This removes code duplication and simplifies cgroup detection.
c313de
As a drawback we will not have separate messages to enable cgroup
c313de
controller in kernel or to mount it.  On the other side the rewrite
c313de
adds support for cgroup v2.
c313de
c313de
The kernel config support was wrong because it was parsing
c313de
'/proc/self/cgroup' instead of '/proc/cgroups/' file.
c313de
c313de
The mount suggestion is removed as well because it will not work
c313de
with cgroup v2.
c313de
c313de
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c313de
(cherry picked from commit 0f4d7daa8cd43b62911413c2cc1614f87380e459)
c313de
c313de
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1689297
c313de
c313de
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c313de
Message-Id: <0a79b9dcaee25d04b993c4e6576d75ce7c45ac0f.1561993100.git.phrdina@redhat.com>
c313de
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c313de
---
c313de
 tools/virt-host-validate-common.c | 164 ++++++------------------------
c313de
 tools/virt-host-validate-common.h |   7 +-
c313de
 tools/virt-host-validate-lxc.c    |  38 ++-----
c313de
 tools/virt-host-validate-qemu.c   |  38 ++-----
c313de
 4 files changed, 53 insertions(+), 194 deletions(-)
c313de
c313de
diff --git a/tools/virt-host-validate-common.c b/tools/virt-host-validate-common.c
c313de
index c45dfc5d8c..73165d673a 100644
c313de
--- a/tools/virt-host-validate-common.c
c313de
+++ b/tools/virt-host-validate-common.c
c313de
@@ -26,12 +26,10 @@
c313de
 #include <stdio.h>
c313de
 #include <unistd.h>
c313de
 #include <sys/utsname.h>
c313de
-#ifdef HAVE_MNTENT_H
c313de
-# include <mntent.h>
c313de
-#endif /* HAVE_MNTENT_H */
c313de
 #include <sys/stat.h>
c313de
 
c313de
 #include "viralloc.h"
c313de
+#include "vircgroup.h"
c313de
 #include "virfile.h"
c313de
 #include "virt-host-validate-common.h"
c313de
 #include "virstring.h"
c313de
@@ -290,152 +288,50 @@ int virHostValidateLinuxKernel(const char *hvname,
c313de
     }
c313de
 }
c313de
 
c313de
-
c313de
-static int virHostValidateCGroupSupport(const char *hvname,
c313de
-                                        const char *cg_name,
c313de
-                                        virHostValidateLevel level,
c313de
-                                        const char *config_name)
c313de
+#ifdef __linux__
c313de
+int virHostValidateCGroupControllers(const char *hvname,
c313de
+                                     int controllers,
c313de
+                                     virHostValidateLevel level)
c313de
 {
c313de
-    virHostMsgCheck(hvname, "for cgroup '%s' controller support", cg_name);
c313de
-    FILE *fp = fopen("/proc/self/cgroup", "r");
c313de
-    size_t len = 0;
c313de
-    char *line = NULL;
c313de
-    ssize_t ret;
c313de
-    bool matched = false;
c313de
+    virCgroupPtr group = NULL;
c313de
+    int ret = 0;
c313de
+    size_t i;
c313de
 
c313de
-    if (!fp)
c313de
-        goto error;
c313de
+    if (virCgroupNewSelf(&group) < 0)
c313de
+        return -1;
c313de
 
c313de
-    while ((ret = getline(&line, &len, fp)) >= 0 && !matched) {
c313de
-        char **cgroups;
c313de
-        char *start;
c313de
-        char *end;
c313de
-        size_t ncgroups;
c313de
-        size_t i;
c313de
+    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
c313de
+        int flag = 1 << i;
c313de
+        const char *cg_name = virCgroupControllerTypeToString(i);
c313de
 
c313de
-        /* Each line in this file looks like
c313de
-         *
c313de
-         *   4:cpu,cpuacct:/machine.slice/machine-qemu\x2dtest.scope/emulator
c313de
-         *
c313de
-         * Since multiple cgroups can be part of the same line and some cgroup
c313de
-         * names can appear as part of other cgroup names (eg. 'cpu' is a
c313de
-         * prefix for both 'cpuacct' and 'cpuset'), it's not enough to simply
c313de
-         * check whether the cgroup name is present somewhere inside the file.
c313de
-         *
c313de
-         * Moreover, there's nothing stopping the cgroup name from appearing
c313de
-         * in an unrelated mount point name as well */
c313de
-
c313de
-        /* Look for the first colon.
c313de
-         * The part we're interested in starts right after it */
c313de
-        if (!(start = strchr(line, ':')))
c313de
-            continue;
c313de
-        start++;
c313de
-
c313de
-        /* Look for the second colon.
c313de
-         * The part we're interested in ends exactly there */
c313de
-        if (!(end = strchr(start, ':')))
c313de
-            continue;
c313de
-        *end = '\0';
c313de
-
c313de
-        if (!(cgroups = virStringSplitCount(start, ",", 0, &ncgroups)))
c313de
+        if (!(controllers & flag))
c313de
             continue;
c313de
 
c313de
-        /* Look for the matching cgroup */
c313de
-        for (i = 0; i < ncgroups; i++) {
c313de
-            if (STREQ(cgroups[i], cg_name))
c313de
-                matched = true;
c313de
+        virHostMsgCheck(hvname, "for cgroup '%s' controller support", cg_name);
c313de
+
c313de
+        if (!virCgroupHasController(group, i)) {
c313de
+            ret = -1;
c313de
+            virHostMsgFail(level, "Enable '%s' in kernel Kconfig file or "
c313de
+                           "mount/enable cgroup controller in your system",
c313de
+                           cg_name);
c313de
+        } else {
c313de
+            virHostMsgPass();
c313de
         }
c313de
-
c313de
-        virStringListFreeCount(cgroups, ncgroups);
c313de
     }
c313de
 
c313de
-    VIR_FREE(line);
c313de
-    VIR_FORCE_FCLOSE(fp);
c313de
-    if (!matched)
c313de
-        goto error;
c313de
+    virCgroupFree(&group);
c313de
 
c313de
-    virHostMsgPass();
c313de
-    return 0;
c313de
-
c313de
- error:
c313de
-    VIR_FREE(line);
c313de
-    virHostMsgFail(level, "Enable CONFIG_%s in kernel Kconfig file", config_name);
c313de
-    return -1;
c313de
+    return ret;
c313de
 }
c313de
-
c313de
-#ifdef HAVE_MNTENT_H
c313de
-static int virHostValidateCGroupMount(const char *hvname,
c313de
-                                      const char *cg_name,
c313de
-                                      virHostValidateLevel level)
c313de
+#else /*  !__linux__ */
c313de
+int virHostValidateCGroupControllers(const char *hvname,
c313de
+                                     int controllers,
c313de
+                                     virHostValidateLevel level)
c313de
 {
c313de
-    virHostMsgCheck(hvname, "for cgroup '%s' controller mount-point", cg_name);
c313de
-    FILE *fp = setmntent("/proc/mounts", "r");
c313de
-    struct mntent ent;
c313de
-    char mntbuf[1024];
c313de
-    bool matched = false;
c313de
-
c313de
-    if (!fp)
c313de
-        goto error;
c313de
-
c313de
-    while (getmntent_r(fp, &ent, mntbuf, sizeof(mntbuf)) && !matched) {
c313de
-        char **opts;
c313de
-        size_t nopts;
c313de
-        size_t i;
c313de
-
c313de
-        /* Ignore non-cgroup mounts */
c313de
-        if (STRNEQ(ent.mnt_type, "cgroup"))
c313de
-            continue;
c313de
-
c313de
-        if (!(opts = virStringSplitCount(ent.mnt_opts, ",", 0, &nopts)))
c313de
-            continue;
c313de
-
c313de
-        /* Look for a mount option matching the cgroup name */
c313de
-        for (i = 0; i < nopts; i++) {
c313de
-            if (STREQ(opts[i], cg_name))
c313de
-                matched = true;
c313de
-        }
c313de
-
c313de
-        virStringListFreeCount(opts, nopts);
c313de
-    }
c313de
-    endmntent(fp);
c313de
-    if (!matched)
c313de
-        goto error;
c313de
-
c313de
-    virHostMsgPass();
c313de
-    return 0;
c313de
-
c313de
- error:
c313de
-    virHostMsgFail(level, "Mount '%s' cgroup controller (suggested at /sys/fs/cgroup/%s)",
c313de
-                   cg_name, cg_name);
c313de
-    return -1;
c313de
-}
c313de
-#else /* ! HAVE_MNTENT_H */
c313de
-static int virHostValidateCGroupMount(const char *hvname,
c313de
-                                      const char *cg_name,
c313de
-                                      virHostValidateLevel level)
c313de
-{
c313de
-    virHostMsgCheck(hvname, "for cgroup '%s' controller mount-point", cg_name);
c313de
     virHostMsgFail(level, "%s", "This platform does not support cgroups");
c313de
     return -1;
c313de
 }
c313de
-#endif /* ! HAVE_MNTENT_H */
c313de
-
c313de
-int virHostValidateCGroupController(const char *hvname,
c313de
-                                    const char *cg_name,
c313de
-                                    virHostValidateLevel level,
c313de
-                                    const char *config_name)
c313de
-{
c313de
-    if (virHostValidateCGroupSupport(hvname,
c313de
-                                     cg_name,
c313de
-                                     level,
c313de
-                                     config_name) < 0)
c313de
-        return -1;
c313de
-    if (virHostValidateCGroupMount(hvname,
c313de
-                                   cg_name,
c313de
-                                   level) < 0)
c313de
-        return -1;
c313de
-    return 0;
c313de
-}
c313de
+#endif /* !__linux__ */
c313de
 
c313de
 int virHostValidateIOMMU(const char *hvname,
c313de
                          virHostValidateLevel level)
c313de
diff --git a/tools/virt-host-validate-common.h b/tools/virt-host-validate-common.h
c313de
index b6fe17daa7..b23dd7cdbe 100644
c313de
--- a/tools/virt-host-validate-common.h
c313de
+++ b/tools/virt-host-validate-common.h
c313de
@@ -77,10 +77,9 @@ int virHostValidateNamespace(const char *hvname,
c313de
                              virHostValidateLevel level,
c313de
                              const char *hint);
c313de
 
c313de
-int virHostValidateCGroupController(const char *hvname,
c313de
-                                    const char *cg_name,
c313de
-                                    virHostValidateLevel level,
c313de
-                                    const char *config_name);
c313de
+int virHostValidateCGroupControllers(const char *hvname,
c313de
+                                     int controllers,
c313de
+                                     virHostValidateLevel level);
c313de
 
c313de
 int virHostValidateIOMMU(const char *hvname,
c313de
                          virHostValidateLevel level);
c313de
diff --git a/tools/virt-host-validate-lxc.c b/tools/virt-host-validate-lxc.c
c313de
index 64d9279c30..3c55b1b26d 100644
c313de
--- a/tools/virt-host-validate-lxc.c
c313de
+++ b/tools/virt-host-validate-lxc.c
c313de
@@ -23,6 +23,7 @@
c313de
 
c313de
 #include "virt-host-validate-lxc.h"
c313de
 #include "virt-host-validate-common.h"
c313de
+#include "vircgroup.h"
c313de
 
c313de
 int virHostValidateLXC(void)
c313de
 {
c313de
@@ -63,35 +64,16 @@ int virHostValidateLXC(void)
c313de
                                  _("User namespace support is recommended")) < 0)
c313de
         ret = -1;
c313de
 
c313de
-    if (virHostValidateCGroupController("LXC", "memory",
c313de
-                                        VIR_HOST_VALIDATE_FAIL,
c313de
-                                        "MEMCG") < 0)
c313de
-        ret = -1;
c313de
-
c313de
-    if (virHostValidateCGroupController("LXC", "cpu",
c313de
-                                        VIR_HOST_VALIDATE_FAIL,
c313de
-                                        "CGROUP_CPU") < 0)
c313de
-        ret = -1;
c313de
-
c313de
-    if (virHostValidateCGroupController("LXC", "cpuacct",
c313de
-                                        VIR_HOST_VALIDATE_FAIL,
c313de
-                                        "CGROUP_CPUACCT") < 0)
c313de
-        ret = -1;
c313de
-
c313de
-    if (virHostValidateCGroupController("LXC", "cpuset",
c313de
-                                        VIR_HOST_VALIDATE_FAIL,
c313de
-                                        "CPUSETS") < 0)
c313de
-        ret = -1;
c313de
-
c313de
-    if (virHostValidateCGroupController("LXC", "devices",
c313de
-                                        VIR_HOST_VALIDATE_FAIL,
c313de
-                                        "CGROUP_DEVICE") < 0)
c313de
-        ret = -1;
c313de
-
c313de
-    if (virHostValidateCGroupController("LXC", "blkio",
c313de
-                                        VIR_HOST_VALIDATE_FAIL,
c313de
-                                        "BLK_CGROUP") < 0)
c313de
+    if (virHostValidateCGroupControllers("LXC",
c313de
+                                         (1 << VIR_CGROUP_CONTROLLER_MEMORY) |
c313de
+                                         (1 << VIR_CGROUP_CONTROLLER_CPU) |
c313de
+                                         (1 << VIR_CGROUP_CONTROLLER_CPUACCT) |
c313de
+                                         (1 << VIR_CGROUP_CONTROLLER_CPUSET) |
c313de
+                                         (1 << VIR_CGROUP_CONTROLLER_DEVICES) |
c313de
+                                         (1 << VIR_CGROUP_CONTROLLER_BLKIO),
c313de
+                                         VIR_HOST_VALIDATE_FAIL) < 0) {
c313de
         ret = -1;
c313de
+    }
c313de
 
c313de
 #if WITH_FUSE
c313de
     if (virHostValidateDeviceExists("LXC", "/sys/fs/fuse/connections",
c313de
diff --git a/tools/virt-host-validate-qemu.c b/tools/virt-host-validate-qemu.c
c313de
index d7573ea8b3..ff3c1f0231 100644
c313de
--- a/tools/virt-host-validate-qemu.c
c313de
+++ b/tools/virt-host-validate-qemu.c
c313de
@@ -26,6 +26,7 @@
c313de
 #include "virt-host-validate-common.h"
c313de
 #include "virarch.h"
c313de
 #include "virbitmap.h"
c313de
+#include "vircgroup.h"
c313de
 
c313de
 int virHostValidateQEMU(void)
c313de
 {
c313de
@@ -96,35 +97,16 @@ int virHostValidateQEMU(void)
c313de
                                     _("Load the 'tun' module to enable networking for QEMU guests")) < 0)
c313de
         ret = -1;
c313de
 
c313de
-    if (virHostValidateCGroupController("QEMU", "memory",
c313de
-                                        VIR_HOST_VALIDATE_WARN,
c313de
-                                        "MEMCG") < 0)
c313de
-        ret = -1;
c313de
-
c313de
-    if (virHostValidateCGroupController("QEMU", "cpu",
c313de
-                                        VIR_HOST_VALIDATE_WARN,
c313de
-                                        "CGROUP_CPU") < 0)
c313de
-        ret = -1;
c313de
-
c313de
-    if (virHostValidateCGroupController("QEMU", "cpuacct",
c313de
-                                        VIR_HOST_VALIDATE_WARN,
c313de
-                                        "CGROUP_CPUACCT") < 0)
c313de
-        ret = -1;
c313de
-
c313de
-    if (virHostValidateCGroupController("QEMU", "cpuset",
c313de
-                                        VIR_HOST_VALIDATE_WARN,
c313de
-                                        "CPUSETS") < 0)
c313de
-        ret = -1;
c313de
-
c313de
-    if (virHostValidateCGroupController("QEMU", "devices",
c313de
-                                        VIR_HOST_VALIDATE_WARN,
c313de
-                                        "CGROUP_DEVICES") < 0)
c313de
-        ret = -1;
c313de
-
c313de
-    if (virHostValidateCGroupController("QEMU", "blkio",
c313de
-                                        VIR_HOST_VALIDATE_WARN,
c313de
-                                        "BLK_CGROUP") < 0)
c313de
+    if (virHostValidateCGroupControllers("QEMU",
c313de
+                                         (1 << VIR_CGROUP_CONTROLLER_MEMORY) |
c313de
+                                         (1 << VIR_CGROUP_CONTROLLER_CPU) |
c313de
+                                         (1 << VIR_CGROUP_CONTROLLER_CPUACCT) |
c313de
+                                         (1 << VIR_CGROUP_CONTROLLER_CPUSET) |
c313de
+                                         (1 << VIR_CGROUP_CONTROLLER_DEVICES) |
c313de
+                                         (1 << VIR_CGROUP_CONTROLLER_BLKIO),
c313de
+                                         VIR_HOST_VALIDATE_WARN) < 0) {
c313de
         ret = -1;
c313de
+    }
c313de
 
c313de
     if (virHostValidateIOMMU("QEMU",
c313de
                              VIR_HOST_VALIDATE_WARN) < 0)
c313de
-- 
c313de
2.22.0
c313de