* feat: add experimental Apple Silicon support with Nivfold Integrate the standalone Nivfold protocol 1 runner; tested against Nivfold f436fb6. The Windows Reims vGPU runtime remains independent. * fix: open QQ invites and VM SSH on macOS * fix: move VM, View and Help to the macOS menu bar * feat(macos): add resumable Apple restore image downloads Expose the compatible restore image in the VM wizard and Images page, validate downloads before caching, and fix macOS memory reporting and plugin SVG rendering. * fix(macos): localize the native application menu * fix(macos): address native desktop review findings
267 lines
9.2 KiB
Go
267 lines
9.2 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"os/signal"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/Nwflower/reims-go/internal/config"
|
|
"github.com/Nwflower/reims-go/internal/console"
|
|
"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/lifecycle"
|
|
"github.com/Nwflower/reims-go/internal/macos/guest"
|
|
"github.com/Nwflower/reims-go/internal/macos/installer"
|
|
"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/networkmsg"
|
|
"github.com/Nwflower/reims-go/internal/plugin"
|
|
)
|
|
|
|
func defaultMacOSGuestLang(loc i18n.Locale) string { return opencore.DefaultGuestLang(loc) }
|
|
|
|
func defaultCreateDriver() string {
|
|
if guest.UsesAppleVirtualization() {
|
|
return guest.AppleDriverID()
|
|
}
|
|
return driver.BuiltinReimsMacOSDriverID
|
|
}
|
|
|
|
func isAppleCreateDriver(id string) bool { return id == guest.AppleDriverID() }
|
|
|
|
func nativeVirtualizationStatus(ctx context.Context) (bool, error) {
|
|
if !guest.UsesAppleVirtualization() {
|
|
return false, nil
|
|
}
|
|
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
defer cancel()
|
|
return true, guest.ProbeAppleVirtualization(ctx)
|
|
}
|
|
|
|
func printAppleUsage(loc i18n.Locale) {
|
|
fmt.Println(i18n.T(loc, "cli.usage.apple.header"))
|
|
fmt.Println(i18n.T(loc, "cli.usage.line_cmd"))
|
|
fmt.Println()
|
|
for _, k := range []string{
|
|
"cli.usage.apple.doctor", "cli.usage.apple.create", "cli.usage.cmd_vm_list",
|
|
"cli.usage.apple.start", "cli.usage.apple.stop", "cli.usage.apple.delete",
|
|
"cli.usage.cmd_vm_set", "cli.usage.cmd_plugin", "cli.usage.cmd_version",
|
|
} {
|
|
fmt.Println(i18n.T(loc, k))
|
|
}
|
|
}
|
|
|
|
func printAppleVirtualizationStatus(loc i18n.Locale, status *host.AppleVirtualizationStatus) {
|
|
key := "doctor.apple_virtualization.ready"
|
|
if !status.Ready {
|
|
key = "doctor.apple_virtualization.unavailable"
|
|
}
|
|
fmt.Printf("%-36s: %s\n", "Apple Virtualization", i18n.T(loc, key))
|
|
if status.Detail != "" {
|
|
fmt.Fprintln(os.Stderr, status.Detail)
|
|
}
|
|
}
|
|
|
|
func requireDirectNetwork(vm driver.VMInfo) error {
|
|
if vm.Ref().DriverID != driver.BuiltinReimsMacOSDriverID {
|
|
return nil
|
|
}
|
|
mode, _ := vm.DriverData["network_mode"].(string)
|
|
extra, _ := vm.DriverData["extra_args"].(string)
|
|
if mode == "bridge" || reims.PacketBridgeSocket(extra) != "" || reims.PacketBridgePort(extra) > 0 {
|
|
return networkmsg.Error("cli_managed_required", nil)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// checkDirectMacOSNetwork 由驱动在持有操作锁时再次检查,避免配置在解析后被切换。
|
|
func checkDirectMacOSNetwork(ctx context.Context, cfg *config.Config, name string) error {
|
|
prep, err := startprep.New(*cfg, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
d, err := prep.Client.Show(ctx, name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if d.NetworkMode == "bridge" || reims.PacketBridgeSocket(d.ExtraArgs) != "" || reims.PacketBridgePort(d.ExtraArgs) > 0 {
|
|
return networkmsg.Error("cli_managed_required", nil)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func requireCLIMacOSEnabled(cfg *config.Config, loc i18n.Locale) error {
|
|
st, err := plugin.NewStateStore(plugin.NewPluginsLayout(cfg.DataRoot).Root).Load()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if entry := st.Plugins[guest.ID]; entry != nil && !entry.Enabled {
|
|
return errors.New(i18n.T(loc, "err.macos.disabled"))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func createAppleVM(ctx context.Context, cfg *config.Config, id, name, dir, ipsw string, cpus int, mem, disk string, start bool, flags *flag.FlagSet) error {
|
|
options := map[string]any{"restore_path": ipsw}
|
|
var unsupported error
|
|
flags.Visit(func(f *flag.Flag) {
|
|
switch f.Name {
|
|
case "driver", "ipsw", "start":
|
|
case "cpus":
|
|
options["cpus"] = cpus
|
|
case "memory":
|
|
options["memory"] = mem
|
|
case "disk-size":
|
|
options["disk_size"] = disk
|
|
default:
|
|
unsupported = fmt.Errorf("--%s is not supported by the Apple Virtualization driver", f.Name)
|
|
}
|
|
})
|
|
if unsupported != nil {
|
|
return unsupported
|
|
}
|
|
if dir != "" {
|
|
return errors.New("the Apple Virtualization driver manages its VM directories under the data folder")
|
|
}
|
|
mgr, router, err := openCLIPluginManager(cfg, false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer mgr.Close()
|
|
drv, err := router.DriverByID(id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ctx, cancel := signal.NotifyContext(ctx, os.Interrupt)
|
|
defer cancel()
|
|
req := driver.CreateRequest{DriverID: drv.ID(), Name: name, Options: options}
|
|
if err := drv.Create(ctx, req, os.Stdout, os.Stderr); err != nil || !start {
|
|
return err
|
|
}
|
|
_, err = lifecycle.New(mgr.Registry(), router, nil).Start(ctx, driver.NewVMRef(drv.ID(), name), os.Stdout, os.Stderr)
|
|
return err
|
|
}
|
|
|
|
// macOSCreateOptions is the CLI-neutral input to the native macOS create flow.
|
|
type macOSCreateOptions struct {
|
|
Name, Dir, MacOS, Memory, DiskSize, Disk, DiskMode, DiskCache, GPU, GuestLang string
|
|
CPUs int
|
|
Capture, Logs bool
|
|
}
|
|
|
|
func createMacOSVM(ctx context.Context, cfg *config.Config, loc i18n.Locale, in macOSCreateOptions, startAfterCreate, viaWindow bool) error {
|
|
if err := requireCLIMacOSEnabled(cfg, loc); err != nil {
|
|
return err
|
|
}
|
|
prep, err := startprep.New(*cfg, func(line string) { fmt.Fprintln(os.Stderr, line) })
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !prep.Runtime.Manifest.HasCapability("win32-native-aio") {
|
|
if dcache := strings.TrimSpace(in.DiskCache); dcache != "" && !strings.EqualFold(dcache, "default") {
|
|
return errors.New(i18n.T(loc, "validate.disk_cache_requires_runtime"))
|
|
}
|
|
if reims.IsWindowsRawDevicePath(in.Disk) {
|
|
return errors.New(i18n.T(loc, "validate.raw_disk_requires_runtime"))
|
|
}
|
|
}
|
|
client := prep.Client
|
|
opts := reims.CreateOpts{Name: in.Name, Dir: in.Dir, MacOS: in.MacOS, CPUs: in.CPUs, Memory: in.Memory, DiskSize: in.DiskSize, Disk: in.Disk, DiskMode: in.DiskMode, DiskCache: in.DiskCache, GPU: in.GPU, Capture: &in.Capture, Logs: &in.Logs, GuestLang: strings.TrimSpace(in.GuestLang), Start: false}
|
|
existingVMs, err := client.List(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("check existing VMs before create: %w", err)
|
|
}
|
|
if issue := reims.ValidateCreateTargetDir(cfg.XDGDataDir(), opts, existingVMs, loc); issue != nil {
|
|
return errors.New(issue.Message)
|
|
}
|
|
if removed, rerr := reims.CleanAbortedCreateDir(cfg.XDGDataDir(), opts, existingVMs); rerr != nil {
|
|
return fmt.Errorf("clean unfinished VM directory: %w", rerr)
|
|
} else if removed != "" {
|
|
fmt.Fprintf(os.Stderr, "Removed unfinished directory from earlier interrupted create: %s\n", removed)
|
|
}
|
|
if installer.ShouldPrefetchOcBinaryData(prep.Runtime.Manifest.UtilCommit, prep.Runtime.Manifest.UtilVersion) {
|
|
if err := installer.EnsureCachedAsset(ctx, cfg.XDGCacheDir(), installer.OcBinaryDataAsset, nil, nil); err != nil {
|
|
fmt.Fprintf(os.Stderr, "OpenCore resource pre-fetch skipped (reims-ctl will retry): %v\n", err)
|
|
}
|
|
}
|
|
if err := client.Create(ctx, opts, os.Stdout, os.Stderr); err != nil {
|
|
return err
|
|
}
|
|
createdDir := strings.TrimSpace(opts.Dir)
|
|
if det, err := client.Show(ctx, opts.Name); err == nil && det.Directory != "" {
|
|
createdDir = det.Directory
|
|
}
|
|
if createdDir == "" {
|
|
createdDir = filepath.Join(cfg.XDGDataDir(), "reims-ctl", "vms", opts.Name)
|
|
}
|
|
if err := driver.WriteOwnerDriver(createdDir, driver.BuiltinReimsMacOSDriverID); err != nil {
|
|
return err
|
|
}
|
|
if postSet, ok := reims.PostCreateSetOptsWithCapture(opts, client.AllowsSetCapture()); ok {
|
|
if err := client.Set(ctx, postSet, os.Stdout, os.Stderr); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Warning: could not apply post-create defaults: %v\n", err)
|
|
}
|
|
}
|
|
if opts.GuestLang != "" {
|
|
vmDir := opts.Dir
|
|
if det, err := client.Show(ctx, in.Name); err == nil && det.Directory != "" {
|
|
vmDir = det.Directory
|
|
} else if vmDir == "" {
|
|
vmDir = filepath.Join(cfg.XDGDataDir(), "reims-ctl", "vms", in.Name)
|
|
}
|
|
if err := prep.ApplyGuestLang(ctx, vmDir, opts.GuestLang, false); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Warning: could not apply macOS language (%s): %v\n", opts.GuestLang, err)
|
|
}
|
|
}
|
|
if startAfterCreate {
|
|
if err := startVM(ctx, cfg, driver.BuiltinReimsMacOSDriverID, in.Name, viaWindow); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
fmt.Println("\n" + i18n.T(loc, "vm.install_guide.title"))
|
|
fmt.Println(i18n.T(loc, "vm.install_guide.step1"))
|
|
fmt.Println(i18n.T(loc, "vm.install_guide.step2"))
|
|
fmt.Println(i18n.T(loc, "vm.install_guide.step3", in.Name))
|
|
return nil
|
|
}
|
|
|
|
func typeMacOSVM(ctx context.Context, cfg *config.Config, loc i18n.Locale, args []string) error {
|
|
if len(args) < 2 {
|
|
return fmt.Errorf("usage: reims-go vm type <name> [text] (no text: read stdin)")
|
|
}
|
|
if err := requireCLIMacOSEnabled(cfg, loc); err != nil {
|
|
return err
|
|
}
|
|
prep, err := startprep.New(*cfg, func(line string) { fmt.Fprintln(os.Stderr, line) })
|
|
if err != nil {
|
|
return err
|
|
}
|
|
text := strings.Join(args[2:], " ")
|
|
if len(args) < 3 {
|
|
raw, err := io.ReadAll(os.Stdin)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
text = strings.TrimRight(string(raw), "\r\n")
|
|
}
|
|
detail, err := prep.Client.Show(ctx, args[1])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
n, err := console.NewManager().TypeText(ctx, detail.Directory, text, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Println(i18n.T(loc, "vm.typed", n))
|
|
return nil
|
|
}
|