Back to Microsandbox

Snapshots

docs/sdk/python/snapshots.mdx

0.6.925.3 KB
Original Source

Create and manage disk-only snapshots of stopped sandboxes. See Snapshots for usage and lifecycle concepts.

SandboxHandle

<span className="msb-recv">handle.</span><span className="msb-hn">snapshot()</span>

python
async def snapshot(self, name: str) -> Snapshot

Snapshot this sandbox under a bare name in the default snapshots directory (~/.microsandbox/snapshots/<name>/). The sandbox must be stopped or crashed. To place the artifact elsewhere, use Snapshot.save() / Snapshot.load() or move the self-contained artifact directory. Called on a SandboxHandle, obtained from Sandbox.get().

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>name</code><span className="msb-type">str</span></div> <div className="msb-param-desc">Snapshot name; resolved under the default snapshots directory.</div> </div> </div> <p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><a className="msb-type" href="#snapshot">Snapshot</a></div> <div className="msb-param-desc">The captured snapshot.</div> </div> </div> <Accordion title="Example">
python
handle = await Sandbox.get("baseline")
snap = await handle.snapshot("after-pip-install")
print(snap.digest)
</Accordion>

Sandbox

<span className="msb-recv">Sandbox.</span><span className="msb-hn">create()</span>

python
@staticmethod
async def create(name: str, *, from_snapshot: str | os.PathLike | None = None, **kwargs) -> Sandbox

Boot a fresh sandbox from a snapshot artifact by passing from_snapshot= as a peer of image=. The two are mutually exclusive: pass exactly one. See Sandbox.create() for the full set of configuration kwargs.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>name</code><span className="msb-type">str</span></div> <div className="msb-param-desc">Sandbox name, up to 128 UTF-8 bytes.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>from_snapshot</code><span className="msb-type">str | os.PathLike | None</span></div> <div className="msb-param-desc">Snapshot bare name or artifact path to boot from instead of <code>image=</code>.</div> </div> </div> <p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><a className="msb-type" href="/sdk/python/sandbox#instance-methods">Sandbox</a></div> <div className="msb-param-desc">Running sandbox.</div> </div> </div> <Accordion title="Example">
python
# Boot from a snapshot
sb = await Sandbox.create("worker", from_snapshot="after-pip-install")

# Or from an image (existing flow, unchanged)
sb = await Sandbox.create("worker", image="python:3.12")
</Accordion>

Snapshot

<div className="msb-tags"><span className="msb-tag is-type">class</span></div> <p className="msb-backref">Returned by <a href="#handle-snapshot">snapshot()</a> · <a href="#snapshot-create">Snapshot.create()</a> · <a href="#snapshot-open">Snapshot.open()</a> · <a href="#snapshot-list_dir">Snapshot.list_dir()</a> · <a href="#handle-open">handle.open()</a></p>

A fully-parsed snapshot artifact. Properties are read-only attributes (not async).

<span className="msb-recv">snapshot.</span><span className="msb-hn">path</span>

str

Path to the artifact directory

<span className="msb-recv">snapshot.</span><span className="msb-hn">digest</span>

str

Canonical content digest (sha256:hex). The snapshot's identity

<span className="msb-recv">snapshot.</span><span className="msb-hn">size_bytes</span>

int \| None

Apparent size of the captured upper layer in bytes (sparse on disk), when recorded

<span className="msb-recv">snapshot.</span><span className="msb-hn">image_ref</span>

str

Image reference the snapshot was taken from

<span className="msb-recv">snapshot.</span><span className="msb-hn">image_manifest_digest</span>

str

OCI manifest digest of the pinned image

<span className="msb-recv">snapshot.</span><span className="msb-hn">state_kind</span>

SnapshotStateKind

File-backed or checkpoint-backed state

<span className="msb-recv">snapshot.</span><span className="msb-hn">format</span>

SnapshotFormat \| None

On-disk format for file-backed state

<span className="msb-recv">snapshot.</span><span className="msb-hn">scope</span>

SnapshotScope

Captured state scope

<span className="msb-recv">snapshot.</span><span className="msb-hn">fstype</span>

str \| None

Filesystem type for file-backed state (e.g. "ext4")

<span className="msb-recv">snapshot.</span><span className="msb-hn">checkpoint_id</span>

str \| None

Checkpoint identifier for checkpoint-backed state

<span className="msb-recv">snapshot.</span><span className="msb-hn">checkpoint_manifest_digest</span>

str \| None

Checkpoint manifest digest for checkpoint-backed state

<span className="msb-recv">snapshot.</span><span className="msb-hn">parent</span>

str \| None

Parent snapshot's digest, or None for a root

<span className="msb-recv">snapshot.</span><span className="msb-hn">created_at</span>

str

RFC 3339 timestamp

<span className="msb-recv">snapshot.</span><span className="msb-hn">labels</span>

dict[str, str]

User-supplied labels

<span className="msb-recv">snapshot.</span><span className="msb-hn">source_sandbox</span>

str \| None

Best-effort source-sandbox name

<span className="msb-recv">Snapshot.</span><span className="msb-hn">create()</span>

python
@staticmethod
async def create(
    name: str,
    *,
    from_sandbox: str,
    dest_dir: str | os.PathLike[str] | None = None,
    labels: dict[str, str] | None = None,
    force: bool = False,
    record_integrity: bool = False,
    resumable: bool = False,
) -> Snapshot

Create a snapshot from a stopped or crashed sandbox. name is resolved under the default snapshots directory (~/.microsandbox/snapshots/<name>/), or under dest_dir= when given; from_sandbox= names the sandbox to capture and is required.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>name</code><span className="msb-type">str</span></div> <div className="msb-param-desc">Bare snapshot name; resolved under the default snapshots directory.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>from_sandbox</code><span className="msb-type">str</span></div> <div className="msb-param-desc">Name of the stopped or crashed sandbox to capture. Required.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>dest_dir</code><span className="msb-type">str | os.PathLike[str] | None</span></div> <div className="msb-param-desc">Parent directory to create the artifact in; the artifact lands at <code>dest_dir/&lt;name&gt;</code>. Defaults to the snapshots directory.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>labels</code><span className="msb-type">dict[str, str] | None</span></div> <div className="msb-param-desc">User-supplied labels stored in the manifest.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>force</code><span className="msb-type">bool</span></div> <div className="msb-param-desc">Overwrite an existing artifact with the same name. Default <code>False</code>.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>record_integrity</code><span className="msb-type">bool</span></div> <div className="msb-param-desc">Record an integrity hash in the manifest so the artifact can be verified later. Default <code>False</code>.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>resumable</code><span className="msb-type">bool</span></div> <div className="msb-param-desc">Request a <code>"resumable"</code> snapshot (disk plus VM state). Accepted, but currently fails with an <code>Unsupported</code> error; resumable snapshots have not landed yet. Default <code>False</code>.</div> </div> </div> <p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><a className="msb-type" href="#snapshot">Snapshot</a></div> <div className="msb-param-desc">The captured snapshot.</div> </div> </div> <Accordion title="Example">
python
snap = await Snapshot.create(
    "after-pip-install",
    from_sandbox="baseline",
    labels={"stage": "post-deps"},
    record_integrity=True,
)
</Accordion>

<span className="msb-recv">Snapshot.</span><span className="msb-hn">open()</span>

python
@staticmethod
async def open(path_or_name: str) -> Snapshot
<Accordion title="Example">
python
snap = await Snapshot.open("after-pip-install")
print(snap.image_ref)
</Accordion>

Open an existing artifact by bare name (resolved under the default snapshots directory) or path. Cheap metadata validation only; does not read the upper file. Use verify() for content checks.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>path_or_name</code><span className="msb-type">str</span></div> <div className="msb-param-desc">Bare snapshot name or artifact directory path.</div> </div> </div> <p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><a className="msb-type" href="#snapshot">Snapshot</a></div> <div className="msb-param-desc">The opened snapshot.</div> </div> </div>

<span className="msb-recv">Snapshot.</span><span className="msb-hn">get()</span>

python
@staticmethod
async def get(name_or_digest: str) -> SnapshotHandle
<Accordion title="Example">
python
h = await Snapshot.get("after-pip-install")
print(h.digest)
</Accordion>

Look up a handle in the local index by name, digest, or path.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>name_or_digest</code><span className="msb-type">str</span></div> <div className="msb-param-desc">Snapshot name, digest, or path.</div> </div> </div> <p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><a className="msb-type" href="#snapshothandle">SnapshotHandle</a></div> <div className="msb-param-desc">Lightweight handle backed by an index row.</div> </div> </div>

<span className="msb-recv">Snapshot.</span><span className="msb-hn">list()</span>

python
@staticmethod
async def list() -> list[SnapshotHandle]
<Accordion title="Example">
python
for h in await Snapshot.list():
    print(h.name, h.digest)
</Accordion>

List indexed snapshots from the local DB cache.

<p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><a className="msb-type" href="#snapshothandle">list[SnapshotHandle]</a></div> <div className="msb-param-desc">Indexed snapshot handles.</div> </div> </div>

<span className="msb-recv">Snapshot.</span><span className="msb-hn">list_dir()</span>

python
@staticmethod
async def list_dir(dir: str | os.PathLike) -> list[Snapshot]

Walk a directory and parse each subdirectory's manifest. Does not touch the index, useful for inspecting external snapshot collections (e.g. a mounted volume of artifacts that were never loaded). Skips entries that don't look like snapshot artifacts.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>dir</code><span className="msb-type">str | os.PathLike</span></div> <div className="msb-param-desc">Directory to scan for artifacts.</div> </div> </div> <p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><a className="msb-type" href="#snapshot">list[Snapshot]</a></div> <div className="msb-param-desc">One snapshot per valid artifact directory.</div> </div> </div>

<span className="msb-recv">Snapshot.</span><span className="msb-hn">remove()</span>

python
@staticmethod
async def remove(path_or_name: str, *, force: bool = False) -> None
<Accordion title="Example">
python
await Snapshot.remove("after-pip-install", force=True)
</Accordion>

Remove a snapshot artifact and its index row. Refuses if the snapshot has indexed children unless force=True.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>path_or_name</code><span className="msb-type">str</span></div> <div className="msb-param-desc">Bare snapshot name or artifact path.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>force</code><span className="msb-type">bool</span></div> <div className="msb-param-desc">Remove even if the snapshot has indexed children. Default <code>False</code>.</div> </div> </div>

<span className="msb-recv">Snapshot.</span><span className="msb-hn">reindex()</span>

python
@staticmethod
async def reindex(dir: str | os.PathLike | None = None) -> int
<Accordion title="Example">
python
count = await Snapshot.reindex()
print(f"indexed {count} snapshots")
</Accordion>

Walk dir (default: configured snapshots dir) and rebuild the local index. Returns the number of artifacts indexed.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>dir</code><span className="msb-type">str | os.PathLike | None</span></div> <div className="msb-param-desc">Directory to scan. Default: the configured snapshots directory.</div> </div> </div> <p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><span className="msb-type">int</span></div> <div className="msb-param-desc">Number of artifacts indexed.</div> </div> </div> <Accordion title="Example">
python
count = await Snapshot.reindex()
print(f"indexed {count} snapshots")
</Accordion>

<span className="msb-recv">Snapshot.</span><span className="msb-hn">save()</span>

<div className="msb-tags"><span className="msb-tag is-static">static</span><span className="msb-tag is-async">async</span></div>
python
@staticmethod
async def save(
    name_or_path: str,
    out: str | os.PathLike,
    *,
    with_parents: bool = False,
    with_image: bool = False,
    plain_tar: bool = False,
) -> None
<Accordion title="Example">
python
await Snapshot.save(
    "after-pip-install",
    "/tmp/after-pip-install.tar.zst",
    with_parents=True,
)
</Accordion>

Bundle a snapshot into a .tar.zst archive. The existing snapshot manifest is archived as-is; create the snapshot with recorded integrity when the archive will cross a trust boundary.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>name_or_path</code><span className="msb-type">str</span></div> <div className="msb-param-desc">Snapshot bare name or artifact path to save.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>out</code><span className="msb-type">str | os.PathLike</span></div> <div className="msb-param-desc">Output archive path.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>with_parents</code><span className="msb-type">bool</span></div> <div className="msb-param-desc">Include the snapshot's parent chain. Default <code>False</code>.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>with_image</code><span className="msb-type">bool</span></div> <div className="msb-param-desc">Include the pinned base image. Default <code>False</code>.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>plain_tar</code><span className="msb-type">bool</span></div> <div className="msb-param-desc">Write an uncompressed <code>.tar</code> instead of <code>.tar.zst</code>. Default <code>False</code>.</div> </div> </div> <Accordion title="Example">
python
await Snapshot.save(
    "after-pip-install",
    "/tmp/after-pip-install.tar.zst",
    with_parents=True,
)
</Accordion>
<p className="msb-member-group">Move artifacts</p>

<span className="msb-recv">Snapshot.</span><span className="msb-hn">load()</span>

<div className="msb-tags"><span className="msb-tag is-static">static</span><span className="msb-tag is-async">async</span></div>
python
@staticmethod
async def load(
    archive: str | os.PathLike,
    *,
    dest: str | os.PathLike | None = None,
) -> SnapshotHandle

Unpack a snapshot archive (.tar.zst or .tar) into the snapshots directory. Structural and archive-entry checks run during import; recorded payload integrity is preserved for explicit verify(). Compression is detected from magic bytes.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>archive</code><span className="msb-type">str | os.PathLike</span></div> <div className="msb-param-desc">Archive path (<code>.tar.zst</code> or <code>.tar</code>).</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>dest</code><span className="msb-type">str | os.PathLike | None</span></div> <div className="msb-param-desc">Destination directory. Default: the configured snapshots directory.</div> </div> </div> <p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><a className="msb-type" href="#snapshothandle">SnapshotHandle</a></div> <div className="msb-param-desc">Handle to the loaded snapshot.</div> </div> </div> <Accordion title="Example">
python
h = await Snapshot.load("/tmp/after-pip-install.tar.zst")
print(h.path)
</Accordion>
<p className="msb-member-group">Inspect</p>

<span className="msb-recv">snap.</span><span className="msb-hn">verify()</span>

python
async def verify(self) -> dict[str, Any]
<Accordion title="Example">
python
report = await snap.verify()
if report["upper"]["kind"] == "verified":
    print(f"hash matches: {report['upper']['digest']}")
else:
    print("no integrity hash recorded")
</Accordion>

Recompute the upper layer's recorded content integrity and compare against the descriptor. Current BLAKE3 Merkle integrity skips known all-hole subtrees and hashes allocated leaves in batches; released SHA descriptors retain their exact, potentially O(logical size), verifier. Returns not_recorded without reading payload contents when no integrity was stored.

<p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><span className="msb-type">dict[str, Any]</span></div> <div className="msb-param-desc">Verification report. The <code>upper.kind</code> field is <code>"not_recorded"</code> when no integrity hash was stored, or <code>"verified"</code> with the recomputed digest.</div> </div> </div>

The report shape:

python
{
    "digest": "sha256:...",
    "path": "/path/to/artifact",
    "upper": {"kind": "not_recorded"}                            # no integrity recorded
        | {"kind": "verified", "algorithm": "...", "digest": "sha256:..."},
}

SnapshotHandle

<div className="msb-tags"><span className="msb-tag is-type">class</span></div> <p className="msb-backref">Returned by <a href="#snapshot-get">Snapshot.get()</a> · <a href="#snapshot-list">Snapshot.list()</a> · <a href="#snapshot-load">Snapshot.load()</a></p>

Lightweight handle backed by an index row. Properties are read-only attributes (not async).

<span className="msb-recv">handle.</span><span className="msb-hn">digest</span>

str

Manifest digest, canonical identity

<span className="msb-recv">handle.</span><span className="msb-hn">name</span>

str \| None

Convenience alias

<span className="msb-recv">handle.</span><span className="msb-hn">parent_digest</span>

str \| None

Parent snapshot digest, or None for a root

<span className="msb-recv">handle.</span><span className="msb-hn">image_ref</span>

str

Image the snapshot was taken from

<span className="msb-recv">handle.</span><span className="msb-hn">state_kind</span>

SnapshotStateKind

File-backed or checkpoint-backed state

<span className="msb-recv">handle.</span><span className="msb-hn">format</span>

SnapshotFormat \| None

On-disk format for file-backed state

<span className="msb-recv">handle.</span><span className="msb-hn">scope</span>

SnapshotScope

Captured state scope

<span className="msb-recv">handle.</span><span className="msb-hn">fstype</span>

str \| None

Filesystem type for file-backed state

<span className="msb-recv">handle.</span><span className="msb-hn">checkpoint_manifest_digest</span>

str \| None

Checkpoint manifest digest for checkpoint-backed state

<span className="msb-recv">handle.</span><span className="msb-hn">size_bytes</span>

int \| None

Apparent upper size at index time

<span className="msb-recv">handle.</span><span className="msb-hn">locality</span>

str

Artifact locality reported by the index

<span className="msb-recv">handle.</span><span className="msb-hn">availability</span>

str

Artifact availability reported by the index

<span className="msb-recv">handle.</span><span className="msb-hn">migration_state</span>

str

Current migration state

<span className="msb-recv">handle.</span><span className="msb-hn">migration_error_code</span>

str \| None

Migration error code, when migration failed

<span className="msb-recv">handle.</span><span className="msb-hn">created_at</span>

float

ms since Unix epoch

<span className="msb-recv">handle.</span><span className="msb-hn">path</span>

str

Local artifact directory path

<span className="msb-recv">handle.</span><span className="msb-hn">open()</span>

python
async def open(self) -> Snapshot
<Accordion title="Example">
python
h = await Snapshot.get("after-pip-install")
snap = await h.open()
print(snap.fstype)
</Accordion>

Load the full Snapshot metadata for this handle. Metadata-validated only; does not read the upper file.

<p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><a className="msb-type" href="#snapshot">Snapshot</a></div> <div className="msb-param-desc">The opened snapshot.</div> </div> </div>

<span className="msb-recv">handle.</span><span className="msb-hn">remove()</span>

python
async def remove(self, *, force: bool = False) -> None
<Accordion title="Example">
python
h = await Snapshot.get("after-pip-install")
await h.remove(force=False)
</Accordion>

Remove this snapshot artifact and its index row. Refuses if the snapshot has indexed children unless force=True.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>force</code><span className="msb-type">bool</span></div> <div className="msb-param-desc">Remove even if the snapshot has indexed children. Default <code>False</code>.</div> </div> </div>

Types

SnapshotStateKind

<p className="msb-backref">Returned by <a href="#snapshot">Snapshot.state_kind</a> · <a href="#snapshothandle">SnapshotHandle.state_kind</a></p>

Snapshot state representation.

MemberValueDescription
SnapshotStateKind.FILE"file"File-backed upper-layer state
SnapshotStateKind.CHECKPOINT"checkpoint"Checkpoint-manifest-backed state

SnapshotFormat

<p className="msb-backref">Returned by <a href="#snapshot">Snapshot.format</a> · <a href="#snapshothandle">SnapshotHandle.format</a></p>

On-disk format for file-backed snapshot state.

MemberValueDescription
SnapshotFormat.RAW"raw"Raw disk image
SnapshotFormat.QCOW2"qcow2"QEMU copy-on-write v2 image

SnapshotScope

<p className="msb-backref">Returned by <a href="#snapshot">Snapshot.scope</a> · <a href="#snapshothandle">SnapshotHandle.scope</a></p>

Captured snapshot state scope.

MemberValueDescription
SnapshotScope.DISK"disk"Disk-only state
SnapshotScope.RESUMABLE"resumable"Disk, memory, and device state
python
from microsandbox import SnapshotFormat, SnapshotScope, SnapshotStateKind

assert snapshot.state_kind is SnapshotStateKind.FILE
assert snapshot.format is SnapshotFormat.RAW
assert snapshot.scope is SnapshotScope.DISK