* refactor: 收拢核心管线、强化错误处理、优化热路径与更新体积 * fix: preserve download recovery and bound PBZX decoding Keep VM diagnostics available during plugin initialization failures and verify settings through the unified driver lifecycle.
635 lines
20 KiB
Go
635 lines
20 KiB
Go
package macosdriver
|
|
|
|
import (
|
|
"cmp"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"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/i18n"
|
|
"github.com/Nwflower/reims-go/internal/macos/opencore"
|
|
"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)
|
|
det.AutoResolution = vmstart.SupportsAutoResolution(det.Graphics) && vmstart.ReadVMDisplay(det.Directory).AutoResolution
|
|
marker := startprep.ReadRecoveryMarker(det.Directory)
|
|
det.RecoveryMode = marker == "on" || marker == "want"
|
|
rawBytes, err := json.Marshal(det)
|
|
if err != nil {
|
|
return driver.VMInfo{}, fmt.Errorf("encode macOS VM detail: %w", err)
|
|
}
|
|
var data map[string]any
|
|
if err := json.Unmarshal(rawBytes, &data); err != nil {
|
|
return driver.VMInfo{}, fmt.Errorf("decode macOS VM detail: %w", err)
|
|
}
|
|
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)
|
|
var registered []reims.VMListItem
|
|
xdgDataDir := ""
|
|
if strings.TrimSpace(cfg.DataRoot) != "" {
|
|
xdgDataDir = cfg.XDGDataDir()
|
|
if regVMs, err := reims.ListRegisteredVMs(cfg.DataRoot); err == nil {
|
|
for _, vm := range regVMs {
|
|
registered = append(registered, reims.VMListItem{Name: vm.Name, Dir: vm.Directory})
|
|
}
|
|
}
|
|
}
|
|
if issue := reims.ValidateCreateTargetDir(xdgDataDir, opts, registered, cfg.Locale); issue != nil {
|
|
issues = append(issues, *issue)
|
|
}
|
|
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, active, err := d.startClient()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var opts reims.CreateOpts
|
|
if err := decodeMap(req.Options, &opts); err != nil {
|
|
return err
|
|
}
|
|
loc := i18n.EffectiveLocale(d.getConfig().Locale)
|
|
if !active.Manifest.HasCapability("win32-native-aio") {
|
|
if dcache := strings.TrimSpace(opts.DiskCache); dcache != "" && !strings.EqualFold(dcache, "default") {
|
|
return errors.New(i18n.T(loc, "validate.disk_cache_requires_runtime"))
|
|
}
|
|
if reims.IsWindowsRawDevicePath(opts.Disk) {
|
|
return errors.New(i18n.T(loc, "validate.raw_disk_requires_runtime"))
|
|
}
|
|
}
|
|
if opts.Name == "" {
|
|
opts.Name = req.Name
|
|
}
|
|
if opts.Dir == "" {
|
|
opts.Dir = req.Dir
|
|
}
|
|
applyAutoResolution := opts.AutoResolution != nil && *opts.AutoResolution && vmstart.SupportsAutoResolution(opts.Graphics)
|
|
if applyAutoResolution {
|
|
opts.Resolution, _ = host.RecommendAutoResolution(host.PrimaryDisplayOf(host.ConnectedDisplays()), opts.Borderless != nil && *opts.Borderless, c.DisplayFixedMode)
|
|
}
|
|
startAfterCreate := applyAutoResolution && opts.Start
|
|
if startAfterCreate {
|
|
opts.Start = false
|
|
}
|
|
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)
|
|
}
|
|
if err := driver.WriteOwnerDriver(vmDir, d.ID()); err != nil {
|
|
return err
|
|
}
|
|
if applyAutoResolution {
|
|
if _, err := vmstart.SaveVMAutoResolution(vmDir, true); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if startAfterCreate {
|
|
_, err := d.Start(ctx, driver.StartRequest{Ref: driver.NewVMRef(d.ID(), opts.Name), Stdout: stdout, Stderr: stderr})
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
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, active, err := d.startClient()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var opts reims.SetOpts
|
|
if err := decodeMap(req.Changes, &opts); err != nil {
|
|
return err
|
|
}
|
|
if strings.TrimSpace(opts.DiskCache) != "" && !active.Manifest.HasCapability("win32-native-aio") {
|
|
return errors.New(i18n.T(i18n.EffectiveLocale(d.getConfig().Locale), "validate.disk_cache_requires_runtime"))
|
|
}
|
|
opts.Name = req.Name
|
|
cur, release, err := startprep.LockMutableVM(ctx, c, req.Name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer release()
|
|
return ApplyVMOptions(ctx, c, cur, opts, stdout, stderr, func(cur reims.VMDetail) error {
|
|
live := cur.State == "running" || cur.State == "paused"
|
|
if opts.GuestLang == "" && (live || 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, live)
|
|
}
|
|
prep.ReplayGuestLang(ctx, cur.Directory)
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func autoResolutionAfterSet(cur reims.VMDetail, opts reims.SetOpts) bool {
|
|
if !vmstart.SupportsAutoResolution(cmp.Or(opts.Graphics, cur.Graphics)) {
|
|
return false
|
|
}
|
|
if opts.AutoResolution != nil {
|
|
return *opts.AutoResolution
|
|
}
|
|
return cur.AutoResolution && vmstart.SupportsAutoResolution(cur.Graphics) && strings.TrimSpace(opts.Resolution) == ""
|
|
}
|
|
|
|
type SettingsState struct {
|
|
CPUs, Displays int
|
|
Memory, Resolution, GPU, DiskController, DiskCache, AudioBackend, GuestLang string
|
|
AutoResolution, Borderless, NestedVirt, Audio bool
|
|
}
|
|
|
|
type SettingsOutcome struct {
|
|
Valid, Running bool
|
|
Before, After SettingsState
|
|
}
|
|
|
|
type settingsOutcomeKey struct{}
|
|
|
|
func WithSettingsOutcome(ctx context.Context, outcome *SettingsOutcome) context.Context {
|
|
return context.WithValue(ctx, settingsOutcomeKey{}, outcome)
|
|
}
|
|
|
|
func configuredResolution(cur reims.VMDetail) string {
|
|
if raw, ok := reims.ReadConfiguredResolution(cur.Directory); ok {
|
|
return raw
|
|
}
|
|
return cur.Resolution
|
|
}
|
|
|
|
func needsAutoResolutionSync(cur reims.VMDetail, configured, resolution string) bool {
|
|
return !vmstart.SupportsAutoResolution(cur.Graphics) || !strings.EqualFold(strings.TrimSpace(configured), resolution)
|
|
}
|
|
|
|
// ApplyVMOptions saves the settings batch while the caller holds the VM operation lock.
|
|
func ApplyVMOptions(ctx context.Context, c *reims.Client, cur reims.VMDetail, opts reims.SetOpts, stdout, stderr io.Writer, applyGuestLang func(reims.VMDetail) error) (err error) {
|
|
live := cur.State == "running" || cur.State == "paused"
|
|
prevDisplay := vmstart.ReadVMDisplay(cur.Directory)
|
|
cur.AutoResolution = prevDisplay.AutoResolution
|
|
nextDisplay := prevDisplay
|
|
nextDisplay.AutoResolution = autoResolutionAfterSet(cur, opts)
|
|
cur.Resolution = configuredResolution(cur)
|
|
syncAutoResolution := nextDisplay.AutoResolution && opts.AutoResolution != nil
|
|
if syncAutoResolution {
|
|
opts.Resolution = ""
|
|
if !live {
|
|
borderless := cur.Borderless
|
|
if opts.Borderless != nil {
|
|
borderless = *opts.Borderless
|
|
}
|
|
target, _ := vmstart.ResolveTargetDisplay(prevDisplay, host.ConnectedDisplays())
|
|
resolution, _ := host.RecommendAutoResolution(target, borderless, c.DisplayFixedMode)
|
|
if needsAutoResolutionSync(cur, cur.Resolution, resolution) {
|
|
opts.Resolution = resolution
|
|
}
|
|
}
|
|
}
|
|
outcome, _ := ctx.Value(settingsOutcomeKey{}).(*SettingsOutcome)
|
|
if strings.TrimSpace(opts.GuestLang) != "" {
|
|
cur.GuestLang = opencore.ReadSavedGuestLang(cur.Directory)
|
|
if cur.GuestLang == "" {
|
|
if path := opencore.ResolveOpenCorePath(cur.Directory); path != "" {
|
|
cur.GuestLang, _ = opencore.ReadImagePrevLang(path)
|
|
}
|
|
}
|
|
}
|
|
ctlCommitted := false
|
|
if nextDisplay != prevDisplay {
|
|
if _, err := vmstart.SaveVMDisplay(cur.Directory, nextDisplay); err != nil {
|
|
return err
|
|
}
|
|
defer func() {
|
|
if err != nil && !ctlCommitted {
|
|
if _, rollbackErr := vmstart.SaveVMDisplay(cur.Directory, prevDisplay); rollbackErr != nil {
|
|
err = errors.Join(err, rollbackErr)
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
if opts.NestedVirt != nil {
|
|
prevNestedVirt, readErr := vmstart.ReadVMNestedVirt(cur.Directory)
|
|
if readErr != nil {
|
|
return readErr
|
|
}
|
|
cur.NestedVirt = prevNestedVirt.Enabled
|
|
if err := vmstart.SaveVMNestedVirt(cur.Directory, vmstart.VMNestedVirtConfig{Enabled: *opts.NestedVirt}); err != nil {
|
|
return err
|
|
}
|
|
defer func() {
|
|
if err != nil && !ctlCommitted {
|
|
if rollbackErr := vmstart.SaveVMNestedVirt(cur.Directory, prevNestedVirt); rollbackErr != nil {
|
|
err = errors.Join(err, rollbackErr)
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
guestLangAfterSet := !live && opts.Graphics != "" && opts.Graphics != cur.Graphics
|
|
if !guestLangAfterSet {
|
|
if err := applyGuestLang(cur); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if reims.HasCtlSetChanges(opts) {
|
|
if err := c.Set(ctx, opts, stdout, stderr); err != nil {
|
|
return err
|
|
}
|
|
ctlCommitted = true
|
|
}
|
|
if guestLangAfterSet {
|
|
if err := applyGuestLang(cur); err != nil {
|
|
if outcome != nil {
|
|
*outcome = settingsOutcome(cur, opts, nextDisplay.AutoResolution, live)
|
|
}
|
|
return err
|
|
}
|
|
}
|
|
if outcome != nil {
|
|
*outcome = settingsOutcome(cur, opts, nextDisplay.AutoResolution, live)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func settingsOutcome(cur reims.VMDetail, applied reims.SetOpts, autoResolution, live bool) SettingsOutcome {
|
|
before := SettingsState{
|
|
CPUs: cur.CPUs, Displays: cur.Displays, Memory: cur.Memory, Resolution: cur.Resolution,
|
|
GPU: cur.GPU, DiskController: cur.DiskController, DiskCache: cmp.Or(cur.DiskCache, "default"),
|
|
AudioBackend: cmp.Or(cur.AudioBackend, "auto"), GuestLang: cmp.Or(cur.GuestLang, "en-US:0"),
|
|
AutoResolution: vmstart.SupportsAutoResolution(cur.Graphics) && cur.AutoResolution,
|
|
Borderless: cur.Borderless, NestedVirt: cur.NestedVirt, Audio: cur.Audio,
|
|
}
|
|
after := before
|
|
after.AutoResolution = autoResolution
|
|
after.Memory = cmp.Or(applied.Memory, before.Memory)
|
|
after.GPU = cmp.Or(applied.GPU, before.GPU)
|
|
if dc := strings.ToLower(strings.TrimSpace(applied.DiskController)); dc != "" {
|
|
after.DiskController = dc
|
|
}
|
|
if dcache := strings.ToLower(strings.TrimSpace(applied.DiskCache)); dcache != "" {
|
|
after.DiskCache = dcache
|
|
}
|
|
after.AudioBackend = cmp.Or(applied.AudioBackend, before.AudioBackend)
|
|
if applied.Resolution != "" {
|
|
after.Resolution = applied.Resolution
|
|
if raw, found := reims.ReadConfiguredResolution(cur.Directory); found {
|
|
after.Resolution = raw
|
|
}
|
|
}
|
|
if applied.CPUs > 0 {
|
|
after.CPUs = applied.CPUs
|
|
}
|
|
if applied.Displays > 0 {
|
|
after.Displays = applied.Displays
|
|
}
|
|
if applied.Borderless != nil {
|
|
after.Borderless = *applied.Borderless
|
|
}
|
|
if applied.NestedVirt != nil {
|
|
after.NestedVirt = *applied.NestedVirt
|
|
}
|
|
if applied.Audio != nil {
|
|
after.Audio = *applied.Audio
|
|
}
|
|
if strings.TrimSpace(applied.GuestLang) != "" {
|
|
after.GuestLang = cmp.Or(opencore.ReadSavedGuestLang(cur.Directory), before.GuestLang)
|
|
}
|
|
return SettingsOutcome{Valid: true, Running: live, Before: before, After: after}
|
|
}
|
|
|
|
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)
|
|
}
|