From d9e42255325cbe1697328acb5b8c36fb71abf165 Mon Sep 17 00:00:00 2001 From: Joseph Zhao Date: Thu, 11 Jun 2026 21:38:26 +0800 Subject: [PATCH] fix(starry): provide /sys/fs/cgroup mount point in sysfs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The hand-built sysfs root only exposed class/bus/devices/dev/kernel and had no `fs/` directory. Once sysfs is mounted over /sys it shadows the rootfs's own /sys/fs/cgroup, so systemd's `mount("/sys/fs/cgroup")` failed with ENOENT and it could not set up the cgroup hierarchy. 在 sysfs 中补 `fs/cgroup` 空目录作为挂载点,对齐 Linux 由内核在 sysfs 中 提供该目录的行为。systemd 据此成功在其上挂载 tmpfs + cgroup2。 --- os/StarryOS/kernel/src/pseudofs/sysfs.rs | 13 +++++++++++++ .../system/c-regression-test-sysfs-class/src/main.c | 13 +++++++++++++ 2 files changed, 26 insertions(+) diff --git a/os/StarryOS/kernel/src/pseudofs/sysfs.rs b/os/StarryOS/kernel/src/pseudofs/sysfs.rs index 28a3803408..52e8dd0977 100644 --- a/os/StarryOS/kernel/src/pseudofs/sysfs.rs +++ b/os/StarryOS/kernel/src/pseudofs/sysfs.rs @@ -93,6 +93,19 @@ fn builder(fs: Arc) -> DirMaker { ); SimpleDir::new_maker(fs.clone(), Arc::new(kernel)) }); + // `/sys/fs/cgroup` is the mount point systemd lays its cgroup hierarchy on + // (it mounts tmpfs then cgroup2 here). On Linux the kernel provides this + // empty directory inside sysfs; once sysfs is mounted over /sys it shadows + // the rootfs's own /sys/fs/cgroup, so the mount point must exist here or + // `mount("/sys/fs/cgroup")` fails with ENOENT. + root.add("fs", { + let mut fs_dir = DirMapping::new(); + fs_dir.add( + "cgroup", + SimpleDir::new_maker(fs.clone(), Arc::new(DirMapping::new())), + ); + SimpleDir::new_maker(fs.clone(), Arc::new(fs_dir)) + }); SimpleDir::new_maker(fs.clone(), Arc::new(root)) } diff --git a/test-suit/starryos/qemu-smp1/system/c-regression-test-sysfs-class/src/main.c b/test-suit/starryos/qemu-smp1/system/c-regression-test-sysfs-class/src/main.c index ad4857fdf1..668fe6e650 100644 --- a/test-suit/starryos/qemu-smp1/system/c-regression-test-sysfs-class/src/main.c +++ b/test-suit/starryos/qemu-smp1/system/c-regression-test-sysfs-class/src/main.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -82,5 +83,17 @@ int main(void) CHECK(S_ISLNK(st.st_mode), "/sys/class/graphics/fb0 应是软链"); + /* /sys/fs/cgroup 必须由 sysfs 自身提供 —— sysfs 盖在 /sys 上后 + * rootfs 里的同名目录不可见,systemd 在这里挂 cgroup 层级 */ + CHECK_RET(stat("/sys/fs", &st), 0, "/sys/fs 应可 stat"); + CHECK(S_ISDIR(st.st_mode), "/sys/fs 应是目录"); + CHECK_RET(stat("/sys/fs/cgroup", &st), 0, "/sys/fs/cgroup 应可 stat"); + CHECK(S_ISDIR(st.st_mode), "/sys/fs/cgroup 应是目录"); + + CHECK_RET(mount("none", "/sys/fs/cgroup", "tmpfs", 0, NULL), 0, + "tmpfs 应能挂载到 /sys/fs/cgroup"); + CHECK_RET(umount2("/sys/fs/cgroup", 0), 0, + "应能卸载 /sys/fs/cgroup 上的 tmpfs"); + TEST_DONE(); }