| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "os/user" |
| 9 | "path/filepath" |
| 10 | "runtime" |
| 11 | "slices" |
| 12 | "strings" |
| 13 | ) |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | func qemuBinary() (bin, install string) { |
| 19 | manager := "" |
| 20 | if _, err := exec.LookPath("apt-get"); err == nil { |
| 21 | manager = "apt-get" |
| 22 | } else if _, err := exec.LookPath("dnf"); err == nil { |
| 23 | manager = "dnf" |
| 24 | } |
| 25 | return qemuBinaryFor(runtime.GOARCH, manager) |
| 26 | } |
| 27 | |
| 28 | func qemuBinaryFor(goarch, manager string) (bin, install string) { |
| 29 | bin, deb, rpm := "qemu-system-"+goarch, "qemu-system", "qemu-system-"+goarch+"-core" |
| 30 | switch goarch { |
| 31 | case "amd64": |
| 32 | bin, deb, rpm = "qemu-system-x86_64", "qemu-system-x86", "qemu-system-x86-core" |
| 33 | case "arm64": |
| 34 | bin, deb, rpm = "qemu-system-aarch64", "qemu-system-arm", "qemu-system-aarch64-core" |
| 35 | } |
| 36 | switch manager { |
| 37 | case "dnf": |
| 38 | return bin, "sudo dnf install " + rpm + " qemu-img" |
| 39 | case "apt-get": |
| 40 | return bin, "sudo apt-get install " + deb + " qemu-utils" |
| 41 | } |
| 42 | return bin, "your distribution's QEMU package (with Nix, 'nix run github:sylvinus/agent-vm' brings its own)" |
| 43 | } |
| 44 | |
| 45 | |
| 46 | func WSLVersion() int { |
| 47 | b, err := os.ReadFile("/proc/sys/kernel/osrelease") |
| 48 | rel := string(b) |
| 49 | switch { |
| 50 | case err != nil: |
| 51 | return 0 |
| 52 | case strings.Contains(rel, "WSL2"): |
| 53 | return 2 |
| 54 | case strings.Contains(strings.ToLower(rel), "microsoft"): |
| 55 | return 1 |
| 56 | } |
| 57 | return 0 |
| 58 | } |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | func LinuxPrereqs(w io.Writer) bool { |
| 64 | if runtime.GOOS != "linux" { |
| 65 | return true |
| 66 | } |
| 67 | ok := true |
| 68 | bin, install := qemuBinary() |
| 69 | |
| 70 | |
| 71 | for _, b := range []string{bin, "qemu-img"} { |
| 72 | if _, err := exec.LookPath(b); err != nil { |
| 73 | fmt.Fprintf(w, "Error: Lima needs '%s' on PATH (not found).\n Install with: %s\n", b, install) |
| 74 | ok = false |
| 75 | } |
| 76 | } |
| 77 | _, err := os.Stat("/dev/kvm") |
| 78 | switch { |
| 79 | case err != nil: |
| 80 | fmt.Fprintln(w, "Error: /dev/kvm does not exist (KVM unavailable).") |
| 81 | |
| 82 | |
| 83 | switch WSLVersion() { |
| 84 | case 1: |
| 85 | fmt.Fprintln(w, " This looks like WSL1, which cannot run VMs.\n Upgrade the distribution to WSL2: wsl --set-version <distro> 2") |
| 86 | case 2: |
| 87 | fmt.Fprintln(w, " This looks like WSL2 without nested virtualization: KVM is not\n passed through from the Windows host. Update WSL (wsl --update),\n enable virtualization on the host, then retry.") |
| 88 | default: |
| 89 | fmt.Fprintln(w, " Hardware virtualization may be disabled in BIOS, or the kernel\n lacks KVM support (nested virt in a guest VM, etc.).") |
| 90 | } |
| 91 | ok = false |
| 92 | case !canReadWrite("/dev/kvm"): |
| 93 | fmt.Fprintln(w, "Error: /dev/kvm exists but you don't have read/write access.") |
| 94 | if !inGroup("kvm") { |
| 95 | fmt.Fprintln(w, " Fix: sudo usermod -aG kvm \"$USER\"\n Then log out and back in (or run 'newgrp kvm') so the new\n group membership takes effect.") |
| 96 | } else { |
| 97 | fmt.Fprintln(w, " You're already in the kvm group but /dev/kvm denies access.\n Check ownership/mode: ls -l /dev/kvm") |
| 98 | } |
| 99 | ok = false |
| 100 | } |
| 101 | return ok |
| 102 | } |
| 103 | |
| 104 | |
| 105 | func canReadWrite(p string) bool { |
| 106 | f, err := os.OpenFile(p, os.O_RDWR, 0) |
| 107 | if err != nil { |
| 108 | return false |
| 109 | } |
| 110 | f.Close() |
| 111 | return true |
| 112 | } |
| 113 | |
| 114 | func inGroup(name string) bool { |
| 115 | u, err := user.Current() |
| 116 | if err != nil { |
| 117 | return false |
| 118 | } |
| 119 | ids, _ := u.GroupIds() |
| 120 | g, err := user.LookupGroup(name) |
| 121 | return err == nil && slices.Contains(ids, g.Gid) |
| 122 | } |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | func WindowsPrereqs(out, w io.Writer) bool { |
| 129 | if runtime.GOOS != "windows" { |
| 130 | return true |
| 131 | } |
| 132 | if err := ConfigureWindowsRuntime(); err != nil { |
| 133 | fmt.Fprintln(w, err) |
| 134 | return false |
| 135 | } |
| 136 | if err := windowsHypervisorReady(); err != nil { |
| 137 | fmt.Fprintln(w, err) |
| 138 | return false |
| 139 | } |
| 140 | bin, _ := qemuBinary() |
| 141 | _, imageErr := exec.LookPath("qemu-img") |
| 142 | if _, err := exec.LookPath(bin); err == nil && imageErr == nil { |
| 143 | return true |
| 144 | } |
| 145 | dir := os.Getenv("AGENT_VM_QEMU_DIR") |
| 146 | if dir == "" { |
| 147 | dir = `C:\Program Files\qemu` |
| 148 | } |
| 149 | if _, err := os.Stat(filepath.Join(dir, bin+".exe")); err == nil { |
| 150 | if _, err := os.Stat(filepath.Join(dir, "qemu-img.exe")); err == nil { |
| 151 | os.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH")) |
| 152 | return true |
| 153 | } |
| 154 | } |
| 155 | fmt.Fprintf(w, "Error: Lima needs '%s' on PATH (not found).\n Install QEMU with: winget install -e --id SoftwareFreedomConservancy.QEMU\n QEMU also needs the 'Windows Hypervisor Platform' Windows feature (see 'agent-vm doctor').\n", bin) |
| 156 | return false |
| 157 | } |
| 158 | |
| 159 | |
| 160 | |
| 161 | const WHPXOn = "DISM /Online /Enable-Feature /FeatureName:HypervisorPlatform /All" |
| 162 | |
| 163 | |
| 164 | |
| 165 | func WindowsStartHint(w io.Writer, logs ...string) { |
| 166 | if runtime.GOOS != "windows" { |
| 167 | return |
| 168 | } |
| 169 | for _, l := range logs { |
| 170 | if b, err := os.ReadFile(l); err == nil && strings.Contains(strings.ToLower(string(b)), "whpx") { |
| 171 | if windowsHypervisorReady() == nil { |
| 172 | fmt.Fprintln(w, "Windows Hypervisor Platform is enabled, but QEMU could not initialize WHPX. Check the original QEMU error and any custom QEMU arguments; Cage uses kernel-irqchip=off for WHPX compatibility.") |
| 173 | return |
| 174 | } |
| 175 | fmt.Fprintf(w, `QEMU could not use the Windows hypervisor. It needs the "Windows Hypervisor |
| 176 | Platform" Windows feature, which an administrator turns on once, followed by |
| 177 | a reboot: in Windows Features, or from an administrator terminal: |
| 178 | %s |
| 179 | On a managed laptop, that is a request to your IT department. |
| 180 | `, WHPXOn) |
| 181 | return |
| 182 | } |
| 183 | } |
| 184 | } |