Skip to content

Commit

Permalink
virtme-ng-init: allow to mount legacy cgroupfs (v1)
Browse files Browse the repository at this point in the history
Allow to mount legacy cgroup when SYSTEMD_CGROUP_ENABLE_LEGACY_FORCE=1
is passed to the kernel boot command options.

This mimics systemd's behavior, as specified in the systemd
documentation:
```
CHANGES WITH 256 in spe:
...
        * Support for cgroup v1 ('legacy' and 'hybrid' hierarchies) is now
          considered obsolete and systemd by default will refuse to boot under it.
          To forcibly reenable cgroup v1 support, SYSTEMD_CGROUP_ENABLE_LEGACY_FORCE=1
          must be set on kernel command line.
...
```

Moreover, with cgroup v1, only try to mount the same legacy cgroup
subsystems supported by systemd (also from the systemd doc):
```
* on cgroup v1: `cpu`, `cpuacct`, `blkio`, `memory`, `devices`, `pids`
```

Signed-off-by: Andrea Righi <andrea.righi@canonical.com>
  • Loading branch information
arighi committed Mar 2, 2024
1 parent c4bad3d commit 450872b
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,6 @@ const KERNEL_MOUNTS: &[MountInfo] = &[
flags: 0,
fsdata: "",
},
MountInfo {
source: "cgroup2",
target: "/sys/fs/cgroup",
fs_type: "cgroup2",
flags: 0,
fsdata: "",
},
];

const SYSTEM_MOUNTS: &[MountInfo] = &[
Expand Down Expand Up @@ -415,6 +408,25 @@ fn mount_kernel_filesystems() {
}
}

fn mount_cgroupfs() {
// If SYSTEMD_CGROUP_ENABLE_LEGACY_FORCE=1 is passed we can mimic systemd's behavior and mount
// the legacy cgroup v1 layout.
let cmdline = std::fs::read_to_string("/proc/cmdline").unwrap();
if cmdline.contains("SYSTEMD_CGROUP_ENABLE_LEGACY_FORCE=1") {
utils::do_mount("cgroup", "/sys/fs/cgroup", "tmpfs", 0, "");
let subsystems = vec!["cpu", "cpuacct", "blkio", "memory", "devices", "pids"];
for subsys in &subsystems {
let target = format!("/sys/fs/cgroup/{}", subsys);
utils::do_mkdir(&target);
// Don't treat failure as critical here, since the kernel may not
// support all the legacy cgroups.
utils::do_mount(subsys, &target, "cgroup", 0, subsys);
}
} else {
utils::do_mount("cgroup2", "/sys/fs/cgroup", "cgroup2", 0, "");
}
}

fn mount_virtme_overlays() {
for (key, path) in env::vars() {
if key.starts_with("virtme_rw_overlay") {
Expand Down Expand Up @@ -988,6 +1000,7 @@ fn main() {
configure_environment();
configure_hostname();
mount_kernel_filesystems();
mount_cgroupfs();
configure_limits();
mount_virtme_overlays();
mount_sys_filesystems();
Expand Down

0 comments on commit 450872b

Please sign in to comment.