Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,14 @@ The 4 KiB guest helper in `internal/guest/regional/Regional.java` includes its c

Available for supported Windows x86_64 Android 14 API 34 images. Import all five components before enabling an instance.

- After the official [ModAVDKernelSU asset](https://github.com/ModAVD/ModAVDKernelSU/releases) is published, download in System Check or run `modavd components fetch --kernelsu`. Until then the manager does not show KernelSU; once a self-built module is imported with the next command, it appears in System Check and in the instance's More actions.
- Download in System Check or run `modavd components fetch --kernelsu`. The kernel module is the official [ModAVDKernelSU asset](https://github.com/ModAVD/ModAVDKernelSU/releases), checked against a pinned size and SHA-256.
- `modavd components import-ksu <file or folder>…`. For a locally built `kernelsu.ko`, add `--lkm-sha256 <SHA256>` with a digest you have verified. The full kernel release must match.
- `modavd ksu <i> install|status|modules`, `modavd ksu <i> module install <zip>… [--no-reboot]`, or `modavd ksu <i> module remove <id> [--no-reboot]`.
- If boot fails, run `modavd ksu <i> uninstall` to restore stock boot. The guest app and data remain by default. `--purge --yes` removes only managed KernelSU-specific data and the app, leaving the shared module directory intact.

Third-party modules run as root inside the guest. Use trusted sources. Once KernelSU is enabled, the guest's existing root traces remain and KernelSU adds more, such as the KernelSU Manager app and its kernel module. Games and apps may detect them, and some games refuse to run or penalize the account.

Known issues:

- Once enabled, the instance takes a few extra seconds to shut down each time (`/data` times out unmounting while Android shuts down), and its next start replays the file system journal and runs an extra disk check. No data loss was seen in testing; the cause is still being investigated, see [ModAVD/ModAVD#309](https://github.com/ModAVD/ModAVD/issues/309).
- Once enabled, each system call is about 12–25 % slower; the effect on game frame rates has not been measured yet, see [ModAVD/ModAVD#311](https://github.com/ModAVD/ModAVD/issues/311). For performance measurements and benchmarks, use an instance without KernelSU.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -466,9 +466,14 @@ go test ./...

仅支持 Windows x86_64 的 Android 14 API 34 受支持镜像。先导入五个组件,再给单个实例启用。

- [ModAVDKernelSU](https://github.com/ModAVD/ModAVDKernelSU/releases) 的正式附件发布后,可在「电脑检查」下载,或运行 `modavd components fetch --kernelsu`。发布以前,管理器里不显示 KernelSU;用下一条导入自己编的模块以后,「电脑检查」和实例的「更多操作」里才有它。
- 在「电脑检查」下载,或运行 `modavd components fetch --kernelsu`。内核模块是 [ModAVDKernelSU](https://github.com/ModAVD/ModAVDKernelSU/releases) 的正式附件,按固定的大小和 SHA-256 核对。
- `modavd components import-ksu <文件或文件夹>…`。自编的 `kernelsu.ko` 需要额外传入已核对的 `--lkm-sha256 <SHA256>`,并匹配完整内核版本。
- `modavd ksu <i> install|status|modules`,或 `modavd ksu <i> module install <zip>… [--no-reboot]` 和 `modavd ksu <i> module remove <id> [--no-reboot]`。
- 启动异常时用 `modavd ksu <i> uninstall` 恢复原版启动。默认保留客体应用和数据。`--purge --yes` 只清理该功能管理的 KernelSU 专用数据和应用,保留共享模块目录。

第三方模块在客体内以 root 运行,请只使用可信来源。启用以后,客体原有的 root 痕迹还在,还会多出 KernelSU 管理器应用、内核模块等痕迹,游戏和应用可能检测到,有的游戏会因此拒绝运行或处罚账号。

已知问题:

- 启用以后,实例每次关机会多等几秒(安卓关机时 `/data` 卸载超时),下次开机会回放文件系统日志、多做一遍磁盘检查。验证时没有看到丢数据,原因还在查,见 [ModAVD/ModAVD#309](https://github.com/ModAVD/ModAVD/issues/309)。
- 启用以后,每次系统调用慢约 12–25 %,对游戏帧率的影响还没量,见 [ModAVD/ModAVD#311](https://github.com/ModAVD/ModAVD/issues/311)。量性能、跑分时请用没启用 KernelSU 的实例。
4 changes: 3 additions & 1 deletion internal/cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,9 @@ func init() {
details: "KernelSU 的五个文件(kernelsu.ko、ksuinit、ksud、KernelSU.apk、meta-overlayfs.zip)要先用 components import-ksu 导入到根目录的 sdk\\kernelsu。" +
"install 会为该实例单独生成带 ksuinit 和 kernelsu.ko 的 ramdisk(不改全局系统镜像),冷启动装入 ksud、KernelSU Manager 和 meta-overlayfs,再冷启动一次使其生效。" +
"uninstall 移除实例的 KernelSU ramdisk 恢复原版启动(加 --purge --yes 清理受管理的 KernelSU 数据和管理器应用)。" +
"module install 支持一次导入多个 .zip 模块(如 ReZygisk 与 Vector/LSPosed),自动按依赖顺序刷入并冷启动一次生效;加 --no-reboot 可暂不重启。",
"module install 支持一次导入多个 .zip 模块(如 ReZygisk 与 Vector/LSPosed),自动按依赖顺序刷入并冷启动一次生效;加 --no-reboot 可暂不重启。" +
"已知问题:启用以后实例每次关机会多等几秒,下次开机会多做一遍磁盘检查,原因还在查(ModAVD/ModAVD#309);" +
"启用以后系统调用会变慢,这台实例的性能会比不启用时低一些(ModAVD/ModAVD#311)。",
flags: []flagSpec{
{name: "yes", kind: kindBool, help: "确认清理 KernelSU 客体数据"},
{name: "purge", kind: kindBool, help: "uninstall 时同时清理受管理的 KernelSU 数据和 KernelSU Manager"},
Expand Down
8 changes: 8 additions & 0 deletions internal/cli/kernelsu.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ func kernelSUStateLine(s components.KernelSUStatus) string {
case len(missing) < len(s.Files):
state = " 不全,缺 " + strings.Join(missing, "、")
}
if s.DownloadAvailable {
return "✗ KernelSU" + state + "(可选;用 components fetch --kernelsu 或 components import-ksu <文件或文件夹> 补全)"
}
return "✗ KernelSU" + state + "(可选;用 components import-ksu <文件或文件夹> 导入)"
}

Expand Down Expand Up @@ -237,6 +240,11 @@ func formatKernelSUResult(w io.Writer, r *emu.KernelSUResult) {
fmt.Fprintf(w, "(%s)", r.Version)
}
fmt.Fprintln(w)
if r.Installed {
// ModAVD/ModAVD#309, #311: said after every successful install, not warnings of this run (--json unchanged)
fmt.Fprintln(w, "已知问题:启用 KernelSU 的实例每次关机会多等几秒,下次开机会多做一遍磁盘检查;原因还在查,见 https://github.com/ModAVD/ModAVD/issues/309")
fmt.Fprintln(w, "已知问题:启用 KernelSU 后系统调用会变慢,这台实例的性能会比不启用时低一些,见 https://github.com/ModAVD/ModAVD/issues/311")
}
case "uninstall":
fmt.Fprintf(w, "实例 %d 已移除 KernelSU\n", r.Index)
default:
Expand Down
26 changes: 25 additions & 1 deletion internal/cli/kernelsu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestComponentsStatusAndImportKernelSU(t *testing.T) {
t.Fatalf("exit %d, kernelsu %v", res.code, data["kernelsu"])
}
res = h.run(t, nil, "components")
if !strings.Contains(res.stdout, "✗ KernelSU 未安装(可选") {
if !strings.Contains(res.stdout, "✗ KernelSU 未安装(可选") || !strings.Contains(res.stdout, "components fetch --kernelsu") {
t.Fatalf("human status missing KernelSU line: %s", res.stdout)
}

Expand Down Expand Up @@ -128,6 +128,30 @@ func TestKernelSUCommands(t *testing.T) {
}
}

// A successful install says the known shutdown and performance issues (ModAVD/ModAVD#309, #311), each on its own
// line; --json keeps its shape and warnings.
res := h.run(t, nil, "ksu", "2", "install")
if !strings.Contains(res.stdout, "实例 2 已启用 KernelSU") || !strings.Contains(res.stdout, "已知问题") ||
!strings.Contains(res.stdout, "issues/309") || strings.Contains(res.stdout, "警告") {
t.Fatalf("install output: %q", res.stdout)
}
if !strings.Contains(res.stdout, "\n已知问题:启用 KernelSU 后系统调用会变慢,这台实例的性能会比不启用时低一些,见 https://github.com/ModAVD/ModAVD/issues/311\n") ||
strings.Index(res.stdout, "issues/311") < strings.Index(res.stdout, "issues/309") {
t.Fatalf("install output lacks the performance issue line: %q", res.stdout)
}
if r := h.run(t, nil, "help", "ksu"); !strings.Contains(r.stdout, "ModAVD/ModAVD#309") ||
!strings.Contains(r.stdout, "系统调用会变慢") || !strings.Contains(r.stdout, "ModAVD/ModAVD#311") {
t.Fatalf("ksu help lacks the known issues: %q", r.stdout)
}
if res := h.run(t, nil, "ksu", "2", "status"); strings.Contains(res.stdout, "已知问题") {
t.Fatalf("status should not repeat the known issue: %q", res.stdout)
}
res = h.run(t, nil, "ksu", "2", "install", "--json")
data := envelope(t, res.stdout)["data"].(map[string]any)
if w, ok := data["warnings"].([]any); !ok || len(w) != 0 || strings.Contains(res.stdout, "已知问题") {
t.Fatalf("install --json: %s", res.stdout)
}

// Bad usage checks
if res := h.run(t, nil, "ksu", "2", "install", "--purge"); res.code != 2 {
t.Fatalf("expected usage error for install --purge, got %d", res.code)
Expand Down
96 changes: 96 additions & 0 deletions internal/components/kernelsu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,102 @@ func TestKernelSULKMAdvice(t *testing.T) {
}
}

// TestOfficialKernelSUProfilePinned: the built-in profile pins the official kernelsu.ko (size and SHA-256), so the
// official module can be downloaded and the manager offers KernelSU.
func TestOfficialKernelSUProfilePinned(t *testing.T) {
profile := kernelsu.CurrentProfile()
if !kernelSUDownloadReady(profile) {
t.Fatalf("the built-in KernelSU profile is not downloadable: %+v", profile.Files)
}
lkm := profile.Files[0]
if !lkm.IsLKM || lkm.Size != 259976 || lkm.SHA256 != "309e048bc2aebcfa493c2b43274f4298c9105438c3be48d23be50b93aa4557c6" ||
lkm.DownloadURL != kernelsu.OfficialLKMDownloadURL {
t.Fatalf("official LKM pin: %+v", lkm)
}
}

// TestOfficialKernelSURefusesOtherLKM: with the pin, a kernelsu.ko that is not the official one (here one built for
// the right kernel) is refused as not matching the pin; the command line says --lkm-sha256 trusts a self-built one,
// the page points at its download button and the command, and --lkm-sha256 with the module's digest takes it.
func TestOfficialKernelSURefusesOtherLKM(t *testing.T) {
profile := kernelsu.CurrentProfile()
data := makeSyntheticLKM(profile.KernelRelease + " SMP preempt mod_unload modversions")
ko := filepath.Join(t.TempDir(), "kernelsu.ko")
if err := os.WriteFile(ko, data, 0o644); err != nil {
t.Fatal(err)
}
for _, c := range []struct {
name string
fromPage bool
want []string
}{
{"command line", false, []string{"--lkm-sha256"}},
{"page", true, []string{"下载 KernelSU 组件", "modavd components import-ksu", "--lkm-sha256"}},
} {
_, err := classifyKernelSUImport([]ImportFile{{Path: ko}}, profile, profile.KernelRelease, "", c.fromPage)
if errs.CodeOf(err) != errs.ComponentsRejected {
t.Fatalf("%s: want the module refused, got %v", c.name, err)
}
e := errs.As(err)
if !strings.Contains(e.Message, "固定") {
t.Errorf("%s: message %q should say it does not match the pin", c.name, e.Message)
}
if strings.Contains(e.Advice, "尚未发布") {
t.Errorf("%s: advice %q still says the module is unreleased", c.name, e.Advice)
}
for _, w := range c.want {
if !strings.Contains(e.Advice, w) {
t.Errorf("%s: advice %q lacks %q", c.name, e.Advice, w)
}
}
}
sum := sha256.Sum256(data)
if _, err := classifyKernelSUImport([]ImportFile{{Path: ko}}, profile, profile.KernelRelease, hex.EncodeToString(sum[:]), false); err != nil {
t.Fatalf("--lkm-sha256 should trust the self-built module: %v", err)
}
}

// TestFetchKernelSUOfficialChecksPin: with the built-in profile the root offers the download, fetch asks for the
// official kernelsu.ko at its pinned size, and a download that is not that file is refused (no network: a stub).
func TestFetchKernelSUOfficialChecksPin(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("KernelSU import is Windows-only")
}
dir := testutil.TempDir(t)
bin := filepath.Join(dir, "sdk", "emulator", root.CurrentPlatform().Emulator)
if err := os.MkdirAll(filepath.Dir(bin), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(bin, []byte("emulator"), 0o644); err != nil {
t.Fatal(err)
}
r, err := root.New(dir)
if err != nil {
t.Fatal(err)
}
st := KernelSUState(r)
if !st.DownloadAvailable || st.Ready || st.Files[0].DownloadURL != kernelsu.OfficialLKMDownloadURL {
t.Fatalf("state with the built-in profile: %+v", st)
}
old := kernelSUDownloadFile
t.Cleanup(func() { kernelSUDownloadFile = old })
var asked []download.FileOptions
kernelSUDownloadFile = func(_ context.Context, _ *download.Client, opts download.FileOptions) error {
asked = append(asked, opts)
return os.WriteFile(opts.Path, make([]byte, opts.Size), 0o644) // the right size, not the official content
}
_, err = FetchKernelSU(context.Background(), r, Options{})
if errs.CodeOf(err) != errs.ComponentsRejected || !strings.Contains(errs.As(err).Message, "kernelsu.ko") {
t.Fatalf("a download that is not the official module: %v", err)
}
if len(asked) != 1 || asked[0].URL != kernelsu.OfficialLKMDownloadURL || asked[0].Size != kernelsu.OfficialLKMSize {
t.Fatalf("downloads %+v", asked)
}
if KernelSUState(r).Ready {
t.Fatal("a refused download should leave KernelSU not ready")
}
}

func TestFetchKernelSURequiresMatchingDownloads(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("KernelSU import is Windows-only")
Expand Down
5 changes: 4 additions & 1 deletion internal/i18n/locales/en/cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"Google Play 版镜像的压缩包": "Google Play system image archive",
"HTTP 代理": "HTTP proxy",
"KernelSU 的五个文件已经齐全、校验无误,没有改动(给的文件也核对过了)": "All five KernelSU files are already present and verified; nothing changed (the given files were also verified)",
"KernelSU 的五个文件(kernelsu.ko、ksuinit、ksud、KernelSU.apk、meta-overlayfs.zip)要先用 components import-ksu 导入到根目录的 sdk\\kernelsu。install 会为该实例单独生成带 ksuinit 和 kernelsu.ko 的 ramdisk(不改全局系统镜像),冷启动装入 ksud、KernelSU Manager 和 meta-overlayfs,再冷启动一次使其生效。uninstall 移除实例的 KernelSU ramdisk 恢复原版启动(加 --purge --yes 清理受管理的 KernelSU 数据和管理器应用)。module install 支持一次导入多个 .zip 模块(如 ReZygisk 与 Vector/LSPosed),自动按依赖顺序刷入并冷启动一次生效;加 --no-reboot 可暂不重启。": "Import the five KernelSU files (kernelsu.ko, ksuinit, ksud, KernelSU.apk, meta-overlayfs.zip) into sdk\\kernelsu first with components import-ksu. install builds a per-instance ramdisk containing ksuinit and kernelsu.ko without touching the global system image, cold boots to install ksud, KernelSU Manager, and meta-overlayfs, then cold boots once more to activate them. uninstall removes the instance's KernelSU ramdisk to restore stock boot (add --purge --yes to remove managed KernelSU data and the manager app). module install accepts multiple .zip modules at once (such as ReZygisk and Vector/LSPosed), installs them in dependency order, and cold boots once; pass --no-reboot to skip the reboot.",
"KernelSU 的五个文件(kernelsu.ko、ksuinit、ksud、KernelSU.apk、meta-overlayfs.zip)要先用 components import-ksu 导入到根目录的 sdk\\kernelsu。install 会为该实例单独生成带 ksuinit 和 kernelsu.ko 的 ramdisk(不改全局系统镜像),冷启动装入 ksud、KernelSU Manager 和 meta-overlayfs,再冷启动一次使其生效。uninstall 移除实例的 KernelSU ramdisk 恢复原版启动(加 --purge --yes 清理受管理的 KernelSU 数据和管理器应用)。module install 支持一次导入多个 .zip 模块(如 ReZygisk 与 Vector/LSPosed),自动按依赖顺序刷入并冷启动一次生效;加 --no-reboot 可暂不重启。已知问题:启用以后实例每次关机会多等几秒,下次开机会多做一遍磁盘检查,原因还在查(ModAVD/ModAVD#309);启用以后系统调用会变慢,这台实例的性能会比不启用时低一些(ModAVD/ModAVD#311)。": "Import the five KernelSU files (kernelsu.ko, ksuinit, ksud, KernelSU.apk, meta-overlayfs.zip) into sdk\\kernelsu first with components import-ksu. install builds a per-instance ramdisk containing ksuinit and kernelsu.ko without touching the global system image, cold boots to install ksud, KernelSU Manager, and meta-overlayfs, then cold boots once more to activate them. uninstall removes the instance's KernelSU ramdisk to restore stock boot (add --purge --yes to remove managed KernelSU data and the manager app). module install accepts multiple .zip modules at once (such as ReZygisk and Vector/LSPosed), installs them in dependency order, and cold boots once; pass --no-reboot to skip the reboot. Known issue: once enabled, the instance takes a few extra seconds to shut down each time and runs an extra disk check on its next start; the cause is still being investigated (ModAVD/ModAVD#309). Also, once enabled, system calls are slower, so the instance performs somewhat worse than without KernelSU (ModAVD/ModAVD#311).",
"MCP 服务已就绪,根目录 %s": "MCP server is ready, root folder %s",
"Mirror 酱(CDK %v)": "MirrorChyan (CDK %v)",
"ModAVD 实例 %d 关机失败: %s": "ModAVD instance %d failed to shut down: %s",
Expand Down Expand Up @@ -120,6 +120,7 @@
"✓ Google Play 商店": "✓ Google Play Store",
"✗ Google Play 商店%v(可选;用 components fetch --play-store 或 components import-playstore <文件> 补全)": "✗ Google Play Store%v (optional; add it with components fetch --play-store or components import-playstore <file>)",
"✗ KernelSU%v(可选;用 components import-ksu <文件或文件夹> 导入)": "✗ KernelSU%v (optional; import with components import-ksu <file or folder>)",
"✗ KernelSU%v(可选;用 components fetch --kernelsu 或 components import-ksu <文件或文件夹> 补全)": "✗ KernelSU%v (optional; add it with components fetch --kernelsu or components import-ksu <file or folder>)",
"一台实例的状态": "Status of one instance",
"一台实例的详细信息": "Details of one instance",
"一次新建 N 台(1–9),按编号从小到大一台做完再建下一台;不能和 --index、--resume 一起用": "Create N instances (1–9), one after another in number order; not together with --index or --resume",
Expand Down Expand Up @@ -225,6 +226,8 @@
"实例 %d 已刷入 %d 个 KernelSU 模块:\n": "Flashed %[2]d KernelSU modules into instance %[1]d:\n",
"实例 %d 已卸载 KernelSU 模块:%s\n": "Uninstalled KernelSU module %[2]s from instance %[1]d\n",
"实例 %d 已启用 KernelSU": "Enabled KernelSU on instance %d",
"已知问题:启用 KernelSU 的实例每次关机会多等几秒,下次开机会多做一遍磁盘检查;原因还在查,见 https://github.com/ModAVD/ModAVD/issues/309": "Known issue: an instance with KernelSU enabled takes a few extra seconds to shut down each time and runs an extra disk check on its next start; the cause is still being investigated, see https://github.com/ModAVD/ModAVD/issues/309",
"已知问题:启用 KernelSU 后系统调用会变慢,这台实例的性能会比不启用时低一些,见 https://github.com/ModAVD/ModAVD/issues/311": "Known issue: with KernelSU enabled, system calls are slower, so the instance performs somewhat worse than without it; see https://github.com/ModAVD/ModAVD/issues/311",
"实例 %d 已移除 KernelSU\n": "Removed KernelSU from instance %d\n",
"实例 %d 已触发磁盘回收(trim)\n": "Instance %d disk reclaim (trim) triggered\n",
"实例 %d 执行桥 %s:%s(脚本退出码 %d)\n": "Instance %d execution bridge %s: %s (script exit code %d)\n",
Expand Down
Loading
Loading