Skip to main content
Create, manage, and mount named volumes. See Volumes for usage examples.

Volume

Volume::get_default()

Get the Cloud account’s always-present default volume. It has no user-assigned name, cannot be removed, and supports direct filesystem operations through .fs(). The local backend returns a typed Unsupported error; it never substitutes a directory from the caller’s machine.

Volume::builder()

Create a builder for configuring a new named volume. Directory-backed volumes are the default; call .disk() then .size() for a raw ext4 disk-image volume. Volume names must start with an alphanumeric character and contain only alphanumeric characters, dots, hyphens, and underscores. See VolumeBuilder for all options.

Parameters

nameimpl Into<String>
Volume name, e.g. “pip-cache”.

Returns

Builder for configuring the volume.

Volume::create()

Provision a volume from a VolumeConfig. Routes through the active backend. Locally this inserts a database record and creates the host directory (formatting a disk.raw for disk volumes). Fails with VolumeAlreadyExists if a volume of the same name already exists. Most callers use Volume::builder(), which calls this internally.

Parameters

Volume configuration. VolumeConfig is an alias for VolumeSpec.

Returns

The created volume.

Volume::get()

Get a handle to an existing named volume. Use the handle to access the volume’s filesystem from the host, read its metadata, or delete it. Fails with VolumeNotFound if no volume by that name exists.

Parameters

name&str
Volume name.

Returns

Handle for host-side operations.

Volume::list()

List all named volumes, newest first.

Returns

All volume handles.

Volume::remove()

Delete a named volume and its contents from disk. Locally the database record is deleted first, then the directory, so an orphaned directory is easier to detect than an orphaned record. Fails with VolumeNotFound if the volume does not exist.

Parameters

name&str
Volume name.

Instance methods

A live Volume, returned by Volume::create() or VolumeBuilder::create(). Carries the backend it was created on.

vol.name()

The unique name identifying this volume.

Returns

&str
Volume name.

vol.kind()

The storage kind: Directory or Disk.

Returns

Storage kind.

vol.fs()

Get a filesystem handle for reading and writing the volume’s files directly, without a running sandbox. Local volumes route to tokio::fs; Cloud volumes route through the authenticated volume API. See VolumeFs for the operations.

Returns

Filesystem handle.

vol.path()

The host-side directory where this volume’s data is stored.

Returns

&Path
Host data directory, e.g. ~/.microsandbox/volumes/pip-cache/.

vol.disk_path()

Host path to the managed raw disk image (disk.raw) for disk volumes. Returns None for directory volumes.

Returns

Option<PathBuf>
Path to disk.raw, or None for directory volumes.

vol.capacity_bytes()

Disk capacity in bytes for disk volumes. None for directory volumes.

Returns

Option<u64>
Capacity in bytes, or None.

vol.disk_format()

Disk image format for disk volumes (always "raw" for managed disk volumes). None for directory volumes.

Returns

Option<&str>
Format string, or None.

vol.disk_fstype()

Inner disk filesystem type for disk volumes (always "ext4" for managed disk volumes). None for directory volumes.

Returns

Option<&str>
Filesystem type, or None.

vol.backend_kind()

Which backend variant this volume is bound to: Local or Cloud.

Returns

BackendKind
Local or Cloud.

vol.local()

Returns Some for local-backed volumes and None otherwise.

Returns

Option<&VolumeLocalState>
Local state, or None.

vol.cloud()

Returns Some for cloud-backed volumes and None otherwise.

Returns

Option<&VolumeCloudState>
Cloud state, or None.

VolumeHandle

Returned by Volume::get() · Volume::list()

A metadata and lifecycle handle for a named volume.

h.name()

The unique name identifying this volume.

Returns

&str
Volume name.

h.kind()

The storage kind: Directory or Disk.

Returns

Storage kind.

h.fs()

Get a filesystem handle for reading and writing the volume’s files directly, without a running sandbox. See VolumeFs.

Returns

Filesystem handle.

h.remove()

Delete this volume and its contents. Locally the database record is removed first, then the directory.

