| 1 | package vm |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | |
| 11 | "github.com/docker/go-units" |
| 12 | "github.com/sylvinus/agent-vm/internal/cage" |
| 13 | "go.yaml.in/yaml/v3" |
| 14 | ) |
| 15 | |
| 16 | |
| 17 | |
| 18 | type MacOSResourceRequirements struct { |
| 19 | Version int `json:"version"` |
| 20 | MemoryGiB int `json:"minimum_memory_gib"` |
| 21 | CPUs int `json:"minimum_cpus"` |
| 22 | Source string `json:"source"` |
| 23 | } |
| 24 | |
| 25 | const macOSRequirementsFile = "cage-macos-resource-requirements.json" |
| 26 | |
| 27 | func (requirements MacOSResourceRequirements) Limits(allocation cage.ResourceAllocation) (int, int, error) { |
| 28 | memory, cpus, err := allocation.Limits() |
| 29 | if err != nil { |
| 30 | return 0, 0, err |
| 31 | } |
| 32 | if memory < requirements.MemoryGiB || cpus < requirements.CPUs { |
| 33 | return 0, 0, fmt.Errorf("这台 macOS 客机至少需要 %d GiB 内存与 %d 个 CPU 逻辑核心,请调整资源配置", requirements.MemoryGiB, requirements.CPUs) |
| 34 | } |
| 35 | return memory, cpus, nil |
| 36 | } |
| 37 | |
| 38 | func ReadMacOSResourceRequirements(instanceDir string) (MacOSResourceRequirements, error) { |
| 39 | var requirements MacOSResourceRequirements |
| 40 | data, err := os.ReadFile(filepath.Join(instanceDir, macOSRequirementsFile)) |
| 41 | if err == nil { |
| 42 | if json.Unmarshal(data, &requirements) != nil || requirements.Version != 1 || requirements.MemoryGiB < 1 || requirements.CPUs < 1 || |
| 43 | (requirements.Source != "restore-image" && requirements.Source != "existing-configuration") { |
| 44 | return requirements, errors.New("macOS 最低资源要求记录无效,请检查原环境配置") |
| 45 | } |
| 46 | if requirements.Source == "restore-image" { |
| 47 | return requirements, nil |
| 48 | } |
| 49 | } |
| 50 | if err != nil && !errors.Is(err, os.ErrNotExist) { |
| 51 | return requirements, errors.New("无法读取 macOS 最低资源要求记录") |
| 52 | } |
| 53 | data, err = os.ReadFile(filepath.Join(instanceDir, "lima.yaml")) |
| 54 | if err != nil { |
| 55 | return requirements, errors.New("无法读取这台 macOS 客机的启动配置") |
| 56 | } |
| 57 | var config struct { |
| 58 | OS string `yaml:"os"` |
| 59 | Memory string `yaml:"memory"` |
| 60 | CPUs int `yaml:"cpus"` |
| 61 | Images []struct { |
| 62 | Location string `yaml:"location"` |
| 63 | } `yaml:"images"` |
| 64 | } |
| 65 | if yaml.Unmarshal(data, &config) != nil || config.OS != "Darwin" { |
| 66 | return requirements, errors.New("macOS 客机启动配置无效") |
| 67 | } |
| 68 | candidates := []string{filepath.Join(instanceDir, "image.ipsw")} |
| 69 | if len(config.Images) == 1 && filepath.IsAbs(config.Images[0].Location) { |
| 70 | candidates = append(candidates, config.Images[0].Location) |
| 71 | } |
| 72 | for _, image := range candidates { |
| 73 | if info, err := os.Stat(image); err == nil && info.Mode().IsRegular() { |
| 74 | if requirements, err := macOSRestoreResourceRequirements(image); err == nil { |
| 75 | return requirements, nil |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | if requirements.Version == 1 { |
| 80 | return requirements, nil |
| 81 | } |
| 82 | memory, err := units.RAMInBytes(config.Memory) |
| 83 | if err != nil || memory < 1<<30 || config.CPUs < 1 { |
| 84 | return requirements, errors.New("无法确定已有 macOS 客机的可启动资源配置") |
| 85 | } |
| 86 | return MacOSResourceRequirements{Version: 1, MemoryGiB: int((memory-1)/(1<<30) + 1), CPUs: config.CPUs, Source: "existing-configuration"}, nil |
| 87 | } |
| 88 | |
| 89 | func saveMacOSResourceRequirements(instanceDir string, requirements MacOSResourceRequirements) error { |
| 90 | data, err := json.Marshal(requirements) |
| 91 | if err != nil { |
| 92 | return err |
| 93 | } |
| 94 | if current, err := os.ReadFile(filepath.Join(instanceDir, macOSRequirementsFile)); err == nil && bytes.Equal(current, data) { |
| 95 | return nil |
| 96 | } |
| 97 | file, err := os.CreateTemp(instanceDir, ".macos-resources-*") |
| 98 | if err != nil { |
| 99 | return err |
| 100 | } |
| 101 | defer os.Remove(file.Name()) |
| 102 | _, err = file.Write(data) |
| 103 | if err = errors.Join(err, file.Close()); err != nil { |
| 104 | return err |
| 105 | } |
| 106 | return os.Rename(file.Name(), filepath.Join(instanceDir, macOSRequirementsFile)) |
| 107 | } |