render / rpms / libvirt

Forked from rpms/libvirt 4 months ago
Clone
8c03ec
From 9cf56b5a0d1394fef10afdd763dc8005457bbaf5 Mon Sep 17 00:00:00 2001
8c03ec
Message-Id: <9cf56b5a0d1394fef10afdd763dc8005457bbaf5@dist-git>
8c03ec
From: Pavel Hrdina <phrdina@redhat.com>
8c03ec
Date: Fri, 19 Feb 2021 13:33:51 +0100
8c03ec
Subject: [PATCH] vircgroupv2: properly detect placement of running VM
8c03ec
MIME-Version: 1.0
8c03ec
Content-Type: text/plain; charset=UTF-8
8c03ec
Content-Transfer-Encoding: 8bit
8c03ec
8c03ec
When libvirtd starts a VM it internally stores a path to the main
8c03ec
cgroup. When we restart libvirtd we should get to the same state.
8c03ec
8c03ec
When we start a VM on host with systemd the cgroup is created for us and
8c03ec
the process is already placed into that cgroup and we detect the path
8c03ec
created by systemd using /proc/$PID/cgroup. After that we create
8c03ec
sub-cgroups and move all threads there.
8c03ec
8c03ec
Once libvirtd is restarted we again detect the cgroup path using
8c03ec
/proc/$PID/cgroup, but in this case we will get a different path because
8c03ec
the main thread was moved to a "emulator" cgroup.
8c03ec
8c03ec
Instead of ignoring the "emulator" directory when validating cgroups
8c03ec
remove it completely when detecting cgroup otherwise cgroups will not
8c03ec
work properly when libvirtd is restarted.
8c03ec
8c03ec
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
8c03ec
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
8c03ec
(cherry picked from commit 902c6644a8ec292789d561b3188e576c37a86872)
8c03ec
8c03ec
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1798463
8c03ec
8c03ec
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
8c03ec
Message-Id: <10fb6b61cbb4f9caf8e8ba7706ec01d1da41fc67.1613737828.git.phrdina@redhat.com>
8c03ec
Reviewed-by: Ján Tomko <jtomko@redhat.com>
8c03ec
---
8c03ec
 src/util/vircgroupv2.c | 17 ++++++++++-------
8c03ec
 1 file changed, 10 insertions(+), 7 deletions(-)
8c03ec
8c03ec
diff --git a/src/util/vircgroupv2.c b/src/util/vircgroupv2.c
8c03ec
index 92ae3ec839..4682a6a920 100644
8c03ec
--- a/src/util/vircgroupv2.c
8c03ec
+++ b/src/util/vircgroupv2.c
8c03ec
@@ -121,12 +121,6 @@ virCgroupV2ValidateMachineGroup(virCgroupPtr group,
8c03ec
     if (!(tmp = strrchr(group->unified.placement, '/')))
8c03ec
         return false;
8c03ec
 
8c03ec
-    if (STREQ(tmp, "/emulator")) {
8c03ec
-        *tmp = '\0';
8c03ec
-
8c03ec
-        if (!(tmp = strrchr(group->unified.placement, '/')))
8c03ec
-            return false;
8c03ec
-    }
8c03ec
     tmp++;
8c03ec
 
8c03ec
     if (STRNEQ(tmp, partmachinename) &&
8c03ec
@@ -197,6 +191,9 @@ virCgroupV2DetectPlacement(virCgroupPtr group,
8c03ec
                            const char *controllers,
8c03ec
                            const char *selfpath)
8c03ec
 {
8c03ec
+    g_autofree char *placement = g_strdup(selfpath);
8c03ec
+    char *tmp = NULL;
8c03ec
+
8c03ec
     if (group->unified.placement)
8c03ec
         return 0;
8c03ec
 
8c03ec
@@ -207,12 +204,18 @@ virCgroupV2DetectPlacement(virCgroupPtr group,
8c03ec
     if (STRNEQ(controllers, ""))
8c03ec
         return 0;
8c03ec
 
8c03ec
+    /* Running VM will have the main thread placed in emulator cgroup
8c03ec
+     * but we need to get the main cgroup. */
8c03ec
+    tmp = g_strrstr(placement, "/emulator");
8c03ec
+    if (tmp)
8c03ec
+        *tmp = '\0';
8c03ec
+
8c03ec
     /*
8c03ec
      * selfpath == "/" + path="" -> "/"
8c03ec
      * selfpath == "/libvirt.service" + path == "" -> "/libvirt.service"
8c03ec
      * selfpath == "/libvirt.service" + path == "foo" -> "/libvirt.service/foo"
8c03ec
      */
8c03ec
-    group->unified.placement = g_strdup_printf("%s%s%s", selfpath,
8c03ec
+    group->unified.placement = g_strdup_printf("%s%s%s", placement,
8c03ec
                                                (STREQ(selfpath, "/") || STREQ(path, "") ? "" : "/"), path);
8c03ec
 
8c03ec
     return 0;
8c03ec
-- 
8c03ec
2.30.0
8c03ec