# Mounting volumes

https://virtx.sh/docs/tutorials/mounting-volumes/

Create a volume, mount it into sandboxes, and mount remote stores such as Notion or Google Drive inside it.

A **volume** is named storage that exists apart from any sandbox.
You mount it at a path in a sandbox, read-write or read-only, and it is kept when the sandbox is removed.

Use a volume instead of the sandbox's own disk when:

- Files must survive after the sandbox is removed. Everything else a sandbox writes is deleted with it.
- Several sandboxes need the same files at the same time.
- Your code needs to read or write the files while no sandbox is running.

## Create a volume

`create_volume` creates an empty volume. If you don't give a name, the server picks one.
You read and write a volume's files from your code with the same calls as a sandbox's; see [Reading and writing files](https://virtx.sh/docs/tutorials/reading-writing-files/).

**Python**

```python
volume = await client.create_volume("data")
await volume.write_file("config.json", '{"retries": 3}')
```

**Node.js**

```js
const volume = await client.createVolume('data')
await volume.writeFile('config.json', '{"retries": 3}')
```

**Rust**

```rust
use virtx::Files; // write_file and read_dir come from this trait

let volume = client.create_volume(Some("data")).await?;
volume.write_file("config.json", r#"{"retries": 3}"#, None).await?;
```

## Mount it into a sandbox

Pass `volumes` to `create_sandbox`. For each volume, give the volume, the path to mount it at, and whether it is read-only.
Files the sandbox writes under that path are stored in the volume.

**Python**

```python
from virtx import SandboxVolume

sandbox = await client.create_sandbox(
    "alpine:latest",
    volumes=[SandboxVolume(volume, "/data")],
)
await sandbox.exec(["cat", "/data/config.json"]).complete(print=True)
```

**Node.js**

```js
const sandbox = await client.createSandbox('alpine:latest', {
  volumes: [{ volume, path: '/data' }],
})
await sandbox.exec(['cat', '/data/config.json']).complete({ print: true })
```

**Rust**

```rust
let args = SandboxCreateArgs::new("alpine:latest").volume(volume.name(), "/data");
let sandbox = client.create_sandbox(&args).await?;
sandbox.exec(&SandboxExecArgs::new(["cat", "/data/config.json"])).complete(true).await?;
```

Set `readonly` to mount it read-only: `SandboxVolume(volume, "/data", readonly=True)` in Python, `{ volume, path: '/data', readonly: true }` in Node.js, or `.readonly_volume(volume.name(), "/data")` in Rust.
A sandbox can mount several volumes. They are mounted in the order you list them, so a later volume can be mounted inside an earlier one. Two volumes at the same path are rejected.

If the image's working directory is under a volume's path and the volume doesn't have it, the sandbox makes that directory in the volume as its VM boots. The volume keeps it, empty, even though no command wrote it. On a read-only mount the directory can't be made, and commands run in `/` instead.

## Mount a remote store inside it

A volume can also show the contents of a remote store, such as Notion or Google Drive, as files.
`mount` attaches the store at a new directory in the volume, and every sandbox that mounts the volume sees the store's files there.
Remote mounts are read-only. To change a file, copy it out of the mount into the volume first.

These are the stores a volume can mount, with the credentials each takes:

| Store | Shows | Source (Python) | Credentials |
| --- | --- | --- | --- |
| Notion | The pages a Notion integration can reach | `NotionSource` | `api_key` |
| Google Drive | A Google Drive, with Docs, Sheets and Slides as files, read-only | `GdriveSource` | `client_id`, `client_secret`, `refresh_token` with the `drive.readonly` scope |
| OneDrive | A OneDrive | `OnedriveSource` | `client_id`, `refresh_token`, and `client_secret` for a confidential app |

In Node.js, a source is an object with a `type` (`'notion'`, `'gdrive'` or `'onedrive'`) and the credentials in camelCase.
In Rust, it is a `MountSource` variant.
VirtX doesn't obtain refresh tokens. Your app must get one through the service's consent screen and pass it in.

**Python**

```python
import os

from virtx import NotionSource

await volume.mount("docs/notion", NotionSource(os.environ["NOTION_API_KEY"]))
for entry in await volume.read_dir("docs/notion"):
    print(entry.name)
```

**Node.js**

```js
await volume.mount('docs/notion', { type: 'notion', apiKey: process.env.NOTION_API_KEY })
for (const entry of await volume.readDir('docs/notion')) console.log(entry.name)
```

**Rust**

```rust
let source = MountSource::Notion { api_key: std::env::var("NOTION_API_KEY")? };
volume.mount("docs/notion", source).await?;
for entry in volume.read_dir("docs/notion").await? {
    println!("{}", entry.name);
}
```

`mounts` lists a volume's mounts, without their credentials. `unmount` removes a mount and leaves the remote store unchanged.

## Remove a volume

`remove` deletes the volume and everything in it. It fails with `409` while a sandbox has the volume mounted, so remove those sandboxes first.

## Troubleshooting

1. **`409`, `exists`, `a volume named … is already here`**

   **Cause:** Another volume has that name.

   **Fix:** Use `get_volume` to open it, or pick another name.

2. **`404`, `not_found`, `no volume here is named …`**

   **Cause:** No volume has that name.

   **Fix:** Check the name against `list_volume`.

3. **`400`, `invalid`, `… another volume is there`**

   **Cause:** Two volumes are mounted at one path.

   **Fix:** Give each its own path.

4. **`409`, `exists`, `a mount is at … already`**

   **Cause:** A store is already mounted at that path.

   **Fix:** `unmount` it first, or use another path.

5. **`400`, `invalid`, `… would be inside or over the mount at …`**

   **Cause:** A mount inside or over another one, or at the volume's root.

   **Fix:** Mount each store in its own subdirectory.

6. **`400`, `invalid`, `… is or holds the mount at …; unmount it first`**

   **Cause:** A remove or rename of a path that holds a mount.

   **Fix:** `unmount` it first.

7. **`409`, `in_use`, `volume … is mounted by a machine or being removed`**

   **Cause:** A sandbox still mounts the volume.

   **Fix:** Remove that sandbox first.

8. **`400`, `invalid`, `making the mount's store: …`**

   **Cause:** The store refused the credentials, or they are malformed.

   **Fix:** Check the API key or refresh token, and for Google Drive the `drive.readonly` scope.

## Next

- [Reading and writing files](https://virtx.sh/docs/tutorials/reading-writing-files/): the file calls a volume shares with a sandbox.
- [Managing projects](https://virtx.sh/docs/tutorials/projects/): share volumes with your team.
