
# Disks

> Attach ext4 images, http(s) images and persistent disks.

```js
import { createImageFromKwsparse, createKernel, bundledBoot, openDisk } from "unkernel/node";

const vm = await createKernel({
  distro,
  disks: [await openDisk("data.img")], // the data disk, or any ext4 image or https:// URL
});
```

## The data disk

A disk labelled `kwdata` is the guest's persistent data disk: the root file system is an overlay of the read-only distro image under your changes, and with a data disk attached each distro keeps its changes in `/data/distros/<name>/` on it.

`createImageFromKwsparse(file, template)` makes a new data disk from the template the kernel carries (`bundledBoot(module).datadisk`).

## In the browser

- `openHttpDisk(url)` reads an image over http(s) with range requests and keeps your writes in IndexedDB.
- `openDataDisk()` gives you the persistent data disk that [unkernel.dev](https://unkernel.dev/) uses, in IndexedDB by default. `openDataDisk({ backend: "opfs" })` keeps it in the origin private file system instead, several times faster; it runs only in a dedicated worker, so pass it to a kernel in a worker as a spec: `disks: [{ kind: "data", template: "bundled", backend: "opfs" }]` with `createKernel({ worker: true })`. `migrateDataDisk()` copies an IndexedDB data disk to OPFS, one way: it fences the IndexedDB copy, which `openDataDisk()` then refuses, and `clearMovedDataDisk()` removes its blocks afterwards.

## Your own disks

Anything that implements `BlockDevice` works too, and a disk of your own can be the root:

```js
const vm = await createKernel({
  disks: [myDisk],
  cmdline: "console=hvc0 root=/dev/wda rw",
  initrd: null,
});
```
