> ## Documentation Index
> Fetch the complete documentation index at: https://docs.archil.com/llms.txt
> Use this file to discover all available pages before exploring further.

# E2B

> Mount an Archil disk in E2B sandboxes so their files outlive the sandbox.

[E2B](https://e2b.dev) sandboxes are short-lived: when one is killed or times out, its files go with it. Mount an Archil disk in the sandbox and anything written there stays on the disk, where the next sandbox can mount it and your app can read it with the Archil SDK, without starting a sandbox. E2B sandboxes support FUSE, so the disk mounts as a regular directory.

## Prerequisites

Export your Archil API key and region, and your [E2B API key](https://e2b.dev/dashboard?tab=keys), then install both SDKs:

<CodeGroup>
  ```bash TypeScript theme={null}
  export ARCHIL_API_KEY=key-...
  export ARCHIL_REGION=aws-us-east-1
  export E2B_API_KEY=e2b_...
  npm init -y && npm pkg set type=module
  npm install disk e2b tsx
  ```

  ```bash Python theme={null}
  export ARCHIL_API_KEY=key-...
  export ARCHIL_REGION=aws-us-east-1
  export E2B_API_KEY=e2b_...
  pip install archil e2b
  ```
</CodeGroup>

## Mount a disk

First, build an E2B template with the `archil` client installed and create a disk. You only do this once:

<CodeGroup>
  ```typescript setup.ts theme={null}
  import * as archil from "disk";
  import { Template } from "e2b";

  const template = Template()
    .fromBaseImage()
    .runCmd("apt-get update && curl -fsSL https://archil.com/install | sh", { user: "root" });
  await Template.build(template, "archil");

  const { disk, token } = await archil.createDisk({ name: "e2b-data" });
  console.log(`export ARCHIL_DISK_ID=${disk.id} ARCHIL_MOUNT_TOKEN=${token}`);
  ```

  ```python setup.py theme={null}
  import archil
  from e2b import Template

  template = (
      Template()
      .from_base_image()
      .run_cmd("apt-get update && curl -fsSL https://archil.com/install | sh", user="root")
  )
  Template.build(template, "archil")

  result = archil.create_disk(name="e2b-data")
  print(f"export ARCHIL_DISK_ID={result.disk.id} ARCHIL_MOUNT_TOKEN={result.token}")
  ```
</CodeGroup>

Run it with `eval "$(npx tsx setup.ts)"` (or `eval "$(python setup.py)"`) to export the disk ID and the disk's [mount token](/concepts/authorization#token-based-disk-authorization), which grants access to this disk only.

Now start a sandbox from the template, mount the disk at `/mnt/archil`, and write a file. `archil` needs root, so these commands run with `user: "root"`, and the token is passed only to the mount command:

<CodeGroup>
  ```typescript mount.ts theme={null}
  import { Sandbox } from "e2b";

  const { ARCHIL_DISK_ID, ARCHIL_REGION, ARCHIL_MOUNT_TOKEN } = process.env;
  const sandbox = await Sandbox.create("archil");
  try {
    await sandbox.commands.run(
      `mkdir -p /mnt/archil && archil mount ${ARCHIL_DISK_ID} /mnt/archil --region ${ARCHIL_REGION}`,
      { user: "root", envs: { ARCHIL_MOUNT_TOKEN: ARCHIL_MOUNT_TOKEN! } },
    );
    try {
      const { stdout } = await sandbox.commands.run(
        `echo "Hello from ${sandbox.sandboxId}" >> /mnt/archil/hello.txt && cat /mnt/archil/hello.txt`,
        { user: "root" },
      );
      console.log(stdout);
    } finally {
      // Flush buffered writes and release the disk before the sandbox goes away
      await sandbox.commands.run("archil unmount /mnt/archil", { user: "root" });
    }
  } finally {
    await sandbox.kill();
  }
  ```

  ```python mount.py theme={null}
  import os

  from e2b import Sandbox

  sandbox = Sandbox.create("archil")
  try:
      sandbox.commands.run(
          f"mkdir -p /mnt/archil && archil mount {os.environ['ARCHIL_DISK_ID']} /mnt/archil --region {os.environ['ARCHIL_REGION']}",
          user="root",
          envs={"ARCHIL_MOUNT_TOKEN": os.environ["ARCHIL_MOUNT_TOKEN"]},
      )
      try:
          result = sandbox.commands.run(
              f'echo "Hello from {sandbox.sandbox_id}" >> /mnt/archil/hello.txt && cat /mnt/archil/hello.txt',
              user="root",
          )
          print(result.stdout)
      finally:
          # Flush buffered writes and release the disk before the sandbox goes away
          sandbox.commands.run("archil unmount /mnt/archil", user="root")
  finally:
      sandbox.kill()
  ```
</CodeGroup>

The `archil` client buffers recent writes, so the script unmounts before it kills the sandbox: `archil unmount` returns once every write has reached the disk.

Run `npx tsx mount.ts` (or `python mount.py`) twice. Each run starts a new sandbox, and the second one finds the line the first one wrote:

```text theme={null}
Hello from ihd5w6hpg6f9zvh9cbf8w

Hello from ihd5w6hpg6f9zvh9cbf8w
Hello from iq88j4auz0rll06x5jgfh
```

A plain mount is exclusive: one sandbox holds the disk at a time. To mount it in several sandboxes at once, add `--shared`, and see [Sharing disks](/concepts/sharing-disks).

## Read it from anywhere

Both sandboxes are gone, and the file is still on the disk. Read it from your own machine or app with the SDK:

<CodeGroup>
  ```typescript read.ts theme={null}
  import * as archil from "disk";

  const disk = await archil.getDisk(process.env.ARCHIL_DISK_ID!);
  console.log(new TextDecoder().decode(await disk.getObject("hello.txt")));
  ```

  ```python read.py theme={null}
  import os

  import archil

  disk = archil.get_disk(os.environ["ARCHIL_DISK_ID"])
  print(disk.get_object("hello.txt").decode())
  ```
</CodeGroup>

If a sandbox is killed before it unmounts, see [Troubleshooting mounts on other platforms](/guides/platforms/troubleshooting).

## Clean up

Delete the disk with the [`disk` CLI](/reference/disk-cli), and the E2B template:

```bash theme={null}
npx disk delete "$ARCHIL_DISK_ID"
npx @e2b/cli template delete archil -y
```

## Next steps

* [`archil` CLI](/reference/archil-cli): every mount option, including `--shared` and `--read-only`.
* [Persistent sandboxes](/compute/sandboxes/introduction): Archil's own sandboxes, with the disk mounted for you.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.