h.used_bytes()

Disk usage snapshot from when this handle was created. Not live, call Volume::get() again for a fresh reading.

Returns

u64
Bytes used at handle-creation time.

h.quota_mib()

Maximum storage in MiB, or None if unlimited.

Returns

Option<u32>
Quota in MiB, or None.

h.capacity_bytes()

Disk capacity in bytes for disk volumes. None for directory volumes.

Returns

Option<u64>
Capacity in bytes, or None.

h.disk_format()

Disk image format for disk volumes. None for directory volumes.

Returns

Option<&str>
Format string, or None.

h.disk_fstype()

Inner disk filesystem type for disk volumes. None for directory volumes.

Returns

Option<&str>
Filesystem type, or None.

h.disk_path()

Host path to the managed raw disk image (disk.raw) for local disk volumes. None otherwise.

Returns

Option<PathBuf>
Path to disk.raw, or None.

h.labels()

Key-value labels for organizing and filtering volumes.

Returns

&[(String, String)]
Label pairs.

h.created_at()

When this volume was first created, if recorded.

Returns

Option<DateTime<Utc>>
Creation timestamp, or None.

h.backend_kind()

Which backend variant this handle is bound to: Local or Cloud.

Returns

BackendKind
Local or Cloud.

h.local()

Returns Some for local-backed handles and None otherwise.

Returns

Option<&VolumeHandleLocalState>
Local state, or None.

h.cloud()

Returns Some for cloud-backed handles and None otherwise.

Returns

Option<&VolumeHandleCloudState>
Cloud state, or None.

VolumeFs

Returned by Volume::fs() · VolumeHandle::fs()

Host-side filesystem operations for a named volume.

fs.read()

Read an entire file into memory as raw bytes.

Parameters

path&str
File path relative to the volume root.

Returns

Bytes
File contents.

fs.read_to_string()

Read an entire file into memory as a UTF-8 string.

Parameters

path&str
File path relative to the volume root.

Returns

String
File contents as UTF-8.

fs.read_stream()

Open a file for streaming reads. Returns a VolumeFsReadStream that yields 64 KiB chunks, so large files don’t have to be held in memory at once.

Parameters

path&str
File path relative to the volume root.

Returns

Chunked reader.

fs.write()

Write data to a file, creating parent directories as needed. Overwrites if the file already exists.

Parameters

path&str
File path relative to the volume root.
dataimpl AsRef<[u8]>
Bytes to write.

fs.write_stream()

Open a file for streaming writes. Returns a VolumeFsWriteSink that accepts chunks of bytes. Creates parent directories as needed.

Parameters

path&str
File path relative to the volume root.

Returns

Chunked writer.

fs.list()

List the immediate children of a directory (non-recursive). Each entry includes the path, kind, size, permissions, and modification time.

Parameters

path&str
Directory path relative to the volume root.

Returns

Directory entries.

fs.mkdir()

Create a directory and any missing parents.

Parameters

path&str
Directory path relative to the volume root.

fs.remove()

Delete a single file. Use remove_dir() for directories.

Parameters

path&str
File path relative to the volume root.

fs.remove_dir()

Remove a directory and its contents recursively. Targeting the volume root is rejected.

Parameters

path&str
Directory path relative to the volume root.

fs.copy()

Copy a file within the volume. Creates the destination’s parent directories as needed.

Parameters

from&str
Source path relative to the volume root.
to&str
Destination path relative to the volume root.

fs.rename()

Rename or move a file or directory. Creates the destination’s parent directories as needed.

Parameters

from&str
Source path relative to the volume root.
to&str
Destination path relative to the volume root.

fs.stat()

Get metadata for a file or directory: kind, size, permission bits, read-only flag, and timestamps.

Parameters

path&str
Path relative to the volume root.

Returns

Entry metadata.

fs.exists()

Check whether a file or directory exists at the given path. Returns false rather than an error if the path is absent.

Parameters

path&str
Path relative to the volume root.

Returns

bool
true if the path exists.

VolumeBuilder

Returned by Volume::builder()

