Build Guest Tools images in the macOS driver after all start hooks pass, through a host callback rather than a new macOS-to-guesttools dependency. Add driver.OptimizationAdvisor, keep generic hardware evaluation in host, and move WHPX recommendations and tests into macosdriver for both the UI and diagnostics.
246 lines
6.7 KiB
Go
246 lines
6.7 KiB
Go
package macosdriver
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
|
|
"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/plugin"
|
|
rt "github.com/Nwflower/reims-go/internal/runtime"
|
|
)
|
|
|
|
// 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
|
|
WHPXFallbackEnabled func() bool
|
|
ColdRebootEnabled func() bool
|
|
MigrateLedger func(*plugin.ArgLedger)
|
|
PrepareGuestToolsImage func(string, string) 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, err := d.client()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
items, err := c.List(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := make([]driver.VMInfo, 0, len(items))
|
|
for _, it := range items {
|
|
out = append(out, driver.VMInfo{
|
|
Name: it.Name,
|
|
DriverID: d.ID(),
|
|
Directory: it.Dir,
|
|
DriverName: "macOS (Reims vGPU)",
|
|
State: it.State,
|
|
PID: it.PID,
|
|
Capabilities: d.Capabilities(),
|
|
})
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) Show(ctx context.Context, name string) (driver.VMInfo, error) {
|
|
c, err := d.client()
|
|
if err != nil {
|
|
return driver.VMInfo{}, err
|
|
}
|
|
det, err := c.Show(ctx, name)
|
|
if err != nil {
|
|
return driver.VMInfo{}, err
|
|
}
|
|
rawBytes, _ := json.Marshal(det)
|
|
var data map[string]any
|
|
_ = json.Unmarshal(rawBytes, &data)
|
|
return driver.VMInfo{
|
|
Name: det.Name,
|
|
DriverID: d.ID(),
|
|
DriverName: "macOS (Reims vGPU)",
|
|
State: det.State,
|
|
CPUs: det.CPUs,
|
|
Memory: det.Memory,
|
|
DiskSummary: det.Disk,
|
|
Directory: det.Directory,
|
|
Capabilities: d.Capabilities(),
|
|
DriverData: data,
|
|
}, 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.ValidateCreateLocalized(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
|
|
}
|
|
return c.Create(ctx, opts, stdout, stderr)
|
|
}
|
|
|
|
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.ValidateSetLocalized(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
|
|
return c.Set(ctx, opts, stdout, stderr)
|
|
}
|
|
|
|
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
|
|
}
|
|
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 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)
|
|
}
|