| 1 | package guestos |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/hex" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "io" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "strings" |
| 12 | |
| 13 | "github.com/google/uuid" |
| 14 | "github.com/sylvinus/agent-vm/internal/hostimage" |
| 15 | ) |
| 16 | |
| 17 | |
| 18 | |
| 19 | type Installation struct { |
| 20 | Version int `json:"version"` |
| 21 | System string `json:"system"` |
| 22 | Media string `json:"media,omitempty"` |
| 23 | SHA256 string `json:"sha256,omitempty"` |
| 24 | EditorTerms bool `json:"editor_terms,omitempty"` |
| 25 | ID uuid.UUID `json:"id"` |
| 26 | Source hostimage.Plan `json:"source,omitzero"` |
| 27 | |
| 28 | |
| 29 | SourceKind string `json:"source_kind,omitempty"` |
| 30 | } |
| 31 | |
| 32 | const installRecord = "guest-install.json" |
| 33 | const LinuxImageSource = "linux-image" |
| 34 | const Ubuntu2204 = "ubuntu-22.04" |
| 35 | |
| 36 | |
| 37 | |
| 38 | func WindowsCloneCleanupPending(profile string) (bool, error) { |
| 39 | install, err := LoadInstallation(profile) |
| 40 | if err != nil || install == nil || install.Source.Host.System != Windows11 { |
| 41 | return false, err |
| 42 | } |
| 43 | path := filepath.Join(profile, "windows-setup-"+strings.ReplaceAll(install.ID.String(), "-", "")+".dpapi") |
| 44 | _, err = os.Lstat(path) |
| 45 | if errors.Is(err, os.ErrNotExist) { |
| 46 | return false, nil |
| 47 | } |
| 48 | return err == nil, err |
| 49 | } |
| 50 | |
| 51 | func (i Installation) Validate() error { |
| 52 | if i.Version != 1 || i.ID == uuid.Nil || (i.System != MacOS && i.System != Windows11 && !hostimage.IsLinux(i.System)) { |
| 53 | return errors.New("客户机安装记录无效") |
| 54 | } |
| 55 | if i.SourceKind != "" && (i.SourceKind != LinuxImageSource || i.System != Ubuntu2204 || i.Source.Host.System != i.System) { |
| 56 | return errors.New("客户机镜像选择方式无效") |
| 57 | } |
| 58 | if i.Media != "" && (!filepath.IsAbs(i.Media) || strings.ContainsAny(i.Media, "\r\n\x00") || filepath.Clean(i.Media) != i.Media) { |
| 59 | return errors.New("请选择本机的安装镜像") |
| 60 | } |
| 61 | if i.Source.Host.System != "" { |
| 62 | if i.System != i.Source.Host.System || (i.System == Windows11 && !i.EditorTerms) { |
| 63 | return errors.New("本机系统安装选择无效,请重新检查") |
| 64 | } |
| 65 | if i.Media != "" && i.Media != i.Source.Path { |
| 66 | return errors.New("安装介质与已选择的本机系统来源不一致") |
| 67 | } |
| 68 | return i.Source.Validate() |
| 69 | } |
| 70 | if hostimage.IsLinux(i.System) { |
| 71 | return errors.New("缺少 Linux 本机系统安装来源") |
| 72 | } |
| 73 | if i.System == MacOS { |
| 74 | if i.SHA256 != "" || (i.Media != "" && !strings.EqualFold(filepath.Ext(i.Media), ".ipsw")) { |
| 75 | return errors.New("请选择 Apple 的 .ipsw 恢复镜像") |
| 76 | } |
| 77 | } else if hash, err := hex.DecodeString(i.SHA256); i.Media == "" || !i.EditorTerms || err != nil || len(hash) != 32 || !strings.EqualFold(filepath.Ext(i.Media), ".iso") { |
| 78 | return errors.New("请选择 Windows 11 x64 ISO,并填写这份镜像的完整 SHA-256") |
| 79 | } |
| 80 | return nil |
| 81 | } |
| 82 | |
| 83 | func LoadInstallation(profile string) (*Installation, error) { |
| 84 | path := filepath.Join(profile, installRecord) |
| 85 | info, err := os.Lstat(path) |
| 86 | if errors.Is(err, os.ErrNotExist) { |
| 87 | return nil, nil |
| 88 | } |
| 89 | if err != nil { |
| 90 | return nil, err |
| 91 | } |
| 92 | if !info.Mode().IsRegular() || info.Size() > 32<<10 { |
| 93 | return nil, errors.New("客户机安装记录无法读取") |
| 94 | } |
| 95 | data, err := os.ReadFile(path) |
| 96 | if err != nil { |
| 97 | return nil, err |
| 98 | } |
| 99 | var i Installation |
| 100 | decoder := json.NewDecoder(bytes.NewReader(data)) |
| 101 | decoder.DisallowUnknownFields() |
| 102 | if decoder.Decode(&i) != nil || decoder.Decode(new(any)) != io.EOF { |
| 103 | return nil, errors.New("客户机安装记录格式无效") |
| 104 | } |
| 105 | if err := i.Validate(); err != nil { |
| 106 | return nil, err |
| 107 | } |
| 108 | if i.Source.Host.System != "" { |
| 109 | var ready struct { |
| 110 | ID uuid.UUID `json:"id"` |
| 111 | Source hostimage.Plan `json:"source"` |
| 112 | } |
| 113 | mediaPath := filepath.Join(profile, "guest-install-media-"+i.ID.String()+".json") |
| 114 | info, err := os.Lstat(mediaPath) |
| 115 | if err == nil { |
| 116 | if !info.Mode().IsRegular() || info.Size() > 32<<10 { |
| 117 | return nil, errors.New("已校验的安装材料记录无效") |
| 118 | } |
| 119 | data, readErr := os.ReadFile(mediaPath) |
| 120 | if readErr != nil || json.Unmarshal(data, &ready) != nil || ready.ID != i.ID { |
| 121 | return nil, errors.New("无法读取已校验的安装材料") |
| 122 | } |
| 123 | if err := matchPreparedMedia(i.Source, ready.Source); err != nil { |
| 124 | return nil, err |
| 125 | } |
| 126 | i.Source, i.Media = ready.Source, ready.Source.Path |
| 127 | i.SHA256 = strings.TrimPrefix(ready.Source.Digest, "sha256:") |
| 128 | } else if !errors.Is(err, os.ErrNotExist) { |
| 129 | return nil, err |
| 130 | } |
| 131 | } |
| 132 | return &i, i.Validate() |
| 133 | } |
| 134 | |
| 135 | func SaveInstallation(profile string, i Installation) error { |
| 136 | if err := i.Validate(); err != nil { |
| 137 | return err |
| 138 | } |
| 139 | if current, err := LoadInstallation(profile); err != nil { |
| 140 | return err |
| 141 | } else if current != nil { |
| 142 | if *current == i { |
| 143 | return nil |
| 144 | } |
| 145 | return errors.New("已有其他安装选择,请继续原安装") |
| 146 | } |
| 147 | data, err := json.Marshal(i) |
| 148 | if err != nil { |
| 149 | return err |
| 150 | } |
| 151 | return publishBinding(profile, installRecord, append(data, '\n')) |
| 152 | } |
| 153 | |
| 154 | func matchPreparedMedia(chosen, ready hostimage.Plan) error { |
| 155 | if err := ready.Validate(); err != nil { |
| 156 | return err |
| 157 | } |
| 158 | if ready.Path == "" || ready.Digest == "" || ready.Host != chosen.Host || ready.Version != chosen.Version || ready.Build != chosen.Build || |
| 159 | ready.Format != chosen.Format || ready.URL != chosen.URL || (chosen.Digest != "" && ready.Digest != chosen.Digest) || |
| 160 | (chosen.Bytes != 0 && ready.Bytes != chosen.Bytes) || (chosen.Path != "" && ready.Path != chosen.Path) || |
| 161 | (chosen.Modified != 0 && ready.Modified != chosen.Modified) { |
| 162 | return errors.New("已校验的系统材料与原安装选择不一致") |
| 163 | } |
| 164 | return nil |
| 165 | } |
| 166 | |
| 167 | |
| 168 | |
| 169 | func RecordPreparedMedia(profile string, id uuid.UUID, ready hostimage.Plan) error { |
| 170 | current, err := LoadInstallation(profile) |
| 171 | if err != nil { |
| 172 | return err |
| 173 | } |
| 174 | if current == nil || current.ID != id { |
| 175 | return errors.New("安装选择已变化") |
| 176 | } |
| 177 | if err := matchPreparedMedia(current.Source, ready); err != nil { |
| 178 | return err |
| 179 | } |
| 180 | data, err := json.Marshal(struct { |
| 181 | ID uuid.UUID `json:"id"` |
| 182 | Source hostimage.Plan `json:"source"` |
| 183 | }{id, ready}) |
| 184 | if err != nil { |
| 185 | return err |
| 186 | } |
| 187 | return publishBinding(profile, "guest-install-media-"+id.String()+".json", data) |
| 188 | } |
| 189 | |
| 190 | func FinishLinuxInstallation(profile string, id uuid.UUID) error { |
| 191 | installed, err := LoadInstallation(profile) |
| 192 | if err != nil { |
| 193 | return err |
| 194 | } |
| 195 | if installed == nil || installed.ID != id || !hostimage.IsLinux(installed.System) { |
| 196 | return errors.New("Linux 安装记录已变化") |
| 197 | } |
| 198 | |
| 199 | system, err := hostimage.LinuxSystem(filepath.Join(profile, "state")) |
| 200 | if err != nil { |
| 201 | return err |
| 202 | } |
| 203 | if system != installed.System { |
| 204 | return errors.New("Linux 安装尚未保存所选系统,保留安装记录以便继续") |
| 205 | } |
| 206 | return os.Remove(filepath.Join(profile, installRecord)) |
| 207 | } |
| 208 | |
| 209 | |
| 210 | |
| 211 | func UnstartedInstallation(profile string, i Installation) (bool, error) { |
| 212 | paths := []string{"macos-guest.json", "windows-guest.json", "windows-install-vm.txt", |
| 213 | filepath.Join("state", "windows-"+strings.ReplaceAll(i.ID.String(), "-", "")), |
| 214 | filepath.Join("state", "lima", "mac"), filepath.Join("state", "lima", "base-building"), filepath.Join("state", "lima", "base")} |
| 215 | for _, rel := range paths { |
| 216 | _, err := os.Lstat(filepath.Join(profile, rel)) |
| 217 | if err == nil { |
| 218 | return false, nil |
| 219 | } |
| 220 | if !errors.Is(err, os.ErrNotExist) { |
| 221 | return false, err |
| 222 | } |
| 223 | } |
| 224 | return true, nil |
| 225 | } |
| 226 | |
| 227 | func ClearUnstartedInstallation(profile string, id uuid.UUID) error { |
| 228 | pending, err := LoadInstallation(profile) |
| 229 | if err != nil { |
| 230 | return err |
| 231 | } |
| 232 | if pending == nil || pending.ID != id { |
| 233 | return errors.New("客户机安装选择已改变") |
| 234 | } |
| 235 | canClear, err := UnstartedInstallation(profile, *pending) |
| 236 | if err != nil { |
| 237 | return err |
| 238 | } |
| 239 | if !canClear { |
| 240 | return errors.New("已开始创建客户机,请继续原安装") |
| 241 | } |
| 242 | if hostimage.IsLinux(pending.System) { |
| 243 | if err := hostimage.ClearLinuxTemplate(filepath.Join(profile, "state"), pending.Source); err != nil { |
| 244 | return err |
| 245 | } |
| 246 | } |
| 247 | return os.Remove(filepath.Join(profile, installRecord)) |
| 248 | } |
| 249 | |
| 250 | |
| 251 | |
| 252 | func ValidMacOSInstallPassword(value string) bool { |
| 253 | return len(value) >= 8 && len(value) <= 256 && strings.IndexFunc(value, func(r rune) bool { return r < ' ' || r > '~' }) < 0 |
| 254 | } |