# Sandbox plugin

The sandbox plugin lets you run commands and manipulate the filesystem inside an instance, over its [plugin endpoint](/plugins#how-it-works):

- **Commands**: start a shell command inside the instance, inspect it, read its output, feed its standard input, wait for it, and signal it.
- **Filesystem**: create directories, and read, write, and upload files.

The [API reference](/api/plugins/sandbox) lists every request of the plugin.

Load the sandbox plugin with `--plugin` when you create, run, or [edit](/plugins#changing-plugins-on-an-existing-instance) an instance:

```bash
unikraft instance run \
  --metro fra \
  --image official/alpine:latest \
  --volume :/data:size=1GiB \
  --plugin 'name=sandbox,image=plugins/sandbox:latest,config={"persist_path":"/data"}'
```

The plugin takes these keys in `config`, all optional:

| Key | Default | Description |
|-----|---------|-------------|
| `persist_path` | None | Directory where the plugin keeps commands and their output, so that they survive a restart |
| `inherit_env` | `false` | `true` starts each command from the [environment of the instance](#environment) |

An instance with the sandbox plugin loaded accepts five `unikraft` commands:

| Command | Description |
|---------|-------------|
| [`unikraft instance exec`](/cli/unikraft/instances/exec) | Runs a command on the instance, where `--dir` sets the working directory, `--env` sets the environment, and the command's exit status becomes the CLI's. |
| [`unikraft instance shell`](/cli/unikraft/instances/shell) | Opens an interactive shell that runs on your machine and sends each command to the instance, so paths resolve against the instance. |
| [`unikraft instance copy`](/cli/unikraft/instances/copy) | Copies a single file in either direction, to or from a path that you write as `<instance>:<path>`, or as `<metro>/<instance>:<path>` to pick the metro (alias: `cp`). |
| [`unikraft instance write`](/cli/unikraft/instances/write) | Uploads a single local file to the instance, where `--parents` creates missing directories, and `--append` adds to the remote file instead of replacing it. |
| [`unikraft instance read`](/cli/unikraft/instances/read) | Downloads a single file from the instance to an optional local path, by default the remote file's base name. |

```bash
unikraft instance exec my-instance -- ls -la /var/log
unikraft instance shell my-instance
unikraft instance copy ./config.json my-instance:/etc/app.json
unikraft instance write my-instance ./data.bin /var/lib/app.bin --parents
unikraft instance read my-instance /var/log/app.log
```

The commands use the plugin named `sandbox` by default.
Pass `--plugin-name <name>` to use a sandbox plugin that you loaded under a different name.
If the instance has no sandbox plugin, the commands offer to attach one and restart the instance, and `--plugin-image` picks the image to attach.
The offer only appears in an interactive terminal.
Elsewhere, the error says how to attach the plugin.

## Terminal

From a terminal, `unikraft instance exec` runs the command on a pseudo-terminal on the instance.
Full-screen and interactive programs such as `vim`, `top`, `less`, or a password prompt then work.
The CLI puts your terminal in raw mode, and passes on its size when it changes.
`-T` (`--no-tty`) runs the command on pipes instead, and `-t` (`--tty`) fails when standard input or output isn't a terminal.

```bash
unikraft instance exec my-instance -- top
```

In `unikraft instance shell`, a command gets the pseudo-terminal when it reads from and writes to the terminal, so not in a pipe or with a redirection.
`ctrl-c` then goes to that command, as in a local shell.
`unikraft instance shell -c` runs its commands on pipes.
The instance terminal of the dashboard also runs commands on a pseudo-terminal.

## Environment

By default, a command sees only the environment that the request gives it.
With `unikraft instance exec`, that's the `--env` values and nothing else:

```bash
unikraft instance exec my-instance --env DEBUG=true -- env
```

The plugin still finds a program such as `env` through its own `PATH`.
The command doesn't get that `PATH`, so a program that it starts by name may fail.
Pass a `PATH` with `--env` in that case.

Set `inherit_env` to `true` in the plugin `config` to start each command from the environment of the instance.
That environment has the `ENV` values of the image, the values set on the instance, and a default `PATH`.
The request's values then add to it, and replace a variable of the same name:

```bash
unikraft instance run \
  --metro fra \
  --image official/alpine:latest \
  --plugin 'name=sandbox,image=plugins/sandbox:latest,config={"inherit_env":true}'
```

`unikraft instance shell` reads the environment of the instance when it connects, whatever the value of `inherit_env`.
Its `--env` values add to that environment.

## Long-running commands

The plugin API bounds every wait.
A request that waits for a command returns after at most 55 seconds, with status `408` if the command still runs.
The API gateway ends a request that stays silent for about a minute, so the bound keeps a long command from failing there.
A client that waits over HTTP asks again after each `408`.

The CLI and the [Go SDK](/sdks/go#sandboxes) don't wait over HTTP.
They follow each command over a single WebSocket, at `/commands/<command>/ws` on the plugin endpoint.
The connection carries the output as the command writes it, the standard input, terminal resizes, and the exit status.
The plugin keeps an idle connection alive, so a command that writes nothing for a long time stays connected.

The [JavaScript SDK](/sdks/js/sandboxes/commands) waits over HTTP and repeats the request.

## Shell builtins

Inside `unikraft instance shell`, a line that starts with `:` runs in the CLI instead of on the instance, so you can manage the instance you're in without leaving it:

```bash
$ :mount data /var/lib/app --readonly
$ :restart
```

The builtins are `:get`, `:volumes`, `:edit`, `:mount`, `:unmount`, `:start`, `:stop`, `:restart`, and `:suspend`, and `:help` lists them.
The same builtins work in the instance terminal of the dashboard.

A few limitations apply:

- The instance must run on the [`base-compat` runtime](/platform/images#dockerfile-kraftfile-and-runtime).
  Use an image that ships the standard Linux utilities, such as `official/alpine:latest` or `official/debian-slim:latest`, so that more commands work in the sandbox.
- The shell is experimental and has no job control, so `ctrl-z`, `bg`, and `fg` don't work.
- `copy`, `read`, and `write` carry one regular file at a time, and don't carry file permissions across.
- Without a pseudo-terminal, `exec` forwards standard input only when you redirect it from a file or a pipe.
- On Windows, the shell is the same POSIX-style shell as on Linux and macOS, not PowerShell.

## Learn more

* [Plugins](/plugins): how plugins load, and how the platform authorizes requests to them.
* [Sandbox plugin API reference](/api/plugins/sandbox): every request of the plugin.
* [Sandboxes](/use-cases/sandboxes): run untrusted code in a hardware-isolated microVM.
* [Network shield](/plugins/netshield): filter the network traffic of a sandbox, and add credentials to its requests.
* [JavaScript SDK](/sdks/js/sandboxes) and [Go SDK](/sdks/go#sandboxes): use the sandbox plugin from code.
