| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "runtime" |
| 9 | |
| 10 | "github.com/sylvinus/agent-vm/internal/guestos" |
| 11 | "github.com/sylvinus/agent-vm/internal/hostimage" |
| 12 | "github.com/sylvinus/agent-vm/internal/limaembed" |
| 13 | ) |
| 14 | |
| 15 | func linuxGuestSystems(platform, arch string) []string { |
| 16 | if platform == "darwin" && arch == "arm64" || (platform == "windows" || platform == "linux") && arch == "amd64" { |
| 17 | return []string{guestos.Ubuntu2204} |
| 18 | } |
| 19 | return nil |
| 20 | } |
| 21 | |
| 22 | func linuxGuestImagePlan(ctx context.Context, profile, system, arch string) (hostimage.Plan, error) { |
| 23 | if system != guestos.Ubuntu2204 || arch != "amd64" && arch != "arm64" { |
| 24 | return hostimage.Plan{}, errors.New("此主机暂不支持所选 Linux 客机镜像") |
| 25 | } |
| 26 | template, err := limaembed.ReadImageTemplate(system) |
| 27 | if err != nil { |
| 28 | return hostimage.Plan{}, err |
| 29 | } |
| 30 | home, err := os.UserHomeDir() |
| 31 | if err != nil { |
| 32 | return hostimage.Plan{}, err |
| 33 | } |
| 34 | identity := hostimage.Identity{System: system, Version: "22.04", Arch: arch} |
| 35 | |
| 36 | |
| 37 | return hostimage.Resolve(ctx, identity, filepath.Join(profile, "state", "guest-media", system), filepath.Join(home, "Downloads"), template, nil) |
| 38 | } |
| 39 | |
| 40 | func (s *Server) installedLinuxSystem() (string, error) { |
| 41 | return hostimage.LinuxSystem(filepath.Join(s.profile, "state")) |
| 42 | } |
| 43 | |
| 44 | func canInstallCatalogLinux(system string) bool { |
| 45 | for _, candidate := range linuxGuestSystems(runtime.GOOS, runtime.GOARCH) { |
| 46 | if system == candidate { |
| 47 | return true |
| 48 | } |
| 49 | } |
| 50 | return false |
| 51 | } |