跳到主要内容

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

提交记录
修改README.md+32 −31看文件
@@ -8,7 +8,7 @@ Supports [Claude Code](https://claude.ai/code), [OpenCode](https://github.com/an
88
99Never install attack vectors such as npm, claude or even Docker on your host machine again!
1010
11删除:Feedbacks welcome!
11新增:Feedback welcome!
1212
1313## Prerequisites
1414
@@ -43,9 +43,10 @@ Options:
4343|------|-------------|---------|
4444| `--disk GB` | VM disk size in GB | 20 |
4545| `--memory GB` | VM memory in GB | 8 |
46新增:| `--cpus N` | Number of CPUs | 4 |
4647
4748```bash
48删除:agent-vm setup --disk 50 --memory 16 # Larger VM for heavy workloads
49新增:agent-vm setup --disk 50 --memory 16 --cpus 8 # Larger VM for heavy workloads
4950```
5051
5152### Run an agent in a VM
@@ -57,7 +58,7 @@ agent-vm opencode # OpenCode
5758agent-vm codex # Codex CLI
5859```
5960
60删除:Creates a persistent VM for the current directory (or reuses it if one already exists), mounts your working directory, and runs the agent with full permissions. The VM persists after the agent exits so you can reconnect later.
61新增:Creates a persistent VM for the current directory (or reuses it if one already exists), mounts your working directory, and runs the agent with full permissions. The VM persists after the agent exits so you can reconnect later. Ports opened inside the VM (e.g. by Docker containers or dev servers) are automatically forwarded to your host by Lima.
6162
6263Each agent runs with its respective auto-approve flag:
6364- `claude` runs with `--dangerously-skip-permissions`
@@ -73,29 +74,30 @@ agent-vm opencode -p "refactor auth module" # OpenCode with a prompt
7374agent-vm codex -q "explain this codebase" # Codex with a query
7475```
7576
76删除:### Shell access
77新增:### Shell access and running commands
7778
7879```bash
79删除:agent-vm shell
80新增:agent-vm shell # Open a zsh shell in the VM
81新增:agent-vm run npm install # Run a one-off command in the VM
82新增:agent-vm run docker compose up -d # Start services
8083```
8184
82删除:Opens a zsh shell in the same persistent VM for the current directory. Useful for debugging, running commands alongside an agent, or using agents not built in.
83删除:
8485### VM lifecycle
8586
8687Each directory gets its own persistent VM. You can manage it with:
8788
8889```bash
89删除:agent-vm status # Show VM status for the current directory
90删除:agent-vm stop # Stop the VM (can be restarted later)
91删除:agent-vm destroy # Stop and permanently delete the VM
90新增:agent-vm status # Show VM status for the current directory
91新增:agent-vm stop # Stop the VM (can be restarted later)
92新增:agent-vm destroy # Stop and permanently delete the VM
93新增:agent-vm destroy-all # Stop and delete all agent-vm VMs
9294```
9395
9496To resize an existing VM's disk or memory, just pass `--disk` or `--memory` again — the VM will be stopped, reconfigured, and restarted automatically:
9597
9698```bash
9799agent-vm --disk 50 claude # Grow disk to 50GB, then run Claude
98删除:agent-vm --memory 16 shell # Increase memory to 16GB, then open shell
100新增:agent-vm --memory 16 --cpus 8 shell # Increase memory and CPUs, then open shell
99101```
100102
101103Note: disk can only be grown, not shrunk.
@@ -103,9 +105,21 @@ Note: disk can only be grown, not shrunk.
103105Running `agent-vm setup` again updates the base template but does **not** update existing VMs. You'll see a warning when using a VM cloned from an older base. Use `--reset` to re-clone:
104106
105107```bash
106删除:agent-vm --reset claude # Destroy and re-clone VM, then run Claude
108新增:agent-vm --reset claude # Destroy and re-clone VM, then run Claude
107109```
108110
111新增:### Offline mode and read-only mounts
112新增:
113新增:```bash
114新增:agent-vm --offline claude # Block outbound internet access
115新增:agent-vm --readonly shell # Mount project directory as read-only
116新增:agent-vm --offline --readonly claude # Both
117新增:```
118新增:
119新增:`--offline` blocks outbound internet from the VM using iptables while preserving host/VM communication (mounts, port forwarding). Useful for ensuring agents don't phone home or download unexpected packages.
120新增:
121新增:`--readonly` remounts the project directory as read-only. Useful for code review or audit tasks where the agent shouldn't modify files. Both flags are per-session and reset when the VM restarts.
122新增:
109123## Customization
110124
111125### Per-user setup: `~/.agent-vm/setup.sh`
@@ -141,7 +155,7 @@ docker compose up -d
141155
142156The base VM comes with [Chrome DevTools MCP](https://github.com/ChromeDevTools/chrome-devtools-mcp) pre-configured for Claude, giving the agent headless browser access.
143157
144删除:To add more MCP servers, add them to `~/.claude.json` in your `~/.agent-vm.setup.sh`, or edit the file directly inside a VM via `agent-vm shell`. Add entries to the `mcpServers` object:
158新增:To add more MCP servers, add them to `~/.claude.json` in your `~/.agent-vm/setup.sh`, or edit the file directly inside a VM via `agent-vm shell`. Add entries to the `mcpServers` object:
145159
146160```json
147161{
@@ -158,22 +172,14 @@ To add more MCP servers, add them to `~/.claude.json` in your `~/.agent-vm.setup
158172}
159173```
160174
161删除:## Shared configuration
162删除:
163删除:The VM mounts your host's `~/.claude/` directory. Login credentials, skills, user-level `CLAUDE.md`, conversation history, and settings all persist across VMs automatically.
164删除:
165删除:On first use, you'll need to authenticate. Credentials are stored in `~/.claude/.credentials.json` and reused by all subsequent VMs.
166删除:
167删除:This also means the configuration is shared with any Claude Code installation on the host — though we recommend running Claude exclusively in VMs for security.
168删除:
169175## How it works
170176
1711771. **`agent-vm setup`** creates a Debian 13 VM with Lima, runs `agent-vm.setup.sh` inside it to install dev tools + Chrome + agents, and stops it as a reusable base template
172删除:2. **`agent-vm claude|opencode|codex [args]`** clones the base template into a persistent per-directory VM, mounts your working directory and `~/.claude/`, runs optional `.agent-vm.runtime.sh`, then launches the agent with full permissions
178新增:2. **`agent-vm claude|opencode|codex [args]`** clones the base template into a persistent per-directory VM, mounts your working directory, runs optional runtime scripts (`~/.agent-vm/runtime.sh` then `.agent-vm.runtime.sh`), then launches the agent with full permissions
1731793. The VM persists after exit. Running any agent command or `agent-vm shell` in the same directory reuses the same VM
1741804. Use `agent-vm stop` to stop the VM or `agent-vm destroy` to delete it
175181
176删除:Ports opened inside the VM (e.g. by Docker containers) are automatically forwarded to your host by Lima.
182新增:Each VM is fully isolated — agents must authenticate independently inside their VM (e.g. `claude login`). Credentials persist within the VM across restarts but are not shared between VMs or with the host.
177183
178184## Project structure
179185
@@ -203,14 +209,9 @@ This is not a theoretical risk. The [Shai-Hulud](https://unit42.paloaltonetworks
203209
204210An AI agent running with `--dangerously-skip-permissions` on your host would give such an attack full access to everything: your SSH keys, your cloud credentials, your browser sessions, your entire filesystem.
205211
206删除:**agent-vm keeps your credentials on the host and runs all code inside the VM.** The VM only has access to:
207删除:
208删除:- Your project directory (read-write mount)
209删除:- `~/.claude/` for agent credentials and settings
210删除:
211删除:It has **no access** to your SSH keys, npm tokens, cloud credentials, git config, browser sessions, or anything else on your host. If a supply chain attack executes inside the VM, it finds nothing to steal (except your source code) and nowhere to spread.
212新增:**agent-vm runs all code inside the VM.** The VM only has access to your project directory (read-write mount, or read-only with `--readonly`). It has **no access** to your SSH keys, npm tokens, cloud credentials, git config, browser sessions, or anything else on your host. If a supply chain attack executes inside the VM, it finds nothing to steal (except your source code) and nowhere to spread. Use `--offline` to block internet access entirely.
212213
213删除:Meanwhile, your host machine stays clean. You don't need Node.js, Docker, or any dev tooling installed locally. The only host dependency is Lima. Your SSH keys and signing credentials never enter the VM as we recommend running `git commit` on the host yourself.
214新增:Meanwhile, your host machine stays clean. You don't need Node.js, Docker, or any dev tooling installed locally. The only host dependency is Lima. Your SSH keys and signing credentials never enter the VM — we recommend running `git commit` on the host yourself.
214215
215216### Why not Docker?
216217
@@ -218,7 +219,7 @@ Meanwhile, your host machine stays clean. You don't need Node.js, Docker, or any
218219|---|---|---|---|
219220| Agent can run any command | Yes | Yes | Yes |
220221| File system isolation | None | Partial (shared kernel) | Full |
221删除:| Network isolation | None | Partial | Full |
222新增:| Network isolation | None | Partial | Optional (`--offline`) |
222223| Can run Docker inside | Yes | Requires DinD or socket mount | Yes (native) |
223224| Kernel-level isolation | None | None (shares host kernel) | Full (separate kernel) |
224225| Protection from container escapes | None | None | Yes |
修改agent-vm.setup.sh+2 −1看文件
@@ -22,7 +22,8 @@ sudo apt-get install -y \
2222 python3 python3-pip python3-venv \
2323 ripgrep fd-find htop \
2424 unzip zip \
25删除: ca-certificates
25新增: ca-certificates \
26新增: iptables
2627
2728# Set zsh as default shell
2829sudo chsh -s /usr/bin/zsh "$(whoami)"
修改agent-vm.sh+156 −37看文件
@@ -21,7 +21,6 @@
2121
2222AGENT_VM_TEMPLATE="agent-vm-base"
2323AGENT_VM_STATE_DIR="${HOME}/.agent-vm"
24删除:AGENT_VM_CONFIG_DIR="${HOME}/.claude"
2524AGENT_VM_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
2625
2726# Generate a deterministic VM name for a directory
@@ -50,13 +49,16 @@ _agent_vm_ensure_running() {
5049 local vm_name="$1"
5150 local host_dir="$2"
5251 shift 2
53删除: local disk="" memory="" reset=""
52新增: local disk="" memory="" cpus="" reset="" offline="" rdonly=""
5453 while [[ $# -gt 0 ]]; do
5554 case "$1" in
56删除: --disk) disk="$2"; shift 2 ;;
57删除: --memory) memory="$2"; shift 2 ;;
58删除: --reset) reset=1; shift ;;
59删除: *) shift ;;
55新增: --disk) disk="$2"; shift 2 ;;
56新增: --memory) memory="$2"; shift 2 ;;
57新增: --cpus) cpus="$2"; shift 2 ;;
58新增: --reset) reset=1; shift ;;
59新增: --offline) offline=1; shift ;;
60新增: --readonly) rdonly=1; shift ;;
61新增: *) shift ;;
6062 esac
6163 done
6264
@@ -65,8 +67,6 @@ _agent_vm_ensure_running() {
6567 return 1
6668 fi
6769
68删除: mkdir -p "$AGENT_VM_CONFIG_DIR"
69删除:
7070 # Destroy existing VM if --reset was requested
7171 if [[ -n "$reset" ]] && _agent_vm_exists "$vm_name"; then
7272 echo "Resetting VM '$vm_name'..."
@@ -78,22 +78,24 @@ _agent_vm_ensure_running() {
7878 if ! _agent_vm_exists "$vm_name"; then
7979 echo "Creating VM '$vm_name'..."
8080 local clone_args=(
81删除: --set ".mounts=[{\"location\":\"${host_dir}\",\"writable\":true},{\"location\":\"${AGENT_VM_CONFIG_DIR}\",\"mountPoint\":\"/home/${USER}.linux/.claude\",\"writable\":true}]"
81新增: --set ".mounts=[{\"location\":\"${host_dir}\",\"writable\":true}]"
8282 --tty=false
8383 )
8484 [[ -n "$disk" ]] && clone_args+=(--set ".disk=\"${disk}GiB\"")
8585 [[ -n "$memory" ]] && clone_args+=(--set ".memory=\"${memory}GiB\"")
86新增: [[ -n "$cpus" ]] && clone_args+=(--set ".cpus=${cpus}")
8687 limactl clone "$AGENT_VM_TEMPLATE" "$vm_name" "${clone_args[@]}" &>/dev/null
8788 # Record which base version this VM was cloned from
8889 local base_ver="$AGENT_VM_STATE_DIR/.agent-vm-base-version"
8990 if [[ -f "$base_ver" ]]; then
9091 cp "$base_ver" "$AGENT_VM_STATE_DIR/.agent-vm-version-${vm_name}"
9192 fi
92删除: elif [[ -n "$disk" || -n "$memory" ]]; then
93删除: # Auto-resize existing VM if --disk or --memory changed
93新增: elif [[ -n "$disk" || -n "$memory" || -n "$cpus" ]]; then
94新增: # Auto-resize existing VM if --disk, --memory, or --cpus changed
9495 local edit_args=()
9596 [[ -n "$disk" ]] && edit_args+=(--set ".disk=\"${disk}GiB\"")
9697 [[ -n "$memory" ]] && edit_args+=(--set ".memory=\"${memory}GiB\"")
98新增: [[ -n "$cpus" ]] && edit_args+=(--set ".cpus=${cpus}")
9799 if _agent_vm_running "$vm_name"; then
98100 echo "VM '$vm_name' is currently running. It must be stopped to apply new resource settings."
99101 printf "Stop the VM and apply changes? [y/N] "
@@ -133,6 +135,22 @@ _agent_vm_ensure_running() {
133135 echo "Running project runtime setup..."
134136 limactl shell --workdir "$host_dir" "$vm_name" zsh -l < "${host_dir}/.agent-vm.runtime.sh"
135137 fi
138新增:
139新增: # Apply per-session restrictions
140新增: if [[ -n "$offline" ]]; then
141新增: echo "Enabling offline mode..."
142新增: limactl shell "$vm_name" sudo iptables -F OUTPUT 2>/dev/null
143新增: limactl shell "$vm_name" sudo iptables -A OUTPUT -o lo -j ACCEPT
144新增: limactl shell "$vm_name" sudo iptables -A OUTPUT -d 10.0.0.0/8 -j ACCEPT
145新增: limactl shell "$vm_name" sudo iptables -A OUTPUT -d 172.16.0.0/12 -j ACCEPT
146新增: limactl shell "$vm_name" sudo iptables -A OUTPUT -d 192.168.0.0/16 -j ACCEPT
147新增: limactl shell "$vm_name" sudo iptables -P OUTPUT DROP
148新增: fi
149新增:
150新增: if [[ -n "$rdonly" ]]; then
151新增: echo "Mounting project directory as read-only..."
152新增: limactl shell "$vm_name" sudo mount -o remount,ro "$host_dir"
153新增: fi
136154}
137155
138156agent-vm() {
@@ -148,8 +166,16 @@ agent-vm() {
148166 vm_opts+=(--memory "$2"); shift 2 ;;
149167 --memory=*)
150168 vm_opts+=(--memory "${1#*=}"); shift ;;
169新增: --cpus)
170新增: vm_opts+=(--cpus "$2"); shift 2 ;;
171新增: --cpus=*)
172新增: vm_opts+=(--cpus "${1#*=}"); shift ;;
151173 --reset)
152174 vm_opts+=(--reset); shift ;;
175新增: --offline)
176新增: vm_opts+=(--offline); shift ;;
177新增: --readonly)
178新增: vm_opts+=(--readonly); shift ;;
153179 *)
154180 break ;;
155181 esac
@@ -174,12 +200,18 @@ agent-vm() {
174200 shell)
175201 _agent_vm_shell "${vm_opts[@]}" "$@"
176202 ;;
203新增: run)
204新增: _agent_vm_run "${vm_opts[@]}" "$@"
205新增: ;;
177206 stop)
178207 _agent_vm_stop "$@"
179208 ;;
180209 destroy)
181210 _agent_vm_destroy "$@"
182211 ;;
212新增: destroy-all)
213新增: _agent_vm_destroy_all "$@"
214新增: ;;
183215 list)
184216 _agent_vm_list "$@"
185217 ;;
@@ -207,25 +239,33 @@ Commands:
207239 opencode [args] Run OpenCode in the VM for the current directory
208240 codex [args] Run Codex CLI in the VM for the current directory
209241 shell Open a shell in the VM for the current directory
242新增: run <cmd> [args] Run a command in the VM for the current directory
210243 stop Stop the VM for the current directory
211244 destroy Stop and delete the VM for the current directory
245新增: destroy-all Stop and delete all agent-vm VMs
212246 list List all agent-vm VMs
213247 status Show VM status for the current directory
214248 help Show this help
215249
216删除:VM options (for claude, opencode, codex, shell):
250新增:VM options (for claude, opencode, codex, shell, run):
217251 --disk GB VM disk size (default: 20)
218252 --memory GB VM memory (default: 8)
253新增: --cpus N Number of CPUs (default: 4)
219254 --reset Destroy and re-clone the VM from the base template
255新增: --offline Block outbound internet (keeps host/VM communication)
256新增: --readonly Mount the project directory as read-only
220257
221258Examples:
222259 agent-vm setup # Create base VM
223260 agent-vm claude # Run Claude in a VM
224261 agent-vm opencode # Run OpenCode in a VM
225262 agent-vm codex # Run Codex in a VM
226删除: agent-vm --disk 50 --memory 16 claude # With custom resources
263新增: agent-vm --disk 50 --memory 16 --cpus 8 claude # Custom resources
227264 agent-vm --reset claude # Fresh VM from base template
265新增: agent-vm --offline claude # No internet access
266新增: agent-vm --readonly shell # Read-only project mount
228267 agent-vm shell # Shell into the VM
268新增: agent-vm run npm install # Run a command in the VM
229269 agent-vm claude -p "fix lint errors" # Pass args to claude
230270
231271VMs are persistent and unique per directory. Running "agent-vm shell" or
@@ -243,17 +283,19 @@ EOF
243283_agent_vm_setup() {
244284 local disk=20
245285 local memory=8
286新增: local cpus=""
246287
247288 while [[ $# -gt 0 ]]; do
248289 case "$1" in
249290 --help|-h)
250删除: echo "Usage: agent-vm setup [--disk GB] [--memory GB]"
291新增: echo "Usage: agent-vm setup [--disk GB] [--memory GB] [--cpus N]"
251292 echo ""
252293 echo "Create a base VM template with dev tools and agents pre-installed."
253294 echo ""
254295 echo "Options:"
255296 echo " --disk GB VM disk size (default: 20)"
256297 echo " --memory GB VM memory (default: 8)"
298新增: echo " --cpus N Number of CPUs (default: 4)"
257299 echo " --help Show this help"
258300 return 0
259301 ;;
@@ -273,9 +315,19 @@ _agent_vm_setup() {
273315 memory="${1#*=}"
274316 shift
275317 ;;
318新增: --cpus)
319新增: cpus="$2"
320新增: shift 2
321新增: ;;
322新增: --cpus=*)
323新增: cpus="${1#*=}"
324新增: shift
325新增: ;;
326新增: --reset|--offline|--readonly)
327新增: shift ;;
276328 *)
277329 echo "Unknown option: $1" >&2
278删除: echo "Usage: agent-vm setup [--disk GB] [--memory GB]" >&2
330新增: echo "Usage: agent-vm setup [--disk GB] [--memory GB] [--cpus N]" >&2
279331 return 1
280332 ;;
281333 esac
@@ -295,11 +347,15 @@ _agent_vm_setup() {
295347 limactl delete "$AGENT_VM_TEMPLATE" --force &>/dev/null
296348
297349 echo "Creating base VM..."
350新增: local create_args=(
351新增: --set '.mounts=[]'
352新增: --disk="$disk"
353新增: --memory="$memory"
354新增: --tty=false
355新增: )
356新增: [[ -n "$cpus" ]] && create_args+=(--cpus="$cpus")
298357 limactl create --name="$AGENT_VM_TEMPLATE" template:debian-13 \
299删除: --set '.mounts=[]' \
300删除: --disk="$disk" \
301删除: --memory="$memory" \
302删除: --tty=false &>/dev/null || { echo "Error: Failed to create base VM." >&2; return 1; }
358新增: "${create_args[@]}" &>/dev/null || { echo "Error: Failed to create base VM." >&2; return 1; }
303359 limactl start "$AGENT_VM_TEMPLATE" &>/dev/null || { echo "Error: Failed to start base VM." >&2; return 1; }
304360
305361 # Run the setup script inside the VM
@@ -315,8 +371,6 @@ _agent_vm_setup() {
315371
316372 limactl stop "$AGENT_VM_TEMPLATE" &>/dev/null
317373
318删除: mkdir -p "$AGENT_VM_CONFIG_DIR"
319删除:
320374 # Record base VM version so we can warn about stale clones
321375 mkdir -p "$AGENT_VM_STATE_DIR"
322376 date +%s > "$AGENT_VM_STATE_DIR/.agent-vm-base-version"
@@ -331,10 +385,13 @@ _agent_vm_claude() {
331385 local args=()
332386 while [[ $# -gt 0 ]]; do
333387 case "$1" in
334删除: --disk) vm_opts+=(--disk "$2"); shift 2 ;;
335删除: --memory) vm_opts+=(--memory "$2"); shift 2 ;;
336删除: --reset) vm_opts+=(--reset); shift ;;
337删除: *) args+=("$1"); shift ;;
388新增: --disk) vm_opts+=(--disk "$2"); shift 2 ;;
389新增: --memory) vm_opts+=(--memory "$2"); shift 2 ;;
390新增: --cpus) vm_opts+=(--cpus "$2"); shift 2 ;;
391新增: --reset) vm_opts+=(--reset); shift ;;
392新增: --offline) vm_opts+=(--offline); shift ;;
393新增: --readonly) vm_opts+=(--readonly); shift ;;
394新增: *) args+=("$1"); shift ;;
338395 esac
339396 done
340397 local host_dir
@@ -352,10 +409,13 @@ _agent_vm_opencode() {
352409 local args=()
353410 while [[ $# -gt 0 ]]; do
354411 case "$1" in
355删除: --disk) vm_opts+=(--disk "$2"); shift 2 ;;
356删除: --memory) vm_opts+=(--memory "$2"); shift 2 ;;
357删除: --reset) vm_opts+=(--reset); shift ;;
358删除: *) args+=("$1"); shift ;;
412新增: --disk) vm_opts+=(--disk "$2"); shift 2 ;;
413新增: --memory) vm_opts+=(--memory "$2"); shift 2 ;;
414新增: --cpus) vm_opts+=(--cpus "$2"); shift 2 ;;
415新增: --reset) vm_opts+=(--reset); shift ;;
416新增: --offline) vm_opts+=(--offline); shift ;;
417新增: --readonly) vm_opts+=(--readonly); shift ;;
418新增: *) args+=("$1"); shift ;;
359419 esac
360420 done
361421 local host_dir
@@ -375,10 +435,13 @@ _agent_vm_codex() {
375435 local args=()
376436 while [[ $# -gt 0 ]]; do
377437 case "$1" in
378删除: --disk) vm_opts+=(--disk "$2"); shift 2 ;;
379删除: --memory) vm_opts+=(--memory "$2"); shift 2 ;;
380删除: --reset) vm_opts+=(--reset); shift ;;
381删除: *) args+=("$1"); shift ;;
438新增: --disk) vm_opts+=(--disk "$2"); shift 2 ;;
439新增: --memory) vm_opts+=(--memory "$2"); shift 2 ;;
440新增: --cpus) vm_opts+=(--cpus "$2"); shift 2 ;;
441新增: --reset) vm_opts+=(--reset); shift ;;
442新增: --offline) vm_opts+=(--offline); shift ;;
443新增: --readonly) vm_opts+=(--readonly); shift ;;
444新增: *) args+=("$1"); shift ;;
382445 esac
383446 done
384447 local host_dir
@@ -395,10 +458,13 @@ _agent_vm_shell() {
395458 local vm_opts=()
396459 while [[ $# -gt 0 ]]; do
397460 case "$1" in
398删除: --disk) vm_opts+=(--disk "$2"); shift 2 ;;
399删除: --memory) vm_opts+=(--memory "$2"); shift 2 ;;
400删除: --reset) vm_opts+=(--reset); shift ;;
401删除: *) shift ;;
461新增: --disk) vm_opts+=(--disk "$2"); shift 2 ;;
462新增: --memory) vm_opts+=(--memory "$2"); shift 2 ;;
463新增: --cpus) vm_opts+=(--cpus "$2"); shift 2 ;;
464新增: --reset) vm_opts+=(--reset); shift ;;
465新增: --offline) vm_opts+=(--offline); shift ;;
466新增: --readonly) vm_opts+=(--readonly); shift ;;
467新增: *) shift ;;
402468 esac
403469 done
404470 local host_dir
@@ -413,6 +479,34 @@ _agent_vm_shell() {
413479 limactl shell --workdir "$host_dir" "$vm_name" zsh -l
414480}
415481
482新增:_agent_vm_run() {
483新增: local vm_opts=()
484新增: local args=()
485新增: while [[ $# -gt 0 ]]; do
486新增: case "$1" in
487新增: --disk) vm_opts+=(--disk "$2"); shift 2 ;;
488新增: --memory) vm_opts+=(--memory "$2"); shift 2 ;;
489新增: --cpus) vm_opts+=(--cpus "$2"); shift 2 ;;
490新增: --reset) vm_opts+=(--reset); shift ;;
491新增: --offline) vm_opts+=(--offline); shift ;;
492新增: --readonly) vm_opts+=(--readonly); shift ;;
493新增: *) args+=("$1"); shift ;;
494新增: esac
495新增: done
496新增: if [[ ${#args[@]} -eq 0 ]]; then
497新增: echo "Usage: agent-vm run <command> [args]" >&2
498新增: return 1
499新增: fi
500新增: local host_dir
501新增: host_dir="$(pwd)"
502新增: local vm_name
503新增: vm_name="$(_agent_vm_name "$host_dir")"
504新增:
505新增: _agent_vm_ensure_running "$vm_name" "$host_dir" "${vm_opts[@]}" || return 1
506新增:
507新增: limactl shell --workdir "$host_dir" "$vm_name" "${args[@]}"
508新增:}
509新增:
416510_agent_vm_stop() {
417511 local host_dir
418512 host_dir="$(pwd)"
@@ -447,6 +541,31 @@ _agent_vm_destroy() {
447541 echo "VM destroyed."
448542}
449543
544新增:_agent_vm_destroy_all() {
545新增: local vms
546新增: vms=$(limactl list -q 2>/dev/null | grep "^agent-vm-" || true)
547新增: if [[ -z "$vms" ]]; then
548新增: echo "No agent-vm VMs found."
549新增: return 0
550新增: fi
551新增: echo "This will destroy the following VMs:"
552新增: echo "$vms"
553新增: printf "Continue? [y/N] "
554新增: local reply
555新增: read -r reply
556新增: if [[ ! "$reply" =~ ^[Yy]$ ]]; then
557新增: echo "Aborted."
558新增: return 0
559新增: fi
560新增: echo "$vms" | while read -r vm; do
561新增: echo "Destroying $vm..."
562新增: limactl stop "$vm" &>/dev/null
563新增: limactl delete "$vm" --force &>/dev/null
564新增: rm -f "$AGENT_VM_STATE_DIR/.agent-vm-version-${vm}"
565新增: done
566新增: echo "All VMs destroyed."
567新增:}
568新增:
450569_agent_vm_list() {
451570 limactl list | head -1
452571 limactl list | grep "^agent-vm-" || echo "(no VMs)"