From fc116a7bd08d76da98d6e4cfaad1f1b23edff16f Mon Sep 17 00:00:00 2001 From: Cheek Xie Date: Wed, 18 Nov 2026 10:54:19 +0700 Subject: [PATCH] sys: pass -y to sysupgrade and factory-reset from web UI (Closes #182) Co-Authored-By: Claude Code --- docs/modules/sys.md | 8 ++++---- mr/mod_sys_fw.go | 10 +++++----- mr/mod_sys_test.go | 16 +++++++++++++++- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/docs/modules/sys.md b/docs/modules/sys.md index c5c4ce3..814b555 100644 --- a/docs/modules/sys.md +++ b/docs/modules/sys.md @@ -615,8 +615,8 @@ ruleset uses conntrack), so there is no cost before the firewall. The mon module | `sys.restore` | POST | `{data (base64 tar.gz ≤ 2.9 MB), confirm: 60-600}` → starts the apply job; poll `job`, then `confirm` / `revert` as for any apply | | `sys.fw` | GET | `{sysupgrade, factory_reset (scripts present), tmp_free, max_image, max_chunk, upload {total, received}, run {state: "" \| running \| failed \| done \| stale, rc, message}}` | | `sys.fwupload` | POST | `{offset, total, data (base64 ≤ 2 MiB)}` → `{received}`; must continue exactly at the end of the file (409 + `received` otherwise, so a lost response can be resumed); `{cancel: true}` deletes it | -| `sys.fwupgrade` | POST | `{sha256}` — must match the uploaded file, then `/usr/libexec/mr/sysupgrade ` runs detached | -| `sys.factoryreset` | POST | `{confirm: "RESET"}` → `/usr/libexec/mr/factory-reset` runs detached | +| `sys.fwupgrade` | POST | `{sha256}` — must match the uploaded file, then `/usr/libexec/mr/sysupgrade -y ` runs detached | +| `sys.factoryreset` | POST | `{confirm: "RESET"}` → `/usr/libexec/mr/factory-reset -y` runs detached | Mutations are POST only. `sys.backup` returns secrets only when asked for (the user's own download), never the web UI password hash. The firmware image lives in `/tmp/mr-upgrade/` (created 0700 by root; @@ -676,10 +676,10 @@ warn); issued and failed certificates are events of type `cert`. ## Platform interface -* `/usr/libexec/mr/sysupgrade IMAGE` — IMAGE is `/tmp/mr-upgrade/firmware.img`; exit ≠ 0 = refused, +* `/usr/libexec/mr/sysupgrade -y IMAGE` — IMAGE is `/tmp/mr-upgrade/firmware.img`; exit ≠ 0 = refused, reason on stderr (shown in the web UI); on success it flashes and reboots. `/tmp` must be a tmpfs with room for the image (+8 MiB). -* `/usr/libexec/mr/factory-reset` — wipes the configuration and reboots. +* `/usr/libexec/mr/factory-reset -y` — wipes the configuration and reboots. * Boot runlevel must contain `modules` and `sysctl` (build/m2 does). ## Changes to the real home output (examples/router.yaml) diff --git a/mr/mod_sys_fw.go b/mr/mod_sys_fw.go index 665f2cf..6fd82b7 100644 --- a/mr/mod_sys_fw.go +++ b/mr/mod_sys_fw.go @@ -3,9 +3,9 @@ package main // sys module: firmware upgrade and factory reset (web UI). The platform ships the scripts that do // the real work: // -// /usr/libexec/mr/sysupgrade IMAGE checks and flashes IMAGE (a file in /tmp) and reboots; -// exit code != 0 = refused, reason on stderr -// /usr/libexec/mr/factory-reset wipes the configuration and reboots +// /usr/libexec/mr/sysupgrade -y IMAGE checks and flashes IMAGE (a file in /tmp) and reboots; +// exit code != 0 = refused, reason on stderr +// /usr/libexec/mr/factory-reset -y wipes the configuration and reboots // // When a script is missing the web UI shows "当前构建不支持". The image is uploaded in chunks // (the API body limit is 4 MiB): each POST carries {offset, total, data (base64)} and must continue @@ -255,7 +255,7 @@ func apiSysFwUpgrade(r apiReq) apiResp { } appendChangeLog("webui: firmware upgrade started (sha256 " + sum[:16] + "…)") logf("webui: firmware upgrade, image sha256 %s", sum) - if err := fwStart("sysupgrade", fwSysupgrade, fwImage()); err != nil { + if err := fwStart("sysupgrade", fwSysupgrade, "-y", fwImage()); err != nil { return errResp(500, "%v", err) } return apiResp{body: map[string]any{"ok": true, "sha256": sum}} @@ -281,7 +281,7 @@ func apiSysFactoryReset(r apiReq) apiResp { } appendChangeLog("webui: factory reset") logf("webui: factory reset") - if err := fwStart("factory-reset", fwFactoryReset); err != nil { + if err := fwStart("factory-reset", fwFactoryReset, "-y"); err != nil { return errResp(500, "%v", err) } return apiResp{body: map[string]any{"ok": true}} diff --git a/mr/mod_sys_test.go b/mr/mod_sys_test.go index 6fb59fb..9003c1b 100644 --- a/mr/mod_sys_test.go +++ b/mr/mod_sys_test.go @@ -637,7 +637,7 @@ func TestSysFirmwareUpload(t *testing.T) { if st := apiSysFw(apiReq{method: "GET"}).body.(map[string]any); st["sysupgrade"] != false { t.Error("sysupgrade reported without the script") } - os.WriteFile(fwSysupgrade, []byte("#!/bin/sh\necho \"refused: $1 is not an AX6000 image\" >&2\nexit 3\n"), 0755) + os.WriteFile(fwSysupgrade, []byte("#!/bin/sh\n[ \"$1\" = -y ] || { echo \"missing -y\" >&2; exit 2; }\necho \"refused: $2 is not an AX6000 image\" >&2\nexit 3\n"), 0755) if r := post(apiSysFwUpload, chunk(0, 4000)); r.status != 0 { t.Fatalf("first chunk: %v", r.body) } @@ -685,6 +685,20 @@ func TestSysFirmwareUpload(t *testing.T) { if r := post(apiSysFactoryReset, map[string]any{"confirm": "RESET"}); r.status != 501 { t.Errorf("factory reset without the script: %d", r.status) } + os.WriteFile(fwFactoryReset, []byte("#!/bin/sh\n[ \"$1\" = -y ] || { echo \"missing -y\" >&2; exit 2; }\necho \"ok\"\n"), 0755) + if r := post(apiSysFactoryReset, map[string]any{"confirm": "RESET"}); r.status != 0 { + t.Fatalf("factory reset: %v", r.body) + } + for i := 0; i < 50; i++ { + st = fwRunStatus() + if st["state"] != "running" { + break + } + time.Sleep(100 * time.Millisecond) + } + if st["state"] != "done" || st["rc"] != 0 { + t.Errorf("factory reset run status: %v", st) + } if r := post(apiSysFwUpload, map[string]any{"cancel": true}); r.status != 0 { t.Error("cancel") }