|
| 1 | +# Windows Sandbox Example |
| 2 | + |
| 3 | +Run a Windows guest in an OpenSandbox sandbox via KVM/QEMU using the [`dockur/windows`](https://github.com/dockur/windows) image. |
| 4 | + |
| 5 | +## How it works |
| 6 | + |
| 7 | +OpenSandbox creates a Linux container running KVM/QEMU, which boots a Windows guest OS inside it. The Windows profile (`platform.os=windows`) automatically configures the required devices, capabilities, OEM scripts, and port mappings — you only need to specify `platform` and `resource` in the SDK call. |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +- OpenSandbox server running (e.g. `http://localhost:8080`) |
| 12 | +- Host with `/dev/kvm` and `/dev/net/tun` present |
| 13 | +- Server `storage.allowed_host_paths` configured for any host bind mounts |
| 14 | + |
| 15 | +## Start OpenSandbox server [local] |
| 16 | + |
| 17 | +```shell |
| 18 | +uv pip install opensandbox-server |
| 19 | +opensandbox-server init-config ~/.sandbox.toml --example docker |
| 20 | +opensandbox-server |
| 21 | +``` |
| 22 | + |
| 23 | +## Run the example |
| 24 | + |
| 25 | +```shell |
| 26 | +uv pip install opensandbox |
| 27 | +python main.py |
| 28 | +``` |
| 29 | + |
| 30 | +The script will: |
| 31 | + |
| 32 | +1. Create a Windows sandbox with `dockurr/windows:latest` and Windows 11 |
| 33 | +2. Wait until the sandbox is healthy (first boot can take several minutes) |
| 34 | +3. Print the execd, RDP (3389), and web console (8006) endpoints |
| 35 | +4. Execute a test command and print the output |
| 36 | + |
| 37 | +## Environment Variables |
| 38 | + |
| 39 | +- `SANDBOX_DOMAIN`: Sandbox service address (default: `localhost:8080`) |
| 40 | +- `SANDBOX_API_KEY`: API key if your server requires authentication (optional for local) |
| 41 | + |
| 42 | +## Customization |
| 43 | + |
| 44 | +### Resource limits |
| 45 | + |
| 46 | +The Windows profile enforces minimum resources: **cpu >= 2, memory >= 4G, disk >= 64G**. The example uses 4 CPU, 8G RAM, and 64G disk. You can adjust these in the `main.py` `resource` dict. |
| 47 | + |
| 48 | +### Persistent storage |
| 49 | + |
| 50 | +Bind a host directory to `/storage` for a persistent system disk (add to the `SandboxSync.create` call): |
| 51 | + |
| 52 | +```python |
| 53 | +from opensandbox.models.sandboxes import Host, Volume |
| 54 | + |
| 55 | +volumes = [ |
| 56 | + Volume( |
| 57 | + name="win-storage", |
| 58 | + host=Host(path="/data/opensandbox/windows-storage"), |
| 59 | + mount_path="/storage", |
| 60 | + read_only=False, |
| 61 | + ), |
| 62 | +] |
| 63 | +``` |
| 64 | + |
| 65 | +### Local ISO |
| 66 | + |
| 67 | +Bind a Windows install ISO to `/boot.iso` to avoid repeated downloads: |
| 68 | + |
| 69 | +```python |
| 70 | +volumes = [ |
| 71 | + Volume( |
| 72 | + name="win-iso", |
| 73 | + host=Host(path="/data/iso/Win11_23H2.iso"), |
| 74 | + mount_path="/boot.iso", |
| 75 | + read_only=True, |
| 76 | + ), |
| 77 | +] |
| 78 | +``` |
| 79 | + |
| 80 | +### Windows guest configuration |
| 81 | + |
| 82 | +Pass [dockur/windows environment variables](https://github.com/dockur/windows) through the `env` parameter: |
| 83 | + |
| 84 | +```python |
| 85 | +env = { |
| 86 | + "VERSION": "11l", |
| 87 | + "USERNAME": "Docker", |
| 88 | + "PASSWORD": "your-secure-password", |
| 89 | + "LANGUAGE": "Chinese", |
| 90 | + "REGION": "zh-CN", |
| 91 | + "KEYBOARD": "zh-CN", |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +Do not manually set `CPU_CORES`, `RAM_SIZE`, or `DISK_SIZE` — they are derived from `resourceLimits` automatically. |
| 96 | + |
| 97 | +## Exposed ports |
| 98 | + |
| 99 | +| Port | Service | |
| 100 | +|------|---------| |
| 101 | +| 44772 | execd (sandbox execution API) | |
| 102 | +| 8080 | HTTP service | |
| 103 | +| 3389 | RDP (native Remote Desktop) | |
| 104 | +| 8006 | Web console (noVNC) | |
| 105 | + |
| 106 | +## Troubleshooting |
| 107 | + |
| 108 | +- **`Unsupported platform.os 'windows'`**: Server build has no Windows profile; upgrade OpenSandbox server. |
| 109 | +- **`INVALID_PARAMETER` for resourceLimits**: Ensure cpu >= 2, memory >= 4G, disk >= 64G. |
| 110 | +- **Stays Pending a long time**: First Windows install is slow; check host resources and `/storage` space, increase `ready_timeout`. |
| 111 | +- **Status Running but endpoint unreachable**: Verify endpoint resolution returns a valid address; check `USER_PORTS` if you need additional ports forwarded. |
| 112 | + |
| 113 | +### ENI CNI network issue (Alibaba Cloud ACK) |
| 114 | + |
| 115 | +On clusters using ENI-based CNIs (e.g. Alibaba Cloud ACK Terway in ENI mode), dockur/windows fails at startup with: |
| 116 | + |
| 117 | +``` |
| 118 | +❯ ERROR: This container does not support host mode networking! |
| 119 | +``` |
| 120 | + |
| 121 | +or: |
| 122 | + |
| 123 | +``` |
| 124 | +❯ ERROR: Status 1 while: ethtool -i "$VM_NET_DEV" |
| 125 | +``` |
| 126 | + |
| 127 | +**Root cause**: The image's `network.sh` uses `ethtool -i` to check the network interface. ENI interfaces have real PCI bus-info, which triggers a false "host mode" detection. Standard veth-based CNIs (Calico, Flannel, Cilium) do NOT have this problem. |
| 128 | + |
| 129 | +**Solution**: Use the provided `main_fix_net.py` example, which patches the script at runtime and sets `NETWORK=slirp` for QEMU user-mode NAT: |
| 130 | + |
| 131 | +```shell |
| 132 | +python main_fix_net.py |
| 133 | +``` |
| 134 | + |
| 135 | +See [`main_fix_net.py`](./main_fix_net.py) for the full implementation. |
| 136 | + |
| 137 | +**How it works**: |
| 138 | + |
| 139 | +1. `sed` replaces three lines in `/run/network.sh` with empty variable assignments (`result=""`, `nic=""`, `bus=""`), preventing the ethtool check from aborting the script. |
| 140 | +2. `NETWORK=slirp` tells the script to use QEMU's SLIRP networking (user-mode NAT), which doesn't require a real NIC. |
| 141 | +3. `exec /usr/bin/tini -s /run/entry.sh` launches the original image entrypoint after patching. |
| 142 | + |
| 143 | +This approach keeps the Pod's independent IP and requires no image rebuild or `hostNetwork`. |
| 144 | + |
| 145 | +## Windows Sandbox from pool |
| 146 | + |
| 147 | +Use a pre-warmed K8s pool for faster Windows sandbox startup. |
| 148 | + |
| 149 | +### 1. Create the pool |
| 150 | + |
| 151 | +Apply the pool manifest (the image, resources, device mounts, and OEM scripts are pre-configured): |
| 152 | + |
| 153 | +```shell |
| 154 | +kubectl apply -f pool-win-example.yaml |
| 155 | +``` |
| 156 | + |
| 157 | +### 2. Start the OpenSandbox server [k8s] |
| 158 | + |
| 159 | +```shell |
| 160 | +uv pip install opensandbox-server |
| 161 | +opensandbox-server init-config ~/.sandbox.toml --example k8s |
| 162 | +opensandbox-server |
| 163 | +``` |
| 164 | + |
| 165 | +### 3. Run the pool example |
| 166 | + |
| 167 | +```shell |
| 168 | +uv pip install opensandbox |
| 169 | +python main_use_pool.py |
| 170 | +``` |
| 171 | + |
| 172 | +The script acquires a sandbox from `pool-win-example`, prints endpoints, and runs a command. |
| 173 | + |
| 174 | +### Environment variables (pool) |
| 175 | + |
| 176 | +- `SANDBOX_DOMAIN`: Sandbox service address (default: `localhost:8080`) |
| 177 | +- `SANDBOX_API_KEY`: API key if your server requires authentication |
| 178 | + |
| 179 | +## References |
| 180 | + |
| 181 | +- [Windows sandbox guide](../../docs/windows-sandbox.md) |
| 182 | +- [dockur/windows](https://github.com/dockur/windows) |
0 commit comments