microsandbox-rb 0.17.5 → 0.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +326 -1
- data/Cargo.lock +708 -1113
- data/DESIGN.md +52 -36
- data/README.md +25 -19
- data/SECURITY.md +15 -7
- data/docs/configuration.md +153 -17
- data/docs/errors.md +8 -2
- data/docs/execution.md +17 -0
- data/docs/images.md +74 -2
- data/docs/lifecycle.md +130 -6
- data/docs/observability.md +9 -7
- data/docs/releasing.md +1 -1
- data/docs/runtime.md +126 -43
- data/docs/surface.md +55 -14
- data/ext/microsandbox/Cargo.toml +22 -20
- data/ext/microsandbox/extconf.rb +9 -8
- data/ext/microsandbox/src/backend.rs +21 -15
- data/ext/microsandbox/src/error.rs +59 -1
- data/ext/microsandbox/src/host_path.rs +37 -28
- data/ext/microsandbox/src/lib.rs +314 -42
- data/ext/microsandbox/src/runtime.rs +5 -5
- data/ext/microsandbox/src/sandbox.rs +508 -241
- data/ext/microsandbox/src/snapshot.rs +63 -20
- data/ext/microsandbox/src/storage.rs +44 -0
- data/lib/microsandbox/errors.rb +63 -0
- data/lib/microsandbox/exec_handle.rb +2 -1
- data/lib/microsandbox/runtime_info.rb +65 -0
- data/lib/microsandbox/sandbox.rb +544 -158
- data/lib/microsandbox/snapshot.rb +101 -28
- data/lib/microsandbox/storage.rb +205 -0
- data/lib/microsandbox/version.rb +5 -4
- data/lib/microsandbox.rb +259 -60
- data/rust-toolchain.toml +1 -1
- data/sig/microsandbox.rbs +113 -7
- metadata +5 -2
data/docs/images.md
CHANGED
|
@@ -59,7 +59,79 @@ Without `registry_auth:`, the core's default credential resolution still applies
|
|
|
59
59
|
## Snapshots
|
|
60
60
|
|
|
61
61
|
`Microsandbox::Snapshot` covers create / open / get / list / list_dir / reindex /
|
|
62
|
-
remove / verify / save / load
|
|
63
|
-
|
|
62
|
+
remove / verify / save / load. `Snapshot.create` captures the disk by default;
|
|
63
|
+
`full: true` (it replaced `resumable:` in 0.18.0) captures a resumable
|
|
64
|
+
checkpoint, and `guest_flush:` (`:auto`, `:required`, `:skip`) chooses the
|
|
65
|
+
optional guest writeback before capture (a stopped source cannot satisfy
|
|
66
|
+
`:required`). `Sandbox.restore` boots a new sandbox from a snapshot (see
|
|
67
|
+
[Restoring](#restoring-a-snapshot)); it replaced `Sandbox.create(from_snapshot:)`,
|
|
68
|
+
which raises with directions since 0.18.0. Snapshot failures raise the typed `Snapshot*Error` classes listed in
|
|
64
69
|
[errors.md](errors.md). Payload integrity is opt-in (`record_integrity:`), and
|
|
65
70
|
`verify` can report `:not_recorded` for snapshots taken without it.
|
|
71
|
+
|
|
72
|
+
Since 0.18.0 (runtime `v0.7`) snapshots live in **groups**. `Snapshot.create(name,
|
|
73
|
+
from_sandbox: src)` publishes the artifact into the group named after `src`, as a
|
|
74
|
+
member aliased `name`, and makes it the group head. Selectors for `open` / `get` /
|
|
75
|
+
`verify` / `save` / `remove` resolve like this:
|
|
76
|
+
|
|
77
|
+
| Selector | Selects |
|
|
78
|
+
|---|---|
|
|
79
|
+
| `"src"` (a bare name) | the group's current head |
|
|
80
|
+
| `"src:name"` | that member |
|
|
81
|
+
| `info.id` (`snap_…`) or `info.digest` (`sha256:…`) | that snapshot |
|
|
82
|
+
| `info.path` (anything containing `/` or starting with `.`) | that artifact directory |
|
|
83
|
+
| `info.reference` | that snapshot, read the way its backend wrote it (`SnapshotInfo#reference_kind`) |
|
|
84
|
+
|
|
85
|
+
So the `name` given to `create` is not a selector on its own any more: keep the
|
|
86
|
+
returned `SnapshotInfo#id`. `Snapshot.load` puts the copy into a fresh generated
|
|
87
|
+
group with no head selected, so address it by `id` or `path` too. `create(force:
|
|
88
|
+
true)` is rejected (grouped snapshots are immutable). The group surface itself
|
|
89
|
+
(choosing a group, moving the head) is not bound yet.
|
|
90
|
+
|
|
91
|
+
```ruby
|
|
92
|
+
info = Microsandbox::Snapshot.create("before-upgrade", from_sandbox: "worker")
|
|
93
|
+
Microsandbox::Snapshot.verify(info.id)
|
|
94
|
+
Microsandbox::Snapshot.get("worker:before-upgrade").id == info.id # => true
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Restoring a snapshot
|
|
98
|
+
|
|
99
|
+
`Sandbox.restore(snapshot, name:, ...)` boots a new sandbox from a snapshot:
|
|
100
|
+
a disk snapshot cold-boots its captured disk, and a full one resumes the
|
|
101
|
+
captured execution (`disk_only: true` cold-boots its disk instead). Pass a
|
|
102
|
+
`SnapshotInfo` (it restores by `#reference`), an id, `"group:member"`, a
|
|
103
|
+
digest, or a path; a relative path is anchored to the current directory when
|
|
104
|
+
you call it. A bare name selects a group head.
|
|
105
|
+
|
|
106
|
+
```ruby
|
|
107
|
+
info = Microsandbox::Snapshot.create("before-upgrade", from_sandbox: "worker")
|
|
108
|
+
|
|
109
|
+
Microsandbox::Sandbox.restore(info, name: "worker-replay") do |sb|
|
|
110
|
+
sb.shell("cat /root/state.txt")
|
|
111
|
+
end # stopped here
|
|
112
|
+
|
|
113
|
+
sb = Microsandbox::Sandbox.restore(info.id, name: "worker-2", cpus: 2, memory: 1024)
|
|
114
|
+
# ... keeps running, even after this process exits, until stopped
|
|
115
|
+
sb.stop
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
The snapshot keeps its image, command, environment and network
|
|
119
|
+
configuration; restore takes only destination choices: `cpus:`/`memory:`
|
|
120
|
+
(disk boot; a full restore takes only the captured values), `user:`,
|
|
121
|
+
`log_level:`, `max_duration:`, `idle_timeout:`, `volumes:`, `ports:`,
|
|
122
|
+
`disable_network:` (disk boot), `cow_memory:` (full restore; `forked:` is a
|
|
123
|
+
deprecated alias), `disk_only:`, `snapshot_base:` (the base of a dependent
|
|
124
|
+
archive), `allow_missing_resources:` and `external_mount_policy:` (`:strict` or
|
|
125
|
+
`:relaxed`). Overriding the network policy, security profile, vsock routes or
|
|
126
|
+
connection caps on restore is not bound yet.
|
|
127
|
+
|
|
128
|
+
A captured bind or named mount needs a destination: pass it in `volumes:`
|
|
129
|
+
under the captured guest path (`volumes: { "/data" => "/srv/data" }`), or pass
|
|
130
|
+
`allow_missing_resources: true` to restore without it (the mount stays
|
|
131
|
+
unavailable). Otherwise restore raises `InvalidConfigError` ("restore requires
|
|
132
|
+
destination bindings for: mount /data; …").
|
|
133
|
+
|
|
134
|
+
Upstream always restores **detached**: `#owns_lifecycle?` is `false`, and a
|
|
135
|
+
sandbox restored without a block keeps running after your process exits
|
|
136
|
+
(0.17's `create(from_snapshot:)` was stopped with it). The block form stops it
|
|
137
|
+
on exit, and an interrupt that lands before `restore` returns stops it too.
|
data/docs/lifecycle.md
CHANGED
|
@@ -32,9 +32,12 @@ sb = Microsandbox::Sandbox.create("box", image: "public.ecr.aws/docker/library/a
|
|
|
32
32
|
begin
|
|
33
33
|
# ...
|
|
34
34
|
ensure
|
|
35
|
-
sb.stop # graceful
|
|
35
|
+
sb.stop # graceful; waits without a deadline (never escalates)
|
|
36
|
+
# sb.stop_with_timeout(10) # graceful within 10s, else StopTimeoutError (no kill)
|
|
36
37
|
# sb.stop_and_wait # graceful, then wait → ExitStatus(#exit_code, #success?)
|
|
37
|
-
# sb.kill # force
|
|
38
|
+
# sb.kill # force; then waits up to 5s to see it stopped
|
|
39
|
+
# sb.kill_with_timeout(10) # force; one 10s bound over the whole call
|
|
40
|
+
# sb.drain # graceful drain
|
|
38
41
|
end
|
|
39
42
|
|
|
40
43
|
# Inspect / manage existing sandboxes. `list` returns a cursor-paginated,
|
|
@@ -44,7 +47,7 @@ Microsandbox::Sandbox.list.map(&:name) # enumerate the page's handles
|
|
|
44
47
|
# next page: Sandbox.list_with(cursor: page.next_cursor, limit: 50)
|
|
45
48
|
h = Microsandbox::Sandbox.get("box") # => Microsandbox::SandboxHandle
|
|
46
49
|
h.status # :running, :stopped, :created, ...
|
|
47
|
-
h.stop_with_timeout(5) #
|
|
50
|
+
h.stop_with_timeout(5) # graceful within 5s, else StopTimeoutError
|
|
48
51
|
h.request_stop # fire-and-return; pair with #wait_until_stopped
|
|
49
52
|
h.request_kill
|
|
50
53
|
h.request_drain
|
|
@@ -75,9 +78,9 @@ for `Thread#raise` and `Thread#kill`, and for `Sandbox.connect_or_create`,
|
|
|
75
78
|
that merely connected to a sandbox already running for someone else leaves
|
|
76
79
|
it running, exactly as its block form does.
|
|
77
80
|
|
|
78
|
-
The cleanup stop is best-effort
|
|
79
|
-
exception you get, and the sandbox is
|
|
80
|
-
object is garbage-collected.
|
|
81
|
+
The cleanup stop is best-effort and bounded like a block form's (below): if
|
|
82
|
+
it fails, the interrupt is still the exception you get, and the sandbox is
|
|
83
|
+
torn down when its now unreferenced object is garbage-collected.
|
|
81
84
|
|
|
82
85
|
A Ctrl-C (`SIGINT`) is the one interrupt that cannot be held back until the
|
|
83
86
|
sandbox is in your hands: one that lands during the create is raised the
|
|
@@ -90,6 +93,51 @@ an exception you deferred with `Thread.handle_interrupt(X => :on_blocking)` is
|
|
|
90
93
|
not raised by them: you get the sandbox, and the exception arrives at the
|
|
91
94
|
thread's next blocking operation.
|
|
92
95
|
|
|
96
|
+
### Stopping since runtime `v0.7`
|
|
97
|
+
|
|
98
|
+
A graceful stop no longer escalates to a kill. `#stop` sends the shutdown
|
|
99
|
+
request and waits, without a deadline, until the runtime has released the
|
|
100
|
+
sandbox. `#stop_with_timeout(n)` waits at most `n` seconds and then raises
|
|
101
|
+
`Microsandbox::StopTimeoutError` — it kills nothing, and the shutdown may still
|
|
102
|
+
complete later (a zero budget times out before the request is sent). The
|
|
103
|
+
budget covers the whole call, including waiting for the sandbox's transition
|
|
104
|
+
lock while another lifecycle operation on the same name holds it.
|
|
105
|
+
|
|
106
|
+
End it for sure with `#kill`. `#kill` first waits, **without a deadline**, for
|
|
107
|
+
that transition lock, then sends the kill and waits up to 5 seconds to see the
|
|
108
|
+
sandbox stopped. `#kill_with_timeout(n)` puts one budget of `n` seconds over
|
|
109
|
+
all of it, lock included, and raises `Microsandbox::Error` when it runs out
|
|
110
|
+
(the sandbox may then still be running). `restart`/`destroy` take a graceful
|
|
111
|
+
budget in `timeout:` (10 seconds by default) and raise `StopTimeoutError` when
|
|
112
|
+
it runs out, unless `force: true`.
|
|
113
|
+
|
|
114
|
+
The block forms of `create`, `connect_or_create` and `restore` stop the sandbox
|
|
115
|
+
on exit with a budget of `Microsandbox::Sandbox::BLOCK_STOP_TIMEOUT` (10)
|
|
116
|
+
seconds and, when that runs out, kill it with a further 15-second budget; both
|
|
117
|
+
cover the transition lock, so leaving a block takes at most about 25 seconds
|
|
118
|
+
even when a guest does not shut down or another process holds the lock.
|
|
119
|
+
Errors in that teardown are swallowed (the block's own result or exception
|
|
120
|
+
wins). When the kill fails or runs out of time, the SDK warns once on
|
|
121
|
+
`$stderr` — `cleanup gave up on sandbox "<name>", which may still be running`
|
|
122
|
+
— and leaves the sandbox as it is: stop or kill it yourself later.
|
|
123
|
+
|
|
124
|
+
```ruby
|
|
125
|
+
begin
|
|
126
|
+
sb.stop_with_timeout(10)
|
|
127
|
+
rescue Microsandbox::StopTimeoutError
|
|
128
|
+
sb.kill
|
|
129
|
+
end
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
`create(replace: true)` does not follow this model: it still escalates. When
|
|
133
|
+
the name belongs to a running sandbox it sends `TERM`, waits the replacement
|
|
134
|
+
grace (`replace_with_timeout:`, 10 seconds by default; `0` skips the `TERM`),
|
|
135
|
+
sends `KILL`, and gives the runtime 5 seconds to prove it exited. If it has
|
|
136
|
+
not, the create raises `Microsandbox::SandboxStillRunningError` rather than
|
|
137
|
+
reusing the name; it raises the same when an untracked runtime endpoint may
|
|
138
|
+
still be live, and a signal that fails raises its own error. None of this
|
|
139
|
+
involves `stop_with_timeout` or `Snapshot.create`'s `force:`.
|
|
140
|
+
|
|
93
141
|
## Convergent lifecycle
|
|
94
142
|
|
|
95
143
|
Idempotent operations that take a name to the state you want, whatever state
|
|
@@ -127,6 +175,82 @@ ran cannot be taken down by the teardown either. `Sandbox#owns_lifecycle?`
|
|
|
127
175
|
reports which case you are in; `Sandbox#detach` hands the VM off so it
|
|
128
176
|
outlives the block.
|
|
129
177
|
|
|
178
|
+
## Restoring from a snapshot
|
|
179
|
+
|
|
180
|
+
`Sandbox.restore(snapshot, name:)` boots a new sandbox from a snapshot (it
|
|
181
|
+
replaced 0.17's `create(from_snapshot:)`). The block form stops it on exit,
|
|
182
|
+
like `create`'s. Without a block it keeps running after your process exits:
|
|
183
|
+
upstream restores **detached**, so `#owns_lifecycle?` is `false` and nothing
|
|
184
|
+
stops it for you. Stop it with `#stop`/`#destroy`, or bound it with
|
|
185
|
+
`max_duration:`/`idle_timeout:`.
|
|
186
|
+
|
|
187
|
+
```ruby
|
|
188
|
+
info = Microsandbox::Snapshot.create("base", from_sandbox: "worker")
|
|
189
|
+
Microsandbox::Sandbox.restore(info, name: "replay") { |sb| sb.shell("make test") }
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
Selectors, destination options and mount bindings are covered in
|
|
193
|
+
[images.md](images.md#restoring-a-snapshot).
|
|
194
|
+
|
|
195
|
+
## Upgrading sandboxes created before 0.18.0
|
|
196
|
+
|
|
197
|
+
Runtime `v0.7` keeps a sandbox's disks in a protected storage layout. A
|
|
198
|
+
sandbox created by 0.17.x or earlier still has the old layout, and it is
|
|
199
|
+
moved into the new one before its first start under 0.18.0:
|
|
200
|
+
|
|
201
|
+
- **Linux**: automatically, when the sandbox next starts.
|
|
202
|
+
- **macOS**: not automatically. The start raises `Microsandbox::Error`
|
|
203
|
+
("macOS legacy storage requires explicit maintenance recovery …"). Stop every pre-0.18 VM in every runtime
|
|
204
|
+
home first (old full snapshots can leave writers behind in another home),
|
|
205
|
+
make sure no older tool will start one or write to this storage, then
|
|
206
|
+
recover each stopped sandbox:
|
|
207
|
+
|
|
208
|
+
```ruby
|
|
209
|
+
Microsandbox::Sandbox.recover_storage("worker", legacy_writers_stopped: true)
|
|
210
|
+
# => #<Microsandbox::StorageRecovery name="worker" ready=true>
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
`legacy_writers_stopped: true` is your confirmation of that maintenance
|
|
214
|
+
window; the runtime cannot check it from one home's catalog, so it never
|
|
215
|
+
assumes it. Recovery does not boot the sandbox, and it keeps the original
|
|
216
|
+
disks (`#originals_retained?`). The sandbox must be stopped
|
|
217
|
+
(`SandboxStillRunningError` otherwise). Sandboxes created under 0.18.0, and
|
|
218
|
+
ones already migrated, need none of this.
|
|
219
|
+
|
|
220
|
+
To see where each sandbox stands first, `Microsandbox.diagnose_storage`
|
|
221
|
+
inspects the local runtime home without starting, stopping, repairing or
|
|
222
|
+
migrating anything — including a home that only 0.17.x has opened so far:
|
|
223
|
+
|
|
224
|
+
```ruby
|
|
225
|
+
report = Microsandbox.diagnose_storage(all_sandboxes: true) # or names: ["worker"]
|
|
226
|
+
report.sandboxes.each { |s| puts "#{s.name}: #{s.status} #{s.reason}" }
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
| `status` | Meaning |
|
|
230
|
+
|---|---|
|
|
231
|
+
| `:ready` | already in the protected layout |
|
|
232
|
+
| `:eligible` | Linux: migrates on its next start |
|
|
233
|
+
| `:maintenance_required` | macOS: run `recover_storage(legacy_writers_stopped: true)` |
|
|
234
|
+
| `:recovery_required` | the old metadata is ambiguous or damaged: pick a view (below) |
|
|
235
|
+
| `:in_progress` | an interrupted migration; `recover_storage` resumes it |
|
|
236
|
+
| `:unavailable` | the check could not finish; `reason` says why (`:not_found`, `:invalid_configuration`, `:incomplete_creation`, `:storage_unavailable`) |
|
|
237
|
+
|
|
238
|
+
A sandbox whose report has a `running?` VM gets a provisional answer, and a
|
|
239
|
+
recovery revalidates everything under locks anyway. For `:recovery_required`,
|
|
240
|
+
`inspection.candidates` lists the complete disk views found: choose one per
|
|
241
|
+
disk and acknowledge that it may lack later writes:
|
|
242
|
+
|
|
243
|
+
```ruby
|
|
244
|
+
candidate = report.sandboxes.first.inspection.candidates.first
|
|
245
|
+
Microsandbox::Sandbox.recover_storage("worker",
|
|
246
|
+
selections: { candidate.disk_id => candidate.id }, accept_selected_view: true,
|
|
247
|
+
legacy_writers_stopped: true) # macOS
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
Both calls are local-only and read the runtime home that `MSB_HOME` (or
|
|
251
|
+
`diagnose_storage(home:)`) names; a home with no catalog yet raises
|
|
252
|
+
`IoError`. `recover_storage` checks the runtime first, like a start.
|
|
253
|
+
|
|
130
254
|
## The live `Sandbox` / `SandboxHandle` split
|
|
131
255
|
|
|
132
256
|
Upstream `v0.5.8` split the lifecycle into the live `Sandbox` and a
|
data/docs/observability.md
CHANGED
|
@@ -4,9 +4,9 @@
|
|
|
4
4
|
|
|
5
5
|
```ruby
|
|
6
6
|
Microsandbox::Sandbox.create("obs", image: "public.ecr.aws/docker/library/alpine:latest") do |sb|
|
|
7
|
-
#
|
|
8
|
-
#
|
|
9
|
-
#
|
|
7
|
+
# The metrics slot goes live a beat after create returns, so `metrics` can
|
|
8
|
+
# briefly raise "... no verified live metrics slot" right after boot — retry
|
|
9
|
+
# for a few hundred ms rather than treating the first failure as fatal.
|
|
10
10
|
m = sb.metrics # => Microsandbox::Metrics
|
|
11
11
|
m.cpu_percent
|
|
12
12
|
m.memory_bytes
|
|
@@ -25,15 +25,17 @@ and `sb.log_stream` (`LogStream` over `LogEntry`) — both single-pass, see
|
|
|
25
25
|
|
|
26
26
|
`MetricsDisabledError` means metrics are turned off for the sandbox. The
|
|
27
27
|
metrics slot goes live a moment *after* `create` returns; a call inside that
|
|
28
|
-
window raises the base `Microsandbox::Error` with
|
|
29
|
-
`no live metrics slot` (
|
|
30
|
-
|
|
28
|
+
window raises the base `Microsandbox::Error` with the message
|
|
29
|
+
`sandbox N has no verified live metrics slot` (runtime `v0.7`; it read
|
|
30
|
+
`no live metrics slot` before 0.18.0) — not `MetricsUnavailableError`, which
|
|
31
|
+
the pinned runtime never raises. Retry on that message, and match both
|
|
32
|
+
spellings if you support older gems too:
|
|
31
33
|
|
|
32
34
|
```ruby
|
|
33
35
|
metrics = begin
|
|
34
36
|
sb.metrics
|
|
35
37
|
rescue Microsandbox::Error => e
|
|
36
|
-
raise unless e.message.
|
|
38
|
+
raise unless e.message.match?(/no (verified )?live metrics slot/)
|
|
37
39
|
sleep 0.1
|
|
38
40
|
retry
|
|
39
41
|
end
|
data/docs/releasing.md
CHANGED
|
@@ -24,7 +24,7 @@ or credential setup is needed.
|
|
|
24
24
|
everywhere it is named:
|
|
25
25
|
- `ext/microsandbox/Cargo.toml` — the `tag = "vX.Y.Z"` on **all three**
|
|
26
26
|
upstream git deps (`microsandbox`, `microsandbox-network`,
|
|
27
|
-
`microsandbox-
|
|
27
|
+
`microsandbox-utils`);
|
|
28
28
|
- `lib/microsandbox/version.rb` — `Microsandbox::RUNTIME_VERSION`;
|
|
29
29
|
- `binaries/lib/microsandbox/binaries.rb` —
|
|
30
30
|
`Microsandbox::Binaries::RUNTIME_VERSION`;
|
data/docs/runtime.md
CHANGED
|
@@ -30,46 +30,124 @@ should skip it: it is a separate, optional gem precisely so nobody has to fetch
|
|
|
30
30
|
### Fallback — first-use download
|
|
31
31
|
|
|
32
32
|
Without the companion gem, the `msb` runtime and `libkrunfw` firmware are
|
|
33
|
-
provisioned into
|
|
34
|
-
`Sandbox.create`/`start` downloads them
|
|
35
|
-
|
|
36
|
-
call `install`
|
|
33
|
+
provisioned into the runtime home (`MSB_HOME`, default `~/.microsandbox`)
|
|
34
|
+
automatically on first use: the first `Sandbox.create`/`start` downloads them
|
|
35
|
+
if no runtime resolves. To provision ahead of time — e.g. while baking a
|
|
36
|
+
container image, or to avoid the first-call latency — call `install`
|
|
37
|
+
explicitly:
|
|
37
38
|
|
|
38
39
|
```ruby
|
|
39
|
-
Microsandbox.install
|
|
40
|
+
Microsandbox.install # no-op when the home already holds this runtime
|
|
40
41
|
```
|
|
41
42
|
|
|
42
43
|
Set `MICROSANDBOX_NO_AUTO_INSTALL` to disable the automatic first-use download
|
|
43
|
-
(e.g. on air-gapped hosts that provision the runtime out of band).
|
|
44
|
-
|
|
45
|
-
the
|
|
46
|
-
|
|
47
|
-
|
|
44
|
+
(e.g. on air-gapped hosts that provision the runtime out of band). With it set
|
|
45
|
+
the SDK never writes a runtime file: a missing runtime surfaces as
|
|
46
|
+
`RuntimeNotInstalledError` from the operation itself, and a mismatched one (see
|
|
47
|
+
below) is only reported. None of this applies when the companion gem supplies
|
|
48
|
+
the runtime: its binaries are already the matching version, so `ensure_runtime!`
|
|
49
|
+
skips the installer entirely and nothing is written to `~/.microsandbox`.
|
|
50
|
+
`Microsandbox.setup` customizes the provisioning step itself.
|
|
51
|
+
|
|
52
|
+
### One runtime version
|
|
53
|
+
|
|
54
|
+
Runtime v0.7 launches a local sandbox only when the `msb` executable is
|
|
55
|
+
**exactly** the runtime this SDK embeds (`Microsandbox::RUNTIME_VERSION`); it
|
|
56
|
+
reads the version embedded in the executable, and refuses every other one —
|
|
57
|
+
an older `0.7.x`, a pre-0.7 `msb` (which carries no embedded version), and a
|
|
58
|
+
wrapper script in front of `msb` (likewise unversioned). Before the first local
|
|
59
|
+
sandbox start `Microsandbox.ensure_runtime!` checks the selected runtime:
|
|
60
|
+
|
|
61
|
+
| Selected runtime | What the SDK does |
|
|
62
|
+
|---|---|
|
|
63
|
+
| this version | nothing |
|
|
64
|
+
| none at all | warns, then `install`s into the runtime home |
|
|
65
|
+
| the runtime home's, another version | warns, then `install(force: true)` replaces the pair in the home |
|
|
66
|
+
| `MSB_PATH`, `runtime_path=`, a configured path — another version | never touched: warns that every local sandbox start will be refused |
|
|
67
|
+
| a partial pair | raises `RuntimeIncompleteError`, naming the files to delete or the variable to set |
|
|
68
|
+
|
|
69
|
+
With `MICROSANDBOX_NO_AUTO_INSTALL` set nothing is installed or replaced in any
|
|
70
|
+
of these cases; a mismatch is reported with the same warning.
|
|
71
|
+
|
|
72
|
+
The selected runtime is the one the local backend will launch. That backend
|
|
73
|
+
reads its runtime configuration — `MSB_HOME`, `MSB_PATH`,
|
|
74
|
+
`MSB_LIBKRUNFW_PATH`, `runtime_path=`/`libkrunfw_path=`, the config file —
|
|
75
|
+
once, when it is first used in a process (any sandbox, image or catalog call,
|
|
76
|
+
`runtime_path`, `default_backend_kind`). If one of them changed afterwards so
|
|
77
|
+
that the home or the selected pair differs, `ensure_runtime!` raises
|
|
78
|
+
`Microsandbox::InvalidConfigError` before it checks or installs anything,
|
|
79
|
+
rather than repairing a runtime that sandboxes will not use. Otherwise both
|
|
80
|
+
the check and any install it makes use the backend's own configuration, so
|
|
81
|
+
changing the environment while it runs cannot redirect them. Set the
|
|
82
|
+
environment before the first SDK call, or call
|
|
83
|
+
`Microsandbox.set_default_backend(:local)` to rebuild the backend from the
|
|
84
|
+
current configuration. `Microsandbox.install` is not affected: it always
|
|
85
|
+
targets the home the current configuration names.
|
|
86
|
+
|
|
87
|
+
`Microsandbox.install(force: false)` checks the runtime home itself — the exact
|
|
88
|
+
two files the resolver expects there (`bin/msb` and `lib/libkrunfw.5.dylib` on
|
|
89
|
+
macOS, `lib/libkrunfw.so.5.6.1` on Linux) — whatever `MSB_PATH` or another
|
|
90
|
+
source would select instead. A complete pair of this version is kept (no
|
|
91
|
+
download) unless `force: true`; another version is replaced; a home with only
|
|
92
|
+
one of the two files is refused, because the installer never repairs a partial
|
|
93
|
+
installation: delete both files and retry.
|
|
48
94
|
|
|
49
95
|
## Runtime path resolution
|
|
50
96
|
|
|
51
|
-
The
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
97
|
+
The runtime is resolved as one **pair** — the `msb` executable and the
|
|
98
|
+
`libkrunfw` library that goes with it — in this order:
|
|
99
|
+
|
|
100
|
+
1. `MSB_PATH`, with `MSB_LIBKRUNFW_PATH` or else the library next to the
|
|
101
|
+
executable (in its directory, or in `../lib`). `MSB_LIBKRUNFW_PATH` alone is
|
|
102
|
+
an error: it needs `MSB_PATH` so the pair is explicit.
|
|
103
|
+
2. The SDK path slots: the `microsandbox-rb-binaries` gem, or
|
|
104
|
+
`runtime_path=` / `libkrunfw_path=`.
|
|
105
|
+
3. The paths in the config file.
|
|
106
|
+
4. The runtime home: `<home>/bin/msb` and `<home>/lib/<libkrunfw>`. A home
|
|
107
|
+
holding only one of them is an error, never skipped.
|
|
108
|
+
|
|
109
|
+
`msb` on `PATH` is no longer consulted. `Microsandbox.runtime_info` reports the
|
|
110
|
+
selected pair and where it came from; `Microsandbox.installed?` says whether
|
|
111
|
+
any source resolves a complete pair (it does not check the version).
|
|
55
112
|
|
|
56
113
|
```ruby
|
|
57
|
-
Microsandbox.
|
|
58
|
-
|
|
59
|
-
|
|
114
|
+
info = Microsandbox.runtime_info # => #<Microsandbox::RuntimeInfo origin=home …>
|
|
115
|
+
info.origin # => :home (:environment, :sdk_package, :configuration, :installed)
|
|
116
|
+
info.msb_path # => "/Users/you/.microsandbox/bin/msb"
|
|
117
|
+
info.libkrunfw_path # => "/Users/you/.microsandbox/lib/libkrunfw.5.dylib"
|
|
118
|
+
info.version # => "0.7.14" (nil for a pre-0.7 msb or a wrapper script)
|
|
119
|
+
info.matches_sdk? # => true
|
|
120
|
+
|
|
121
|
+
Microsandbox.installed? # => true/false (any complete pair, any version)
|
|
122
|
+
Microsandbox.install # install this runtime into the home (idempotent)
|
|
123
|
+
Microsandbox.runtime_path # => the selected msb
|
|
60
124
|
Microsandbox.runtime_path = "/opt/microsandbox/bin/msb" # override (set-once)
|
|
61
|
-
Microsandbox.libkrunfw_path = "/opt/microsandbox/lib/libkrunfw.dylib" # override (set-once)
|
|
125
|
+
Microsandbox.libkrunfw_path = "/opt/microsandbox/lib/libkrunfw.5.dylib" # override (set-once)
|
|
62
126
|
```
|
|
63
127
|
|
|
128
|
+
`origin` is **gem-classified**: runtime v0.7.14 folds `MSB_PATH` and the SDK
|
|
129
|
+
path slots into its configuration before it resolves and reports both as
|
|
130
|
+
`:configuration`. The SDK refines that one value by the resolved path:
|
|
131
|
+
`:environment` when it is `MSB_PATH`, `:sdk_package` when it is the binaries
|
|
132
|
+
gem's `msb` (`MSB_PATH` wins when both match), otherwise `:configuration`
|
|
133
|
+
(`runtime_path=` or the config file). Every other value is upstream's.
|
|
134
|
+
|
|
64
135
|
When `microsandbox-rb-binaries` is installed, `require "microsandbox"` claims
|
|
65
|
-
|
|
66
|
-
|
|
136
|
+
both SDK slots with the gem's vendored `msb` and `libkrunfw`, and
|
|
137
|
+
`runtime_info.origin` is `:sdk_package`. The two gems are versioned in
|
|
67
138
|
lockstep and the companion gem must be the same version **and** built for the
|
|
68
139
|
same upstream runtime — a mismatch of either is reported with a warning and
|
|
69
140
|
skipped, and the SDK falls back to `~/.microsandbox` rather than driving a
|
|
70
|
-
runtime it doesn't match. Because the
|
|
71
|
-
`Microsandbox.runtime_path=`
|
|
72
|
-
variable, which outranks
|
|
141
|
+
runtime it doesn't match. Because the slots are **set-once**,
|
|
142
|
+
`Microsandbox.runtime_path=` and `libkrunfw_path=` are then no-ops: use the
|
|
143
|
+
`MSB_PATH` environment variable, which outranks them, to point at a different
|
|
144
|
+
runtime. If `MSB_PATH` is already set when `microsandbox` is required, the gem
|
|
145
|
+
leaves the `libkrunfw` slot free, so the `MSB_PATH` runtime pairs with its own
|
|
146
|
+
library; setting `MSB_PATH` later, from inside the process, cannot undo that
|
|
147
|
+
claim — set `MSB_LIBKRUNFW_PATH` too in that case.
|
|
148
|
+
|
|
149
|
+
The runtime home must be private: none of its ancestors may be group- or
|
|
150
|
+
world-writable without the sticky bit, or the runtime refuses it.
|
|
73
151
|
|
|
74
152
|
## Backend routing
|
|
75
153
|
|
|
@@ -222,8 +300,8 @@ sandbox from the image pulls it), or in a fresh process started with
|
|
|
222
300
|
## One runtime line per home
|
|
223
301
|
|
|
224
302
|
The runtime home (`MSB_HOME`, default `~/.microsandbox`) belongs to one
|
|
225
|
-
runtime line at a time. This gem embeds runtime
|
|
226
|
-
installs that version of `msb` into the home — replacing a different version
|
|
303
|
+
runtime line at a time. This gem embeds the runtime named by
|
|
304
|
+
`Microsandbox::RUNTIME_VERSION` and, on first use, installs that version of `msb` into the home — replacing a different version
|
|
227
305
|
that is already there, including a newer `msb` installed by the official CLI.
|
|
228
306
|
In the other direction, a `0.7.x` `msb` migrates the catalog to a newer schema
|
|
229
307
|
the first time it opens it, after which every local call from this gem fails
|
|
@@ -238,32 +316,37 @@ export MSB_HOME="$HOME/.microsandbox-rb"
|
|
|
238
316
|
|
|
239
317
|
## Known issues at the pinned runtime
|
|
240
318
|
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
- **
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
319
|
+
Open upstream at runtime `v0.7.14`. See [SECURITY.md](../SECURITY.md) for the
|
|
320
|
+
security advisories.
|
|
321
|
+
|
|
322
|
+
- **A fresh runtime home cannot create a sandbox from a directory or disk
|
|
323
|
+
image** ([superradcompany/microsandbox#2041](https://github.com/superradcompany/microsandbox/issues/2041)).
|
|
324
|
+
Runtime v0.7.14 creates `<home>/sandboxes/<name>` without creating
|
|
325
|
+
`<home>/sandboxes` first, except on the OCI-image path, so on a home where
|
|
326
|
+
no install ever ran (the runtime came from `MSB_PATH` or the binaries gem)
|
|
327
|
+
such a create failed with `No such file or directory`. **Worked around in
|
|
328
|
+
the SDK:** before every local `create` / `connect_or_create` /
|
|
329
|
+
`create_with_progress` / `restore` it creates `<home>/sandboxes` (with the
|
|
330
|
+
mode the OCI path gives it), once a runtime resolves — so a create that is
|
|
331
|
+
about to fail with `RuntimeNotInstalledError` still writes nothing.
|
|
332
|
+
- **Linux: a detached sandbox leaves a zombie VM process**
|
|
333
|
+
([superradcompany/microsandbox#1950](https://github.com/superradcompany/microsandbox/issues/1950)).
|
|
334
|
+
After a sandbox started with `detached: true` (or `#detach`ed, or restored
|
|
335
|
+
without a block) stops, its VM child stays `<defunct>` in the SDK process
|
|
336
|
+
until that process exits. macOS reaps it. A long-running process that
|
|
337
|
+
creates many detached sandboxes accumulates them; prefer attached
|
|
338
|
+
sandboxes there.
|
|
256
339
|
|
|
257
340
|
## Environment variables
|
|
258
341
|
|
|
259
342
|
| Variable | Effect |
|
|
260
343
|
|----------|--------|
|
|
261
|
-
| `MSB_PATH` | Path to the `msb` runtime binary; outranks every other source, including the binaries gem |
|
|
262
|
-
| `MSB_LIBKRUNFW_PATH` | Path to the `libkrunfw` shared library; outranks `Microsandbox.libkrunfw_path
|
|
344
|
+
| `MSB_PATH` | Path to the `msb` runtime binary; outranks every other source, including the binaries gem. Needs `libkrunfw` beside it (or `../lib`), or `MSB_LIBKRUNFW_PATH` |
|
|
345
|
+
| `MSB_LIBKRUNFW_PATH` | Path to the `libkrunfw` shared library; outranks `Microsandbox.libkrunfw_path=`. Only together with `MSB_PATH` |
|
|
263
346
|
| `MSB_HOME` | Runtime home: the catalog, image cache, sandboxes, volumes, snapshots, and the first-use runtime install. Default `~/.microsandbox` |
|
|
264
347
|
| `MSB_BACKEND` | `local` or `cloud`; cloud additionally reads `MSB_API_URL` / `MSB_API_KEY` |
|
|
265
348
|
| `MSB_PROFILE` | Select a named profile from the config file |
|
|
266
349
|
| `MSB_CONFIG_PATH` | Override the `~/.microsandbox/config.json` location |
|
|
267
|
-
| `MICROSANDBOX_NO_AUTO_INSTALL` |
|
|
350
|
+
| `MICROSANDBOX_NO_AUTO_INSTALL` | Never install or replace the runtime: no first-use download, no upgrade of a stale home runtime |
|
|
268
351
|
| `MICROSANDBOX_INTEGRATION` | Set to `1` to run the integration specs (development only) |
|
|
269
352
|
| `MICROSANDBOX_TEST_IMAGE` | Override the integration-spec image (development only) |
|
data/docs/surface.md
CHANGED
|
@@ -6,8 +6,10 @@ counterpart, and [DESIGN.md](../DESIGN.md) explains how the layers fit.
|
|
|
6
6
|
|
|
7
7
|
## Implemented
|
|
8
8
|
|
|
9
|
-
- **Sandbox lifecycle** — the live `Sandbox` (`stop`/`
|
|
10
|
-
`drain`/`wait`/`status`/`detach
|
|
9
|
+
- **Sandbox lifecycle** — the live `Sandbox` (`stop`/`stop_with_timeout`/
|
|
10
|
+
`stop_and_wait`/`kill`/`kill_with_timeout`/`drain`/`wait`/`status`/`detach`/
|
|
11
|
+
`owns_lifecycle?`; block forms stop within `BLOCK_STOP_TIMEOUT`, then kill),
|
|
12
|
+
the `SandboxHandle`
|
|
11
13
|
controls from `Sandbox.get` (`stop_with_timeout`/`request_stop`/
|
|
12
14
|
`request_kill`/`request_drain`/`wait_until_stopped`/`config`/`config_json`/
|
|
13
15
|
`snapshot`), the cursor-paginated `list`/`list_with` with label filters, and
|
|
@@ -32,8 +34,25 @@ counterpart, and [DESIGN.md](../DESIGN.md) explains how the layers fit.
|
|
|
32
34
|
(`Sandbox.create_with_progress` → `PullSession`).
|
|
33
35
|
- **Named volumes** — `Volume.create`/`get`/`list`/`remove`/`get_default`, plus
|
|
34
36
|
host-side `Volume.fs`/`VolumeInfo#fs` read/write.
|
|
35
|
-
- **Snapshots** — create
|
|
36
|
-
|
|
37
|
+
- **Snapshots** — create (disk scope, or `full:` for a resumable checkpoint;
|
|
38
|
+
`guest_flush:`)/open/list/list_dir/reindex/verify/save/load, opt-in
|
|
39
|
+
`record_integrity:`, `SnapshotInfo#reference`/`#reference_kind`.
|
|
40
|
+
Selectors follow runtime `v0.7` snapshot groups (a bare name selects a group
|
|
41
|
+
head; `group:member`, id, digest or path select one artifact — see
|
|
42
|
+
[images.md](images.md#snapshots)); the group surface itself (`group:`, head
|
|
43
|
+
selection) is not bound yet.
|
|
44
|
+
- **Restore** — `Sandbox.restore(snapshot, name:)` boots a new (upstream:
|
|
45
|
+
detached) sandbox from a snapshot, with the destination options `cpus`/
|
|
46
|
+
`memory`/`user`/`log_level`/`max_duration`/`idle_timeout`/`volumes`/`ports`/
|
|
47
|
+
`disable_network`/`cow_memory` (`forked` deprecated)/`disk_only`/
|
|
48
|
+
`snapshot_base`/`allow_missing_resources`/`external_mount_policy` (see
|
|
49
|
+
[images.md](images.md#restoring-a-snapshot)). It replaced create's
|
|
50
|
+
`from_snapshot:`.
|
|
51
|
+
- **Storage upgrade** — `Microsandbox.diagnose_storage` (read-only
|
|
52
|
+
inspection → `StorageDiagnosis`) and `Sandbox.recover_storage` (import a
|
|
53
|
+
stopped pre-0.18 sandbox's disks into the runtime `v0.7` protected layout →
|
|
54
|
+
`StorageRecovery`; macOS needs `legacy_writers_stopped: true`). See
|
|
55
|
+
[lifecycle.md](lifecycle.md#upgrading-sandboxes-created-before-0180).
|
|
37
56
|
- **Rootfs patches** — `Microsandbox::Patch` (text/file/append/copy_file/
|
|
38
57
|
copy_dir/symlink/mkdir/remove) via `create(patches:)`.
|
|
39
58
|
- **Root disk** — structured `root_disk:` (managed / tmpfs / disk / flat via
|
|
@@ -42,18 +61,30 @@ counterpart, and [DESIGN.md](../DESIGN.md) explains how the layers fit.
|
|
|
42
61
|
- **Network configuration** — composable profiles, custom per-rule
|
|
43
62
|
`Microsandbox::NetworkPolicy`/`Rule`/`Destination`, custom DNS (`dns:`),
|
|
44
63
|
TLS-interception tuning (`tls:`), guest address pools (`ipv4_pool:`/
|
|
45
|
-
`ipv6_pool:`), `
|
|
64
|
+
`ipv6_pool:`), `max_tcp_connections`/`max_udp_connections` (the deprecated
|
|
65
|
+
`max_connections` aliases the TCP cap), `trust_host_cas`, `strict` hostname mode,
|
|
66
|
+
the HTTP deny response (`http:` — JSON or text `403`, custom messages, or
|
|
67
|
+
off; see [configuration.md](configuration.md#http-deny-response)),
|
|
46
68
|
outbound SOCKS4/SOCKS5 `proxy:` (`OutboundProxy`/`SecretSource`),
|
|
47
69
|
`rate_limiter:`, `vsock:`.
|
|
48
|
-
- **Secrets** —
|
|
49
|
-
|
|
70
|
+
- **Secrets** — per-secret `allow:` lists (exact hosts, `*.example.com`
|
|
71
|
+
wildcards, or `"*"` for any host), `allow_placeholder_for:` hosts that may
|
|
72
|
+
receive the unchanged placeholder, `placeholder:`, `require_tls_identity:`,
|
|
73
|
+
`substitution:` locations (headers / named header fields / query / body),
|
|
74
|
+
per-secret `violation_action:` and the sandbox-wide `secret_violation_action:`
|
|
75
|
+
(`block` / `block_and_log` / `block_and_terminate`). See
|
|
76
|
+
[configuration.md](configuration.md#secrets) for the 0.17 → 0.18 key map;
|
|
77
|
+
the removed keys and `on_secret_violation:` raise with their replacement.
|
|
50
78
|
- **SSH** — `Sandbox#ssh` → `SshClient`/`SftpClient`/`SshServer`, with
|
|
51
79
|
`inactivity_timeout:`.
|
|
52
80
|
- **Raw agent client** — `Microsandbox::AgentClient` → `AgentStream`/
|
|
53
81
|
`AgentFrame`.
|
|
54
|
-
- **Runtime provisioning** — `install`/`installed?`/`ensure_runtime!`/
|
|
55
|
-
`runtime_path`/`libkrunfw_path`,
|
|
56
|
-
|
|
82
|
+
- **Runtime provisioning** — `install(force:)`/`installed?`/`ensure_runtime!`/
|
|
83
|
+
`runtime_path`/`libkrunfw_path`, `runtime_info` → `RuntimeInfo` (the
|
|
84
|
+
resolved `msb` + `libkrunfw` pair, its gem-classified `origin` and embedded
|
|
85
|
+
`version`), customizable via `Microsandbox.setup`; the version-aware
|
|
86
|
+
bootstrap (the `msb` must be exactly `RUNTIME_VERSION`); the companion
|
|
87
|
+
`microsandbox-rb-binaries` gem.
|
|
57
88
|
- **Registries** — `registry_auth`/`registry_insecure`/`registry_ca_certs` on
|
|
58
89
|
`create`.
|
|
59
90
|
- **Errors** — the typed hierarchy in [errors.md](errors.md).
|
|
@@ -70,12 +101,22 @@ Create options span resources (`cpus`/`memory`/`max_cpus`/`max_memory`),
|
|
|
70
101
|
disk-image `fstype`, `ports`/`ports_udp`, network policy + config,
|
|
71
102
|
`log_level`/`quiet_logs`/`security`/`rlimits`/`pull_policy`/`secrets`/`patches`/`volumes`
|
|
72
103
|
(bind/named/tmpfs/disk with mount policies, `uid`/`gid`, `quota_mib`),
|
|
73
|
-
`idle_timeout`/`max_duration`, `
|
|
74
|
-
`replace`/`replace_with_timeout`.
|
|
104
|
+
`secret_violation_action`, `idle_timeout`/`max_duration`, `detached`, and
|
|
105
|
+
`replace`/`replace_with_timeout`. Two keywords remain only to fail loudly
|
|
106
|
+
since 0.18.0: `from_snapshot` and `on_secret_violation`.
|
|
75
107
|
|
|
76
108
|
## Not yet exposed
|
|
77
109
|
|
|
78
110
|
A few secondary upstream knobs are not yet exposed: per-published-port host
|
|
79
|
-
bind address (ports always bind loopback), network interface overrides,
|
|
111
|
+
bind address (ports always bind loopback), network interface overrides,
|
|
80
112
|
inline named-volume create-mode (pre-create the volume with `Volume.create`,
|
|
81
|
-
then mount it with `{ named: "…" }`)
|
|
113
|
+
then mount it with `{ named: "…" }`), HTTP CONNECT outbound proxies (`proxy:`
|
|
114
|
+
takes SOCKS4/SOCKS5), the inbound published-port caps
|
|
115
|
+
(`max_inbound_tcp_connections`, `tcp_accept_queue_size`), `nested_virt`, and
|
|
116
|
+
detached jobs.
|
|
117
|
+
|
|
118
|
+
Restore does not yet take the network policy, security profile, vsock routes,
|
|
119
|
+
connection caps, UDP ports, captured-disk selection (`captured`), resource
|
|
120
|
+
inheritance (`dangerously_inherit_resources`), `guest_clock`, or a progress
|
|
121
|
+
stream; snapshot groups, live branches/forks and `restore_warnings` are not
|
|
122
|
+
bound either (planned for 0.19.0).
|