| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | package vz |
| 7 | |
| 8 | import ( |
| 9 | "context" |
| 10 | "errors" |
| 11 | "fmt" |
| 12 | "io/fs" |
| 13 | "os" |
| 14 | "path/filepath" |
| 15 | "time" |
| 16 | |
| 17 | "github.com/Code-Hex/vz/v3" |
| 18 | "github.com/sirupsen/logrus" |
| 19 | |
| 20 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 21 | "github.com/lima-vm/lima/v2/pkg/limatype/filenames" |
| 22 | "github.com/lima-vm/lima/v2/pkg/osutil" |
| 23 | "github.com/lima-vm/lima/v2/pkg/progressbar" |
| 24 | ) |
| 25 | |
| 26 | func getMacMachineIdentifier(identifier string) (machineIdentifier, error) { |
| 27 | |
| 28 | if st, err := os.Stat(identifier); err != nil || (st != nil && st.Size() == 0) { |
| 29 | if err != nil && !errors.Is(err, fs.ErrNotExist) { |
| 30 | return nil, err |
| 31 | } |
| 32 | machineIdentifier, err := vz.NewMacMachineIdentifier() |
| 33 | if err != nil { |
| 34 | return nil, err |
| 35 | } |
| 36 | err = os.WriteFile(identifier, machineIdentifier.DataRepresentation(), 0o666) |
| 37 | if err != nil { |
| 38 | return nil, err |
| 39 | } |
| 40 | return machineIdentifier, nil |
| 41 | } |
| 42 | return vz.NewMacMachineIdentifierWithDataPath(identifier) |
| 43 | } |
| 44 | |
| 45 | func newMacPlatformConfiguration(inst *limatype.Instance) (vz.PlatformConfiguration, error) { |
| 46 | identifierFile := filepath.Join(inst.Dir, filenames.VzIdentifier) |
| 47 | machineIdentifier, err := getMacMachineIdentifier(identifierFile) |
| 48 | if err != nil { |
| 49 | return nil, err |
| 50 | } |
| 51 | |
| 52 | var hwModelData []byte |
| 53 | hwModelFile := filepath.Join(inst.Dir, filenames.VzHwModel) |
| 54 | if osutil.FileExists(hwModelFile) { |
| 55 | hwModelData, err = os.ReadFile(hwModelFile) |
| 56 | if err != nil { |
| 57 | return nil, err |
| 58 | } |
| 59 | } else { |
| 60 | if err = ensureIPSW(inst.Dir); err != nil { |
| 61 | return nil, err |
| 62 | } |
| 63 | ipsw := filepath.Join(inst.Dir, filenames.ImageIPSW) |
| 64 | ipswImage, err := vz.LoadMacOSRestoreImageFromPath(ipsw) |
| 65 | if err != nil { |
| 66 | return nil, err |
| 67 | } |
| 68 | if ipswImage == nil { |
| 69 | return nil, fmt.Errorf("missing restore image for %q", ipsw) |
| 70 | } |
| 71 | requirements := ipswImage.MostFeaturefulSupportedConfiguration() |
| 72 | if requirements == nil || requirements.MinimumSupportedCPUCount() == 0 { |
| 73 | return nil, fmt.Errorf("IPSW %q has no configuration supported by this Mac", ipsw) |
| 74 | } |
| 75 | model := requirements.HardwareModel() |
| 76 | if model == nil || !model.Supported() || len(model.DataRepresentation()) == 0 { |
| 77 | return nil, fmt.Errorf("IPSW %q has no supported hardware model", ipsw) |
| 78 | } |
| 79 | hwModelData = model.DataRepresentation() |
| 80 | if err = os.WriteFile(hwModelFile, hwModelData, 0o666); err != nil { |
| 81 | return nil, err |
| 82 | } |
| 83 | } |
| 84 | if len(hwModelData) == 0 { |
| 85 | return nil, fmt.Errorf("empty macOS hardware model in %q", hwModelFile) |
| 86 | } |
| 87 | hwModel, err := vz.NewMacHardwareModelWithData(hwModelData) |
| 88 | if err != nil { |
| 89 | return nil, err |
| 90 | } |
| 91 | |
| 92 | auxFile := filepath.Join(inst.Dir, filenames.VzAux) |
| 93 | var auxOps []vz.NewMacAuxiliaryStorageOption |
| 94 | if !osutil.FileExists(auxFile) { |
| 95 | auxOps = append(auxOps, vz.WithCreatingMacAuxiliaryStorage(hwModel)) |
| 96 | } |
| 97 | aux, err := vz.NewMacAuxiliaryStorage(auxFile, auxOps...) |
| 98 | if err != nil { |
| 99 | return nil, err |
| 100 | } |
| 101 | |
| 102 | platformConfig, err := vz.NewMacPlatformConfiguration( |
| 103 | vz.WithMacMachineIdentifier(machineIdentifier.(*vz.MacMachineIdentifier)), |
| 104 | vz.WithMacHardwareModel(hwModel), |
| 105 | vz.WithMacAuxiliaryStorage(aux), |
| 106 | ) |
| 107 | if err != nil { |
| 108 | return nil, err |
| 109 | } |
| 110 | return platformConfig, nil |
| 111 | } |
| 112 | |
| 113 | func newMacGraphicsDeviceConfiguration(x, y, pixelsPerInch int64) (vz.GraphicsDeviceConfiguration, error) { |
| 114 | graphicsDeviceConfiguration, err := vz.NewMacGraphicsDeviceConfiguration() |
| 115 | if err != nil { |
| 116 | return nil, err |
| 117 | } |
| 118 | scanoutConfiguration, err := vz.NewMacGraphicsDisplayConfiguration(x, y, pixelsPerInch) |
| 119 | if err != nil { |
| 120 | return nil, err |
| 121 | } |
| 122 | graphicsDeviceConfiguration.SetDisplays(scanoutConfiguration) |
| 123 | return graphicsDeviceConfiguration, nil |
| 124 | } |
| 125 | |
| 126 | func newMacPointingDeviceConfiguration() (vz.PointingDeviceConfiguration, error) { |
| 127 | return vz.NewMacTrackpadConfiguration() |
| 128 | } |
| 129 | |
| 130 | func newMacKeyboardConfiguration() (vz.KeyboardConfiguration, error) { |
| 131 | return vz.NewMacKeyboardConfiguration() |
| 132 | } |
| 133 | |
| 134 | func newMacOSBootLoader() (vz.BootLoader, error) { |
| 135 | return vz.NewMacOSBootLoader() |
| 136 | } |
| 137 | |
| 138 | func installMacOS(ctx context.Context, vm *vz.VirtualMachine, ipsw string) error { |
| 139 | installer, err := vz.NewMacOSInstaller(vm, ipsw) |
| 140 | if err != nil { |
| 141 | return err |
| 142 | } |
| 143 | |
| 144 | const barResolution = 100 |
| 145 | bar, err := progressbar.New(barResolution) |
| 146 | if err != nil { |
| 147 | return err |
| 148 | } |
| 149 | bar.Start() |
| 150 | |
| 151 | ctx, cancel := context.WithCancel(ctx) |
| 152 | defer cancel() |
| 153 | |
| 154 | go func() { |
| 155 | ticker := time.NewTicker(3 * time.Second) |
| 156 | defer ticker.Stop() |
| 157 | for { |
| 158 | select { |
| 159 | case <-ctx.Done(): |
| 160 | logrus.WithError(ctx.Err()).Info("cancelling macOS installation") |
| 161 | return |
| 162 | case <-installer.Done(): |
| 163 | logrus.WithError(ctx.Err()).Info("macOS installer exited") |
| 164 | bar.SetCurrent(barResolution) |
| 165 | return |
| 166 | case <-ticker.C: |
| 167 | progress := installer.FractionCompleted() * barResolution |
| 168 | bar.SetCurrent(int64(progress)) |
| 169 | } |
| 170 | } |
| 171 | }() |
| 172 | |
| 173 | err = installer.Install(ctx) |
| 174 | bar.Finish() |
| 175 | if err != nil { |
| 176 | return err |
| 177 | } |
| 178 | |
| 179 | stopped, err := vm.RequestStop() |
| 180 | if err != nil { |
| 181 | return fmt.Errorf("failed to stop VM after installation: %w", err) |
| 182 | } |
| 183 | if !stopped { |
| 184 | logrus.WithError(err).Warn("VM did not stop after installation") |
| 185 | if err = vm.Stop(); err != nil { |
| 186 | return fmt.Errorf("failed to force stop VM after installation: %w", err) |
| 187 | } |
| 188 | } |
| 189 | return nil |
| 190 | } |