cgroups: simplify and fix mounting on non-cgroup namespace aware kernels

parent a7f83fb0
...@@ -1424,7 +1424,7 @@ static inline bool cg_mount_needs_subdirs(int cgroup_automount_type) ...@@ -1424,7 +1424,7 @@ static inline bool cg_mount_needs_subdirs(int cgroup_automount_type)
* control/the/cg/path. * control/the/cg/path.
*/ */
static int cg_legacy_mount_controllers(int cgroup_automount_type, struct hierarchy *h, static int cg_legacy_mount_controllers(int cgroup_automount_type, struct hierarchy *h,
char *controllerpath, char *cgpath, char *hierarchy_mnt, char *cgpath,
const char *container_cgroup) const char *container_cgroup)
{ {
__do_free char *sourcepath = NULL; __do_free char *sourcepath = NULL;
...@@ -1433,21 +1433,21 @@ static int cg_legacy_mount_controllers(int cgroup_automount_type, struct hierarc ...@@ -1433,21 +1433,21 @@ static int cg_legacy_mount_controllers(int cgroup_automount_type, struct hierarc
if ((cgroup_automount_type == LXC_AUTO_CGROUP_RO) || if ((cgroup_automount_type == LXC_AUTO_CGROUP_RO) ||
(cgroup_automount_type == LXC_AUTO_CGROUP_MIXED)) { (cgroup_automount_type == LXC_AUTO_CGROUP_MIXED)) {
ret = mount(controllerpath, controllerpath, "cgroup", MS_BIND, NULL); ret = mount(hierarchy_mnt, hierarchy_mnt, "cgroup", MS_BIND, NULL);
if (ret < 0) if (ret < 0)
return log_error_errno(-1, errno, "Failed to bind mount \"%s\" onto \"%s\"", return log_error_errno(-1, errno, "Failed to bind mount \"%s\" onto \"%s\"",
controllerpath, controllerpath); hierarchy_mnt, hierarchy_mnt);
remount_flags = add_required_remount_flags(controllerpath, remount_flags = add_required_remount_flags(hierarchy_mnt,
controllerpath, hierarchy_mnt,
flags | MS_REMOUNT); flags | MS_REMOUNT);
ret = mount(controllerpath, controllerpath, "cgroup", ret = mount(hierarchy_mnt, hierarchy_mnt, "cgroup",
remount_flags | MS_REMOUNT | MS_BIND | MS_RDONLY, remount_flags | MS_REMOUNT | MS_BIND | MS_RDONLY,
NULL); NULL);
if (ret < 0) if (ret < 0)
return log_error_errno(-1, errno, "Failed to remount \"%s\" ro", controllerpath); return log_error_errno(-1, errno, "Failed to remount \"%s\" ro", hierarchy_mnt);
INFO("Remounted %s read-only", controllerpath); INFO("Remounted %s read-only", hierarchy_mnt);
} }
sourcepath = make_cgroup_path(h, h->container_base_path, container_cgroup, NULL); sourcepath = make_cgroup_path(h, h->container_base_path, container_cgroup, NULL);
...@@ -1770,17 +1770,12 @@ __cgfsng_ops static bool cgfsng_mount(struct cgroup_ops *ops, ...@@ -1770,17 +1770,12 @@ __cgfsng_ops static bool cgfsng_mount(struct cgroup_ops *ops,
DEFAULT_CGROUP_MOUNTPOINT_RELATIVE); DEFAULT_CGROUP_MOUNTPOINT_RELATIVE);
for (int i = 0; ops->hierarchies[i]; i++) { for (int i = 0; ops->hierarchies[i]; i++) {
__do_free char *controllerpath = NULL, *path2 = NULL; __do_free char *hierarchy_mnt = NULL, *path2 = NULL;
struct hierarchy *h = ops->hierarchies[i]; struct hierarchy *h = ops->hierarchies[i];
char *controller = h->mountpoint;
if (!controller) ret = mkdirat(dfd_mnt_tmpfs, h->mountpoint, 0000);
continue;
controller++;
ret = mkdirat(dfd_mnt_tmpfs, controller, 0000);
if (ret < 0) if (ret < 0)
return log_error_errno(false, errno, "Failed to create cgroup mountpoint %d(%s)", dfd_mnt_tmpfs, controller); return syserrno(false, "Failed to create cgroup mountpoint %d(%s)", dfd_mnt_tmpfs, h->mountpoint);
if (in_cgroup_ns && wants_force_mount) { if (in_cgroup_ns && wants_force_mount) {
/* /*
...@@ -1788,7 +1783,8 @@ __cgfsng_ops static bool cgfsng_mount(struct cgroup_ops *ops, ...@@ -1788,7 +1783,8 @@ __cgfsng_ops static bool cgfsng_mount(struct cgroup_ops *ops,
* will not have CAP_SYS_ADMIN after it has started we * will not have CAP_SYS_ADMIN after it has started we
* need to mount the cgroups manually. * need to mount the cgroups manually.
*/ */
ret = cgroupfs_mount(cgroup_automount_type, h, rootfs, dfd_mnt_tmpfs, controller); ret = cgroupfs_mount(cgroup_automount_type, h, rootfs,
dfd_mnt_tmpfs, h->mountpoint);
if (ret < 0) if (ret < 0)
return false; return false;
...@@ -1796,7 +1792,8 @@ __cgfsng_ops static bool cgfsng_mount(struct cgroup_ops *ops, ...@@ -1796,7 +1792,8 @@ __cgfsng_ops static bool cgfsng_mount(struct cgroup_ops *ops,
} }
/* Here is where the ancient kernel section begins. */ /* Here is where the ancient kernel section begins. */
ret = cgroupfs_bind_mount(cgroup_automount_type, h, rootfs, dfd_mnt_tmpfs, controller); ret = cgroupfs_bind_mount(cgroup_automount_type, h, rootfs,
dfd_mnt_tmpfs, h->mountpoint);
if (ret < 0) if (ret < 0)
return false; return false;
...@@ -1806,13 +1803,16 @@ __cgfsng_ops static bool cgfsng_mount(struct cgroup_ops *ops, ...@@ -1806,13 +1803,16 @@ __cgfsng_ops static bool cgfsng_mount(struct cgroup_ops *ops,
if (!cgroup_root) if (!cgroup_root)
cgroup_root = must_make_path(rootfs_mnt, DEFAULT_CGROUP_MOUNTPOINT, NULL); cgroup_root = must_make_path(rootfs_mnt, DEFAULT_CGROUP_MOUNTPOINT, NULL);
controllerpath = must_make_path(cgroup_root, controller, NULL); hierarchy_mnt = must_make_path(cgroup_root, h->mountpoint, NULL);
path2 = must_make_path(controllerpath, h->container_base_path, ops->container_cgroup, NULL); path2 = must_make_path(hierarchy_mnt, h->container_base_path,
ops->container_cgroup, NULL);
ret = mkdir_p(path2, 0755); ret = mkdir_p(path2, 0755);
if (ret < 0 && (errno != EEXIST)) if (ret < 0 && (errno != EEXIST))
return false; return false;
ret = cg_legacy_mount_controllers(cgroup_automount_type, h, controllerpath, path2, ops->container_cgroup); ret = cg_legacy_mount_controllers(cgroup_automount_type, h,
hierarchy_mnt, path2,
ops->container_cgroup);
if (ret < 0) if (ret < 0)
return false; return false;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment