render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
Pablo Greco 40546a
From 7ee971869f488f806a728eead8be82c287a35fc4 Mon Sep 17 00:00:00 2001
Pablo Greco 40546a
Message-Id: <7ee971869f488f806a728eead8be82c287a35fc4@dist-git>
Pablo Greco 40546a
From: Pavel Hrdina <phrdina@redhat.com>
Pablo Greco 40546a
Date: Mon, 1 Jul 2019 17:06:39 +0200
Pablo Greco 40546a
Subject: [PATCH] vircgroup: extract virCgroupV1BindMount
Pablo Greco 40546a
MIME-Version: 1.0
Pablo Greco 40546a
Content-Type: text/plain; charset=UTF-8
Pablo Greco 40546a
Content-Transfer-Encoding: 8bit
Pablo Greco 40546a
Pablo Greco 40546a
Reviewed-by: Fabiano Fidêncio <fidencio@redhat.com>
Pablo Greco 40546a
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Pablo Greco 40546a
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Pablo Greco 40546a
(cherry picked from commit 8dc1b6ce50a6c007c0d2149ac7106221c99beb6f)
Pablo Greco 40546a
Pablo Greco 40546a
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1689297
Pablo Greco 40546a
Pablo Greco 40546a
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Pablo Greco 40546a
Message-Id: <71e4dbba49af93bcc36eefc92725e493a0447214.1561993100.git.phrdina@redhat.com>
Pablo Greco 40546a
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Pablo Greco 40546a
---
Pablo Greco 40546a
 src/util/vircgroup.c        | 102 +-------------------------------
Pablo Greco 40546a
 src/util/vircgroupbackend.h |   6 ++
Pablo Greco 40546a
 src/util/vircgroupv1.c      | 113 ++++++++++++++++++++++++++++++++++++
Pablo Greco 40546a
 3 files changed, 120 insertions(+), 101 deletions(-)
