# Host Mount Mount host directories into a Cube Sandbox at creation time, giving the sandbox read or read-write access to files that live on the Cubelet host node. ## 1. Background **Cube Sandbox** is a lightweight MicroVM platform compatible with the [E2B SDK](https://e2b.dev). Host mounts are a **Cube-specific extension** to the standard E2B API: they are requested through the `metadata` field of `Sandbox.create()` using the key `host-mount`. ``` Cubelet host node ┌──────────────────────────────────────────────────────┐ │ /data/shared/rw ───────────► /mnt/rw (read-write) │ │ /data/shared/ro ───────────► /mnt/ro (read-only) │ │ │ │ KVM MicroVM (sandbox) │ └──────────────────────────────────────────────────────┘ ``` ### Mount Descriptor Schema ```json [ { "hostPath": "/absolute/path/on/host", "mountPath": "/path/inside/sandbox", "readOnly": false } ] ``` | Field | Type | Description | |-------|------|-------------| | `hostPath` | string | Absolute path on the **Cubelet node** to bind-mount | | `mountPath` | string | Target path inside the sandbox VM | | `readOnly` | bool | `true` = read-only; `false` = read-write | > **Note:** `hostPath` refers to the filesystem of the **Cubelet node** running > the sandbox, not the machine where your script executes. ### Path Restriction For security, `hostPath` must be under one of the **allowed prefixes**. By default only `/data/shared/` is permitted. Attempts to mount paths outside the allowed prefixes (e.g. `/etc`, `/var`) will be rejected with an error. The allowed prefixes are configured in CubeMaster's YAML config: ```yaml extra_conf: allowed_host_mount_prefixes: - "/data/shared/" - "/data/team-assets/" # add more as needed ``` When the list is empty or omitted the default `["/data/shared/"]` applies. The root path `/` is explicitly forbidden and will cause CubeMaster to reject the configuration at startup. Path-traversal attempts (e.g. `/data/shared/../etc`) are resolved before checking and will be rejected. If a disallowed `hostPath` is specified, sandbox creation will fail. The Python SDK raises an `ApiError` exception with HTTP status 500: ```python from cubesandbox import Sandbox from cubesandbox import ApiError try: sandbox = Sandbox.create( template="tpl-xxx", metadata={"host-mount": json.dumps([ {"hostPath": "/etc/passwd", "mountPath": "/mnt/x"} ])} ) except ApiError as e: print(e.status_code) # 500 print(str(e)) # "host-mount" entry[0]: hostPath "/etc/passwd" is not within an allowed mount prefix ``` The raw HTTP response from CubeAPI looks like: ```http HTTP/1.1 500 Internal Server Error Content-Type: application/json { "code": 500, "message": "internal error: \"host-mount\" entry[0]: hostPath \"/etc/passwd\" is not within an allowed mount prefix" } ``` ## 2. Use Cases - Provide large datasets without copying them into the sandbox image - Share a read-only model weights or config directory across many sandboxes - Write sandbox outputs directly to a host path for easy retrieval - Mount a source-code workspace for on-demand code execution ## 3. Prerequisites - A running Cube Sandbox deployment - Python 3.8+ - The host directories (`/data/shared/rw` and `/data/shared/ro` in the example) must exist on the Cubelet node before creating the sandbox - If you want to use a different host path prefix, see `docs/guide/persistent-storage.md` for the `allowed_host_mount_prefixes` configuration and update the example paths accordingly ```bash # On the Cubelet node (or wherever your sandbox VM runs): mkdir -p /data/shared/rw /data/shared/ro echo "hello from host" > /data/shared/ro/greeting.txt ``` Install dependencies: ```bash pip install -r requirements.txt ``` ## 4. Quick Start ### Step 1 — Create a Template ```bash cubemastercli tpl create-from-image \ --image cube-sandbox-int.tencentcloudcr.com/cube-sandbox/sandbox-code:latest \ --writable-layer-size 1G \ --expose-port 49999 \ --expose-port 49983 \ --probe 49999 ``` > **Image registry:** Use `cube-sandbox-int.tencentcloudcr.com` (international) > or `cube-sandbox-cn.tencentcloudcr.com` (mainland China). Note the `template_id` printed on success. ### Step 2 — Configure Environment Variables ```bash cp .env.example .env # edit .env and fill in E2B_API_URL and CUBE_TEMPLATE_ID ``` Or export directly: ```bash export E2B_API_KEY=e2b_000000 export E2B_API_URL=http://:3000 export CUBE_TEMPLATE_ID= ``` ### Step 3 — Run the Example ```bash python create_with_mount.py ``` Expected output: ``` sandbox info: SandboxInfo(sandbox_id='...', template_id='...', ...) mount contents: /mnt/ro: greeting.txt /mnt/rw: ``` ## 5. How It Works ```python with Sandbox.create( template=template_id, metadata={ "host-mount": json.dumps([ {"hostPath": "/data/shared/rw", "mountPath": "/mnt/rw", "readOnly": False}, {"hostPath": "/data/shared/ro", "mountPath": "/mnt/ro", "readOnly": True}, ]) }, ) as sandbox: ... ``` | Step | What happens | |------|-------------| | `Sandbox.create(metadata=...)` | CubeAPI passes the `host-mount` JSON to Cubelet | | Cubelet receives the request | Parses the mount list and performs bind-mounts before booting the VM | | VM boots | The paths appear inside the sandbox at the specified `mountPath` locations | | Read-only mount | The kernel enforces `MS_RDONLY`; writes are rejected with `EROFS` | ## 6. Troubleshooting | Symptom | Likely Cause | Fix | |---------|-------------|-----| | `hostPath "..." is not within an allowed mount prefix` | `hostPath` is outside the configured allowed prefixes | Move data under `/data/shared/`, or update `allowed_host_mount_prefixes` as described in `docs/guide/persistent-storage.md` | | `No such file or directory` inside sandbox | `hostPath` does not exist on the Cubelet node | Create the directory on the node before running | | `Read-only file system` when writing to `/mnt/ro/...` | Mounted with `readOnly: true` | Set that mount to `readOnly: false`, or write to the read-write sandbox path `/mnt/rw/...` instead | | `Template not found` | Wrong template ID | Run `cubemastercli tpl list` | | `Connection refused` | CubeAPI not reachable | Check `E2B_API_URL` and that port 3000 is open | ## 7. Directory Structure ``` host-mount/ ├── README.md # This file ├── create_with_mount.py # Example script ├── env_utils.py # .env loader utility ├── requirements.txt # Python dependencies └── .env.example # Environment variable template ```