Command-line interface
The Microsandbox command-line interface (CLI) is the operator entrypoint for local and cloud sandbox work. The msb command selects a backend, turns workload inputs into sandbox actions, manages images and durable state, exposes status and telemetry, and connects image pulls to registry credentials. It serves both people at a terminal and scripts through stable names, exit codes, and JSON output.
Operator entrypoint
The CLI presents one command model for different execution targets. It keeps backend choice visible and rejects operations that do not fit the selected target instead of silently changing where work runs.
Command groups
The top-level command tree groups operator work by the resource or concern being managed. Sandbox actions are available as short top-level commands and under msb sandbox; sbx is an alias for the grouped form.
| Group | Purpose | Representative commands |
|---|---|---|
| Sandboxes | Create, restore, run, control, inspect, and observe workloads | run, create, restore, fork, start, stop, exec, copy, jobs, logs, inspect, metrics |
| Images | Pull, inspect, archive, remove, and cache OCI images | image, pull, load, save, registry |
| Storage | Manage volumes and snapshots, and inspect or reclaim local use | volume, snapshot, df, prune |
| Services | Manage public names for sandbox ports | service |
| Installation | Check, update, downgrade, remove, and extend the CLI | doctor, self, update, downgrade, install, uninstall, completion |
| Backend selection | Inspect and select the execution target | context |
msb --help shows the grouped entrypoints. msb --tree shows the full hierarchy. Hidden commands support runtime launch and release coordination rather than normal operator work. Human progress and diagnostic text use stderr so that data on stdout stays safe for pipelines.
References: crates/…/main.rs• L26-59 crates/…/main.rs• L65-210 crates/…/main.rs• L496-600
Command dispatch
The CLI normalizes aliases before it dispatches work. Top-level sandbox shortcuts, sandbox or sbx, and snapshot restore therefore reach one sandbox command path.
For backend-sensitive work, the CLI resolves the active backend before it opens local state or contacts a cloud service. Commands that need the local catalog prepare it before creation or restore. Control, diagnosis, context management, and self-management remain available when backend settings need repair.
The CLI also owns the boundary between an operator command and the sandbox runtime. A private machine entrypoint receives protected launch data, checks that the launch intent is valid, and keeps lifecycle ownership with the sandbox process. Guest command failures preserve their exit meaning so automation can tell a failed guest command from a CLI failure.
References: crates/…/main.rs• L233-245 crates/…/main.rs• L382-488 crates/…/main.rs• L804-945 crates/…/machine_cmd.rs• L31-45 crates/…/machine_cmd.rs• L154-224 crates/…/machine_cmd.rs• L446-481
Backend selection
The backend is the target of a CLI operation. A local backend uses the host runtime and local state. A cloud backend sends supported work to the configured service.
Context profiles
msb context shows the selected backend, its source, its profile, and any selection problem without printing credentials. context list shows saved profiles and policy status. context use saves a profile preference. context reset removes that preference.
Environment and managed settings can override the saved preference. The CLI reports that override so an operator does not mistake a saved profile for the active target. Context changes do not need a backend connection or a local catalog, which makes them useful for recovery.
References: crates/…/context.rs• L12-92 crates/…/context.rs• L43-120 crates/…/context.rs• L178-229
Backend boundaries
The selected backend controls where sandboxes, volumes, snapshots, services, and related state are read or changed. --local and --remote provide an explicit target for supported resource commands and cannot be used together.
Local-only operations include host image-cache work, local storage accounting, and live local metrics. Cloud-only service operations report how to select a cloud backend when the active target is local. The CLI does not fall back to another backend to make a command succeed.
References: crates/…/main.rs• L73-80 crates/…/main.rs• L290-305 crates/…/main.rs• L386-395 crates/…/common.rs• L58-73 crates/…/service.rs• L20-24 crates/…/metrics.rs• L107-118
Sandbox definitions
Sandbox definitions describe the workload that the CLI sends to the runtime. They can select an OCI image, snapshot, host disk, or host directory and can add resources, runtime rules, storage, network policy, secrets, and startup actions.
Configuration resolution
run, create, and installed commands can load a root file plus scoped files. The resolver merges sources in command-line order, validates the merged definition, and then applies explicit flags. An explicit image or snapshot selection takes precedence over a value from a file.
The file format is sparse and strict. Unknown fields, project-only fields, duplicate keys, unsupported YAML features, missing environment references, and invalid resource values fail before sandbox creation. Paths from a file stay relative to that file. Secret values remain host-side references rather than becoming part of the resolved definition.
This diagram shows how source files and command flags become one sandbox definition.
flowchart LR
A[Root configuration] --> C[Ordered merge]
B[Scoped configuration] --> C
C --> D[Validated sandbox definition]
E[Explicit CLI values] --> D
D --> F[Create, run, restore, or install]
References: crates/…/sandbox_config.rs• L46-94 crates/…/sandbox_config.rs• L551-603 crates/…/sandbox_config.rs• L689-823 crates/…/sandbox_config.rs• L825-892 crates/…/common.rs• L129-150
Sandbox policy
Shared sandbox options define the policy passed to creation and run flows. The policy covers compute limits, working state, mounts, environment, scripts, lifecycle limits, host communication, network access, and protected secrets.
Networking and secrets are policy controls, not ordinary workload text. Related flags are checked as a set, and secret values must come from host environment references. This keeps credentials out of command arguments and saved sandbox definitions.
References: crates/…/common.rs• L145-258 crates/…/common.rs• L260-355 crates/…/common.rs• L699-720 crates/…/common.rs• L1149-1244
Sandbox operation
Sandbox commands cover the workload lifecycle from creation to removal. A sandbox can be one-off and cleaned up after a command, or named and retained for later control, execution, inspection, and reuse.
Execution lifecycle
msb run creates or reuses a sandbox and runs a command in it. An unnamed run is temporary. A named run keeps state and reuses it unless --replace is requested. msb create boots an idle sandbox. Detached run starts the workload in the background and prints its name.
Lifecycle commands start, stop, pause, resume, restart, wait for, and remove retained sandboxes. restore creates a sandbox from saved state. fork creates independent children from live execution without first making a durable full snapshot. A full snapshot can resume its captured workload, so a new command may need a later exec.
The child page Sandbox operation command flows explains lifecycle, execution, file transfer, and shutdown flows in more detail.
This diagram shows the difference between a one-off run and a named sandbox.
flowchart TD
A[Image or snapshot definition] --> B{Named?}
B -->|No| C[Run workload]
C --> D[Runtime cleanup]
B -->|Yes| E[Retained sandbox]
E --> F[Start, restore, or fork]
F --> G[Execute and observe]
G --> H[Stop, pause, resume, or restart]
H --> E
E --> I[Wait or remove]
References: crates/…/run.rs• L18-68 crates/…/run.rs• L110-140 crates/…/run.rs• L183-303 crates/…/create.rs• L12-27 crates/…/create.rs• L33-90 crates/…/branch.rs• L11-103 crates/…/restore.rs• L23-55 crates/…/restore.rs• L215-269
Command execution
Execution connects a host terminal or pipe to a command inside a sandbox. The CLI chooses interactive, captured, or byte-streamed I/O from the terminal state and the operator's flags. It returns the guest command's exit result.
Detached execution separates command lifetime from terminal lifetime. It creates a sandbox-owned job and prints a job ID. Later commands can list, inspect, wait for, attach to, signal, kill, or close input for that job. Job IDs are meaningful only with their owning sandbox.
References: crates/…/exec.rs• L27-85 crates/…/exec.rs• L92-191 crates/…/exec.rs• L198-301 crates/…/jobs.rs• L14-52 crates/…/jobs.rs• L76-167 crates/…/jobs.rs• L169-263
File transfer
msb copy moves files and directories between the host and a sandbox, or between two sandboxes. A sandbox endpoint uses SANDBOX:/absolute/path; at least one endpoint must be a sandbox path.
The command keeps familiar copy behavior for existing directories and preserves useful file modes. Copy-out confines guest links to the chosen host destination instead of allowing guest paths to escape it.
References: crates/…/copy.rs• L38-86 crates/…/copy.rs• L92-149 crates/…/copy.rs• L181-229 crates/…/copy.rs• L450-528 crates/…/destination_tests.rs• L194-232
Sandbox modification
msb modify changes supported settings on an existing sandbox through a plan. The plan shows the requested change, its effect, and whether it applies live, at the next start, or after a restart.
Resource changes can be accepted before the guest reaches the requested state. The CLI reports requested, actual, enforced, and convergence state so an operator can distinguish host enforcement from guest completion. Compaction is a separate operation and cannot be mixed with normal edits.
References: crates/…/modify.rs• L25-147 crates/…/modify.rs• L154-278 crates/…/modify.rs• L566-710 crates/…/live_resize.rs• L215-327 crates/…/live_resize.rs• L329-435
Images and durable state
Images and durable state provide repeatable inputs and data for sandbox work. The CLI treats image cache, named volumes, snapshots, and runtime memory as separate resource classes.
OCI image workflows
pull and image pull download OCI content into the local cache. image ls and image inspect show cached references and metadata. load imports an archive. save exports cached images in Docker-compatible or OCI form. image rm and image prune remove cache data that is no longer needed.
The cache is shared by local sandboxes and can reuse image layers. Pulling before the first start moves registry work out of the boot path. Normal removal protects images still needed by sandboxes or snapshots.
References: crates/…/image.rs• L22-55 crates/…/image.rs• L168-192 crates/…/image.rs• L212-355 crates/…/image.rs• L419-477 crates/…/image.rs• L713-825 crates/…/pull.rs• L12-60
Volumes and snapshots
Named volumes keep data outside an image so that it can outlive a sandbox. They can be directory-backed or fixed-size disk resources. The volume command creates, lists, inspects, and removes them.
Snapshots preserve reusable sandbox state. Disk snapshots preserve disk state. Full snapshots also preserve memory and execution state so a workload can resume. Snapshot groups organize checkpoint history and select a head. Archives exchange snapshot data between hosts. --from-snapshot accepts one group or a set of explicit paths and resolves the selected checkpoint before launch.
References: crates/…/volume.rs• L13-37 crates/…/volume.rs• L97-140 crates/…/snapshot.rs• L22-75 crates/…/snapshot.rs• L389-405 crates/…/snapshot.rs• L499-605 crates/…/snapshot.rs• L608-724 crates/…/snapshot_source.rs• L14-51 crates/…/snapshot_source.rs• L131-191
Storage accounting
msb df provides a point-in-time view of local use across images, snapshots, sandbox state, volumes, and memory artifacts. It separates logical size, allocated blocks, use, and reclaimable data.
msb prune targets only unused runtime memory cache. It previews candidates, requires confirmation when needed, and scans again before removal. It does not remove saved snapshots, sandbox disks, volumes, images, or stable lock files.
References: crates/…/storage.rs• L21-58 crates/…/storage.rs• L64-88 crates/…/storage.rs• L90-132 crates/…/storage.rs• L204-246
Public services
The service surface gives a cloud-backed sandbox port a stable public URL. It changes public reachability without changing the sandbox lifecycle.
Public URL lifecycle
msb service create binds a service slug to a running sandbox port. update changes the target and enables the service. enable and disable change public availability while retaining the service record. inspect, list, and remove expose or release that public name.
Targets use SANDBOX:PORT with a port from 1 through 65535. Human output shows the slug, status, target, URL, and times. JSON output uses the public service record shape.
This diagram shows how a stable service name connects to a sandbox port.
flowchart LR
A[Service slug] --> B[Cloud service]
B --> C[Public URL]
B --> D[Sandbox port]
E[Create or update] --> B
F[Enable or disable] --> B
G[Inspect or list] --> B
H[Remove] --> B
References: crates/…/service.rs• L28-68 crates/…/service.rs• L145-240 crates/…/service.rs• L248-305 crates/…/output.rs• L11-50
Observability and diagnostics
The CLI provides state views, workload output, resource metrics, and repair guidance. These views help an operator understand both what the catalog wants and what the running sandbox has applied.
Sandbox state
msb list discovers stored sandboxes and can filter by state or label. msb status focuses on running sandboxes and summarizes image, command, resources, ports, and state. msb inspect provides the detailed view.
Inspection can show desired configuration beside active configuration. This makes restart-bound changes visible instead of presenting them as already active. ping checks agent reachability. touch refreshes the idle timer.
References: crates/…/list.rs• L15-99 crates/…/ps.rs• L14-35 crates/…/ps.rs• L52-104 crates/…/inspect.rs• L61-137 crates/…/inspect.rs• L395-471 crates/…/ping.rs• L15-33 crates/…/touch.rs• L13-27
Logs and metrics
msb logs reads captured workload output and can follow new entries. It can select output sources, time ranges, jobs, text matches, session IDs, timestamps, and JSON Lines. Non-text bytes remain representable in JSON output.
msb metrics reports CPU, memory, disk, network, and uptime for local sandboxes. A terminal can watch a table. Automation can consume one JSON Lines record per sandbox at each interval. Metrics describe use; status describes allocation and configuration.
References: crates/…/logs.rs• L43-109 crates/…/logs.rs• L180-255 crates/…/logs.rs• L454-507 crates/…/metrics.rs• L41-70 crates/…/metrics.rs• L107-155 crates/…/metrics.rs• L177-259
Storage diagnosis and repair
Bare msb doctor checks host runtime and virtualization readiness. A named target or --all changes the command to read-only local sandbox storage diagnosis. Exit codes distinguish ready storage, an actionable blocker, and an incomplete inspection.
msb self repair repairs selected local sandboxes without starting them and keeps original disk files. Interactive repair can ask for consent to stop a writer or enter a maintenance window. Scripted and JSON repair require an explicit target and never invent that consent.
Repair reports partial progress. Completed stops and repairs remain visible when another target fails or the operator cancels. Support output uses fixed failure classes and excludes guest contents, credentials, names, and host paths.
This diagram shows how diagnosis controls whether repair can proceed.
flowchart TD
A[Storage target] --> B[Read-only diagnosis]
B --> C{Ready?}
C -->|Yes| D[Report ready]
C -->|No| E{Writer or maintenance blocker?}
E -->|Yes| F[Ask for explicit consent]
E -->|No| G{Safe repair established?}
F -->|Accepted| H[Stop required writer]
F -->|Declined| I[Skip target]
H --> J[Re-diagnose]
J --> G
G -->|Yes| K[Repair and retain originals]
G -->|No| L[Report investigation needed]
References: crates/…/self_cmd.rs• L139-165 crates/…/storage_doctor.rs• L43-83 crates/…/repair.rs• L23-47 crates/…/repair.rs• L66-187 crates/…/interactive.rs• L252-390 crates/…/interactive.rs• L393-477
Registry interaction
The registry surface connects OCI image access to operator credentials. It lets an operator authenticate once and lets later pulls use a selected credential source without placing a password in a command argument.
Credential lifecycle
msb registry login reads a password or token from a hidden prompt or stdin. It stores the secret in the operating system credential store and keeps safe metadata in Microsandbox configuration. logout removes the credential. registry ls lists registry names, users, and source labels without printing secrets.
Image pulls can use explicit credentials, the operating system store, host configuration, Docker credentials, or anonymous access. A sandbox configuration can also name a registry user and a host environment variable for its password.
References: crates/…/registry.rs• L21-67 crates/…/registry.rs• L73-197 crates/…/image.rs• L288-301 crates/…/sandbox_config.rs• L1396-1413
CLI installation and extension
The installation surface keeps the operator entrypoint usable across supported hosts. It also turns a validated sandbox definition into a repeatable shell command.
Host readiness and release changes
msb doctor checks host virtualization and runtime prerequisites. It can apply supported fixes after confirmation. update and self update install a newer CLI and runtime support. self downgrade checks target compatibility, protects local state with operation ownership, and records progress so an interrupted change can recover.
self uninstall removes selected managed state or the full installation after confirmation. Maintenance commands remain usable while ordinary sandbox commands cannot resolve a valid backend. Local execution still needs the host's supported virtualization system.
References: crates/…/self_cmd.rs• L62-120 crates/…/self_cmd.rs• L487-557 crates/…/self_cmd.rs• L807-927 crates/…/self_cmd.rs• L970-1073 crates/…/tests.rs• L53-127 crates/…/tests.rs• L143-197
Installed sandbox commands
msb install creates a shell command that invokes a chosen sandbox setup through msb run. The generated command can reuse persistent state or create a fresh sandbox on each call.
The CLI validates the resolved setup before writing the command. It stores stable configuration paths and replaces only commands previously generated by Microsandbox. msb uninstall removes those generated commands without overwriting unrelated files.
References: crates/…/install.rs• L32-121 crates/…/install.rs• L127-213 crates/…/install.rs• L274-374 crates/…/uninstall.rs• L14-81 crates/…/completion.rs• L12-39
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the public command groups, aliases, canonical routing, destination selectors, and hidden runtime commands aligned in crates/…/main.rs.
- Keep backend resolution, catalog preparation, local-only boundaries, and backend-independent maintenance behavior aligned.
- Keep context profile status, policy checks, saved preference behavior, and secret-safe output aligned.
- Keep configuration source order, path anchoring, strict validation, image precedence, and secret references aligned across files and flags.
- Keep sandbox reuse, replacement, temporary cleanup, restore selection, fork behavior, and lifecycle ownership aligned across
run,create,restore, andmachine. - Keep job selectors scoped to the owning sandbox across
exec,jobs,wait,logs,inspect, and job controls. - Keep copy endpoint parsing, existing-directory behavior, file modes, and host-side link confinement aligned across host platforms.
- Keep desired state, active state, restart policy, resize acknowledgement, and convergence output aligned across
modify,inspect,status, and metrics. - Keep image cache references aligned with image removal, pruning, snapshot dependencies, and pull materialization.
- Keep snapshot groups, heads, checkpoint selectors, archive dependencies, saved bindings, and restore entrypoints aligned.
- Keep service slug ownership, cloud-only routing, target validation, pagination, and JSON records aligned with the service API.
- Keep storage preview, ownership recheck, confirmation, partial repair results, cancellation, exit codes, and privacy-safe reports aligned.
- Keep registry credential metadata separate from secret material and preserve the pull credential selection order.
- Keep launch payload protection, restore intent, lifecycle ownership, and runtime capability checks aligned between the launcher and
msb machine. - Keep progress and diagnostics on stderr and final data on stdout when changing tables, errors, JSON, or completion output.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The active backend is unavailable or wrong | The operation fails or reads a different environment than expected | Check msb context or use --local or --remote before backend-sensitive work |
| A local host lacks virtualization support | A local sandbox cannot boot | Run msb doctor before local execution |
| A sandbox file has an unknown field or missing environment reference | Configuration fails before creation | Use the sparse schema and define every referenced host variable |
A named run finds an existing sandbox | Creation settings are ignored and the existing state is reused | Use --replace, or use exec for a new command |
| A full snapshot is treated as disk-only state | Captured processes do not resume | Check the snapshot scope and restore mode |
| A job ID is paired with another sandbox | Job lookup fails | Treat job IDs as sandbox-scoped |
| A copy endpoint with a colon is ambiguous | The transfer is rejected | Use SANDBOX:/absolute/path or prefix a local path with ./ |
| A restart-bound change lacks a restart choice | The change is not active now | Review the plan and choose --restart or --next-start |
| A required image or snapshot dependency is removed | A later start or restore can fail | Inspect references and use normal removal or prune behavior |
| A service command uses the local backend | No public URL changes | Select a cloud backend |
| Registry credentials are incomplete or unavailable | Image pull fails before workload start | Use registry login or provide a complete host-side credential reference |
| Storage repair lacks writer or maintenance consent | Repair stops without unsafe mutation | Stop the writer and use the interactive repair flow when consent is required |
| A resize is accepted but the guest does not converge | Host enforcement and guest state differ | Use modify --wait and treat accepted as unconfirmed |
| A followed log misses rotation | Follow stops instead of silently skipping entries | Restart msb logs -f |
| Launch data is missing, unprotected, or has the wrong restore intent | The machine process exits before runtime start | Keep the launcher payload and hidden machine flags as one contract |
Key files
| File | Why to read it |
|---|---|
| crates/…/main.rs | Defines the public command tree, aliases, destination selection, backend dispatch, and top-level error behavior. |
| crates/…/context.rs | Shows backend profile inspection and safe preference management. |
| crates/…/common.rs | Defines shared sandbox policy, backend checks, target selection, and command support rules. |
| crates/…/sandbox_config.rs | Defines strict configuration loading, source merging, path handling, image sources, and validation. |
| crates/…/machine_cmd.rs | Shows the protected handoff from the CLI launcher to the sandbox runtime. |
| crates/…/run.rs | Shows create, reuse, command execution, input forwarding, and lifecycle cleanup. |
| crates/…/exec.rs | Shows interactive, captured, streamed, detached, and timeout execution. |
| crates/…/jobs.rs | Defines detached command ownership and sandbox-scoped controls. |
| crates/…/copy.rs | Defines host-to-sandbox, sandbox-to-host, and sandbox-to-sandbox transfer. |
| crates/…/modify.rs | Shows planned changes, restart policy, and resource convergence. |
| crates/…/snapshot.rs | Defines snapshot capture, groups, verification, archive exchange, and head selection. |
| crates/…/snapshot_source.rs | Defines snapshot source classification and launch selection. |
| crates/…/image.rs | Defines image pull, cache, archive, removal, and prune workflows. |
| crates/…/service.rs | Defines cloud-only public URL management for sandbox ports. |
| crates/…/logs.rs | Defines captured output selection, follow behavior, filtering, and JSON Lines output. |
| crates/…/metrics.rs | Defines live and streamed local resource observation. |
| crates/…/self_cmd.rs | Defines host checks, installation changes, update, downgrade, and recovery. |
| crates/…/repair.rs | Defines scripted storage diagnosis and safe repair boundaries. |
| crates/…/interactive.rs | Defines consent, per-target repair, cancellation, and partial outcomes. |
| crates/…/registry.rs | Defines registry credential storage and secret-safe listing. |
| crates/…/install.rs | Defines generated persistent and temporary sandbox commands. |
| docs/cli-sandbox-operations.md | Explains the child-page command flows for sandbox lifecycle, execution, transfer, and shutdown. |