render / rpms / libvirt

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