| 1 | |
| 2 | |
| 3 | @@ -24,6 +24,7 @@ |
| 4 | "github.com/lima-vm/go-qcow2reader/image/asif" |
| 5 | "github.com/lima-vm/go-qcow2reader/image/raw" |
| 6 | "github.com/sirupsen/logrus" |
| 7 | + "golang.org/x/sys/unix" |
| 8 | |
| 9 | "github.com/lima-vm/lima/v2/pkg/driver" |
| 10 | "github.com/lima-vm/lima/v2/pkg/driverutil" |
| 11 | @@ -34,6 +35,7 @@ |
| 12 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 13 | "github.com/lima-vm/lima/v2/pkg/limatype/filenames" |
| 14 | "github.com/lima-vm/lima/v2/pkg/limayaml" |
| 15 | + "github.com/lima-vm/lima/v2/pkg/lockutil" |
| 16 | "github.com/lima-vm/lima/v2/pkg/osutil" |
| 17 | "github.com/lima-vm/lima/v2/pkg/reflectutil" |
| 18 | "github.com/lima-vm/lima/v2/pkg/sshutil" |
| 19 | @@ -370,39 +372,62 @@ |
| 20 | } |
| 21 | |
| 22 | func (l *LimaVzDriver) CreateDisk(ctx context.Context) error { |
| 23 | - if *l.Instance.Config.OS == limatype.DARWIN { |
| 24 | + if *l.Instance.Config.OS != limatype.DARWIN { |
| 25 | + return driverutil.EnsureDisk(ctx, l.Instance.Dir, *l.Instance.Config.Disk, l.diskImageFormat) |
| 26 | + } |
| 27 | + return lockutil.WithDirLock(l.Instance.Dir, func() error { |
| 28 | + if err := ctx.Err(); err != nil { |
| 29 | + return err |
| 30 | + } |
| 31 | disk := filepath.Join(l.Instance.Dir, filenames.Disk) |
| 32 | - if !osutil.FileExists(disk) { |
| 33 | - if err := l.createDiskMacOSGuest(ctx); err != nil { |
| 34 | - return err |
| 35 | - } |
| 36 | - } |
| 37 | - |
| 38 | - patchedMarker := disk + ".patched" // empty file |
| 39 | - if !osutil.FileExists(patchedMarker) { |
| 40 | - logrus.Infof("Patching macOS disk %#q", disk) |
| 41 | - if err := macos.Patch(ctx, disk); err != nil { |
| 42 | - return err |
| 43 | - } |
| 44 | - if err := os.WriteFile(patchedMarker, []byte{}, 0o644); err != nil { |
| 45 | - return err |
| 46 | - } |
| 47 | - } |
| 48 | - } |
| 49 | - return driverutil.EnsureDisk(ctx, l.Instance.Dir, *l.Instance.Config.Disk, l.diskImageFormat) |
| 50 | -} |
| 51 | - |
| 52 | -// createDiskMacOSGuest creates `disk` and installs macOS from `image` on it. |
| 53 | -// The function must not be called if `disk` already exists. |
| 54 | -// |
| 55 | -// The function creates the following files: |
| 56 | -// - `image.ipsw`: hardlink to `image` (".ipsw" suffix is required by VZMacOSInstaller) |
| 57 | -// - `disk`: raw or ASIF disk (ASIF requires macOS 26+) |
| 58 | -// |
| 59 | -// After successful installation, `image.ipsw` and `image` are removed. |
| 60 | -func (l *LimaVzDriver) createDiskMacOSGuest(ctx context.Context) error { |
| 61 | - disk := filepath.Join(l.Instance.Dir, filenames.Disk) |
| 62 | - |
| 63 | + if err := restoreMacOSDisk(disk, func(staging string) error { return l.createDiskMacOSGuest(ctx, staging) }); err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + patchedMarker := disk + ".patched" |
| 67 | + if osutil.FileExists(patchedMarker) { |
| 68 | + return nil |
| 69 | + } |
| 70 | + logrus.Infof("Patching macOS disk %#q", disk) |
| 71 | + if err := macos.Patch(ctx, disk); err != nil { |
| 72 | + return fmt.Errorf("preparing existing macOS disk %q: %w", disk, err) |
| 73 | + } |
| 74 | + return os.WriteFile(patchedMarker, nil, 0o644) |
| 75 | + }) |
| 76 | +} |
| 77 | + |
| 78 | +// restoreMacOSDisk is called with the instance lock held. Existing guest disks |
| 79 | +// are never replaced. A process that exits during restore leaves only staging. |
| 80 | +func restoreMacOSDisk(disk string, install func(string) error) error { |
| 81 | + if _, err := os.Lstat(disk); err == nil || !errors.Is(err, os.ErrNotExist) { |
| 82 | + return err |
| 83 | + } |
| 84 | + staging := disk + ".installing" |
| 85 | + // diskutil can append .asif when creating a blank disk. A killed process |
| 86 | + // can leave either name behind. A fresh install also needs a fresh patch. |
| 87 | + for _, path := range []string{staging, staging + ".asif", disk + ".patched"} { |
| 88 | + if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) { |
| 89 | + return err |
| 90 | + } |
| 91 | + } |
| 92 | + defer os.Remove(staging) |
| 93 | + defer os.Remove(staging + ".asif") |
| 94 | + if err := install(staging); err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + if err := unix.RenamexNp(staging, disk, unix.RENAME_EXCL); err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + // Remove only the instance's installation media after the disk is ready. |
| 101 | + for _, filename := range []string{filenames.ImageIPSW, filenames.Image} { |
| 102 | + path := filepath.Join(filepath.Dir(disk), filename) |
| 103 | + if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) { |
| 104 | + logrus.WithError(err).Warnf("Failed to remove %#q", path) |
| 105 | + } |
| 106 | + } |
| 107 | + return nil |
| 108 | +} |
| 109 | + |
| 110 | +func (l *LimaVzDriver) createDiskMacOSGuest(ctx context.Context, disk string) error { |
| 111 | diskSize, err := units.RAMInBytes(*l.Instance.Config.Disk) |
| 112 | if err != nil { |
| 113 | return fmt.Errorf("invalid disk size %#q: %w", *l.Instance.Config.Disk, err) |
| 114 | @@ -428,7 +453,7 @@ |
| 115 | } |
| 116 | ipsw := filepath.Join(l.Instance.Dir, filenames.ImageIPSW) |
| 117 | |
| 118 | - vm, err := createVMForMacInstaller(ctx, l.Instance) |
| 119 | + vm, err := createVMForMacInstaller(ctx, l.Instance, disk) |
| 120 | if err != nil { |
| 121 | return err |
| 122 | } |
| 123 | @@ -438,16 +463,6 @@ |
| 124 | // or can we safely reuse the installed disk image? |
| 125 | if err := installMacOS(ctx, vm, ipsw); err != nil { |
| 126 | return fmt.Errorf("failed to install macOS: %w", err) |
| 127 | - } |
| 128 | - |
| 129 | - filesToClean := []string{ |
| 130 | - ipsw, |
| 131 | - filepath.Join(l.Instance.Dir, filenames.Image), |
| 132 | - } |
| 133 | - for _, file := range filesToClean { |
| 134 | - if err := os.RemoveAll(file); err != nil { |
| 135 | - logrus.WithError(err).Warnf("Failed to remove %#q", file) |
| 136 | - } |
| 137 | } |
| 138 | |
| 139 | return nil |
| 140 | |
| 141 | |
| 142 | @@ -259,7 +259,7 @@ |
| 143 | // createVMForMacInstaller is similar to createVM but only used for VZMacOSInstaller. |
| 144 | // - Only the primary disk is attached. |
| 145 | // - No network. |
| 146 | -func createVMForMacInstaller(_ context.Context, inst *limatype.Instance) (*vz.VirtualMachine, error) { |
| 147 | +func createVMForMacInstaller(_ context.Context, inst *limatype.Instance, disk string) (*vz.VirtualMachine, error) { |
| 148 | vmConfig, err := createInitialConfig(inst) |
| 149 | if err != nil { |
| 150 | return nil, err |
| 151 | @@ -270,7 +270,6 @@ |
| 152 | } |
| 153 | |
| 154 | // Only attach the primary disk here. cidata.iso is not existent at this point. |
| 155 | - disk := filepath.Join(inst.Dir, filenames.Disk) |
| 156 | diffDiskAttachment, err := vz.NewDiskImageStorageDeviceAttachmentWithCacheAndSync(disk, false, diskImageCachingMode, vz.DiskImageSynchronizationModeFsync) |
| 157 | if err != nil { |
| 158 | return nil, err |
| 159 | |
| 160 | |
| 161 | @@ -65,11 +65,24 @@ |
| 162 | if err != nil { |
| 163 | return nil, err |
| 164 | } |
| 165 | - ipswMostFeatureFul := ipswImage.MostFeaturefulSupportedConfiguration() |
| 166 | - hwModelData = ipswMostFeatureFul.HardwareModel().DataRepresentation() |
| 167 | + if ipswImage == nil { |
| 168 | + return nil, fmt.Errorf("missing restore image for %q", ipsw) |
| 169 | + } |
| 170 | + requirements := ipswImage.MostFeaturefulSupportedConfiguration() |
| 171 | + if requirements == nil || requirements.MinimumSupportedCPUCount() == 0 { |
| 172 | + return nil, fmt.Errorf("IPSW %q has no configuration supported by this Mac", ipsw) |
| 173 | + } |
| 174 | + model := requirements.HardwareModel() |
| 175 | + if model == nil || !model.Supported() || len(model.DataRepresentation()) == 0 { |
| 176 | + return nil, fmt.Errorf("IPSW %q has no supported hardware model", ipsw) |
| 177 | + } |
| 178 | + hwModelData = model.DataRepresentation() |
| 179 | if err = os.WriteFile(hwModelFile, hwModelData, 0o666); err != nil { |
| 180 | return nil, err |
| 181 | } |
| 182 | + } |
| 183 | + if len(hwModelData) == 0 { |
| 184 | + return nil, fmt.Errorf("empty macOS hardware model in %q", hwModelFile) |
| 185 | } |
| 186 | hwModel, err := vz.NewMacHardwareModelWithData(hwModelData) |
| 187 | if err != nil { |
| 188 | |
| 189 | |
| 190 | @@ -54,6 +54,38 @@ |
| 191 | }) |
| 192 | } |
| 193 | |
| 194 | +func TestInterruptedMacOSRestorePreservesMediaAndPublishedDisk(t *testing.T) { |
| 195 | + dir := t.TempDir() |
| 196 | + disk := filepath.Join(dir, filenames.Disk) |
| 197 | + media := filepath.Join(dir, filenames.Image) |
| 198 | + assert.NilError(t, os.WriteFile(media, []byte("source"), 0o600)) |
| 199 | + failure := errors.New("interrupted") |
| 200 | + err := restoreMacOSDisk(disk, func(stage string) error { |
| 201 | + assert.NilError(t, os.WriteFile(stage, []byte("partial"), 0o600)) |
| 202 | + _, statErr := os.Stat(disk) |
| 203 | + assert.Assert(t, errors.Is(statErr, os.ErrNotExist)) |
| 204 | + return failure |
| 205 | + }) |
| 206 | + assert.Assert(t, errors.Is(err, failure)) |
| 207 | + _, err = os.Stat(disk) |
| 208 | + assert.Assert(t, errors.Is(err, os.ErrNotExist)) |
| 209 | + _, err = os.Stat(media) |
| 210 | + assert.NilError(t, err) |
| 211 | + // A killed installer cannot execute its deferred cleanup. |
| 212 | + assert.NilError(t, os.WriteFile(disk+".installing", []byte("partial"), 0o600)) |
| 213 | + assert.NilError(t, restoreMacOSDisk(disk, func(stage string) error { |
| 214 | + _, err := os.Stat(stage) |
| 215 | + assert.Assert(t, errors.Is(err, os.ErrNotExist)) |
| 216 | + return os.WriteFile(stage, []byte("ready"), 0o600) |
| 217 | + })) |
| 218 | + data, err := os.ReadFile(disk) |
| 219 | + assert.NilError(t, err) |
| 220 | + assert.Equal(t, string(data), "ready") |
| 221 | + _, err = os.Stat(media) |
| 222 | + assert.Assert(t, errors.Is(err, os.ErrNotExist)) |
| 223 | + assert.NilError(t, restoreMacOSDisk(disk, func(string) error { return errors.New("attempted to reinstall an existing guest disk") })) |
| 224 | +} |
| 225 | + |
| 226 | func TestRemovePIDFile(t *testing.T) { |
| 227 | dir := t.TempDir() |
| 228 | l := &LimaVzDriver{ |