Delete functions that no product code calls, and fold "simple plus WithOptions" pairs into the one signature callers actually use. Rename the live macOS lifecycle functions that were labelled Legacy. The architecture tests now share one source loader and reject functions without a product caller and catalog messages nobody reads. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
415 lines
12 KiB
Go
415 lines
12 KiB
Go
package macosdriver
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"path/filepath"
|
|
|
|
"github.com/Nwflower/reims-go/internal/config"
|
|
"github.com/Nwflower/reims-go/internal/driver"
|
|
"github.com/Nwflower/reims-go/internal/host"
|
|
"github.com/Nwflower/reims-go/internal/macos/reims"
|
|
"github.com/Nwflower/reims-go/internal/macos/startprep"
|
|
"github.com/Nwflower/reims-go/internal/plugin"
|
|
rt "github.com/Nwflower/reims-go/internal/runtime"
|
|
"github.com/Nwflower/reims-go/internal/vmstart"
|
|
)
|
|
|
|
// ReimsMacOSCapabilities lists all capabilities supported by builtin.driver.reims-macos.
|
|
var ReimsMacOSCapabilities = []string{
|
|
driver.CapSnapshot,
|
|
driver.CapCowClone,
|
|
driver.CapResize,
|
|
driver.CapQMP,
|
|
driver.CapSSH,
|
|
driver.CapGuestTools,
|
|
driver.CapAffinity,
|
|
driver.CapOpenCore,
|
|
driver.CapRecovery,
|
|
}
|
|
|
|
// ReimsMacOSDriver adapts internal/macos/reims into the generic driver.Driver interface
|
|
// 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)
|
|
PrepareGuestToolsImage func(string, string) error
|
|
PrepareNetwork func(context.Context, string, bool) (func(), error)
|
|
}
|
|
|
|
type ReimsMacOSDriver struct {
|
|
getConfig func() config.Config
|
|
opts Options
|
|
}
|
|
|
|
var _ driver.Driver = (*ReimsMacOSDriver)(nil)
|
|
|
|
// NewReimsMacOSDriver creates the built-in macOS driver adapter.
|
|
func NewReimsMacOSDriver(getConfig func() config.Config, options ...Options) *ReimsMacOSDriver {
|
|
var opts Options
|
|
if len(options) > 0 {
|
|
opts = options[0]
|
|
}
|
|
return &ReimsMacOSDriver{getConfig: getConfig, opts: opts}
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) ID() string { return driver.BuiltinReimsMacOSDriverID }
|
|
|
|
func (d *ReimsMacOSDriver) DisplayName() plugin.LocalizedText {
|
|
return plugin.LocalizedText{ZhCN: "macOS (Reims vGPU)", EnUS: "macOS (Reims vGPU)"}
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) RuntimeKind() string { return rt.DefaultKind }
|
|
func (d *ReimsMacOSDriver) RuntimeAPIRange() string { return "^1.0.0" }
|
|
func (d *ReimsMacOSDriver) Capabilities() []string {
|
|
return append([]string(nil), ReimsMacOSCapabilities...)
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) CreateSchema() *plugin.SettingsSchema {
|
|
return nil // Uses the native macOS CreateWizard UI
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) SettingsSchema() *plugin.SettingsSchema {
|
|
return nil // Uses the native macOS SettingsModal UI
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) client() (*reims.Client, error) {
|
|
c, _, err := d.startClient()
|
|
return c, err
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) startClient() (*reims.Client, rt.Installed, error) {
|
|
cfg := d.getConfig()
|
|
active, err := rt.ResolveForKind(&cfg, d.RuntimeKind())
|
|
if err != nil {
|
|
return nil, rt.Installed{}, err
|
|
}
|
|
if err := rt.CheckCompatibility(d.RuntimeKind(), d.RuntimeAPIRange(), *active); err != nil {
|
|
return nil, rt.Installed{}, err
|
|
}
|
|
return reims.NewClient(&cfg, *active), *active, nil
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) List(ctx context.Context) ([]driver.VMInfo, error) {
|
|
c, active, err := d.startClient()
|
|
if err != nil {
|
|
if errors.Is(err, rt.ErrNoActiveRuntime) {
|
|
registered, registryErr := reims.ListRegisteredVMs(d.getConfig().DataRoot)
|
|
if registryErr == nil && len(registered) == 0 {
|
|
return nil, nil
|
|
}
|
|
return nil, errors.Join(err, registryErr)
|
|
}
|
|
return nil, err
|
|
}
|
|
items, err := c.List(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
caps := d.vmCapabilities(active, "")
|
|
out := make([]driver.VMInfo, 0, len(items))
|
|
for _, it := range items {
|
|
state := it.State
|
|
if state == "stopped" && reims.HasSuspendState(it.Dir) {
|
|
state = "suspended"
|
|
}
|
|
out = append(out, driver.VMInfo{
|
|
Name: it.Name,
|
|
DriverID: d.ID(),
|
|
Directory: it.Dir,
|
|
DriverName: "macOS (Reims vGPU)",
|
|
State: state,
|
|
PID: it.PID,
|
|
Capabilities: append([]string(nil), caps...),
|
|
GuestOS: macOSGuestOS(),
|
|
})
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func macOSGuestOS() *driver.GuestOSInfo {
|
|
return &driver.GuestOSInfo{Family: driver.GuestFamilyMacOS, Arch: "x86_64"}
|
|
}
|
|
|
|
// PasteCombo is the key combination that pastes inside a macOS guest.
|
|
func (d *ReimsMacOSDriver) PasteCombo() string { return "meta_l+v" }
|
|
|
|
// ConsoleEndpoint tells the host to reach the console through its QMP proxy.
|
|
func (d *ReimsMacOSDriver) ConsoleEndpoint() string { return "qmp" }
|
|
|
|
// Keymap is empty: the macOS guest uses the host's default US layout mapping.
|
|
func (d *ReimsMacOSDriver) Keymap() string { return "" }
|
|
|
|
var (
|
|
_ driver.PhaseReporter = (*ReimsMacOSDriver)(nil)
|
|
_ driver.DriverMetadata = (*ReimsMacOSDriver)(nil)
|
|
)
|
|
|
|
// Phase reports recovery or installer when the VM boots from them.
|
|
func (d *ReimsMacOSDriver) Phase(ctx context.Context, name string) (string, error) {
|
|
c, err := d.client()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
det, err := c.Show(ctx, name)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return macOSPhase(det), nil
|
|
}
|
|
|
|
func macOSPhase(det reims.VMDetail) string {
|
|
marker := startprep.ReadRecoveryMarker(det.Directory)
|
|
switch {
|
|
case det.RecoveryMode || marker == "on" || marker == "want":
|
|
return driver.PhaseRecovery
|
|
case det.HasInstaller:
|
|
return driver.PhaseInstaller
|
|
default:
|
|
return driver.PhaseNormal
|
|
}
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) Show(ctx context.Context, name string) (driver.VMInfo, error) {
|
|
c, active, err := d.startClient()
|
|
if err != nil {
|
|
return driver.VMInfo{}, err
|
|
}
|
|
det, err := c.Show(ctx, name)
|
|
if err != nil {
|
|
return driver.VMInfo{}, err
|
|
}
|
|
enrichSuspendDetail(&det)
|
|
marker := startprep.ReadRecoveryMarker(det.Directory)
|
|
det.RecoveryMode = marker == "on" || marker == "want"
|
|
rawBytes, _ := json.Marshal(det)
|
|
var data map[string]any
|
|
_ = json.Unmarshal(rawBytes, &data)
|
|
info := driver.VMInfo{
|
|
Name: det.Name,
|
|
DriverID: d.ID(),
|
|
DriverName: "macOS (Reims vGPU)",
|
|
State: det.State,
|
|
CPUs: det.CPUs,
|
|
Memory: det.Memory,
|
|
DiskSummary: det.Disk,
|
|
DiskPath: det.DiskPath(),
|
|
Directory: det.Directory,
|
|
Network: det.Network,
|
|
SSHPort: det.SSHPort,
|
|
Capabilities: d.vmCapabilities(active, det.Kind),
|
|
DriverData: data,
|
|
GuestOS: macOSGuestOS(),
|
|
Phase: macOSPhase(det),
|
|
}
|
|
driver.SyncSSHEndpoint(&info)
|
|
return info, nil
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) ValidateCreate(_ context.Context, req driver.CreateRequest) []driver.ValidationIssue {
|
|
var opts reims.CreateOpts
|
|
if err := decodeMap(req.Options, &opts); err != nil {
|
|
return []driver.ValidationIssue{{Field: "options", Message: err.Error()}}
|
|
}
|
|
if opts.Name == "" {
|
|
opts.Name = req.Name
|
|
}
|
|
if opts.Dir == "" {
|
|
opts.Dir = req.Dir
|
|
}
|
|
cfg := d.getConfig()
|
|
rep := host.Inspect(cfg.DataRoot)
|
|
issues := reims.ValidateCreate(opts, rep, cfg.Locale)
|
|
out := make([]driver.ValidationIssue, 0, len(issues))
|
|
for _, is := range issues {
|
|
out = append(out, driver.ValidationIssue{Field: is.Field, Message: is.Message})
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) Create(ctx context.Context, req driver.CreateRequest, stdout, stderr io.Writer) error {
|
|
c, err := d.client()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var opts reims.CreateOpts
|
|
if err := decodeMap(req.Options, &opts); err != nil {
|
|
return err
|
|
}
|
|
if opts.Name == "" {
|
|
opts.Name = req.Name
|
|
}
|
|
if opts.Dir == "" {
|
|
opts.Dir = req.Dir
|
|
}
|
|
if err := c.Create(ctx, opts, stdout, stderr); err != nil {
|
|
return err
|
|
}
|
|
vmDir := opts.Dir
|
|
if det, err := c.Show(ctx, opts.Name); err == nil && det.Directory != "" {
|
|
vmDir = det.Directory
|
|
}
|
|
if vmDir == "" {
|
|
cfg := d.getConfig()
|
|
vmDir = filepath.Join(cfg.XDGDataDir(), "reims-ctl", "vms", opts.Name)
|
|
}
|
|
return driver.WriteOwnerDriver(vmDir, d.ID())
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) ValidateSet(ctx context.Context, req driver.SetRequest) []driver.ValidationIssue {
|
|
var opts reims.SetOpts
|
|
if err := decodeMap(req.Changes, &opts); err != nil {
|
|
return []driver.ValidationIssue{{Field: "changes", Message: err.Error()}}
|
|
}
|
|
opts.Name = req.Name
|
|
var cur reims.VMDetail
|
|
if c, err := d.client(); err == nil {
|
|
if det, err := c.Show(ctx, req.Name); err == nil {
|
|
cur = det
|
|
}
|
|
}
|
|
cfg := d.getConfig()
|
|
rep := host.Inspect(cfg.DataRoot)
|
|
issues := reims.ValidateSet(opts, cur, rep, cfg.Locale)
|
|
out := make([]driver.ValidationIssue, 0, len(issues))
|
|
for _, is := range issues {
|
|
out = append(out, driver.ValidationIssue{Field: is.Field, Message: is.Message})
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) Set(ctx context.Context, req driver.SetRequest, stdout, stderr io.Writer) error {
|
|
c, err := d.client()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var opts reims.SetOpts
|
|
if err := decodeMap(req.Changes, &opts); err != nil {
|
|
return err
|
|
}
|
|
opts.Name = req.Name
|
|
cur, release, err := startprep.LockMutableVM(ctx, c, req.Name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer release()
|
|
if reims.HasCtlSetChanges(opts) {
|
|
if err := c.Set(ctx, opts, stdout, stderr); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if opts.NestedVirt != nil {
|
|
if err := vmstart.SaveVMNestedVirt(cur.Directory, vmstart.VMNestedVirtConfig{Enabled: *opts.NestedVirt}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if opts.GuestLang == "" && (cur.State == "running" || opts.Graphics == "" || opts.Graphics == cur.Graphics) {
|
|
return nil
|
|
}
|
|
prep, err := startprep.New(d.getConfig(), func(line string) {
|
|
if stdout != nil {
|
|
_, _ = fmt.Fprintln(stdout, line)
|
|
}
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if opts.GuestLang != "" {
|
|
return prep.ApplyGuestLang(ctx, cur.Directory, opts.GuestLang, cur.State == "running")
|
|
}
|
|
prep.ReplayGuestLang(ctx, cur.Directory)
|
|
return nil
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) Stop(ctx context.Context, name string, force bool, stdout, stderr io.Writer) error {
|
|
c, err := d.client()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
release, err := startprep.LockVMForStop(ctx, c, name, force)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer release()
|
|
if force {
|
|
return c.StopForce(ctx, name, stdout, stderr)
|
|
}
|
|
return c.Stop(ctx, name, stdout, stderr)
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) Delete(ctx context.Context, name string, keepFiles bool, stdout, stderr io.Writer) error {
|
|
c, err := d.client()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return c.Delete(ctx, name, keepFiles, stdout, stderr)
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) InvokeAction(ctx context.Context, vmName, action string, params map[string]any) (any, error) {
|
|
c, err := d.client()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
switch action {
|
|
case "eject":
|
|
_, release, err := startprep.LockMutableVM(ctx, c, vmName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer release()
|
|
return nil, c.Eject(ctx, vmName, io.Discard, io.Discard)
|
|
case "insert":
|
|
path, _ := params["path"].(string)
|
|
if path == "" {
|
|
return nil, fmt.Errorf("insert requires a non-empty path")
|
|
}
|
|
_, release, err := startprep.LockMutableVM(ctx, c, vmName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer release()
|
|
return nil, c.Insert(ctx, vmName, path, io.Discard, io.Discard)
|
|
case "enter_recovery":
|
|
return d.enterRecovery(ctx, vmName)
|
|
case "finish_install":
|
|
return d.finishInstall(ctx, vmName)
|
|
case "upgrade_nvme":
|
|
det, release, err := startprep.LockMutableVM(ctx, c, vmName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer release()
|
|
if det.HasInstaller {
|
|
return nil, fmt.Errorf("exit recovery mode before switching disk controller")
|
|
}
|
|
return nil, c.Set(ctx, reims.SetOpts{Name: vmName, DiskController: "nvme"}, io.Discard, io.Discard)
|
|
default:
|
|
return nil, fmt.Errorf("unknown macOS action %q", action)
|
|
}
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) ReadLogs(ctx context.Context, name string, serial bool) (string, error) {
|
|
c, err := d.client()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return c.ReadLogs(ctx, name, serial)
|
|
}
|
|
|
|
func decodeMap(m map[string]any, dst any) error {
|
|
if len(m) == 0 {
|
|
return nil
|
|
}
|
|
raw, err := json.Marshal(m)
|
|
if err != nil {
|
|
return fmt.Errorf("marshal options: %w", err)
|
|
}
|
|
return json.Unmarshal(raw, dst)
|
|
}
|