|
| 1 | +--- |
| 2 | +title: create |
| 3 | +description: Create a writable persistent overlay device on top of another block device. |
| 4 | +--- |
| 5 | + |
| 6 | +```ts |
| 7 | +namespace CheerpX { |
| 8 | + class OverlayDevice { |
| 9 | + static async create( |
| 10 | + baseDevice: Device, |
| 11 | + overlayDevice: Device |
| 12 | + ): Promise<OverlayDevice>; |
| 13 | + } |
| 14 | +} |
| 15 | +``` |
| 16 | + |
| 17 | +## Parameters |
| 18 | + |
| 19 | +- **baseDevice (`Device`)** - The underlying device (e.g., [`HttpBytesDevice`](/docs/reference/httpBytesDevice), [`IDBDevice`](/docs/reference/CheerpX.IDBDevice)) that serves as the base layer for the filesystem. |
| 20 | + |
| 21 | +- **overlayDevice (`Device`)** - The writable layer that will overlay the base device, enabling persistent changes. |
| 22 | + |
| 23 | +## Returns |
| 24 | + |
| 25 | +`CheerpX.OverlayDevice.create` returns a [Promise] that resolves to an instance of the `OverlayDevice`. The `OverlayDevice` allows you to overlay a writable layer on top of the base device, enabling persistent changes while still accessing the base data. |
| 26 | + |
| 27 | +## Example |
| 28 | + |
| 29 | +Create an `OverlayDevice` instance to combine a [`HttpBytesDevice`](/docs/reference/httpBytesDevice) for streaming data from an HTTP source and an [`IDBDevice`](/docs/reference/CheerpX.IDBDevice) for caching and persistent local storage. |
| 30 | + |
| 31 | +```ts {8, 12} |
| 32 | +// Create a read-only HttpBytesDevice for streaming disk blocks via HTTP |
| 33 | +const httpDevice = await CheerpX.HttpBytesDevice.create("/cheerpXImage.ext2"); |
| 34 | + |
| 35 | +// Create an IDBDevice for local persistent storage |
| 36 | +const idbDevice = await CheerpX.IDBDevice.create("block_idbDevice"); |
| 37 | + |
| 38 | +// Create an OverlayDevice to combine the two devices |
| 39 | +const overlayDevice = await CheerpX.OverlayDevice.create(httpDevice, idbDevice); |
| 40 | + |
| 41 | +// Mount the overlay device in the CheerpX environment as an ext2 filesystem |
| 42 | +const cx = await CheerpX.Linux.create({ |
| 43 | + mounts: [{ type: "ext2", path: "/", dev: overlayDevice }], |
| 44 | +}); |
| 45 | +``` |
| 46 | + |
| 47 | +In this example, the `OverlayDevice` provides a writable layer on top of the [`HttpBytesDevice`](/docs/reference/httpBytesDevice) (which serves as a read-only block device for streaming), allowing changes to be stored locally via the [`IDBDevice`](/docs/reference/CheerpX.IDBDevice). |
| 48 | + |
| 49 | +For more information, please check out the [Files and File system guide](/docs/guides/File-System-support). This guide provides more details on how to work with files and directories in CheerpX. |
| 50 | + |
| 51 | +[Promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise |
0 commit comments