|
| 1 | +package images |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + |
| 8 | + "github.com/kernel/hypeman/lib/forkvm" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + MacOSMachineVersionLabel = "io.hypeman.machine-image.version" |
| 13 | + MacOSMachineKindLabel = "io.hypeman.machine-image.kind" |
| 14 | + MacOSMachineDiskLabel = "io.hypeman.machine-image.disk-path" |
| 15 | + MacOSMachineFormatLabel = "io.hypeman.machine-image.disk-format" |
| 16 | + MacOSMachineAuxLabel = "io.hypeman.machine-image.aux-path" |
| 17 | + MacOSMachinePlatformLabel = "io.hypeman.machine-image.platform-path" |
| 18 | +) |
| 19 | + |
| 20 | +// stageMacOSMachine copies a validated bundle's boot disk and auxiliary storage to |
| 21 | +// build-private paths, outside the manager lock. It returns the bytes both files |
| 22 | +// occupy, which is the size recorded for accounting. |
| 23 | +func stageMacOSMachine(payload *macOSMachinePayload, diskTemp, auxTemp string) (stagedImageFiles, error) { |
| 24 | + diskSize, err := stageMachineFile(payload.Disk, diskTemp) |
| 25 | + if err != nil { |
| 26 | + return stagedImageFiles{}, fmt.Errorf("stage boot disk: %w", err) |
| 27 | + } |
| 28 | + auxSize, err := stageMachineFile(payload.Aux, auxTemp) |
| 29 | + if err != nil { |
| 30 | + return stagedImageFiles{}, fmt.Errorf("stage auxiliary storage: %w", err) |
| 31 | + } |
| 32 | + return stagedImageFiles{disk: diskTemp, aux: auxTemp, macos: payload.Platform, sizeBytes: diskSize + auxSize}, nil |
| 33 | +} |
| 34 | + |
| 35 | +func stageMachineFile(src, dst string) (int64, error) { |
| 36 | + // Copy helpers create the destination with the registry-supplied source mode, |
| 37 | + // and a multi-GB copy would expose contents before any later chmod. A fresh |
| 38 | + // 0700 directory blocks other local users during the copy; the final file is |
| 39 | + // then made 0600 before it appears at the canonical staging path. |
| 40 | + private, err := os.MkdirTemp(filepath.Dir(dst), ".machine-stage-") |
| 41 | + if err != nil { |
| 42 | + return 0, err |
| 43 | + } |
| 44 | + defer os.RemoveAll(private) |
| 45 | + staged := filepath.Join(private, filepath.Base(dst)) |
| 46 | + if err := forkvm.CopyRegularFile(src, staged); err != nil { |
| 47 | + return 0, err |
| 48 | + } |
| 49 | + if err := os.Chmod(staged, 0600); err != nil { |
| 50 | + return 0, err |
| 51 | + } |
| 52 | + if err := os.Rename(staged, dst); err != nil { |
| 53 | + return 0, err |
| 54 | + } |
| 55 | + info, err := os.Stat(dst) |
| 56 | + if err != nil { |
| 57 | + return 0, err |
| 58 | + } |
| 59 | + return info.Size(), nil |
| 60 | +} |
| 61 | + |
| 62 | +// checkMacOSMachineMetadata validates darwin labels from the config blob, so |
| 63 | +// malformed artifacts fail before layer unpacking. Non-darwin returns false. |
| 64 | +func checkMacOSMachineMetadata(meta *containerMetadata) (bool, error) { |
| 65 | + // Normalize as resolveManifestPlatform does, so aliases such as aarch64 and |
| 66 | + // case variants such as Darwin select the machine path rather than rootfs. |
| 67 | + normalized := Platform{OS: meta.OS, Architecture: meta.Architecture, Variant: meta.Variant}.Normalize() |
| 68 | + if normalized.OS != "darwin" { |
| 69 | + return false, nil |
| 70 | + } |
| 71 | + if normalized.Architecture != "arm64" || normalized.Variant != "" { |
| 72 | + return true, fmt.Errorf("macOS machine requires darwin/arm64") |
| 73 | + } |
| 74 | + labels := meta.Labels |
| 75 | + if labels[MacOSMachineVersionLabel] != "1" || labels[MacOSMachineKindLabel] != "macos-image" || labels[MacOSMachineFormatLabel] != "raw" { |
| 76 | + return true, fmt.Errorf("darwin artifact requires version 1 macos-image with raw disk, not an ordinary container image") |
| 77 | + } |
| 78 | + return true, nil |
| 79 | +} |
| 80 | + |
| 81 | +func parseMacOSMachine(root string, meta *containerMetadata) (*macOSMachinePayload, error) { |
| 82 | + darwin, err := checkMacOSMachineMetadata(meta) |
| 83 | + if err != nil || !darwin { |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + labels := meta.Labels |
| 87 | + return readMacOSMachineBundle(root, labels[MacOSMachineDiskLabel], labels[MacOSMachineAuxLabel], labels[MacOSMachinePlatformLabel], false) |
| 88 | +} |
0 commit comments