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