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

# 挂载与卸载 Volume

> 了解 PPIO Agent 沙箱 Volume 的挂载与卸载流程，查阅存储资源和运行环境的连接方式，为管理任务文件访问提供参考。

Volume 提供持久化存储，可通过挂载路径挂载到 Sandbox。你可以在创建 Sandbox 时挂载 Volume，也可以在运行中的 Sandbox 上在运行时挂载和卸载。

## 创建时挂载

向 `ppio.sandbox.create` 传入 `volume_mounts`（Python）/ `volumeMounts`（JS）——一个从 Sandbox 内挂载路径到 `Volume` 实例或 Volume 名称的映射。

```python Python theme={null}
from ppio_sandbox import PPIO

ppio = PPIO()

volume = ppio.volume.connect("vol_123")

sandbox = ppio.sandbox.create(
    volume_mounts={
        "/mnt/data": volume,              # 一个 Volume 实例
        "/mnt/shared": "my-volume-name",  # 或一个 Volume 名称
    },
)
print("Sandbox created:", sandbox.sandbox_id)
```

```typescript JavaScript & TypeScript theme={null}
import { PPIO } from 'ppio-sandbox'

const ppio = new PPIO()

const volume = await ppio.volume.connect('vol_123')

const sandbox = await ppio.sandbox.create({
  volumeMounts: {
    '/mnt/data': volume,   // 一个 Volume 实例
    '/mnt/shared': 'my-volume-name', // 或一个 Volume 名称
  },
})
console.log('Sandbox created:', sandbox.sandboxId)
```

```bash CLI theme={null}
# CLI 不支持在创建时声明挂载；创建 Sandbox 后使用 volume mount 挂载。
ppio sandbox create <template> -d
ppio volume mount <sandbox_id> --name my-volume-name --path /mnt/data
```

## 运行时挂载

`sandbox.mount_volume(name, path)`（Python）/ `sandbox.mountVolume(name, path)`（JS）按名称将 Volume 挂载到运行中 Sandbox 的指定挂载路径，并返回更新后的 Sandbox 信息。

```python Python theme={null}
sandbox.mount_volume("my-volume-name", "/mnt/data")
```

```typescript JavaScript & TypeScript theme={null}
await sandbox.mountVolume('my-volume-name', '/mnt/data')
```

```bash CLI theme={null}
# 将 Volume 挂载到运行中 Sandbox 的指定路径
ppio volume mount <sandbox_id> --name my-volume-name --path /mnt/data
```

## 卸载

`sandbox.unmount_volume(path)`（Python）/ `sandbox.unmountVolume(path)`（JS）卸载挂载在指定路径上的 Volume。传入 `force` 可强制卸载。

```python Python theme={null}
sandbox.unmount_volume("/mnt/data")

# 强制卸载
sandbox.unmount_volume("/mnt/data", force=True)
```

```typescript JavaScript & TypeScript theme={null}
await sandbox.unmountVolume('/mnt/data')

// 强制卸载
await sandbox.unmountVolume('/mnt/data', { force: true })
```

```bash CLI theme={null}
# 卸载挂载在指定路径上的 Volume；-f 强制卸载
ppio volume unmount <sandbox_id> --path /mnt/data

ppio volume unmount <sandbox_id> --path /mnt/data --force
```


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