- Merge essential builtins (driver, default theme, start prep, host workarounds) into always-on system plugin builtin.core, with state.json and arg-ledger migration plus app-scoped workaround settings honored by both GUI and CLI. - Ensure disabling builtin.bridge.guest-tools stops the localhost bridge without being restarted by feature callers, fix guesttools.Server Start/Close race, and reflect the disabled state in VMPage and SettingsModal. - Add QQ community group entry in About settings with Windows tencent:// protocol detection, cached invite redirect resolution, and copy-ID fallback. - Update docs/plugin-architecture.md, Wails bindings, and i18n strings.
392 lines
13 KiB
Go
392 lines
13 KiB
Go
package builtin
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/Nwflower/reims-go/internal/driver"
|
|
"github.com/Nwflower/reims-go/internal/driver/reims"
|
|
"github.com/Nwflower/reims-go/internal/installer"
|
|
"github.com/Nwflower/reims-go/internal/opencore"
|
|
"github.com/Nwflower/reims-go/internal/plugin"
|
|
"github.com/Nwflower/reims-go/internal/qmp"
|
|
rt "github.com/Nwflower/reims-go/internal/runtime"
|
|
"github.com/Nwflower/reims-go/internal/vmstart"
|
|
)
|
|
|
|
// IDCore is the always-on builtin that holds everything Reims-Go needs to run
|
|
// macOS VMs: the reims driver, the default theme, start preparation and the
|
|
// host workarounds. Users can't switch it off; the two workarounds have their
|
|
// own settings for troubleshooting.
|
|
const IDCore = "builtin.core"
|
|
|
|
// Core app settings (x-scope "app").
|
|
const (
|
|
SettingWHPXFallback = "whpx_fallback"
|
|
SettingWin10ColdReboot = "win10_cold_reboot"
|
|
)
|
|
|
|
// RetiredIDs are the plugin IDs that were merged into IDCore. Their
|
|
// state.json records and arg-ledger owners are migrated on startup.
|
|
var RetiredIDs = []string{
|
|
IDThemeDefault,
|
|
driver.BuiltinReimsMacOSDriverID,
|
|
"builtin.compat.win10-cold-reboot",
|
|
"builtin.compat.whpx-fallback",
|
|
"builtin.prep.offline-helper",
|
|
"builtin.prep.recovery-boot",
|
|
"builtin.prep.guest-lang",
|
|
}
|
|
|
|
// retiredSettings maps a retired plugin that used to have its own switch to
|
|
// the core setting that replaced it.
|
|
var retiredSettings = map[string]string{
|
|
"builtin.compat.whpx-fallback": SettingWHPXFallback,
|
|
"builtin.compat.win10-cold-reboot": SettingWin10ColdReboot,
|
|
}
|
|
|
|
// MigrateRetired drops state.json records of plugins merged into the core and
|
|
// carries a switched-off workaround over to its core setting. Safe to call on
|
|
// every start; it only writes when an old record is still there.
|
|
func MigrateRetired(mgr *plugin.Manager) error {
|
|
removed, err := mgr.RetireRecords(RetiredIDs)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
off := map[string]any{}
|
|
for id, rec := range removed {
|
|
if key, ok := retiredSettings[id]; ok && rec != nil && !rec.Enabled {
|
|
off[key] = false
|
|
}
|
|
}
|
|
if len(off) == 0 {
|
|
return nil
|
|
}
|
|
return mgr.SetSettings(IDCore, "", off)
|
|
}
|
|
|
|
// MigrateLedger hands arg fragments owned by retired plugin IDs to the core,
|
|
// so the next start carries them forward instead of stripping and re-adding
|
|
// them. It reports whether the ledger changed.
|
|
func MigrateLedger(l *plugin.ArgLedger) bool {
|
|
return l.MergeOwners(RetiredIDs, IDCore)
|
|
}
|
|
|
|
// CoreSettingOn reads a boolean core setting from vals. A missing or
|
|
// non-boolean value counts as on, which is the default for both settings.
|
|
func CoreSettingOn(vals map[string]any, key string) bool {
|
|
if v, ok := vals[key].(bool); ok {
|
|
return v
|
|
}
|
|
return true
|
|
}
|
|
|
|
// CoreSettingOnInState reads a core setting from a state.json snapshot, for
|
|
// processes without a Manager. A workaround switched off under its retired
|
|
// plugin ID still counts as off until MigrateRetired has run.
|
|
func CoreSettingOnInState(st *plugin.StateFile, key string) bool {
|
|
if st == nil {
|
|
return true
|
|
}
|
|
for id, k := range retiredSettings {
|
|
if rec := st.Plugins[id]; k == key && rec != nil && !rec.Enabled {
|
|
return false
|
|
}
|
|
}
|
|
if rec := st.Plugins[IDCore]; rec != nil {
|
|
return CoreSettingOn(rec.AppSettings, key)
|
|
}
|
|
return true
|
|
}
|
|
|
|
func coreSettingOn(cb HostCallbacks, key string) bool {
|
|
if cb.GetSetting == nil {
|
|
return true
|
|
}
|
|
v, ok := cb.GetSetting(IDCore, "", key)
|
|
if !ok {
|
|
return true
|
|
}
|
|
return CoreSettingOn(map[string]any{key: v}, key)
|
|
}
|
|
|
|
func defCore(ver string, cb HostCallbacks) plugin.PluginDef {
|
|
return plugin.PluginDef{
|
|
Manifest: plugin.Manifest{
|
|
Schema: 1,
|
|
ID: IDCore,
|
|
Version: ver,
|
|
Name: plugin.LocalizedText{ZhCN: "Reims-Go 核心", EnUS: "Reims-Go Core"},
|
|
Description: plugin.LocalizedText{
|
|
ZhCN: "虚拟机驱动、默认外观、启动准备与宿主兼容补偿,始终启用",
|
|
EnUS: "VM driver, default look, start preparation and host workarounds; always on",
|
|
},
|
|
Publisher: "Reims-Go",
|
|
License: "Apache-2.0",
|
|
Permissions: []string{"driver:vm", "vm:control", "vm:intent", "qmp:write"},
|
|
DefaultEnabled: true,
|
|
Contributes: plugin.Contributes{
|
|
Themes: []plugin.ThemeContribution{{
|
|
ID: IDThemeDefault,
|
|
Name: plugin.LocalizedText{ZhCN: "默认主题", EnUS: "Default"},
|
|
Appearance: "both",
|
|
}},
|
|
},
|
|
Settings: &plugin.SettingsSchema{
|
|
Title: plugin.LocalizedText{ZhCN: "宿主兼容补偿", EnUS: "Host workarounds"},
|
|
Scope: "app",
|
|
Order: []string{SettingWHPXFallback, SettingWin10ColdReboot},
|
|
Properties: map[string]plugin.SettingProperty{
|
|
SettingWHPXFallback: {
|
|
Type: "boolean",
|
|
Title: plugin.LocalizedText{ZhCN: "WHPX 兼容降级", EnUS: "WHPX compatibility fallback"},
|
|
Description: plugin.LocalizedText{
|
|
ZhCN: "宿主不支持嵌套虚拟化属性时自动加 kernel-irqchip=off,避免首次启动崩溃。只在排查问题时关闭",
|
|
EnUS: "Adds kernel-irqchip=off when the host rejects the nested-virtualization property, avoiding a first-boot crash. Turn off only to troubleshoot",
|
|
},
|
|
Default: true,
|
|
Effect: "next_start",
|
|
Scope: "app",
|
|
},
|
|
SettingWin10ColdReboot: {
|
|
Type: "boolean",
|
|
Title: plugin.LocalizedText{ZhCN: "Windows 10 冷重启补偿", EnUS: "Windows 10 cold-reboot workaround"},
|
|
Description: plugin.LocalizedText{
|
|
ZhCN: "Windows 10 上 macOS 重启后自动重新启动虚拟机。只在排查问题时关闭",
|
|
EnUS: "On Windows 10, starts the VM again after macOS reboots. Turn off only to troubleshoot",
|
|
},
|
|
Default: true,
|
|
Effect: "next_start",
|
|
Scope: "app",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
Source: plugin.SourceBuiltin,
|
|
Trust: plugin.TrustOfficial,
|
|
System: true,
|
|
Activate: func(_ context.Context, sc *plugin.Scope) error {
|
|
drv := driver.NewReimsMacOSDriver(cb.GetConfig)
|
|
sc.Registry().RegisterGuestDriver(sc, IDDriverReimsMacOS, drv)
|
|
|
|
hooks := sc.Registry().Hooks()
|
|
hooks.Subscribe(sc, plugin.HookRegistration{
|
|
Event: plugin.EventHostPreflight,
|
|
Builtin: true,
|
|
Handler: func(_ context.Context, raw any) (any, error) {
|
|
p, ok := raw.(plugin.PreflightPayload)
|
|
if !ok || !coreSettingOn(cb, SettingWHPXFallback) {
|
|
return nil, nil
|
|
}
|
|
caps, _ := p.HostFacts["runtime.capabilities"].([]string)
|
|
if whpxFallbackNeeded(p.HostFacts) && !hasCap(caps, "nested-virt-nonfatal") {
|
|
return plugin.PreflightResult{
|
|
Advices: []string{"Host lacks nested-virt property and runtime lacks nested-virt-nonfatal capability; kernel-irqchip=off will be auto-applied"},
|
|
}, nil
|
|
}
|
|
return nil, nil
|
|
},
|
|
})
|
|
// One handler runs every start step in a fixed order. Guest language
|
|
// must follow the recovery step, which may rebuild OpenCore.
|
|
hooks.Subscribe(sc, plugin.HookRegistration{
|
|
Event: plugin.EventVMBeforeStart,
|
|
Builtin: true,
|
|
Handler: func(ctx context.Context, raw any) (any, error) {
|
|
p, ok := raw.(plugin.BeforeStartPayload)
|
|
if !ok || p.DriverID != IDDriverReimsMacOS {
|
|
return nil, nil
|
|
}
|
|
in := plugin.StartIntent{PluginID: IDCore}
|
|
stepOfflineHelper(cb, p, &in)
|
|
stepRecoveryBoot(ctx, cb, p, &in)
|
|
stepGuestLang(ctx, cb, p, &in)
|
|
stepWHPXFallback(cb, p, &in)
|
|
stepWin10ColdReboot(cb, p, &in)
|
|
return in, nil
|
|
},
|
|
})
|
|
hooks.Subscribe(sc, plugin.HookRegistration{
|
|
Event: plugin.EventVMAfterStart,
|
|
Builtin: true,
|
|
Handler: func(_ context.Context, raw any) (any, error) {
|
|
// QMPAddr is only set when stepWin10ColdReboot asked for the watcher.
|
|
ev, ok := raw.(plugin.VMEventPayload)
|
|
if ok && ev.QMPAddr != "" && cb.StartColdReboot != nil {
|
|
cb.StartColdReboot(ev.VMName, ev.QMPAddr)
|
|
}
|
|
return nil, nil
|
|
},
|
|
})
|
|
hooks.Subscribe(sc, plugin.HookRegistration{
|
|
Event: plugin.EventVMAfterStop,
|
|
Builtin: true,
|
|
Handler: func(_ context.Context, raw any) (any, error) {
|
|
ev, ok := raw.(plugin.VMEventPayload)
|
|
if ok && cb.CancelColdReboot != nil {
|
|
cb.CancelColdReboot(ev.VMName)
|
|
}
|
|
return nil, nil
|
|
},
|
|
})
|
|
return nil
|
|
},
|
|
}
|
|
}
|
|
|
|
// Arg-ledger keys owned by the core.
|
|
const (
|
|
keyOfflineHelper = "reimshelp"
|
|
keyKernelIrqchip = "kernel-irqchip"
|
|
keyWin10ColdReboot = "win10-cold-reboot"
|
|
)
|
|
|
|
// stepOfflineHelper attaches the REIMSHELP helper disk while an offline
|
|
// installer is attached and clears it otherwise.
|
|
func stepOfflineHelper(cb HostCallbacks, p plugin.BeforeStartPayload, in *plugin.StartIntent) {
|
|
if !p.HasInstaller || !reims.IsOfflineInstallerPath(p.InstallerPath) || cb.GetConfig == nil {
|
|
in.ClearKeys = append(in.ClearKeys, keyOfflineHelper)
|
|
return
|
|
}
|
|
cfg := cb.GetConfig()
|
|
helperPath, err := installer.EnsureOfflineHelperImage(cfg.XDGCacheDir(), p.InstallerPath)
|
|
if err != nil {
|
|
in.Warnings = append(in.Warnings, "offline install helper disk skipped: "+err.Error())
|
|
return
|
|
}
|
|
in.Args = append(in.Args, plugin.ArgFragment{
|
|
PluginID: IDCore,
|
|
Key: keyOfflineHelper,
|
|
Tokens: plugin.SplitShellTokens(reims.MergeOfflineHelperArgs("", helperPath)),
|
|
Builtin: true,
|
|
Priority: 85,
|
|
})
|
|
}
|
|
|
|
// stepRecoveryBoot applies a recovery-mode switch recorded while the VM was
|
|
// running; updating OpenCore needs a stopped VM.
|
|
func stepRecoveryBoot(ctx context.Context, cb HostCallbacks, p plugin.BeforeStartPayload, in *plugin.StartIntent) {
|
|
if p.VMDir == "" || cb.GetConfig == nil {
|
|
return
|
|
}
|
|
marker := vmstart.ReadRecoveryMarker(p.VMDir)
|
|
if marker != "want" && marker != "restore" {
|
|
return
|
|
}
|
|
cfg := cb.GetConfig()
|
|
active, err := rt.ResolveActive(&cfg)
|
|
if err != nil || active == nil {
|
|
return
|
|
}
|
|
show := marker == "want"
|
|
next := ""
|
|
if show {
|
|
next = "on"
|
|
}
|
|
w := vmstart.LineWriter(func(line string) {
|
|
if cb.Log != nil {
|
|
cb.Log(p.VMName, line)
|
|
}
|
|
})
|
|
if err := reims.NewClient(&cfg, *active).UpdateOpenCore(ctx, p.VMName, show, w, w); err != nil {
|
|
in.Warnings = append(in.Warnings, "could not update OpenCore boot picker: "+err.Error())
|
|
return
|
|
}
|
|
vmstart.WriteRecoveryMarker(p.VMDir, next)
|
|
}
|
|
|
|
// stepGuestLang writes the saved macOS language into OpenCore NVRAM. It runs
|
|
// after stepRecoveryBoot because rebuilding OpenCore drops it.
|
|
func stepGuestLang(ctx context.Context, cb HostCallbacks, p plugin.BeforeStartPayload, in *plugin.StartIntent) {
|
|
if p.VMDir == "" || cb.GetConfig == nil {
|
|
return
|
|
}
|
|
saved := opencore.ReadSavedGuestLang(p.VMDir)
|
|
if saved == "" {
|
|
return
|
|
}
|
|
ocPath := opencore.ResolveOpenCorePath(p.VMDir)
|
|
if ocPath == "" {
|
|
return
|
|
}
|
|
cfg := cb.GetConfig()
|
|
active, err := rt.ResolveActive(&cfg)
|
|
if err != nil || active == nil {
|
|
return
|
|
}
|
|
env := rt.BuildChildEnv(&cfg, *active)
|
|
if _, err := opencore.PatchImagePrevLang(ctx, ocPath, saved, active.QemuImgPath(), env); err != nil {
|
|
in.Warnings = append(in.Warnings, "could not apply macOS language to OpenCore: "+err.Error())
|
|
}
|
|
}
|
|
|
|
// whpxFallbackNeeded reports whether the host rejects the nested-virt property
|
|
// while kernel-irqchip=off is available and the cold-reboot path isn't used.
|
|
func whpxFallbackNeeded(facts map[string]any) bool {
|
|
needsCold, _ := facts["host.needs_cold_reboot"].(bool)
|
|
irqAvail, _ := facts["host.whpx_irqchip"].(bool)
|
|
nestedOK, _ := facts["host.whpx_nested_virt"].(bool)
|
|
return !needsCold && irqAvail && !nestedOK
|
|
}
|
|
|
|
func hasCap(caps []string, want string) bool {
|
|
for _, c := range caps {
|
|
if strings.EqualFold(c, want) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// stepWHPXFallback adds kernel-irqchip=off on hosts that reject the
|
|
// nested-virtualization property when the runtime can't tolerate that.
|
|
func stepWHPXFallback(cb HostCallbacks, p plugin.BeforeStartPayload, in *plugin.StartIntent) {
|
|
if !coreSettingOn(cb, SettingWHPXFallback) || !whpxFallbackNeeded(p.HostFacts) || hasCap(p.RuntimeCaps, "nested-virt-nonfatal") {
|
|
in.ClearKeys = append(in.ClearKeys, keyKernelIrqchip)
|
|
return
|
|
}
|
|
if reims.HasKernelIrqchipArg(p.ExtraArgs) {
|
|
return // already there: ours from an earlier start, or written by the user
|
|
}
|
|
in.Args = append(in.Args, plugin.ArgFragment{
|
|
PluginID: IDCore,
|
|
Key: keyKernelIrqchip,
|
|
Tokens: []string{"-machine", "kernel-irqchip=off"},
|
|
Builtin: true,
|
|
Priority: 90,
|
|
})
|
|
in.Warnings = append(in.Warnings, "Host rejects nested-virtualization property; enabled kernel-irqchip=off automatically")
|
|
}
|
|
|
|
// stepWin10ColdReboot adds -no-reboot with a private QMP socket on Windows 10
|
|
// hosts without WHvResetPartition and asks for the reboot watcher.
|
|
func stepWin10ColdReboot(cb HostCallbacks, p plugin.BeforeStartPayload, in *plugin.StartIntent) {
|
|
needs, _ := p.HostFacts["host.needs_cold_reboot"].(bool)
|
|
if !needs || !coreSettingOn(cb, SettingWin10ColdReboot) {
|
|
in.ClearKeys = append(in.ClearKeys, keyWin10ColdReboot)
|
|
return
|
|
}
|
|
tmpDir := ""
|
|
if cb.GetConfig != nil {
|
|
cfg := cb.GetConfig()
|
|
tmpDir = cfg.TempDir()
|
|
}
|
|
sockPath := qmp.RebootSocketPath(p.VMDir, tmpDir, p.VMName)
|
|
_ = os.MkdirAll(filepath.Dir(sockPath), 0o755)
|
|
_ = os.Remove(sockPath)
|
|
in.Args = append(in.Args, plugin.ArgFragment{
|
|
PluginID: IDCore,
|
|
Key: keyWin10ColdReboot,
|
|
Tokens: plugin.SplitShellTokens(qmp.MergeWin10RebootUnixArgs("", sockPath)),
|
|
Builtin: true,
|
|
Priority: 95,
|
|
})
|
|
in.PostStart = append(in.PostStart, plugin.PostStartAction{
|
|
PluginID: IDCore,
|
|
Kind: "qmp.watch_cold_reboot",
|
|
Params: map[string]any{"qmp_addr": "unix:" + sockPath},
|
|
})
|
|
}
|