| 1 | |
| 2 | |
| 3 | package main |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "errors" |
| 8 | "flag" |
| 9 | "fmt" |
| 10 | "io" |
| 11 | "os" |
| 12 | "path/filepath" |
| 13 | |
| 14 | vzdriver "github.com/lima-vm/lima/v2/pkg/driver/vz" |
| 15 | "github.com/sylvinus/agent-vm/internal/guestos" |
| 16 | "github.com/sylvinus/agent-vm/internal/vm" |
| 17 | ) |
| 18 | |
| 19 | func macGuestLoginAvailable() bool { return vzdriver.OffscreenView != nil } |
| 20 | |
| 21 | func runMacGuest(ctx context.Context, profile, state string, args []string) error { |
| 22 | if len(args) == 0 { |
| 23 | return errors.New("usage: cage guest-os macos create <absolute.ipsw> | bind <user> | start | stop") |
| 24 | } |
| 25 | lima, err := vm.NewLima(filepath.Join(state, "lima"), state) |
| 26 | if err != nil { |
| 27 | return err |
| 28 | } |
| 29 | const name = "mac" |
| 30 | switch args[0] { |
| 31 | case "update-components": |
| 32 | flags := flag.NewFlagSet("update-components", flag.ContinueOnError) |
| 33 | flags.SetOutput(io.Discard) |
| 34 | manifest := flags.String("manifest", "", "") |
| 35 | updateTools := flags.Bool("tools", false, "") |
| 36 | if err := flags.Parse(args[1:]); err != nil || flags.NArg() != 0 || !filepath.IsAbs(*manifest) { |
| 37 | return errors.New("需要本次原生组件更新清单") |
| 38 | } |
| 39 | exe, err := os.Executable() |
| 40 | if err != nil { |
| 41 | return err |
| 42 | } |
| 43 | return guestos.MaintainMacOSComponents(ctx, profile, filepath.Join(filepath.Dir(exe), "guest-os"), *manifest, *updateTools, os.Stdout, os.Stderr) |
| 44 | case "install": |
| 45 | if len(args) != 1 { |
| 46 | return errors.New("unexpected install argument") |
| 47 | } |
| 48 | return installMacWorkbench(ctx, profile, state, lima) |
| 49 | case "create": |
| 50 | if len(args) != 2 { |
| 51 | return errors.New("supply the absolute path to an official macOS IPSW") |
| 52 | } |
| 53 | instances, err := lima.List(ctx) |
| 54 | if err != nil { |
| 55 | return err |
| 56 | } |
| 57 | switch { |
| 58 | case len(instances) == 0: |
| 59 | err = lima.CreateMacOSFromIPSW(ctx, name, args[1], vm.Settings{DiskGiB: 64}, os.Stderr) |
| 60 | case len(instances) == 1 && instances[0].Name == name: |
| 61 | if bound, loadErr := guestos.LoadMacOSProfile(profile); loadErr != nil || bound != nil { |
| 62 | return errors.New("this macOS guest already has a binding; use start") |
| 63 | } |
| 64 | err = lima.MatchMacOSInstallSource(ctx, name, args[1]) |
| 65 | default: |
| 66 | return errors.New("create requires a dedicated macOS guest profile") |
| 67 | } |
| 68 | if err != nil { |
| 69 | return err |
| 70 | } |
| 71 | return lima.Start(ctx, name, os.Stderr) |
| 72 | case "bind": |
| 73 | if len(args) != 2 { |
| 74 | return errors.New("supply the ordinary guest account and its independently verified guest SSH host public key on stdin") |
| 75 | } |
| 76 | if err := lima.CheckMacOSGuest(ctx, name); err != nil { |
| 77 | return err |
| 78 | } |
| 79 | key, err := io.ReadAll(io.LimitReader(os.Stdin, 4097)) |
| 80 | if err != nil || len(key) > 4096 { |
| 81 | return errors.New("could not read the guest host public key") |
| 82 | } |
| 83 | return guestos.BindMacOSProfile(profile, args[1], key) |
| 84 | case "start", "stop": |
| 85 | if len(args) != 1 { |
| 86 | return errors.New("unexpected macOS guest lifecycle argument") |
| 87 | } |
| 88 | m, err := guestos.LoadMacOSProfile(profile) |
| 89 | if err != nil { |
| 90 | return err |
| 91 | } |
| 92 | if m == nil { |
| 93 | return errors.New("this profile is not bound to a prepared macOS guest") |
| 94 | } |
| 95 | if args[0] == "stop" { |
| 96 | return lima.Stop(ctx, m.Instance) |
| 97 | } |
| 98 | if err := lima.CheckMacOSGuest(ctx, m.Instance); err != nil { |
| 99 | return err |
| 100 | } |
| 101 | return lima.Start(ctx, m.Instance, os.Stderr) |
| 102 | default: |
| 103 | return fmt.Errorf("unknown macOS guest command %q", args[0]) |
| 104 | } |
| 105 | } |