Pablo Greco 40546a
Pablo Greco 40546a
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
Pablo Greco 40546a
index d4d02d8f5b..7789966472 100644
Pablo Greco 40546a
--- a/src/util/vircgroup.c
Pablo Greco 40546a
+++ b/src/util/vircgroup.c
Pablo Greco 40546a
@@ -3179,35 +3179,6 @@ virCgroupKillPainfully(virCgroupPtr group)
Pablo Greco 40546a
 }
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
-static char *
Pablo Greco 40546a
-virCgroupIdentifyRoot(virCgroupPtr group)
Pablo Greco 40546a
-{
Pablo Greco 40546a
-    char *ret = NULL;
Pablo Greco 40546a
-    size_t i;
Pablo Greco 40546a
-
Pablo Greco 40546a
-    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
Pablo Greco 40546a
-        char *tmp;
Pablo Greco 40546a
-        if (!group->controllers[i].mountPoint)
Pablo Greco 40546a
-            continue;
Pablo Greco 40546a
-        if (!(tmp = strrchr(group->controllers[i].mountPoint, '/'))) {
Pablo Greco 40546a
-            virReportError(VIR_ERR_INTERNAL_ERROR,
Pablo Greco 40546a
-                           _("Could not find directory separator in %s"),
Pablo Greco 40546a
-                           group->controllers[i].mountPoint);
Pablo Greco 40546a
-            return NULL;
Pablo Greco 40546a
-        }
Pablo Greco 40546a
-
Pablo Greco 40546a
-        if (VIR_STRNDUP(ret, group->controllers[i].mountPoint,
Pablo Greco 40546a
-                        tmp - group->controllers[i].mountPoint) < 0)
Pablo Greco 40546a
-            return NULL;
Pablo Greco 40546a
-        return ret;
Pablo Greco 40546a
-    }
Pablo Greco 40546a
-
Pablo Greco 40546a
-    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
Pablo Greco 40546a
-                   _("Could not find any mounted controllers"));
Pablo Greco 40546a
-    return NULL;
Pablo Greco 40546a
-}
Pablo Greco 40546a
-
Pablo Greco 40546a
-
Pablo Greco 40546a
 /**
Pablo Greco 40546a
  * virCgroupGetCpuCfsQuota:
Pablo Greco 40546a
  *
Pablo Greco 40546a
@@ -3302,78 +3273,7 @@ int
Pablo Greco 40546a
 virCgroupBindMount(virCgroupPtr group, const char *oldroot,
Pablo Greco 40546a
                    const char *mountopts)
Pablo Greco 40546a
 {
Pablo Greco 40546a
-    size_t i;
Pablo Greco 40546a
-    VIR_AUTOFREE(char *) opts = NULL;
Pablo Greco 40546a
-    VIR_AUTOFREE(char *) root = NULL;
Pablo Greco 40546a
-
Pablo Greco 40546a
-    if (!(root = virCgroupIdentifyRoot(group)))
Pablo Greco 40546a
-        return -1;
Pablo Greco 40546a
-
Pablo Greco 40546a
-    VIR_DEBUG("Mounting cgroups at '%s'", root);
Pablo Greco 40546a
-
Pablo Greco 40546a
-    if (virFileMakePath(root) < 0) {
Pablo Greco 40546a
-        virReportSystemError(errno,
Pablo Greco 40546a
-                             _("Unable to create directory %s"),
Pablo Greco 40546a
-                             root);
Pablo Greco 40546a
-        return -1;
Pablo Greco 40546a
-    }
Pablo Greco 40546a
-
Pablo Greco 40546a
-    if (virAsprintf(&opts,
Pablo Greco 40546a
-                    "mode=755,size=65536%s", mountopts) < 0)
Pablo Greco 40546a
-        return -1;
Pablo Greco 40546a
-
Pablo Greco 40546a
-    if (mount("tmpfs", root, "tmpfs", MS_NOSUID|MS_NODEV|MS_NOEXEC, opts) < 0) {
Pablo Greco 40546a
-        virReportSystemError(errno,
Pablo Greco 40546a
-                             _("Failed to mount %s on %s type %s"),
Pablo Greco 40546a
-                             "tmpfs", root, "tmpfs");
Pablo Greco 40546a
-        return -1;
Pablo Greco 40546a
-    }
Pablo Greco 40546a
-
Pablo Greco 40546a
-    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
Pablo Greco 40546a
-        if (!group->controllers[i].mountPoint)
Pablo Greco 40546a
-            continue;
Pablo Greco 40546a
-
Pablo Greco 40546a
-        if (!virFileExists(group->controllers[i].mountPoint)) {
Pablo Greco 40546a
-            VIR_AUTOFREE(char *) src = NULL;
Pablo Greco 40546a
-            if (virAsprintf(&src, "%s%s",
Pablo Greco 40546a
-                            oldroot,
Pablo Greco 40546a
-                            group->controllers[i].mountPoint) < 0)
Pablo Greco 40546a
-                return -1;
Pablo Greco 40546a
-
Pablo Greco 40546a
-            VIR_DEBUG("Create mount point '%s'",
Pablo Greco 40546a
-                      group->controllers[i].mountPoint);
Pablo Greco 40546a
-            if (virFileMakePath(group->controllers[i].mountPoint) < 0) {
Pablo Greco 40546a
-                virReportSystemError(errno,
Pablo Greco 40546a
-                                     _("Unable to create directory %s"),
Pablo Greco 40546a
-                                     group->controllers[i].mountPoint);
Pablo Greco 40546a
-                return -1;
Pablo Greco 40546a
-            }
Pablo Greco 40546a
-
Pablo Greco 40546a
-            if (mount(src, group->controllers[i].mountPoint, "none", MS_BIND,
Pablo Greco 40546a
-                      NULL) < 0) {
Pablo Greco 40546a
-                virReportSystemError(errno,
Pablo Greco 40546a
-                                     _("Failed to bind cgroup '%s' on '%s'"),
Pablo Greco 40546a
-                                     src, group->controllers[i].mountPoint);
Pablo Greco 40546a
-                return -1;
Pablo Greco 40546a
-            }
Pablo Greco 40546a
-        }
Pablo Greco 40546a
-
Pablo Greco 40546a
-        if (group->controllers[i].linkPoint) {
Pablo Greco 40546a
-            VIR_DEBUG("Link mount point '%s' to '%s'",
Pablo Greco 40546a
-                      group->controllers[i].mountPoint,
Pablo Greco 40546a
-                      group->controllers[i].linkPoint);
Pablo Greco 40546a
-            if (symlink(group->controllers[i].mountPoint,
Pablo Greco 40546a
-                        group->controllers[i].linkPoint) < 0) {
Pablo Greco 40546a
-                virReportSystemError(errno,
Pablo Greco 40546a
-                                     _("Unable to symlink directory %s to %s"),
Pablo Greco 40546a
-                                     group->controllers[i].mountPoint,
Pablo Greco 40546a
-                                     group->controllers[i].linkPoint);
Pablo Greco 40546a
-                return -1;
Pablo Greco 40546a
-            }
Pablo Greco 40546a
-        }
Pablo Greco 40546a
-    }
Pablo Greco 40546a
-
Pablo Greco 40546a
-    return 0;
Pablo Greco 40546a
+    return group->backend->bindMount(group, oldroot, mountopts);
Pablo Greco 40546a
 }
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
diff --git a/src/util/vircgroupbackend.h b/src/util/vircgroupbackend.h
Pablo Greco 40546a
index 3e0f6d01f4..caeec3de60 100644
Pablo Greco 40546a
--- a/src/util/vircgroupbackend.h
Pablo Greco 40546a
+++ b/src/util/vircgroupbackend.h
Pablo Greco 40546a
@@ -126,6 +126,11 @@ typedef int
Pablo Greco 40546a
 (*virCgroupHasEmptyTasksCB)(virCgroupPtr cgroup,
Pablo Greco 40546a
                             int controller);
Pablo Greco 40546a
 
Pablo Greco 40546a
+typedef int
Pablo Greco 40546a
+(*virCgroupBindMountCB)(virCgroupPtr group,
Pablo Greco 40546a
+                        const char *oldroot,
Pablo Greco 40546a
+                        const char *mountopts);
Pablo Greco 40546a
+
Pablo Greco 40546a
 struct _virCgroupBackend {
Pablo Greco 40546a
     virCgroupBackendType type;
Pablo Greco 40546a
 
Pablo Greco 40546a
@@ -146,6 +151,7 @@ struct _virCgroupBackend {
Pablo Greco 40546a
     virCgroupRemoveCB remove;
Pablo Greco 40546a
     virCgroupAddTaskCB addTask;
Pablo Greco 40546a
     virCgroupHasEmptyTasksCB hasEmptyTasks;
Pablo Greco 40546a
+    virCgroupBindMountCB bindMount;
Pablo Greco 40546a
 };
Pablo Greco 40546a
 typedef struct _virCgroupBackend virCgroupBackend;
Pablo Greco 40546a
 typedef virCgroupBackend *virCgroupBackendPtr;
Pablo Greco 40546a
diff --git a/src/util/vircgroupv1.c b/src/util/vircgroupv1.c
Pablo Greco 40546a
index b057cdfd9e..d63525dfb0 100644
Pablo Greco 40546a
--- a/src/util/vircgroupv1.c
Pablo Greco 40546a
+++ b/src/util/vircgroupv1.c
Pablo Greco 40546a
@@ -24,6 +24,9 @@
Pablo Greco 40546a
 # include <mntent.h>
Pablo Greco 40546a
 #endif
Pablo Greco 40546a
 #include <sys/stat.h>
Pablo Greco 40546a
+#if defined HAVE_SYS_MOUNT_H
Pablo Greco 40546a
+# include <sys/mount.h>
Pablo Greco 40546a
+#endif
Pablo Greco 40546a
 
Pablo Greco 40546a
 #include "internal.h"
Pablo Greco 40546a
 
Pablo Greco 40546a
@@ -755,6 +758,115 @@ virCgroupV1HasEmptyTasks(virCgroupPtr cgroup,
Pablo Greco 40546a
 }
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
+static char *
Pablo Greco 40546a
+virCgroupV1IdentifyRoot(virCgroupPtr group)
Pablo Greco 40546a
+{
Pablo Greco 40546a
+    char *ret = NULL;
Pablo Greco 40546a
+    size_t i;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
Pablo Greco 40546a
+        char *tmp;
Pablo Greco 40546a
+        if (!group->controllers[i].mountPoint)
Pablo Greco 40546a
+            continue;
Pablo Greco 40546a
+        if (!(tmp = strrchr(group->controllers[i].mountPoint, '/'))) {
Pablo Greco 40546a
+            virReportError(VIR_ERR_INTERNAL_ERROR,
Pablo Greco 40546a
+                           _("Could not find directory separator in %s"),
Pablo Greco 40546a
+                           group->controllers[i].mountPoint);
Pablo Greco 40546a
+            return NULL;
Pablo Greco 40546a
+        }
Pablo Greco 40546a
+
Pablo Greco 40546a
+        if (VIR_STRNDUP(ret, group->controllers[i].mountPoint,
Pablo Greco 40546a
+                        tmp - group->controllers[i].mountPoint) < 0)
Pablo Greco 40546a
+            return NULL;
Pablo Greco 40546a
+        return ret;
Pablo Greco 40546a
+    }
Pablo Greco 40546a
+
Pablo Greco 40546a
+    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
Pablo Greco 40546a
+                   _("Could not find any mounted v1 controllers"));
Pablo Greco 40546a
+    return NULL;
Pablo Greco 40546a
+}
Pablo Greco 40546a
+
Pablo Greco 40546a
+
Pablo Greco 40546a
+static int
Pablo Greco 40546a
+virCgroupV1BindMount(virCgroupPtr group,
Pablo Greco 40546a
+                     const char *oldroot,
Pablo Greco 40546a
+                     const char *mountopts)
Pablo Greco 40546a
+{
Pablo Greco 40546a
+    size_t i;
Pablo Greco 40546a
+    VIR_AUTOFREE(char *) opts = NULL;
Pablo Greco 40546a
+    VIR_AUTOFREE(char *) root = NULL;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if (!(root = virCgroupV1IdentifyRoot(group)))
Pablo Greco 40546a
+        return -1;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    VIR_DEBUG("Mounting cgroups at '%s'", root);
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if (virFileMakePath(root) < 0) {
Pablo Greco 40546a
+        virReportSystemError(errno,
Pablo Greco 40546a
+                             _("Unable to create directory %s"),
Pablo Greco 40546a
+                             root);
Pablo Greco 40546a
+        return -1;
Pablo Greco 40546a
+    }
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if (virAsprintf(&opts,
Pablo Greco 40546a
+                    "mode=755,size=65536%s", mountopts) < 0)
Pablo Greco 40546a
+        return -1;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if (mount("tmpfs", root, "tmpfs", MS_NOSUID|MS_NODEV|MS_NOEXEC, opts) < 0) {
Pablo Greco 40546a
+        virReportSystemError(errno,
Pablo Greco 40546a
+                             _("Failed to mount %s on %s type %s"),
Pablo Greco 40546a
+                             "tmpfs", root, "tmpfs");
Pablo Greco 40546a
+        return -1;
Pablo Greco 40546a
+    }
Pablo Greco 40546a
+
Pablo Greco 40546a
+    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
Pablo Greco 40546a
+        if (!group->controllers[i].mountPoint)
Pablo Greco 40546a
+            continue;
Pablo Greco 40546a
+
Pablo Greco 40546a
+        if (!virFileExists(group->controllers[i].mountPoint)) {
Pablo Greco 40546a
+            VIR_AUTOFREE(char *) src = NULL;
Pablo Greco 40546a
+            if (virAsprintf(&src, "%s%s",
Pablo Greco 40546a
+                            oldroot,
Pablo Greco 40546a
+                            group->controllers[i].mountPoint) < 0)
Pablo Greco 40546a
+                return -1;
Pablo Greco 40546a
+
Pablo Greco 40546a
+            VIR_DEBUG("Create mount point '%s'",
Pablo Greco 40546a
+                      group->controllers[i].mountPoint);
Pablo Greco 40546a
+            if (virFileMakePath(group->controllers[i].mountPoint) < 0) {
Pablo Greco 40546a
+                virReportSystemError(errno,
Pablo Greco 40546a
+                                     _("Unable to create directory %s"),
Pablo Greco 40546a
+                                     group->controllers[i].mountPoint);
Pablo Greco 40546a
+                return -1;
Pablo Greco 40546a
+            }
Pablo Greco 40546a
+
Pablo Greco 40546a
+            if (mount(src, group->controllers[i].mountPoint, "none", MS_BIND,
Pablo Greco 40546a
+                      NULL) < 0) {
Pablo Greco 40546a
+                virReportSystemError(errno,
Pablo Greco 40546a
+                                     _("Failed to bind cgroup '%s' on '%s'"),
Pablo Greco 40546a
+                                     src, group->controllers[i].mountPoint);
Pablo Greco 40546a
+                return -1;
Pablo Greco 40546a
+            }
Pablo Greco 40546a
+        }
Pablo Greco 40546a
+
Pablo Greco 40546a
+        if (group->controllers[i].linkPoint) {
Pablo Greco 40546a
+            VIR_DEBUG("Link mount point '%s' to '%s'",
Pablo Greco 40546a
+                      group->controllers[i].mountPoint,
Pablo Greco 40546a
+                      group->controllers[i].linkPoint);
Pablo Greco 40546a
+            if (symlink(group->controllers[i].mountPoint,
Pablo Greco 40546a
+                        group->controllers[i].linkPoint) < 0) {
Pablo Greco 40546a
+                virReportSystemError(errno,
Pablo Greco 40546a
+                                     _("Unable to symlink directory %s to %s"),
Pablo Greco 40546a
+                                     group->controllers[i].mountPoint,
Pablo Greco 40546a
+                                     group->controllers[i].linkPoint);
Pablo Greco 40546a
+                return -1;
Pablo Greco 40546a
+            }
Pablo Greco 40546a
+        }
Pablo Greco 40546a
+    }
Pablo Greco 40546a
+
Pablo Greco 40546a
+    return 0;
Pablo Greco 40546a
+}
Pablo Greco 40546a
+
Pablo Greco 40546a
+
Pablo Greco 40546a
 virCgroupBackend virCgroupV1Backend = {
Pablo Greco 40546a
     .type = VIR_CGROUP_BACKEND_TYPE_V1,
Pablo Greco 40546a
 
Pablo Greco 40546a
@@ -774,6 +886,7 @@ virCgroupBackend virCgroupV1Backend = {
Pablo Greco 40546a
     .remove = virCgroupV1Remove,
Pablo Greco 40546a
     .addTask = virCgroupV1AddTask,
Pablo Greco 40546a
     .hasEmptyTasks = virCgroupV1HasEmptyTasks,
Pablo Greco 40546a
+    .bindMount = virCgroupV1BindMount,
Pablo Greco 40546a
 };
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
-- 
Pablo Greco 40546a
2.22.0
Pablo Greco 40546a