Builder for configuring a named volume.

volume_builder.directory()

Create a directory-backed named volume (mounted through virtiofs). This is the default.

volume_builder.disk()

Create a raw ext4 disk-image named volume (mounted through virtio-blk). Requires .size().

volume_builder.size()

Set the disk volume’s capacity. Required for disk volumes; rejected for directory volumes. Accepts a bare u32 (MiB) or a SizeExt helper such as 20.gib().

Parameters

sizeimpl Into<Mebibytes>
Capacity in MiB.

volume_builder.quota()

Limit a directory volume’s storage. Accepts a bare u32 (MiB) or a SizeExt helper such as 1.gib(). Omit for unlimited growth (the default). Rejected for disk volumes, which size up front via .size().

Parameters

sizeimpl Into<Mebibytes>
Quota in MiB.

volume_builder.label()

Attach a key-value label for organizing and filtering volumes. Can be called multiple times.

Parameters

keyimpl Into<String>
Label key.
valueimpl Into<String>
Label value.

volume_builder.build()

Materialize the VolumeConfig without creating the volume. Pass the result to Volume::create() to provision it later.

Returns

The volume configuration.

volume_builder.create()

Create the volume on the active backend. Equivalent to Volume::create(self.build()).

Returns

The created volume.

MountBuilder

Used by SandboxBuilder::volume()

Builder for configuring a sandbox volume mount.

mount.bind()

Bind mount a host directory into the guest. Changes in the guest are reflected on the host and vice versa. The host path must be valid UTF-8 and must not contain ,, :, or ;.

Parameters

hostimpl Into<PathBuf>
Directory path on the host.

mount.named()

Mount a named volume created via Volume::create(). The volume must already exist. Persists across sandbox restarts and can be shared between sandboxes. For sandbox-time provisioning, use .named_with().

Parameters

nameimpl Into<String>
Volume name.

mount.named_with()

Mount a named volume with explicit sandbox-time existence behavior, configured via a NamedVolumeBuilder closure. existing (the default) behaves like .named(); create provisions the volume and fails if it already exists; ensure_exists provisions it if missing or reuses a compatible existing volume. The ensure-exists mode validates existing metadata and errors when the kind, quota, capacity, or explicitly requested labels differ; it does not mutate existing metadata.

Parameters

nameimpl Into<String>
Volume name.
Configure existence behavior and creation metadata.

mount.tmpfs()

Use an in-memory filesystem. Contents are discarded when the sandbox stops. Good for scratch space, temp files, and build artifacts. Cap its size with .size().

mount.disk()

Mount a host disk-image file as a virtio-blk device at the guest path. The format defaults from the file extension (.qcow2, .vmdk; anything else is Raw). Override with .format().

Parameters

hostimpl Into<PathBuf>
Disk image path on the host.

mount.format()

Override the disk-image format for a .disk() mount. Valid only with .disk(); calling it on a bind, named, or tmpfs mount errors when the SandboxBuilder is finalized.

Parameters

Disk image format.

mount.fstype()

Set the inner filesystem type for a .disk() mount, for example "ext4". If omitted, agentd probes /proc/filesystems and uses the first type that mounts cleanly. Empty values and the separators ,, ;, :, = are rejected. Valid only with .disk().

Parameters

fstypeimpl Into<String>
Inner filesystem type.

mount.readonly()

Prevent writes to this mount. Enforced both at the host (virtiofs server rejects writes) and in the guest (the kernel returns EROFS).

mount.noexec()

Prevent direct execution of files on this mount. Interpreters can still read scripts from the mount, such as sh /mnt/script.sh, because the interpreter binary executes from a different filesystem.

mount.nosuid()

Ignore setuid and setgid privilege elevation from files on this mount.

mount.nodev()

Ignore device files on this mount.

mount.stat_virtualization()

Set the guest stat virtualization policy for a virtiofs-backed mount. Default: Strict. Valid only for bind and directory-backed named-volume mounts. Tmpfs and disk-image mounts are rejected when the mount is built; disk-backed named volumes are rejected once the backing volume kind is known during sandbox create or start.

