提交记录修改README.md+32 −31看文件
修改agent-vm.setup.sh+2 −1看文件
修改agent-vm.sh+156 −37看文件
| @@ -8,7 +8,7 @@ Supports [Claude Code](https://claude.ai/code), [OpenCode](https://github.com/an | ||
| 8 | 8 | |
| 9 | 9 | Never install attack vectors such as npm, claude or even Docker on your host machine again! |
| 10 | 10 | |
| 11 | 删除:Feedbacks welcome! | |
| 11 | 新增:Feedback welcome! | |
| 12 | 12 | |
| 13 | 13 | ## Prerequisites |
| 14 | 14 | |
| @@ -43,9 +43,10 @@ Options: | ||
| 43 | 43 | |------|-------------|---------| |
| 44 | 44 | | `--disk GB` | VM disk size in GB | 20 | |
| 45 | 45 | | `--memory GB` | VM memory in GB | 8 | |
| 46 | 新增:| `--cpus N` | Number of CPUs | 4 | | |
| 46 | 47 | |
| 47 | 48 | ```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 | |
| 49 | 50 | ``` |
| 50 | 51 | |
| 51 | 52 | ### Run an agent in a VM |
| @@ -57,7 +58,7 @@ agent-vm opencode # OpenCode | ||
| 57 | 58 | agent-vm codex # Codex CLI |
| 58 | 59 | ``` |
| 59 | 60 | |
| 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. | |
| 61 | 62 | |
| 62 | 63 | Each agent runs with its respective auto-approve flag: |
| 63 | 64 | - `claude` runs with `--dangerously-skip-permissions` |
| @@ -73,29 +74,30 @@ agent-vm opencode -p "refactor auth module" # OpenCode with a prompt | ||
| 73 | 74 | agent-vm codex -q "explain this codebase" # Codex with a query |
| 74 | 75 | ``` |
| 75 | 76 | |
| 76 | 删除:### Shell access | |
| 77 | 新增:### Shell access and running commands | |
| 77 | 78 | |
| 78 | 79 | ```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 | |
| 80 | 83 | ``` |
| 81 | 84 | |
| 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 | 删除: | |
| 84 | 85 | ### VM lifecycle |
| 85 | 86 | |
| 86 | 87 | Each directory gets its own persistent VM. You can manage it with: |
| 87 | 88 | |
| 88 | 89 | ```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 | |
| 92 | 94 | ``` |
| 93 | 95 | |
| 94 | 96 | To resize an existing VM's disk or memory, just pass `--disk` or `--memory` again — the VM will be stopped, reconfigured, and restarted automatically: |
| 95 | 97 | |
| 96 | 98 | ```bash |
| 97 | 99 | agent-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 | |
| 99 | 101 | ``` |
| 100 | 102 | |
| 101 | 103 | Note: disk can only be grown, not shrunk. |
| @@ -103,9 +105,21 @@ Note: disk can only be grown, not shrunk. | ||
| 103 | 105 | Running `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: |
| 104 | 106 | |
| 105 | 107 | ```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 | |
| 107 | 109 | ``` |
| 108 | 110 | |
| 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 | 新增: | |
| 109 | 123 | ## Customization |
| 110 | 124 | |
| 111 | 125 | ### Per-user setup: `~/.agent-vm/setup.sh` |
| @@ -141,7 +155,7 @@ docker compose up -d | ||
| 141 | 155 | |
| 142 | 156 | The base VM comes with [Chrome DevTools MCP](https://github.com/ChromeDevTools/chrome-devtools-mcp) pre-configured for Claude, giving the agent headless browser access. |
| 143 | 157 | |
| 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: | |
| 145 | 159 | |
| 146 | 160 | ```json |
| 147 | 161 | { |
| @@ -158,22 +172,14 @@ To add more MCP servers, add them to `~/.claude.json` in your `~/.agent-vm.setup | ||
| 158 | 172 | } |
| 159 | 173 | ``` |
| 160 | 174 | |
| 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 | 删除: | |
| 169 | 175 | ## How it works |
| 170 | 176 | |
| 171 | 177 | 1. **`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 | |
| 173 | 179 | 3. The VM persists after exit. Running any agent command or `agent-vm shell` in the same directory reuses the same VM |
| 174 | 180 | 4. Use `agent-vm stop` to stop the VM or `agent-vm destroy` to delete it |
| 175 | 181 | |
| 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. | |
| 177 | 183 | |
| 178 | 184 | ## Project structure |
| 179 | 185 | |
| @@ -203,14 +209,9 @@ This is not a theoretical risk. The [Shai-Hulud](https://unit42.paloaltonetworks | ||
| 203 | 209 | |
| 204 | 210 | An 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. |
| 205 | 211 | |
| 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. | |
| 212 | 213 | |
| 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. | |
| 214 | 215 | |
| 215 | 216 | ### Why not Docker? |
| 216 | 217 | |
| @@ -218,7 +219,7 @@ Meanwhile, your host machine stays clean. You don't need Node.js, Docker, or any | ||
| 218 | 219 | |---|---|---|---| |
| 219 | 220 | | Agent can run any command | Yes | Yes | Yes | |
| 220 | 221 | | File system isolation | None | Partial (shared kernel) | Full | |
| 221 | 删除:| Network isolation | None | Partial | Full | | |
| 222 | 新增:| Network isolation | None | Partial | Optional (`--offline`) | | |
| 222 | 223 | | Can run Docker inside | Yes | Requires DinD or socket mount | Yes (native) | |
| 223 | 224 | | Kernel-level isolation | None | None (shares host kernel) | Full (separate kernel) | |
| 224 | 225 | | Protection from container escapes | None | None | Yes | |
| @@ -22,7 +22,8 @@ sudo apt-get install -y \ | ||
| 22 | 22 | python3 python3-pip python3-venv \ |
| 23 | 23 | ripgrep fd-find htop \ |
| 24 | 24 | unzip zip \ |
| 25 | 删除: ca-certificates | |
| 25 | 新增: ca-certificates \ | |
| 26 | 新增: iptables | |
| 26 | 27 | |
| 27 | 28 | # Set zsh as default shell |
| 28 | 29 | sudo chsh -s /usr/bin/zsh "$(whoami)" |
| @@ -21,7 +21,6 @@ | ||
| 21 | 21 | |
| 22 | 22 | AGENT_VM_TEMPLATE="agent-vm-base" |
| 23 | 23 | AGENT_VM_STATE_DIR="${HOME}/.agent-vm" |
| 24 | 删除:AGENT_VM_CONFIG_DIR="${HOME}/.claude" | |
| 25 | 24 | AGENT_VM_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)" |
| 26 | 25 | |
| 27 | 26 | # Generate a deterministic VM name for a directory |
| @@ -50,13 +49,16 @@ _agent_vm_ensure_running() { | ||
| 50 | 49 | local vm_name="$1" |
| 51 | 50 | local host_dir="$2" |
| 52 | 51 | shift 2 |
| 53 | 删除: local disk="" memory="" reset="" | |
| 52 | 新增: local disk="" memory="" cpus="" reset="" offline="" rdonly="" | |
| 54 | 53 | while [[ $# -gt 0 ]]; do |
| 55 | 54 | 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 ;; | |
| 60 | 62 | esac |
| 61 | 63 | done |
| 62 | 64 | |
| @@ -65,8 +67,6 @@ _agent_vm_ensure_running() { | ||
| 65 | 67 | return 1 |
| 66 | 68 | fi |
| 67 | 69 | |
| 68 | 删除: mkdir -p "$AGENT_VM_CONFIG_DIR" | |
| 69 | 删除: | |
| 70 | 70 | # Destroy existing VM if --reset was requested |
| 71 | 71 | if [[ -n "$reset" ]] && _agent_vm_exists "$vm_name"; then |
| 72 | 72 | echo "Resetting VM '$vm_name'..." |
| @@ -78,22 +78,24 @@ _agent_vm_ensure_running() { | ||
| 78 | 78 | if ! _agent_vm_exists "$vm_name"; then |
| 79 | 79 | echo "Creating VM '$vm_name'..." |
| 80 | 80 | 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}]" | |
| 82 | 82 | --tty=false |
| 83 | 83 | ) |
| 84 | 84 | [[ -n "$disk" ]] && clone_args+=(--set ".disk=\"${disk}GiB\"") |
| 85 | 85 | [[ -n "$memory" ]] && clone_args+=(--set ".memory=\"${memory}GiB\"") |
| 86 | 新增: [[ -n "$cpus" ]] && clone_args+=(--set ".cpus=${cpus}") | |
| 86 | 87 | limactl clone "$AGENT_VM_TEMPLATE" "$vm_name" "${clone_args[@]}" &>/dev/null |
| 87 | 88 | # Record which base version this VM was cloned from |
| 88 | 89 | local base_ver="$AGENT_VM_STATE_DIR/.agent-vm-base-version" |
| 89 | 90 | if [[ -f "$base_ver" ]]; then |
| 90 | 91 | cp "$base_ver" "$AGENT_VM_STATE_DIR/.agent-vm-version-${vm_name}" |
| 91 | 92 | 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 | |
| 94 | 95 | local edit_args=() |
| 95 | 96 | [[ -n "$disk" ]] && edit_args+=(--set ".disk=\"${disk}GiB\"") |
| 96 | 97 | [[ -n "$memory" ]] && edit_args+=(--set ".memory=\"${memory}GiB\"") |
| 98 | 新增: [[ -n "$cpus" ]] && edit_args+=(--set ".cpus=${cpus}") | |
| 97 | 99 | if _agent_vm_running "$vm_name"; then |
| 98 | 100 | echo "VM '$vm_name' is currently running. It must be stopped to apply new resource settings." |
| 99 | 101 | printf "Stop the VM and apply changes? [y/N] " |
| @@ -133,6 +135,22 @@ _agent_vm_ensure_running() { | ||
| 133 | 135 | echo "Running project runtime setup..." |
| 134 | 136 | limactl shell --workdir "$host_dir" "$vm_name" zsh -l < "${host_dir}/.agent-vm.runtime.sh" |
| 135 | 137 | 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 | |
| 136 | 154 | } |
| 137 | 155 | |
| 138 | 156 | agent-vm() { |
| @@ -148,8 +166,16 @@ agent-vm() { | ||
| 148 | 166 | vm_opts+=(--memory "$2"); shift 2 ;; |
| 149 | 167 | --memory=*) |
| 150 | 168 | vm_opts+=(--memory "${1#*=}"); shift ;; |
| 169 | 新增: --cpus) | |
| 170 | 新增: vm_opts+=(--cpus "$2"); shift 2 ;; | |
| 171 | 新增: --cpus=*) | |
| 172 | 新增: vm_opts+=(--cpus "${1#*=}"); shift ;; | |
| 151 | 173 | --reset) |
| 152 | 174 | vm_opts+=(--reset); shift ;; |
| 175 | 新增: --offline) | |
| 176 | 新增: vm_opts+=(--offline); shift ;; | |
| 177 | 新增: --readonly) | |
| 178 | 新增: vm_opts+=(--readonly); shift ;; | |
| 153 | 179 | *) |
| 154 | 180 | break ;; |
| 155 | 181 | esac |
| @@ -174,12 +200,18 @@ agent-vm() { | ||
| 174 | 200 | shell) |
| 175 | 201 | _agent_vm_shell "${vm_opts[@]}" "$@" |
| 176 | 202 | ;; |
| 203 | 新增: run) | |
| 204 | 新增: _agent_vm_run "${vm_opts[@]}" "$@" | |
| 205 | 新增: ;; | |
| 177 | 206 | stop) |
| 178 | 207 | _agent_vm_stop "$@" |
| 179 | 208 | ;; |
| 180 | 209 | destroy) |
| 181 | 210 | _agent_vm_destroy "$@" |
| 182 | 211 | ;; |
| 212 | 新增: destroy-all) | |
| 213 | 新增: _agent_vm_destroy_all "$@" | |
| 214 | 新增: ;; | |
| 183 | 215 | list) |
| 184 | 216 | _agent_vm_list "$@" |
| 185 | 217 | ;; |
| @@ -207,25 +239,33 @@ Commands: | ||
| 207 | 239 | opencode [args] Run OpenCode in the VM for the current directory |
| 208 | 240 | codex [args] Run Codex CLI in the VM for the current directory |
| 209 | 241 | 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 | |
| 210 | 243 | stop Stop the VM for the current directory |
| 211 | 244 | destroy Stop and delete the VM for the current directory |
| 245 | 新增: destroy-all Stop and delete all agent-vm VMs | |
| 212 | 246 | list List all agent-vm VMs |
| 213 | 247 | status Show VM status for the current directory |
| 214 | 248 | help Show this help |
| 215 | 249 | |
| 216 | 删除:VM options (for claude, opencode, codex, shell): | |
| 250 | 新增:VM options (for claude, opencode, codex, shell, run): | |
| 217 | 251 | --disk GB VM disk size (default: 20) |
| 218 | 252 | --memory GB VM memory (default: 8) |
| 253 | 新增: --cpus N Number of CPUs (default: 4) | |
| 219 | 254 | --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 | |
| 220 | 257 | |
| 221 | 258 | Examples: |
| 222 | 259 | agent-vm setup # Create base VM |
| 223 | 260 | agent-vm claude # Run Claude in a VM |
| 224 | 261 | agent-vm opencode # Run OpenCode in a VM |
| 225 | 262 | 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 | |
| 227 | 264 | 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 | |
| 228 | 267 | agent-vm shell # Shell into the VM |
| 268 | 新增: agent-vm run npm install # Run a command in the VM | |
| 229 | 269 | agent-vm claude -p "fix lint errors" # Pass args to claude |
| 230 | 270 | |
| 231 | 271 | VMs are persistent and unique per directory. Running "agent-vm shell" or |
| @@ -243,17 +283,19 @@ EOF | ||
| 243 | 283 | _agent_vm_setup() { |
| 244 | 284 | local disk=20 |
| 245 | 285 | local memory=8 |
| 286 | 新增: local cpus="" | |
| 246 | 287 | |
| 247 | 288 | while [[ $# -gt 0 ]]; do |
| 248 | 289 | case "$1" in |
| 249 | 290 | --help|-h) |
| 250 | 删除: echo "Usage: agent-vm setup [--disk GB] [--memory GB]" | |
| 291 | 新增: echo "Usage: agent-vm setup [--disk GB] [--memory GB] [--cpus N]" | |
| 251 | 292 | echo "" |
| 252 | 293 | echo "Create a base VM template with dev tools and agents pre-installed." |
| 253 | 294 | echo "" |
| 254 | 295 | echo "Options:" |
| 255 | 296 | echo " --disk GB VM disk size (default: 20)" |
| 256 | 297 | echo " --memory GB VM memory (default: 8)" |
| 298 | 新增: echo " --cpus N Number of CPUs (default: 4)" | |
| 257 | 299 | echo " --help Show this help" |
| 258 | 300 | return 0 |
| 259 | 301 | ;; |
| @@ -273,9 +315,19 @@ _agent_vm_setup() { | ||
| 273 | 315 | memory="${1#*=}" |
| 274 | 316 | shift |
| 275 | 317 | ;; |
| 318 | 新增: --cpus) | |
| 319 | 新增: cpus="$2" | |
| 320 | 新增: shift 2 | |
| 321 | 新增: ;; | |
| 322 | 新增: --cpus=*) | |
| 323 | 新增: cpus="${1#*=}" | |
| 324 | 新增: shift | |
| 325 | 新增: ;; | |
| 326 | 新增: --reset|--offline|--readonly) | |
| 327 | 新增: shift ;; | |
| 276 | 328 | *) |
| 277 | 329 | 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 | |
| 279 | 331 | return 1 |
| 280 | 332 | ;; |
| 281 | 333 | esac |
| @@ -295,11 +347,15 @@ _agent_vm_setup() { | ||
| 295 | 347 | limactl delete "$AGENT_VM_TEMPLATE" --force &>/dev/null |
| 296 | 348 | |
| 297 | 349 | 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") | |
| 298 | 357 | 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; } | |
| 303 | 359 | limactl start "$AGENT_VM_TEMPLATE" &>/dev/null || { echo "Error: Failed to start base VM." >&2; return 1; } |
| 304 | 360 | |
| 305 | 361 | # Run the setup script inside the VM |
| @@ -315,8 +371,6 @@ _agent_vm_setup() { | ||
| 315 | 371 | |
| 316 | 372 | limactl stop "$AGENT_VM_TEMPLATE" &>/dev/null |
| 317 | 373 | |
| 318 | 删除: mkdir -p "$AGENT_VM_CONFIG_DIR" | |
| 319 | 删除: | |
| 320 | 374 | # Record base VM version so we can warn about stale clones |
| 321 | 375 | mkdir -p "$AGENT_VM_STATE_DIR" |
| 322 | 376 | date +%s > "$AGENT_VM_STATE_DIR/.agent-vm-base-version" |
| @@ -331,10 +385,13 @@ _agent_vm_claude() { | ||
| 331 | 385 | local args=() |
| 332 | 386 | while [[ $# -gt 0 ]]; do |
| 333 | 387 | 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 ;; | |
| 338 | 395 | esac |
| 339 | 396 | done |
| 340 | 397 | local host_dir |
| @@ -352,10 +409,13 @@ _agent_vm_opencode() { | ||
| 352 | 409 | local args=() |
| 353 | 410 | while [[ $# -gt 0 ]]; do |
| 354 | 411 | 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 ;; | |
| 359 | 419 | esac |
| 360 | 420 | done |
| 361 | 421 | local host_dir |
| @@ -375,10 +435,13 @@ _agent_vm_codex() { | ||
| 375 | 435 | local args=() |
| 376 | 436 | while [[ $# -gt 0 ]]; do |
| 377 | 437 | 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 ;; | |
| 382 | 445 | esac |
| 383 | 446 | done |
| 384 | 447 | local host_dir |
| @@ -395,10 +458,13 @@ _agent_vm_shell() { | ||
| 395 | 458 | local vm_opts=() |
| 396 | 459 | while [[ $# -gt 0 ]]; do |
| 397 | 460 | 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 ;; | |
| 402 | 468 | esac |
| 403 | 469 | done |
| 404 | 470 | local host_dir |
| @@ -413,6 +479,34 @@ _agent_vm_shell() { | ||
| 413 | 479 | limactl shell --workdir "$host_dir" "$vm_name" zsh -l |
| 414 | 480 | } |
| 415 | 481 | |
| 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 | 新增: | |
| 416 | 510 | _agent_vm_stop() { |
| 417 | 511 | local host_dir |
| 418 | 512 | host_dir="$(pwd)" |
| @@ -447,6 +541,31 @@ _agent_vm_destroy() { | ||
| 447 | 541 | echo "VM destroyed." |
| 448 | 542 | } |
| 449 | 543 | |
| 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 | 新增: | |
| 450 | 569 | _agent_vm_list() { |
| 451 | 570 | limactl list | head -1 |
| 452 | 571 | limactl list | grep "^agent-vm-" || echo "(no VMs)" |

