fix(vm): preserve Guest Tools during direct CLI launches

This commit is contained in:
Nwflower committed 2026-12-01 05:53:56 -07:00
1 parent d573104072
commit 8d94d7976c
6 files changed
+52 -11

No files matched your search

+1
View File
@@ -142,6 +142,7 @@ func Definition(ver string, cb builtin.HostCallbacks, desired bool) plugin.Plugi
Activate: func(ctx context.Context, sc *plugin.Scope) error {
drv := macosdriver.NewReimsMacOSDriver(cb.GetConfig, macosdriver.Options{
OwnerID: ID,
ManagedByDesktop: cb.StartColdReboot != nil,
WHPXFallbackEnabled: func() bool { return settingOn(cb, SettingWHPXFallback) },
ColdRebootEnabled: func() bool { return settingOn(cb, SettingWin10ColdReboot) },
MigrateLedger: func(l *plugin.ArgLedger) { MigrateLedger(l) },
+1
View File
@@ -34,6 +34,7 @@ var ReimsMacOSCapabilities = []string{
// as the compiled-in `builtin.driver.reims-macos` extension (§3.2, §5.3).
type Options struct {
OwnerID string
ManagedByDesktop bool
WHPXFallbackEnabled func() bool
ColdRebootEnabled func() bool
MigrateLedger func(*plugin.ArgLedger)
+11 -5
View File
@@ -18,6 +18,7 @@ import (
"github.com/Nwflower/reims-go/internal/macos/reims"
"github.com/Nwflower/reims-go/internal/macos/startprep"
"github.com/Nwflower/reims-go/internal/plugin"
"github.com/Nwflower/reims-go/internal/plugin/builtin"
"github.com/Nwflower/reims-go/internal/qmp"
rt "github.com/Nwflower/reims-go/internal/runtime"
"github.com/Nwflower/reims-go/internal/vmstart"
@@ -92,7 +93,7 @@ func (d *ReimsMacOSDriver) Start(ctx context.Context, req driver.StartRequest) (
if d.opts.MigrateLedger != nil {
d.opts.MigrateLedger(ledger)
}
baseArgs := stripLegacyManagedExtraArgs(cur.ExtraArgs)
baseArgs := startBaseArgs(cur.ExtraArgs, d.opts.ManagedByDesktop, req.ActivePlugins)
userExtraArgs := plugin.UserExtraArgs(baseArgs, ledger)
ownerIntent, err := d.prepareStart(ctx, cfg, c, active, ref.Name, cur, rep, userExtraArgs, stdout)
if err != nil {
@@ -108,7 +109,7 @@ func (d *ReimsMacOSDriver) Start(ctx context.Context, req driver.StartRequest) (
if active.Manifest.HasCapability("display-fixed-mode") && host.ShouldUseFixedGuestMode(cur.Borderless, cur.Resolution, rep.PrimaryDisplay) {
c.Env = append(c.Env, "REIMS_VGPU_WINDOW_FIXED_MODE=1")
}
if active.Manifest.HasCapability("client-window-close") {
if d.opts.ManagedByDesktop && active.Manifest.HasCapability("client-window-close") {
c.Env = append(c.Env, vmstart.CloseRequestEnv(ref.Name)...)
}
if reconciled.Changed {
@@ -217,7 +218,7 @@ func (d *ReimsMacOSDriver) prepareWHPXFallback(cfg config.Config, active rt.Inst
}
func (d *ReimsMacOSDriver) prepareColdReboot(cfg config.Config, name, vmDir string, rep host.Report, in *plugin.StartIntent) error {
if !rep.NeedsColdRebootWorkaround || !optionEnabled(d.opts.ColdRebootEnabled) {
if !d.opts.ManagedByDesktop || !rep.NeedsColdRebootWorkaround || !optionEnabled(d.opts.ColdRebootEnabled) {
in.ClearKeys = append(in.ClearKeys, keyWin10ColdReboot)
return nil
}
@@ -262,8 +263,13 @@ func (d *ReimsMacOSDriver) prepareHostMemorySMBIOS(cfg config.Config, rep host.R
})
}
func stripLegacyManagedExtraArgs(extraArgs string) string {
s := reims.StripGuestToolsArgs(extraArgs)
func startBaseArgs(extraArgs string, managedByDesktop bool, active map[string]bool) string {
s := extraArgs
// The CLI has no bridge callback to replace the disk it sees in the VM
// configuration. Keep it attached if Guest Tools is still enabled.
if managedByDesktop || (active != nil && !active[builtin.IDGuestTools]) {
s = reims.StripGuestToolsArgs(s)
}
s = reims.StripOfflineHelperArgs(s)
s = qmp.StripLegacyWin10RebootArgs(s)
s = reims.StripIncomingDeferArgs(s)
+30 -1
View File
@@ -2,12 +2,15 @@ package macosdriver
import (
"slices"
"strings"
"testing"
"github.com/Nwflower/reims-go/internal/config"
"github.com/Nwflower/reims-go/internal/host"
"github.com/Nwflower/reims-go/internal/macos/reims"
"github.com/Nwflower/reims-go/internal/plugin"
"github.com/Nwflower/reims-go/internal/plugin/builtin"
"github.com/Nwflower/reims-go/internal/qmp"
rt "github.com/Nwflower/reims-go/internal/runtime"
)
@@ -19,9 +22,35 @@ func argKeys(in plugin.StartIntent) []string {
return keys
}
func TestDirectStartKeepsGuestToolsAndClearsDesktopReboot(t *testing.T) {
const owner = "builtin.guest.macos"
cfg := config.Config{DataRoot: t.TempDir()}
rep := host.Report{NeedsColdRebootWorkaround: true}
root := cfg.DataRoot
tools := reims.MergeGuestToolsArgs("-cpu host", "tools.img")
attached := qmp.MergeWin10RebootUnixArgs(tools, qmp.RebootSocketPath(root, cfg.TempDir(), "mac"))
d := &ReimsMacOSDriver{opts: Options{OwnerID: owner}}
intent := plugin.StartIntent{PluginID: owner}
if err := d.prepareColdReboot(cfg, "mac", root, rep, &intent); err != nil {
t.Fatal(err)
}
if len(intent.PostStart) != 0 || !slices.Contains(intent.ClearKeys, keyWin10ColdReboot) {
t.Fatalf("direct start cold-reboot intent = %+v", intent)
}
for _, enabled := range []bool{true, false} {
active := map[string]bool{owner: true, builtin.IDGuestTools: enabled}
base := startBaseArgs(attached, false, active)
out := plugin.ReconcileStartIntentsForDriver(base, &plugin.ArgLedger{}, active, owner, []plugin.StartIntent{intent})
if strings.Contains(out.ExtraArgs, "-no-reboot") || !strings.Contains(out.ExtraArgs, "-cpu host") ||
reims.HasGuestToolsArgs(out.ExtraArgs) != enabled {
t.Fatalf("Guest Tools enabled=%v: unexpected launch args %q", enabled, out.ExtraArgs)
}
}
}
func TestPrepareWorkaroundsHonourSettingsAndRuntime(t *testing.T) {
whpx, cold := true, true
d := &ReimsMacOSDriver{opts: Options{OwnerID: "test.macos",
d := &ReimsMacOSDriver{opts: Options{OwnerID: "test.macos", ManagedByDesktop: true,
WHPXFallbackEnabled: func() bool { return whpx }, ColdRebootEnabled: func() bool { return cold }}}
rep := host.Report{WHPXCaps: host.WHPXCapabilities{KernelIrqchipAvailable: true, NestedVirtProperty: false}}
var in plugin.StartIntent
+7 -3
View File
@@ -100,7 +100,7 @@ func (d *ReimsMacOSDriver) resumeLocked(ctx context.Context, c *reims.Client, ac
cfg := d.getConfig()
rep := host.Inspect(cfg.DataRoot)
var qmpAddr string
if cur.State == "stopped" && reims.HasSuspendState(cur.Directory) && rep.NeedsColdRebootWorkaround {
if d.opts.ManagedByDesktop && cur.State == "stopped" && reims.HasSuspendState(cur.Directory) && rep.NeedsColdRebootWorkaround {
sockPath := qmp.RebootSocketPath(cur.Directory, cfg.TempDir(), cur.Name)
if qmp.HasWin10RebootUnixArgs(cur.ExtraArgs, sockPath) {
if err := os.Remove(sockPath); err != nil && !os.IsNotExist(err) {
@@ -115,7 +115,7 @@ func (d *ReimsMacOSDriver) resumeLocked(ctx context.Context, c *reims.Client, ac
if active.Manifest.HasCapability("display-fixed-mode") && host.ShouldUseFixedGuestMode(cur.Borderless, cur.Resolution, rep.PrimaryDisplay) {
c.Env = append(c.Env, "REIMS_VGPU_WINDOW_FIXED_MODE=1")
}
if active.Manifest.HasCapability("client-window-close") {
if d.opts.ManagedByDesktop && active.Manifest.HasCapability("client-window-close") {
c.Env = append(c.Env, vmstart.CloseRequestEnv(cur.Name)...)
}
prep, err := startprep.New(cfg, func(line string) { startLog(stdout, line) })
@@ -123,7 +123,11 @@ func (d *ReimsMacOSDriver) resumeLocked(ctx context.Context, c *reims.Client, ac
return driver.StartResult{}, err
}
prep.Client, prep.Runtime = c, active
if err := prep.ResumeSnapshot(ctx, cur, rep, stdout, stderr); err != nil {
launchArgs := cur.ExtraArgs
if !d.opts.ManagedByDesktop {
launchArgs = qmp.StripLegacyWin10RebootArgs(launchArgs)
}
if err := prep.ResumeSnapshot(ctx, cur, launchArgs, rep, stdout, stderr); err != nil {
return driver.StartResult{}, err
}
var res driver.StartResult
+2 -2
View File
@@ -146,7 +146,7 @@ func cleanupSuspendAttempt(tmp string, err error) error {
// ResumeSnapshot owns the incoming launch and snapshot consumption while
// the caller holds LockVM. The original configuration must be restored
// before guest execution can continue.
func (p *Prep) ResumeSnapshot(ctx context.Context, d reims.VMDetail, rep host.Report, out, stderr io.Writer) error {
func (p *Prep) ResumeSnapshot(ctx context.Context, d reims.VMDetail, launchArgs string, rep host.Report, out, stderr io.Writer) error {
if err := p.checkSuspendRuntime(d); err != nil {
return err
}
@@ -167,7 +167,7 @@ func (p *Prep) ResumeSnapshot(ctx context.Context, d reims.VMDetail, rep host.Re
return err
}
c := p.Client
incoming := reims.MergeIncomingDeferArgs(d.ExtraArgs)
incoming := reims.MergeIncomingDeferArgs(launchArgs)
if err := c.Set(ctx, reims.SetOpts{Name: d.Name, ExtraArgs: &incoming}, out, stderr); err != nil {
return err
}