Parameters

Stat virtualization policy.

mount.host_permissions()

Set the host permission propagation policy for a virtiofs-backed mount. Default: Private. Valid only for bind and directory-backed named-volume mounts. Combining StatVirtualization::Off with HostPermissions::Mirror is rejected, since with no overlay the guest chmod already hits the host inode and Mirror would be a no-op.

Parameters

Host permission propagation policy.

mount.size()

Set the size limit for a .tmpfs() mount. Accepts a bare u32 (MiB) or a SizeExt helper such as 1.gib(). Valid only for tmpfs mounts.

Parameters

sizeimpl Into<Mebibytes>
Size limit in MiB.

mount.build()

Validate and materialize the mount. Usually called internally by SandboxBuilder::volume; call it directly only when assembling a VolumeMount by hand. Errors when no mount kind is set, the guest path is not absolute or is /, or a kind-specific option was set on the wrong mount kind.

Returns

VolumeMount
Validated mount specification.

NamedVolumeBuilder

Sub-builder for MountBuilder::named_with(). Selects sandbox-time existence behavior and creation metadata.

Used by MountBuilder::named_with()

Sub-builder for MountBuilder::named_with(). Selects the sandbox-time existence behavior and, for create / ensure_exists, the creation metadata. Defaults to existing and directory-backed.

named.existing()

Require the named volume to already exist. This is the default.

named.create()

Create the named volume at sandbox launch and fail if it already exists.

named.ensure_exists()

Create the named volume if it is missing, or reuse a compatible existing volume. Errors if an existing volume’s kind, quota, capacity, or explicitly requested labels differ.

named.name()

Override the volume name passed to named_with().

Parameters

nameimpl Into<String>
Volume name.

named.directory()

Use directory-backed storage for a created volume. This is the default. Clears any previously set disk capacity.

named.disk()

Use raw ext4 disk-image storage for a created volume. Requires .size(). Clears any previously set quota.

named.size()

Set disk capacity for a created disk volume. Accepts a bare u32 (MiB) or a SizeExt helper.

Parameters

sizeimpl Into<Mebibytes>
Capacity in MiB.

named.quota()

Set a storage quota for a created directory volume. Accepts a bare u32 (MiB) or a SizeExt helper.

Parameters

sizeimpl Into<Mebibytes>
Quota in MiB.

named.label()

Attach a label to a newly-created volume. For ensure_exists, requested labels must match the existing volume. Can be called multiple times.

Parameters

keyimpl Into<String>
Label key.
valueimpl Into<String>
Label value.

VolumeFsReadStream

A streaming reader for file data from a local volume directory. Returned by VolumeFs::read_stream().

Returned by VolumeFs::read_stream()

stream.recv()

Next chunk; None at EOF

Returns

Option<Bytes>

stream.collect()

Read the rest into one buffer

Returns

Bytes

VolumeFsWriteSink

A streaming writer for file data to a local volume directory. Returned by VolumeFs::write_stream().

Returned by VolumeFs::write_stream()

sink.write()

Append a chunk

sink.close()

Flush and close

Types

VolumeKind

Storage kind for a named volume.

Returned by Volume::kind() · VolumeHandle::kind()

VolumeSpec

Configuration for creating a named volume. Re-exported as both VolumeSpec and the alias VolumeConfig.

Used by Volume::create() · returned by VolumeBuilder::build()

MountOptions

Guest mount behavior shared by every mount kind. Set via the MountBuilder toggles; all fields default to false.

StatVirtualization

Stat virtualization policy for a virtiofs-backed mount. Default: Strict. Set via MountBuilder::stat_virtualization().

HostPermissions

Host permission propagation policy for a virtiofs-backed mount. Default: Private. Set via MountBuilder::host_permissions().

DiskImageFormat

Disk image format for virtio-blk root filesystems and volume mounts. Used by MountBuilder::format().

NamedVolumeMode

Sandbox-time behavior for a named volume mount, chosen via NamedVolumeBuilder.