跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

prereqs.go

184 行 · 6.1 KB历史下载
1package host
2
3import (
4 "fmt"
5 "io"
6 "os"
7 "os/exec"
8 "os/user"
9 "path/filepath"
10 "runtime"
11 "slices"
12 "strings"
13)
14
15// qemuBinary is QEMU's emulator for this host's architecture, and the
16// way to install it: the command with apt-get's package, or dnf's where there
17// is dnf and no apt-get; a general hint where there is neither (NixOS, Arch).
18func 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
28func 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// WSLVersion is 2 or 1 under WSL, from the kernel release, 0 elsewhere.
46func 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// LinuxPrereqs checks what Lima needs to run a QEMU VM on Linux, and says
61// how to get it: without this, a failed start says only that it failed. True
62// elsewhere.
63func LinuxPrereqs(w io.Writer) bool {
64 if runtime.GOOS != "linux" {
65 return true
66 }
67 ok := true
68 bin, install := qemuBinary()
69 // Fedora packages qemu-img apart from the emulator, and Lima runs it on
70 // the disk image.
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 // Inside WSL the fix is on the Windows side: WSL1 can never run VMs,
82 // and WSL2 needs the host to let KVM through.
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// canReadWrite: opening it, which /dev/kvm allows without doing anything.
105func 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
114func 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// WindowsPrereqs checks for QEMU and queries the Windows Hypervisor Platform
125// capability without elevation.
126// winget's QEMU does not add itself to PATH:
127// its default folder goes on PATH for this process. True elsewhere.
128func 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// WHPXOn is what an administrator runs, once, then reboots, for QEMU to use
160// WHPX.
161const WHPXOn = "DISM /Online /Enable-Feature /FeatureName:HypervisorPlatform /All"
162
163// WindowsStartHint distinguishes an unavailable hypervisor from a QEMU
164// initialization failure on a host where WHPX is already enabled.
165func 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
176Platform" Windows feature, which an administrator turns on once, followed by
177a reboot: in Windows Features, or from an administrator terminal:
178 %s
179On a managed laptop, that is a request to your IT department.
180`, WHPXOn)
181 return
182 }
183 }
184}