跳到主要内容

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

浏览文件

0007-qemu-features-own-outputs.patch

147 行 · 5.0 KB历史下载
1Subject: [PATCH] qemu: give each help request its own output
2
3inspectFeatures runs `qemu -accel help`, `-netdev help`, `-machine help` and
4`-cpu help` into the same two buffers, and keeps each output as the buffer's
5Bytes() after its run. NetdevHelp is the accelerators' text followed by the
6netdevs', MachineHelp has both and its own, CPUHelp all four. The features
7are searched as text (strings.Contains): a word of an earlier help can match
8a later one's feature.
9
10Each request has its own buffers now (qemuHelp).
11
12Test: TestInspectFeaturesOwnOutputs.
13
14---
15diff --git a/pkg/driver/qemu/features_test.go b/pkg/driver/qemu/features_test.go
16new file mode 100644
17index 00000000..16228077
18--- /dev/null
19+++ b/pkg/driver/qemu/features_test.go
20@@ -0,0 +1,36 @@
21+//go:build !windows
22+
23+// SPDX-FileCopyrightText: Copyright The Lima Authors
24+// SPDX-License-Identifier: Apache-2.0
25+
26+package qemu
27+
28+import (
29+ "context"
30+ "os"
31+ "path/filepath"
32+ "testing"
33+
34+ "gotest.tools/v3/assert"
35+)
36+
37+// Each help text is its own: the features are searched as text, and a netdev
38+// list holding the accelerators' words would match them.
39+func TestInspectFeaturesOwnOutputs(t *testing.T) {
40+ exe := filepath.Join(t.TempDir(), "qemu-fake")
41+ script := `#!/bin/sh
42+case "$*" in
43+ *-cpu*) echo "cpus: max" ;;
44+ *-accel*) echo "accels: tcg" ;;
45+ *-netdev*) echo "netdevs: dgram" ;;
46+ *-machine*) echo "machines: virt" ;;
47+esac
48+`
49+ assert.NilError(t, os.WriteFile(exe, []byte(script), 0o755))
50+ f, err := inspectFeatures(context.Background(), exe, "virt")
51+ assert.NilError(t, err)
52+ assert.Equal(t, string(f.AccelHelp), "accels: tcg\n")
53+ assert.Equal(t, string(f.NetdevHelp), "netdevs: dgram\n")
54+ assert.Equal(t, string(f.MachineHelp), "machines: virt\n")
55+ assert.Equal(t, string(f.CPUHelp), "cpus: max\n")
56+}
57diff --git a/pkg/driver/qemu/qemu.go b/pkg/driver/qemu/qemu.go
58index 1bb7bccf..79318857 100644
59--- a/pkg/driver/qemu/qemu.go
60+++ b/pkg/driver/qemu/qemu.go
61@@ -310,61 +310,40 @@ type features struct {
62 }
63
64 func inspectFeatures(ctx context.Context, exe, machine string) (*features, error) {
65- var (
66- f features
67- stdout bytes.Buffer
68- stderr bytes.Buffer
69- )
70- cmd := exec.CommandContext(ctx, exe, "-M", "none", "-accel", "help")
71- cmd.Stdout = &stdout
72- cmd.Stderr = &stderr
73- if err := cmd.Run(); err != nil {
74- return nil, fmt.Errorf("failed to run %v: stdout=%#q, stderr=%#q", cmd.Args, stdout.String(), stderr.String())
75+ var f features
76+ var err error
77+ if f.AccelHelp, err = qemuHelp(ctx, exe, "-M", "none", "-accel", "help"); err != nil {
78+ return nil, err
79 }
80- f.AccelHelp = stdout.Bytes()
81- // on older versions qemu will write "help" output to stderr
82- if len(f.AccelHelp) == 0 {
83- f.AccelHelp = stderr.Bytes()
84+ if f.NetdevHelp, err = qemuHelp(ctx, exe, "-M", "none", "-netdev", "help"); err != nil {
85+ logrus.Warn(err)
86 }
87-
88- cmd = exec.CommandContext(ctx, exe, "-M", "none", "-netdev", "help")
89- cmd.Stdout = &stdout
90- cmd.Stderr = &stderr
91- if err := cmd.Run(); err != nil {
92- logrus.Warnf("failed to run %v: stdout=%#q, stderr=%#q", cmd.Args, stdout.String(), stderr.String())
93- } else {
94- f.NetdevHelp = stdout.Bytes()
95- if len(f.NetdevHelp) == 0 {
96- f.NetdevHelp = stderr.Bytes()
97- }
98+ if f.MachineHelp, err = qemuHelp(ctx, exe, "-machine", "help"); err != nil {
99+ logrus.Warn(err)
100 }
101-
102- cmd = exec.CommandContext(ctx, exe, "-machine", "help")
103- cmd.Stdout = &stdout
104- cmd.Stderr = &stderr
105- if err := cmd.Run(); err != nil {
106- logrus.Warnf("failed to run %v: stdout=%#q, stderr=%#q", cmd.Args, stdout.String(), stderr.String())
107- } else {
108- f.MachineHelp = stdout.Bytes()
109- if len(f.MachineHelp) == 0 {
110- f.MachineHelp = stderr.Bytes()
111- }
112+ // Avoid error: "No machine specified, and there is no default"
113+ if f.CPUHelp, err = qemuHelp(ctx, exe, "-cpu", "help", "-machine", machine); err != nil {
114+ logrus.Warn(err)
115 }
116+ return &f, nil
117+}
118
119- // Avoid error: "No machine specified, and there is no default"
120- cmd = exec.CommandContext(ctx, exe, "-cpu", "help", "-machine", machine)
121+// qemuHelp is what exe prints for args, a help request. Each one has its own
122+// buffers: the features are searched as text, and one holding another's
123+// output would match words that are not its own.
124+func qemuHelp(ctx context.Context, exe string, args ...string) ([]byte, error) {
125+ var stdout, stderr bytes.Buffer
126+ cmd := exec.CommandContext(ctx, exe, args...)
127 cmd.Stdout = &stdout
128 cmd.Stderr = &stderr
129 if err := cmd.Run(); err != nil {
130- logrus.Warnf("failed to run %v: stdout=%#q, stderr=%#q", cmd.Args, stdout.String(), stderr.String())
131- } else {
132- f.CPUHelp = stdout.Bytes()
133- if len(f.CPUHelp) == 0 {
134- f.CPUHelp = stderr.Bytes()
135- }
136+ return nil, fmt.Errorf("failed to run %v: stdout=%#q, stderr=%#q", cmd.Args, stdout.String(), stderr.String())
137 }
138-
139- return &f, nil
140+ // on older versions qemu will write "help" output to stderr
141+ if stdout.Len() == 0 {
142+ return stderr.Bytes(), nil
143+ }
144+ return stdout.Bytes(), nil
145 }
146
147 // adjustMemBytesDarwinARM64HVF adjusts the memory to be <= 3 GiB, only when the following conditions are met: