
# Snapshots

> Save a running machine and resume it later, in this process or another.

```js
import { readFile, writeFile } from "node:fs/promises";
import { createKernel, decodeSnapshot, encodeSnapshot } from "unkernel";

const vm = await createKernel({ distro });
await vm.exec("sh", ["-c", "echo kept > /tmp/state"]);

// park every task, store the snapshot, then stop
await vm.snapshot({ commit: (snap) => writeFile("vm.snap", encodeSnapshot(snap)) });

// later: the same kernel, disks and devices
const restored = await createKernel({ distro, restore: decodeSnapshot(await readFile("vm.snap")) });
await restored.exec("cat", ["/tmp/state"]); // "kept"
```

The kernel parks every task where its whole state is in memory, so a snapshot is the guest's memory plus a few host values. Without a data disk, that memory also holds the root file system's changes. `encodeSnapshot()` leaves out free pages, and by default a snapshot that stops the machine first drops the guest's clean file cache, so the idle base shell in a 2 GiB guest saved to 46 MiB (2026-11-30).

- `snapshot()` alone copies the machine and lets it run on.
- `snapshot({ stop: true })` stops it right after the copy (exit kind `"stopped"`).
- `snapshot({ commit })` stops it only once `commit` has stored the snapshot. If `commit` rejects or times out (`commitTimeoutMs`), the machine runs on and `snapshot()` rejects with a `SnapshotCommitError`, so a failed save never loses the machine.
- `snapshot({ commit, hold })` keeps the stored machine waiting at that point until `hold` returns `"stop"` or `"continue"`. Nothing runs in the guest while it waits, and if it continues, its clocks have moved on by the wait, as for a VM its hypervisor paused.

## What a restore needs

- **The same kernel build.** The snapshot's `fingerprint` must match. `initrd`, `cmdline` and `memoryMiB` are ignored.
- **The same disks, unchanged.** Disks are not copied: a snapshot records each disk's identity and write generation, and a restore refuses a disk written since then. Once the restored guest writes, the snapshot is stale. The distro image is identified by its content. A writable image file gets a `FILE.kwsnap` beside it with its write generation.
- **The same devices.** `display`, `sound`, `soundIn`, `camera`, `vsock`, `shm`, `gpu` and the network must be given or left out as they were (the network with the same MAC). A snapshot is refused while virtio-gpu holds resources.
- **The same number of CPUs.** `cpus` defaults to the snapshot's.

`createKernel()` rejects with the reason when something does not match, and `snapshotDiskMismatch()`, `snapshotDeviceMismatch()` and `snapshotCpuMismatch()` check ahead.

## What survives

Host [mounts](/sdk/mounts) survive: pass the same mount paths (read-only as before) and the guest's open files on them are opened again by path. [Host commands](/sdk/commands) are replaced by the ones you pass. vsock sessions (`spawn()`, `exec()`, `connect()`, `fetch()`, listeners) and the guest's TCP connections do not survive: the guest's ends are reset. Serial ports, Bluetooth and USB devices come back unplugged.

Terminal sessions survive if they are detachable:

```js
const sh = await vm.spawn("sh", [], { detachable: true });
const snap = await vm.snapshot({ stop: true });

const restored = await createKernel({ distro, restore: snap });
const again = await restored.attach(snap.ttys[0]); // within detachTimeout (default 60 s of guest time)
```

In worker mode the snapshot's memory is copied to the worker; `transferRestore: true` hands it over instead, for a snapshot used once. The CLI's `--input '{save=FILE}'` and `--restore FILE` are built on this.
