This is an automated email from the ASF dual-hosted git repository.

bmahler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mesos.git


The following commit(s) were added to refs/heads/master by this push:
     new 78143cbf0 [cgroups2] Add mounting and unmounting of cgroup2 filesystem.
78143cbf0 is described below

commit 78143cbf015cff9478714addf2634bf87a6b57c0
Author: Devin Leamy <[email protected]>
AuthorDate: Tue Feb 27 15:41:11 2024 -0500

    [cgroups2] Add mounting and unmounting of cgroup2 filesystem.
    
    Introduces:
    - `cgroups2::mount()`: Mount the cgroup2 hierarchy.
    - `cgroups2::mounted()`: Check if the cgroup2 hierarchy is mounted
       at /sys/fs/cgroup.
    - `cgroups2::unmount()`: Unmount the cgroup2 hierarchy from /sys/fs/cgroup.
    
    The root mount point for the cgroup2 file system is hard-coded to be 
/sys/fs/cgroup,
    which is the default mount point on most systems.
    
    Review: https://reviews.apache.org/r/74872/
---
 src/linux/cgroups2.cpp | 89 +++++++++++++++++++++++++++++++++++++++++++++++++-
 src/linux/cgroups2.hpp | 18 +++++++++-
 2 files changed, 105 insertions(+), 2 deletions(-)

diff --git a/src/linux/cgroups2.cpp b/src/linux/cgroups2.cpp
index baa13557e..b42a012bc 100644
--- a/src/linux/cgroups2.cpp
+++ b/src/linux/cgroups2.cpp
@@ -16,22 +16,109 @@
 
 #include <string>
 
+#include <stout/os.hpp>
+#include <stout/path.hpp>
 #include <stout/try.hpp>
 
 #include "linux/cgroups2.hpp"
 #include "linux/fs.hpp"
 
 using std::string;
+using mesos::internal::fs::MountTable;
 
 namespace cgroups2 {
 
-// Name of the cgroupv2 filesystem as found in /proc/filesystems.
+// Name of the cgroups v2 filesystem as found in /proc/filesystems.
 const string FILE_SYSTEM = "cgroup2";
 
+// Mount point for the cgroups2 file system.
+const string MOUNT_POINT = "/sys/fs/cgroup";
+
+
 bool enabled()
 {
   Try<bool> supported = mesos::internal::fs::supported(cgroups2::FILE_SYSTEM);
   return supported.isSome() && *supported;
 }
 
+
+Try<Nothing> mount()
+{
+  if (!cgroups2::enabled()) {
+    return Error("cgroups2 is not enabled");
+  }
+
+  Try<bool> mounted = cgroups2::mounted();
+  if (mounted.isError()) {
+    return Error("Failed to check if cgroups2 filesystem is mounted: "
+                 + mounted.error());
+  }
+  if (*mounted) {
+    return Error("cgroup2 filesystem is already mounted at"
+                 " '" + cgroups2::MOUNT_POINT + "'");
+  }
+
+  Try<Nothing> mkdir = os::mkdir(cgroups2::MOUNT_POINT);
+  if (mkdir.isError()) {
+    return Error("Failed to create cgroups2 directory"
+                 " '" + cgroups2::MOUNT_POINT + "'"
+                 ": " + mkdir.error());
+  }
+
+  return mesos::internal::fs::mount(
+    None(),
+    cgroups2::MOUNT_POINT,
+    cgroups2::FILE_SYSTEM,
+    0,
+    None());
+}
+
+
+Try<bool> mounted()
+{
+  Try<MountTable> mountTable = MountTable::read("/proc/mounts");
+  if (mountTable.isError()) {
+    return Error("Failed to read /proc/mounts: " + mountTable.error());
+  }
+
+  foreach (MountTable::Entry entry, mountTable.get().entries) {
+    if (entry.type == cgroups2::FILE_SYSTEM) {
+      if (entry.dir == MOUNT_POINT) {
+        return true;
+      }
+      return Error("Found cgroups2 mount at an unexpected location"
+                   " '" + entry.dir + "'");
+    }
+  }
+
+  return false;
+}
+
+
+Try<Nothing> unmount()
+{
+  Try<bool> mounted = cgroups2::mounted();
+  if (mounted.isError()) {
+    return Error("Failed to check if the cgroup2 filesystem is mounted: "
+                 + mounted.error());
+  }
+
+  if (!*mounted) {
+    return Error("cgroups2 filesystem is not mounted");
+  }
+
+  Try<Nothing> result = mesos::internal::fs::unmount(MOUNT_POINT);
+  if (result.isError()) {
+    return Error("Failed to unmount the cgroup2 hierarchy" +
+                 " '" + cgroups2::MOUNT_POINT + "': " + result.error());
+  }
+
+  Try<Nothing> rmdir = os::rmdir(cgroups2::MOUNT_POINT);
+  if (rmdir.isError()) {
+    return Error(
+      "Failed to remove directory '" + cgroups2::MOUNT_POINT + "': " +
+      rmdir.error());
+  }
+}
+
 } // namespace cgroups2
diff --git a/src/linux/cgroups2.hpp b/src/linux/cgroups2.hpp
index 833960d30..0c54e464b 100644
--- a/src/linux/cgroups2.hpp
+++ b/src/linux/cgroups2.hpp
@@ -17,11 +17,27 @@
 #ifndef __CGROUPS_V2_HPP__
 #define __CGROUPS_V2_HPP__
 
+#include <stout/nothing.hpp>
+#include <stout/try.hpp>
+
 namespace cgroups2 {
 
 // Checks if cgroups2 is available on the system.
 bool enabled();
 
+// Mounts the cgroups2 file system at /sys/fs/cgroup. Errors if
+// the cgroups v2 file system is already mounted.
+Try<Nothing> mount();
+
+// Checks if the cgroup2 file systems is mounted at /sys/fs/cgroup,
+// returns an error if the mount is found at an unexpected location.
+Try<bool> mounted();
+
+// Unmounts the cgroups2 file system from /sys/fs/cgroup. Errors if
+// the cgroup2 file system is not mounted at /sys/fs/cgroup. It's the
+// responsibility of the caller to ensure all child cgroups have been 
destroyed.
+Try<Nothing> unmount();
+
 } // namespace cgroups2
 
-#endif // __CGROUPS_V2_HPP__
\ No newline at end of file
+#endif // __CGROUPS_V2_HPP__

Reply via email to