Blob Blame History Raw
From 7ee971869f488f806a728eead8be82c287a35fc4 Mon Sep 17 00:00:00 2001
Message-Id: <7ee971869f488f806a728eead8be82c287a35fc4@dist-git>
From: Pavel Hrdina <phrdina@redhat.com>
Date: Mon, 1 Jul 2019 17:06:39 +0200
Subject: [PATCH] vircgroup: extract virCgroupV1BindMount
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Reviewed-by: Fabiano Fidêncio <fidencio@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
(cherry picked from commit 8dc1b6ce50a6c007c0d2149ac7106221c99beb6f)

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1689297

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Message-Id: <71e4dbba49af93bcc36eefc92725e493a0447214.1561993100.git.phrdina@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
---
 src/util/vircgroup.c        | 102 +-------------------------------
 src/util/vircgroupbackend.h |   6 ++
 src/util/vircgroupv1.c      | 113 ++++++++++++++++++++++++++++++++++++
 3 files changed, 120 insertions(+), 101 deletions(-)

diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
index d4d02d8f5b..7789966472 100644
--- a/src/util/vircgroup.c
+++ b/src/util/vircgroup.c
@@ -3179,35 +3179,6 @@ virCgroupKillPainfully(virCgroupPtr group)
 }
 
 
-static char *
-virCgroupIdentifyRoot(virCgroupPtr group)
-{
-    char *ret = NULL;
-    size_t i;
-
-    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
-        char *tmp;
-        if (!group->controllers[i].mountPoint)
-            continue;
-        if (!(tmp = strrchr(group->controllers[i].mountPoint, '/'))) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("Could not find directory separator in %s"),
-                           group->controllers[i].mountPoint);
-            return NULL;
-        }
-
-        if (VIR_STRNDUP(ret, group->controllers[i].mountPoint,
-                        tmp - group->controllers[i].mountPoint) < 0)
-            return NULL;
-        return ret;
-    }
-
-    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                   _("Could not find any mounted controllers"));
-    return NULL;
-}
-
-
 /**
  * virCgroupGetCpuCfsQuota:
  *
@@ -3302,78 +3273,7 @@ int
 virCgroupBindMount(virCgroupPtr group, const char *oldroot,
                    const char *mountopts)
 {
-    size_t i;
-    VIR_AUTOFREE(char *) opts = NULL;
-    VIR_AUTOFREE(char *) root = NULL;
-
-    if (!(root = virCgroupIdentifyRoot(group)))
-        return -1;
-
-    VIR_DEBUG("Mounting cgroups at '%s'", root);
-
-    if (virFileMakePath(root) < 0) {
-        virReportSystemError(errno,
-                             _("Unable to create directory %s"),
-                             root);
-        return -1;
-    }
-
-    if (virAsprintf(&opts,
-                    "mode=755,size=65536%s", mountopts) < 0)
-        return -1;
-
-    if (mount("tmpfs", root, "tmpfs", MS_NOSUID|MS_NODEV|MS_NOEXEC, opts) < 0) {
-        virReportSystemError(errno,
-                             _("Failed to mount %s on %s type %s"),
-                             "tmpfs", root, "tmpfs");
-        return -1;
-    }
-
-    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
-        if (!group->controllers[i].mountPoint)
-            continue;
-
-        if (!virFileExists(group->controllers[i].mountPoint)) {
-            VIR_AUTOFREE(char *) src = NULL;
-            if (virAsprintf(&src, "%s%s",
-                            oldroot,
-                            group->controllers[i].mountPoint) < 0)
-                return -1;
-
-            VIR_DEBUG("Create mount point '%s'",
-                      group->controllers[i].mountPoint);
-            if (virFileMakePath(group->controllers[i].mountPoint) < 0) {
-                virReportSystemError(errno,
-                                     _("Unable to create directory %s"),
-                                     group->controllers[i].mountPoint);
-                return -1;
-            }
-
-            if (mount(src, group->controllers[i].mountPoint, "none", MS_BIND,
-                      NULL) < 0) {
-                virReportSystemError(errno,
-                                     _("Failed to bind cgroup '%s' on '%s'"),
-                                     src, group->controllers[i].mountPoint);
-                return -1;
-            }
-        }
-
-        if (group->controllers[i].linkPoint) {
-            VIR_DEBUG("Link mount point '%s' to '%s'",
-                      group->controllers[i].mountPoint,
-                      group->controllers[i].linkPoint);
-            if (symlink(group->controllers[i].mountPoint,
-                        group->controllers[i].linkPoint) < 0) {
-                virReportSystemError(errno,
-                                     _("Unable to symlink directory %s to %s"),
-                                     group->controllers[i].mountPoint,
-                                     group->controllers[i].linkPoint);
-                return -1;
-            }
-        }
-    }
-
-    return 0;
+    return group->backend->bindMount(group, oldroot, mountopts);
 }
 
 
diff --git a/src/util/vircgroupbackend.h b/src/util/vircgroupbackend.h
index 3e0f6d01f4..caeec3de60 100644
--- a/src/util/vircgroupbackend.h
+++ b/src/util/vircgroupbackend.h
@@ -126,6 +126,11 @@ typedef int
 (*virCgroupHasEmptyTasksCB)(virCgroupPtr cgroup,
                             int controller);
 
+typedef int
+(*virCgroupBindMountCB)(virCgroupPtr group,
+                        const char *oldroot,
+                        const char *mountopts);
+
 struct _virCgroupBackend {
     virCgroupBackendType type;
 
@@ -146,6 +151,7 @@ struct _virCgroupBackend {
     virCgroupRemoveCB remove;
     virCgroupAddTaskCB addTask;
     virCgroupHasEmptyTasksCB hasEmptyTasks;
+    virCgroupBindMountCB bindMount;
 };
 typedef struct _virCgroupBackend virCgroupBackend;
 typedef virCgroupBackend *virCgroupBackendPtr;
diff --git a/src/util/vircgroupv1.c b/src/util/vircgroupv1.c
index b057cdfd9e..d63525dfb0 100644
--- a/src/util/vircgroupv1.c
+++ b/src/util/vircgroupv1.c
@@ -24,6 +24,9 @@
 # include <mntent.h>
 #endif
 #include <sys/stat.h>
+#if defined HAVE_SYS_MOUNT_H
+# include <sys/mount.h>
+#endif
 
 #include "internal.h"
 
@@ -755,6 +758,115 @@ virCgroupV1HasEmptyTasks(virCgroupPtr cgroup,
 }
 
 
+static char *
+virCgroupV1IdentifyRoot(virCgroupPtr group)
+{
+    char *ret = NULL;
+    size_t i;
+
+    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
+        char *tmp;
+        if (!group->controllers[i].mountPoint)
+            continue;
+        if (!(tmp = strrchr(group->controllers[i].mountPoint, '/'))) {
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("Could not find directory separator in %s"),
+                           group->controllers[i].mountPoint);
+            return NULL;
+        }
+
+        if (VIR_STRNDUP(ret, group->controllers[i].mountPoint,
+                        tmp - group->controllers[i].mountPoint) < 0)
+            return NULL;
+        return ret;
+    }
+
+    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                   _("Could not find any mounted v1 controllers"));
+    return NULL;
+}
+
+
+static int
+virCgroupV1BindMount(virCgroupPtr group,
+                     const char *oldroot,
+                     const char *mountopts)
+{
+    size_t i;
+    VIR_AUTOFREE(char *) opts = NULL;
+    VIR_AUTOFREE(char *) root = NULL;
+
+    if (!(root = virCgroupV1IdentifyRoot(group)))
+        return -1;
+
+    VIR_DEBUG("Mounting cgroups at '%s'", root);
+
+    if (virFileMakePath(root) < 0) {
+        virReportSystemError(errno,
+                             _("Unable to create directory %s"),
+                             root);
+        return -1;
+    }
+
+    if (virAsprintf(&opts,
+                    "mode=755,size=65536%s", mountopts) < 0)
+        return -1;
+
+    if (mount("tmpfs", root, "tmpfs", MS_NOSUID|MS_NODEV|MS_NOEXEC, opts) < 0) {
+        virReportSystemError(errno,
+                             _("Failed to mount %s on %s type %s"),
+                             "tmpfs", root, "tmpfs");
+        return -1;
+    }
+
+    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
+        if (!group->controllers[i].mountPoint)
+            continue;
+
+        if (!virFileExists(group->controllers[i].mountPoint)) {
+            VIR_AUTOFREE(char *) src = NULL;
+            if (virAsprintf(&src, "%s%s",
+                            oldroot,
+                            group->controllers[i].mountPoint) < 0)
+                return -1;
+
+            VIR_DEBUG("Create mount point '%s'",
+                      group->controllers[i].mountPoint);
+            if (virFileMakePath(group->controllers[i].mountPoint) < 0) {
+                virReportSystemError(errno,
+                                     _("Unable to create directory %s"),
+                                     group->controllers[i].mountPoint);
+                return -1;
+            }
+
+            if (mount(src, group->controllers[i].mountPoint, "none", MS_BIND,
+                      NULL) < 0) {
+                virReportSystemError(errno,
+                                     _("Failed to bind cgroup '%s' on '%s'"),
+                                     src, group->controllers[i].mountPoint);
+                return -1;
+            }
+        }
+
+        if (group->controllers[i].linkPoint) {
+            VIR_DEBUG("Link mount point '%s' to '%s'",
+                      group->controllers[i].mountPoint,
+                      group->controllers[i].linkPoint);
+            if (symlink(group->controllers[i].mountPoint,
+                        group->controllers[i].linkPoint) < 0) {
+                virReportSystemError(errno,
+                                     _("Unable to symlink directory %s to %s"),
+                                     group->controllers[i].mountPoint,
+                                     group->controllers[i].linkPoint);
+                return -1;
+            }
+        }
+    }
+
+    return 0;
+}
+
+
 virCgroupBackend virCgroupV1Backend = {
     .type = VIR_CGROUP_BACKEND_TYPE_V1,
 
@@ -774,6 +886,7 @@ virCgroupBackend virCgroupV1Backend = {
     .remove = virCgroupV1Remove,
     .addTask = virCgroupV1AddTask,
     .hasEmptyTasks = virCgroupV1HasEmptyTasks,
+    .bindMount = virCgroupV1BindMount,
 };
 
 
-- 
2.22.0