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

# ボリューム

> サンドボックスが存続する間保持される、書き込み可能なスクラッチ領域をサンドボックスに提供します。

<Note>
  The examples on this page omit `auth`. To authenticate with your W\&B API key, install `cwsandbox[wandb]` and pass `auth=AuthStrategy.WANDB` to `Sandbox.run()`, `Sandbox()`, or `Sandbox.session()`. See [Get started](/products/sandboxes/serverless/get-started).
</Note>

ボリュームとは、サンドボックスが指定したパスにマウントするストレージです。**スクラッチボリューム**は 1 つのサンドボックスに属し、そのサンドボックスが削除されると一緒に削除されます。

このページは、コンテナーのファイルシステムの容量を超えるデータをサンドボックスに書き込む必要がある開発者を対象としています。このページを読み終えると、スクラッチボリュームをアタッチできるようになります。

## Scratch volumes

A scratch volume is created with the sandbox and destroyed with it. Declare one with `ScratchVolumeOptions` in the `volumes` list.

```python theme={"system"}
from cwsandbox import Sandbox, ScratchVolumeOptions

with Sandbox.run(
    container_image="ubuntu:22.04",
    volumes=[ScratchVolumeOptions(name="workspace", mount_path="/workspace", size="10Gi")],
) as sandbox:
    sandbox.exec(["sh", "-c", "echo hello > /workspace/data.txt"]).result()
```

`ScratchVolumeOptions` accepts the following fields:

| フィールド | 説明 |
| - | - |
| `name` | サンドボックス内でのボリューム名。マウントとスナップショットはこの名前で参照します。必須。 |
| `mount_path` | プライマリコンテナー上のマウント先の絶対パス。省略すると、ボリュームはマウントされずに宣言のみされます。 |
| `size` | ボリュームのサイズ (例：`"10Gi"`) 。省略するとプラットフォームのデフォルトが使用されます。 |
| `medium` | バッキングストア：`StorageMedium.DISK` (デフォルト) または `StorageMedium.MEMORY`。 |
| `sub_path` | ボリュームのルートではなく、指定した相対パスをマウントします。 |
| `read_only` | ボリュームを読み取り専用でマウントします。デフォルトは `False` です。 |
| `restore_from_snapshot_id` | 作成時に[ファイルシステムスナップショット](/products/sandboxes/serverless/file-system-snapshots)をボリュームにリストアします。 |

### Choose a storage medium

`StorageMedium.DISK` backs the volume with local disk. `StorageMedium.MEMORY` backs it with tmpfs, which is faster but counts against the mounting container's memory request and against the policy's memory ceiling.

Because an unbounded RAM-backed volume could exhaust the node, a `MEMORY` volume must declare a `size`. It may do so as an absolute quantity, or as an integer percentage of the sandbox's resolved memory request, such as `"50%"`. The percentage form is valid only for `MEMORY`. A `DISK` volume must use an absolute size.

```python theme={"system"}
from cwsandbox import Sandbox, ScratchVolumeOptions, StorageMedium

with Sandbox.run(
    container_image="ubuntu:22.04",
    volumes=[
        ScratchVolumeOptions(
            name="fast-cache",
            mount_path="/cache",
            size="50%",
            medium=StorageMedium.MEMORY,
        )
    ],
) as sandbox:
    sandbox.exec(["sh", "-c", "df -h /cache"]).result()
```

### Mount a scratch volume on several containers

`mount_path` is a convenience that mounts the volume on the primary container. In a multi-container sandbox, omit `mount_path` and declare the mounts on each container instead, so that more than one container can reach the same volume.

```python theme={"system"}
from cwsandbox import Container, Sandbox, ScratchVolumeOptions, VolumeMount

with Sandbox.run(
    volumes=[ScratchVolumeOptions(name="shared", size="5Gi")],
    containers=[
        Container(
            name="app",
            image="ubuntu:22.04",
            primary=True,
            volume_mounts=[VolumeMount(volume="shared", mount_path="/data")],
        ),
        Container(
            name="sidecar",
            image="ubuntu:22.04",
            volume_mounts=[VolumeMount(volume="shared", mount_path="/data", read_only=True)],
        ),
    ],
) as sandbox:
    sandbox.exec(["sh", "-c", "echo shared > /data/note.txt"]).result()
```

## Related resources

For more on related sandbox features, see the following resources:

* [ファイルシステムスナップショット](/products/sandboxes/serverless/file-system-snapshots)：サンドボックスのファイルシステムを永続化し、リストアします。
* [サンドボックスの設定](/ja/products/sandboxes/serverless/client/guides/sandbox-configuration)：その他のサンドボックス作成オプション
