| 1 | |
| 2 | |
| 3 | package vm |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "encoding/json" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "io" |
| 11 | "os" |
| 12 | "path/filepath" |
| 13 | "strings" |
| 14 | |
| 15 | "github.com/Code-Hex/vz/v3" |
| 16 | "github.com/lima-vm/lima/v2/pkg/fileutils" |
| 17 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 18 | "github.com/lima-vm/lima/v2/pkg/limatype/dirnames" |
| 19 | ) |
| 20 | |
| 21 | |
| 22 | |
| 23 | func (l *Lima) CheckMacOSGuest(ctx context.Context, name string) error { |
| 24 | inst, err := l.inspect(ctx, name) |
| 25 | if err != nil { |
| 26 | return err |
| 27 | } |
| 28 | c := inst.Config |
| 29 | if c == nil || c.OS == nil || *c.OS != limatype.DARWIN || c.VMType == nil || *c.VMType != limatype.VZ || |
| 30 | len(c.Mounts) != 0 || len(c.AdditionalDisks) != 0 || c.Video.Display == nil || *c.Video.Display != "offscreen" { |
| 31 | return errors.New("expected a headless macOS VZ guest with its own system disk and no host shares") |
| 32 | } |
| 33 | for _, p := range c.PortForwards { |
| 34 | if !p.Ignore { |
| 35 | return errors.New("macOS guest has an unmanaged forwarded port") |
| 36 | } |
| 37 | } |
| 38 | return NetworkConfined(c) |
| 39 | } |
| 40 | |
| 41 | |
| 42 | |
| 43 | func (l *Lima) MatchMacOSInstallSource(ctx context.Context, name, image string) error { |
| 44 | if err := l.CheckMacOSGuest(ctx, name); err != nil { |
| 45 | return err |
| 46 | } |
| 47 | inst, err := l.inspect(ctx, name) |
| 48 | if err != nil { |
| 49 | return err |
| 50 | } |
| 51 | if !filepath.IsAbs(image) || len(inst.Config.Images) != 1 { |
| 52 | return errors.New("an interrupted installation requires its original local IPSW") |
| 53 | } |
| 54 | image, err = filepath.EvalSymlinks(image) |
| 55 | if err != nil { |
| 56 | return err |
| 57 | } |
| 58 | original := inst.Config.Images[0] |
| 59 | if original.Location != image || original.Digest == "" { |
| 60 | return errors.New("the existing macOS installation uses different restore media") |
| 61 | } |
| 62 | info, err := os.Stat(image) |
| 63 | if err != nil { |
| 64 | return err |
| 65 | } |
| 66 | if !info.Mode().IsRegular() { |
| 67 | return errors.New("the restore image must be a regular file") |
| 68 | } |
| 69 | digest, err := fileutils.RestoreImageDigest(ctx, image, info) |
| 70 | if err != nil { |
| 71 | return err |
| 72 | } |
| 73 | if string(original.Digest) != string(digest) { |
| 74 | return errors.New("the existing macOS installation is pinned to a different IPSW digest") |
| 75 | } |
| 76 | return nil |
| 77 | } |
| 78 | |
| 79 | |
| 80 | |
| 81 | func (l *Lima) CreateMacOSFromIPSW(ctx context.Context, name, imagePath string, settings Settings, log io.Writer) error { |
| 82 | if err := ctx.Err(); err != nil { |
| 83 | return err |
| 84 | } |
| 85 | if settings.Mounts != nil && len(*settings.Mounts) != 0 { |
| 86 | return errors.New("macOS installation does not accept shared host directories") |
| 87 | } |
| 88 | if err := dirnames.ValidateInstName(name); err != nil { |
| 89 | return err |
| 90 | } |
| 91 | if !filepath.IsAbs(imagePath) || !strings.EqualFold(filepath.Ext(imagePath), ".ipsw") { |
| 92 | return errors.New("select a local .ipsw file using its absolute path") |
| 93 | } |
| 94 | image, err := filepath.EvalSymlinks(imagePath) |
| 95 | if err != nil { |
| 96 | return err |
| 97 | } |
| 98 | info, err := os.Stat(image) |
| 99 | if err != nil { |
| 100 | return err |
| 101 | } |
| 102 | if !info.Mode().IsRegular() { |
| 103 | return errors.New("the selected IPSW must be a regular file") |
| 104 | } |
| 105 | home, err := dirnames.LimaDir() |
| 106 | if err != nil { |
| 107 | return err |
| 108 | } |
| 109 | |
| 110 | |
| 111 | store, err := os.Stat(home) |
| 112 | if err != nil && !errors.Is(err, os.ErrNotExist) { |
| 113 | return err |
| 114 | } |
| 115 | if store != nil { |
| 116 | for dir := filepath.Dir(image); ; dir = filepath.Dir(dir) { |
| 117 | ancestor, err := os.Stat(dir) |
| 118 | if err != nil { |
| 119 | return err |
| 120 | } |
| 121 | if os.SameFile(ancestor, store) { |
| 122 | return errors.New("keep the source IPSW outside the Lima instance storage directory") |
| 123 | } |
| 124 | if filepath.Dir(dir) == dir { |
| 125 | break |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | restore, err := vz.LoadMacOSRestoreImageFromPath(image) |
| 130 | if err != nil { |
| 131 | return fmt.Errorf("Apple Virtualization could not read the selected restore image: %w", err) |
| 132 | } |
| 133 | if err := applyMacOSRequirements(restore, &settings); err != nil { |
| 134 | return err |
| 135 | } |
| 136 | selectedDigest, err := fileutils.RestoreImageDigest(ctx, image, info) |
| 137 | if err != nil { |
| 138 | return fmt.Errorf("pinning the selected macOS restore image: %w", err) |
| 139 | } |
| 140 | template, err := os.CreateTemp("", "cage-macos-*.yaml") |
| 141 | if err != nil { |
| 142 | return err |
| 143 | } |
| 144 | defer os.Remove(template.Name()) |
| 145 | location, err := json.Marshal(image) |
| 146 | if err == nil { |
| 147 | _, err = fmt.Fprintf(template, macOSInstallTemplate, location, selectedDigest) |
| 148 | } |
| 149 | closeErr := template.Close() |
| 150 | if err := errors.Join(err, closeErr); err != nil { |
| 151 | return err |
| 152 | } |
| 153 | settings.NoContainerd = true |
| 154 | if err := l.Create(ctx, name, template.Name(), settings, log); err != nil { |
| 155 | return err |
| 156 | } |
| 157 | requirements, err := macOSImageResourceRequirements(restore) |
| 158 | if err != nil { |
| 159 | return err |
| 160 | } |
| 161 | return saveMacOSResourceRequirements(filepath.Join(home, name), requirements) |
| 162 | } |
| 163 | |
| 164 | func applyMacOSRequirements(restore *vz.MacOSRestoreImage, settings *Settings) error { |
| 165 | requirements, err := macOSImageResourceRequirements(restore) |
| 166 | if err != nil { |
| 167 | return err |
| 168 | } |
| 169 | minCPU, minGiB := requirements.CPUs, requirements.MemoryGiB |
| 170 | if settings.CPUs == 0 { |
| 171 | settings.CPUs = minCPU |
| 172 | } |
| 173 | if settings.MemoryGiB == 0 { |
| 174 | settings.MemoryGiB = minGiB |
| 175 | } |
| 176 | if settings.CPUs < minCPU || settings.MemoryGiB < minGiB { |
| 177 | return fmt.Errorf("this macOS restore image requires at least %d CPUs and %d GiB RAM", minCPU, minGiB) |
| 178 | } |
| 179 | return nil |
| 180 | } |
| 181 | |
| 182 | const macOSInstallTemplate = `os: Darwin |
| 183 | arch: aarch64 |
| 184 | vmType: vz |
| 185 | images: |
| 186 | - location: %s |
| 187 | arch: aarch64 |
| 188 | digest: %s |
| 189 | user: |
| 190 | name: cageprovision |
| 191 | comment: Cage Provisioning |
| 192 | home: /Users/cageprovision |
| 193 | uid: 501 |
| 194 | osOpts: |
| 195 | Darwin: |
| 196 | suppressFirstLoginSetup: true |
| 197 | provision: |
| 198 | - mode: system |
| 199 | script: | |
| 200 | #!/bin/sh |
| 201 | set -eu |
| 202 | [ "$(/usr/bin/uname -sm)" = "Darwin arm64" ] |
| 203 | case "$(/usr/sbin/sysctl -n hw.model)" in VirtualMac*,*) ;; *) exit 1 ;; esac |
| 204 | /usr/bin/ssh-keygen -A >/dev/null |
| 205 | # This serial port is attached by VZ to this specific private instance. |
| 206 | # Publish only the public key. SSH is pinned before provisioning begins. |
| 207 | /bin/echo "CAGE-GUEST-HOST-KEY $(/bin/cat /etc/ssh/ssh_host_ed25519_key.pub)" >/dev/tty.virtio |
| 208 | video: |
| 209 | display: offscreen |
| 210 | mounts: [] |
| 211 | networks: [] |
| 212 | ssh: |
| 213 | loadDotSSHPubKeys: false |
| 214 | forwardAgent: false |
| 215 | forwardX11: false |
| 216 | portForwards: |
| 217 | - guestIP: 0.0.0.0 |
| 218 | guestIPMustBeZero: false |
| 219 | guestPortRange: [1, 65535] |
| 220 | proto: any |
| 221 | ignore: true |
| 222 | ` |