Back to Microsandbox

Volumes

docs/sdk/go/volumes.mdx

0.5.1042.7 KB
Original Source

Create and manage named persistent volumes, read and write their data directly on the host, and attach them (or bind mounts, tmpfs, and disk images) to sandboxes. See Volumes for usage examples and patterns.

<div className="msb-glance"> <p className="msb-gl"><span className="msb-dot static"></span>Functions<span className="msb-ct">4</span></p> <a className="msb-row" href="#createvolume"><span className="msb-rn">CreateVolume()</span><span className="msb-rg">create a named volume</span></a> <a className="msb-row" href="#getvolume"><span className="msb-rn">GetVolume()</span><span className="msb-rg">look up one by name</span></a> <a className="msb-row" href="#listvolumes"><span className="msb-rn">ListVolumes()</span><span className="msb-rg">all volumes on the host</span></a> <a className="msb-row" href="#removevolume"><span className="msb-rn">RemoveVolume()</span><span className="msb-rg">delete one by name</span></a> <p className="msb-gl"><span className="msb-dot instance"></span>Methods · Volume<span className="msb-ct">4</span></p> <a className="msb-row" href="#v-name"><span className="msb-rn">v.Name()</span><span className="msb-rg">volume name</span></a> <a className="msb-row" href="#v-path"><span className="msb-rn">v.Path()</span><span className="msb-rg">host data directory path</span></a> <a className="msb-row" href="#v-fs"><span className="msb-rn">v.FS()</span><span className="msb-rg">host-side file access</span></a> <a className="msb-row" href="#v-remove"><span className="msb-rn">v.Remove()</span><span className="msb-rg">delete this volume</span></a> <p className="msb-gl"><span className="msb-dot instance"></span>Methods · VolumeHandle<span className="msb-ct">12</span></p> <a className="msb-row" href="#h-name"><span className="msb-rn">h.Name()</span><span className="msb-rg">volume name</span></a> <a className="msb-row" href="#h-path"><span className="msb-rn">h.Path()</span><span className="msb-rg">host data directory path</span></a> <a className="msb-row" href="#h-kind"><span className="msb-rn">h.Kind()</span><span className="msb-rg">dir or disk backing</span></a> <a className="msb-row" href="#h-quotamib"><span className="msb-rn">h.QuotaMiB()</span><span className="msb-rg">quota in MiB, or nil</span></a> <a className="msb-row" href="#h-usedbytes"><span className="msb-rn">h.UsedBytes()</span><span className="msb-rg">space used in bytes</span></a> <a className="msb-row" href="#h-capacitybytes"><span className="msb-rn">h.CapacityBytes()</span><span className="msb-rg">disk capacity in bytes</span></a> <a className="msb-row" href="#h-diskformat"><span className="msb-rn">h.DiskFormat()</span><span className="msb-rg">disk image format</span></a> <a className="msb-row" href="#h-diskfstype"><span className="msb-rn">h.DiskFstype()</span><span className="msb-rg">inner filesystem type</span></a> <a className="msb-row" href="#h-labels"><span className="msb-rn">h.Labels()</span><span className="msb-rg">metadata labels</span></a> <a className="msb-row" href="#h-createdat"><span className="msb-rn">h.CreatedAt()</span><span className="msb-rg">creation timestamp</span></a> <a className="msb-row" href="#h-fs"><span className="msb-rn">h.FS()</span><span className="msb-rg">host-side file access</span></a> <a className="msb-row" href="#h-remove"><span className="msb-rn">h.Remove()</span><span className="msb-rg">delete this volume</span></a> <p className="msb-gl"><span className="msb-dot instance"></span>Methods · VolumeFs<span className="msb-ct">9</span></p> <a className="msb-row" href="#vfs-root"><span className="msb-rn">vfs.Root()</span><span className="msb-rg">absolute root path</span></a> <a className="msb-row" href="#vfs-read"><span className="msb-rn">vfs.Read()</span><span className="msb-rg">read file bytes</span></a> <a className="msb-row" href="#vfs-readstring"><span className="msb-rn">vfs.ReadString()</span><span className="msb-rg">read file as string</span></a> <a className="msb-row" href="#vfs-write"><span className="msb-rn">vfs.Write()</span><span className="msb-rg">write bytes to a file</span></a> <a className="msb-row" href="#vfs-writestring"><span className="msb-rn">vfs.WriteString()</span><span className="msb-rg">write a string</span></a> <a className="msb-row" href="#vfs-mkdir"><span className="msb-rn">vfs.Mkdir()</span><span className="msb-rg">create directory tree</span></a> <a className="msb-row" href="#vfs-remove"><span className="msb-rn">vfs.Remove()</span><span className="msb-rg">delete file or empty dir</span></a> <a className="msb-row" href="#vfs-removeall"><span className="msb-rn">vfs.RemoveAll()</span><span className="msb-rg">recursive delete</span></a> <a className="msb-row" href="#vfs-exists"><span className="msb-rn">vfs.Exists()</span><span className="msb-rg">check for existence</span></a> <p className="msb-gl"><span className="msb-dot builder"></span>Options<span className="msb-ct">9</span></p> <div className="msb-chiprow"> <a className="msb-chip" href="#withvolumekind">WithVolumeKind()</a> <a className="msb-chip" href="#withvolumesize">WithVolumeSize()</a> <a className="msb-chip" href="#withvolumequota">WithVolumeQuota()</a> <a className="msb-chip" href="#withvolumelabels">WithVolumeLabels()</a> <a className="msb-chip" href="#mount-bind">Mount.Bind()</a> <a className="msb-chip" href="#mount-named">Mount.Named()</a> <a className="msb-chip" href="#mount-namedwith">Mount.NamedWith()</a> <a className="msb-chip" href="#mount-tmpfs">Mount.Tmpfs()</a> <a className="msb-chip" href="#mount-disk">Mount.Disk()</a> </div> <p className="msb-gl"><span className="msb-dot type"></span>Types</p> <div className="msb-chiprow"> <a className="msb-typepill" href="#volume">Volume</a> <a className="msb-typepill" href="#volumehandle">VolumeHandle</a> <a className="msb-typepill" href="#volumefs">VolumeFs</a> <a className="msb-typepill" href="#errpathescape">ErrPathEscape</a> <a className="msb-typepill" href="#volumeconfig">VolumeConfig</a> <a className="msb-typepill" href="#volumeoption">VolumeOption</a> <a className="msb-typepill" href="#volumekind">VolumeKind</a> <a className="msb-typepill" href="#mountconfig">MountConfig</a> <a className="msb-typepill" href="#mountkind">MountKind</a> <a className="msb-typepill" href="#mountoptions">MountOptions</a> <a className="msb-typepill" href="#namedvolumeoptions">NamedVolumeOptions</a> <a className="msb-typepill" href="#tmpfsoptions">TmpfsOptions</a> <a className="msb-typepill" href="#diskoptions">DiskOptions</a> </div> </div> <p className="msb-label" id="typical-flow">Typical flow</p>
go
import m "github.com/superradcompany/microsandbox/sdk/go"

vol, err := m.CreateVolume(ctx, "my-data",         // 1. create a named volume
    m.WithVolumeQuota(64),
    m.WithVolumeLabels(map[string]string{"env": "prod"}),
)

vfs := vol.FS()                                    // 2. seed it from the host
err = vfs.WriteString("seed.txt", "hello")

sb, err := m.CreateSandbox(ctx, "worker",          // 3. attach it to a sandbox
    m.WithImage("alpine"),
    m.WithMounts(map[string]m.MountConfig{
        "/data": m.Mount.Named("my-data", m.MountOptions{}),
    }),
)

Named volumes are managed by microsandbox and stored by default under ~/.microsandbox/volumes/<name>/. They persist independently of any sandbox. There is no Rust-side resource to release: Remove deletes the on-disk state and the DB record.

Functions


<span className="msb-recv"></span><span className="msb-hn">CreateVolume()</span>

<div className="msb-tags"><span className="msb-tag is-static">function</span></div>
go
func CreateVolume(ctx context.Context, name string, opts ...VolumeOption) (*Volume, error)

Create a named volume and return a populated *Volume with its name and host path. Configure the kind, quota, disk size, and labels with option functions. Returns ErrVolumeAlreadyExists if a volume with the given name already exists.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>ctx</code><span className="msb-type">context.Context</span></div> <div className="msb-param-desc">Cancels the create.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>name</code><span className="msb-type">string</span></div> <div className="msb-param-desc">Volume name.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>opts</code><a className="msb-type" href="#volumeoption">...VolumeOption</a></div> <div className="msb-param-desc">Functional options, see <a href="#options">Options</a>.</div> </div> </div> <p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><a className="msb-type" href="#volume">*Volume</a></div> <div className="msb-param-desc">Newly created volume with <code>Name</code> and <code>Path</code>.</div> </div> </div> <Accordion title="Example">
go
vol, err := m.CreateVolume(ctx, "docker-data",
    m.WithVolumeKind(m.VolumeKindDisk),
    m.WithVolumeSize(20*1024),
)
</Accordion>

<span className="msb-recv"></span><span className="msb-hn">GetVolume()</span>

<div className="msb-tags"><span className="msb-tag is-static">function</span></div>
go
func GetVolume(ctx context.Context, name string) (*VolumeHandle, error)

Look up a volume by name and return its metadata. Returns ErrVolumeNotFound if no such volume exists.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>ctx</code><span className="msb-type">context.Context</span></div> <div className="msb-param-desc">Cancels the lookup.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>name</code><span className="msb-type">string</span></div> <div className="msb-param-desc">Volume name.</div> </div> </div> <p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><a className="msb-type" href="#volumehandle">*VolumeHandle</a></div> <div className="msb-param-desc">Metadata reference for the volume.</div> </div> </div> <Accordion title="Example">
go
h, err := m.GetVolume(ctx, "my-data")
fmt.Println(h.Path(), h.UsedBytes())
</Accordion>

<span className="msb-recv"></span><span className="msb-hn">ListVolumes()</span>

<div className="msb-tags"><span className="msb-tag is-static">function</span></div>
go
func ListVolumes(ctx context.Context) ([]*VolumeHandle, error)

Return metadata for every named volume on the host.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>ctx</code><span className="msb-type">context.Context</span></div> <div className="msb-param-desc">Cancels the listing.</div> </div> </div> <p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><a className="msb-type" href="#volumehandle">[]*VolumeHandle</a></div> <div className="msb-param-desc">All volume metadata handles.</div> </div> </div> <Accordion title="Example">
go
vols, err := m.ListVolumes(ctx)
for _, h := range vols {
    fmt.Printf("%s — %s\n", h.Name(), h.Kind())
}
</Accordion>

<span className="msb-recv"></span><span className="msb-hn">RemoveVolume()</span>

<div className="msb-tags"><span className="msb-tag is-static">function</span></div>
go
func RemoveVolume(ctx context.Context, name string) error

Delete a volume by name. All sandboxes referencing the volume must be stopped first.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>ctx</code><span className="msb-type">context.Context</span></div> <div className="msb-param-desc">Cancels the removal.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>name</code><span className="msb-type">string</span></div> <div className="msb-param-desc">Volume name.</div> </div> </div> <Accordion title="Example">
go
err := m.RemoveVolume(ctx, "my-data")
</Accordion>

Volume methods

A Volume is returned by CreateVolume and carries the volume's name and host path.


<span className="msb-recv">v.</span><span className="msb-hn">Name()</span>

<div className="msb-tags"><span className="msb-tag is-instance">method</span></div>
go
func (v *Volume) Name() string

Return the volume's name.


<span className="msb-recv">v.</span><span className="msb-hn">Path()</span>

<div className="msb-tags"><span className="msb-tag is-instance">method</span></div>
go
func (v *Volume) Path() string

Return the host filesystem path of the volume's data directory.


<span className="msb-recv">v.</span><span className="msb-hn">FS()</span>

<div className="msb-tags"><span className="msb-tag is-instance">method</span></div>
go
func (v *Volume) FS() *VolumeFs

Return a *VolumeFs for direct host-side file operations on this volume.

<p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><a className="msb-type" href="#volumefs">*VolumeFs</a></div> <div className="msb-param-desc">Host-side filesystem accessor rooted at the volume directory.</div> </div> </div> <Accordion title="Example">
go
vfs := vol.FS()
err := vfs.WriteString("seed.txt", "hello")
</Accordion>

<span className="msb-recv">v.</span><span className="msb-hn">Remove()</span>

<div className="msb-tags"><span className="msb-tag is-instance">method</span></div>
go
func (v *Volume) Remove(ctx context.Context) error

Delete this volume. All sandboxes using it must be stopped. Equivalent to RemoveVolume(ctx, v.Name()).

<Accordion title="Example">
go
err := vol.Remove(ctx)
</Accordion>

VolumeHandle methods

A VolumeHandle is the metadata reference returned by GetVolume and ListVolumes.


<span className="msb-recv">h.</span><span className="msb-hn">Name()</span>

<div className="msb-tags"><span className="msb-tag is-instance">method</span></div>
go
func (h *VolumeHandle) Name() string

Return the volume name.


<span className="msb-recv">h.</span><span className="msb-hn">Path()</span>

<div className="msb-tags"><span className="msb-tag is-instance">method</span></div>
go
func (h *VolumeHandle) Path() string

Return the host filesystem path of the volume's data directory.


<span className="msb-recv">h.</span><span className="msb-hn">Kind()</span>

<div className="msb-tags"><span className="msb-tag is-instance">method</span></div>
go
func (h *VolumeHandle) Kind() VolumeKind

Return the volume storage kind: VolumeKindDir or VolumeKindDisk.

<p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><a className="msb-type" href="#volumekind">VolumeKind</a></div> <div className="msb-param-desc">Storage backing for the volume.</div> </div> </div>

<span className="msb-recv">h.</span><span className="msb-hn">QuotaMiB()</span>

<div className="msb-tags"><span className="msb-tag is-instance">method</span></div>
go
func (h *VolumeHandle) QuotaMiB() *uint32

Return the quota in MiB, or nil if unlimited.


<span className="msb-recv">h.</span><span className="msb-hn">UsedBytes()</span>

<div className="msb-tags"><span className="msb-tag is-instance">method</span></div>
go
func (h *VolumeHandle) UsedBytes() uint64

Return the amount of space used by the volume, in bytes.


<span className="msb-recv">h.</span><span className="msb-hn">CapacityBytes()</span>

<div className="msb-tags"><span className="msb-tag is-instance">method</span></div>
go
func (h *VolumeHandle) CapacityBytes() *uint64

Return the disk capacity in bytes for disk volumes, or nil for directory volumes.


<span className="msb-recv">h.</span><span className="msb-hn">DiskFormat()</span>

<div className="msb-tags"><span className="msb-tag is-instance">method</span></div>
go
func (h *VolumeHandle) DiskFormat() *string

Return the disk image format for disk volumes, or nil for directory volumes.


<span className="msb-recv">h.</span><span className="msb-hn">DiskFstype()</span>

<div className="msb-tags"><span className="msb-tag is-instance">method</span></div>
go
func (h *VolumeHandle) DiskFstype() *string

Return the inner filesystem type for disk volumes, or nil for directory volumes.


<span className="msb-recv">h.</span><span className="msb-hn">Labels()</span>

<div className="msb-tags"><span className="msb-tag is-instance">method</span></div>
go
func (h *VolumeHandle) Labels() map[string]string

Return the labels attached to this volume.


<span className="msb-recv">h.</span><span className="msb-hn">CreatedAt()</span>

<div className="msb-tags"><span className="msb-tag is-instance">method</span></div>
go
func (h *VolumeHandle) CreatedAt() time.Time

Return the creation timestamp, or the zero time.Time value if unknown.


<span className="msb-recv">h.</span><span className="msb-hn">FS()</span>

<div className="msb-tags"><span className="msb-tag is-instance">method</span></div>
go
func (h *VolumeHandle) FS() *VolumeFs

Return a *VolumeFs for direct host-side file operations on this volume.

<p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><a className="msb-type" href="#volumefs">*VolumeFs</a></div> <div className="msb-param-desc">Host-side filesystem accessor rooted at the volume directory.</div> </div> </div>

<span className="msb-recv">h.</span><span className="msb-hn">Remove()</span>

<div className="msb-tags"><span className="msb-tag is-instance">method</span></div>
go
func (h *VolumeHandle) Remove(ctx context.Context) error

Delete this volume. All sandboxes using it must be stopped. Equivalent to RemoveVolume(ctx, h.Name()).

VolumeFs methods

Host-side filesystem operations on a named volume's data directory. Obtain via Volume.FS() or VolumeHandle.FS(). These operations run directly on the host filesystem: no running sandbox is required and no agent protocol is involved.

All path arguments are relative to the volume root. Paths that would escape the root via .., absolute components, or stray symlink chains are rejected with ErrPathEscape.

go
vfs := vol.FS()

if err := vfs.WriteString("seed.txt", "hello"); err != nil {
    return err
}
content, err := vfs.ReadString("seed.txt")

<span className="msb-recv">vfs.</span><span className="msb-hn">Root()</span>

<div className="msb-tags"><span className="msb-tag is-instance">method</span></div>
go
func (fs *VolumeFs) Root() string

Return the absolute host path of the volume's data directory.


<span className="msb-recv">vfs.</span><span className="msb-hn">Read()</span>

<div className="msb-tags"><span className="msb-tag is-instance">method</span></div>
go
func (fs *VolumeFs) Read(relPath string) ([]byte, error)

Read the contents of a file relative to the volume root.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>relPath</code><span className="msb-type">string</span></div> <div className="msb-param-desc">Path relative to the volume root.</div> </div> </div> <p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><span className="msb-type">[]byte</span></div> <div className="msb-param-desc">File contents.</div> </div> </div>

<span className="msb-recv">vfs.</span><span className="msb-hn">ReadString()</span>

<div className="msb-tags"><span className="msb-tag is-instance">method</span></div>
go
func (fs *VolumeFs) ReadString(relPath string) (string, error)

Read a file and return its contents as a UTF-8 string.


<span className="msb-recv">vfs.</span><span className="msb-hn">Write()</span>

<div className="msb-tags"><span className="msb-tag is-instance">method</span></div>
go
func (fs *VolumeFs) Write(relPath string, data []byte) error

Write data to a file, creating or truncating it. Created files use mode 0o644.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>relPath</code><span className="msb-type">string</span></div> <div className="msb-param-desc">Path relative to the volume root.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>data</code><span className="msb-type">[]byte</span></div> <div className="msb-param-desc">Bytes to write.</div> </div> </div>

<span className="msb-recv">vfs.</span><span className="msb-hn">WriteString()</span>

<div className="msb-tags"><span className="msb-tag is-instance">method</span></div>
go
func (fs *VolumeFs) WriteString(relPath, content string) error

Write a string to a file. Created files use mode 0o644.


<span className="msb-recv">vfs.</span><span className="msb-hn">Mkdir()</span>

<div className="msb-tags"><span className="msb-tag is-instance">method</span></div>
go
func (fs *VolumeFs) Mkdir(relPath string) error

Create a directory and all missing parents (mode 0o755).


<span className="msb-recv">vfs.</span><span className="msb-hn">Remove()</span>

<div className="msb-tags"><span className="msb-tag is-instance">method</span></div>
go
func (fs *VolumeFs) Remove(relPath string) error

Delete a single file or empty directory.


<span className="msb-recv">vfs.</span><span className="msb-hn">RemoveAll()</span>

<div className="msb-tags"><span className="msb-tag is-instance">method</span></div>
go
func (fs *VolumeFs) RemoveAll(relPath string) error

Delete a path and any children it contains (recursive).


<span className="msb-recv">vfs.</span><span className="msb-hn">Exists()</span>

<div className="msb-tags"><span className="msb-tag is-instance">method</span></div>
go
func (fs *VolumeFs) Exists(relPath string) (bool, error)

Report whether a file or directory exists at the given path.

<p className="msb-label">Returns</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><span className="msb-type">bool</span></div> <div className="msb-param-desc"><code>true</code> if a file or directory exists at the path.</div> </div> </div>

Options

Functional options passed to CreateVolume, plus the Mount factory helpers that attach a volume, bind mount, tmpfs, or disk image to a sandbox via WithMounts.


<span className="msb-recv"></span><span className="msb-hn">WithVolumeKind()</span>

<div className="msb-tags"><span className="msb-tag is-builder">option</span></div>
go
func WithVolumeKind(kind VolumeKind) VolumeOption

Select the volume kind. Valid values are VolumeKindDir (default) and VolumeKindDisk.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>kind</code><a className="msb-type" href="#volumekind">VolumeKind</a></div> <div className="msb-param-desc">Storage backing for the volume.</div> </div> </div>

<span className="msb-recv"></span><span className="msb-hn">WithVolumeSize()</span>

<div className="msb-tags"><span className="msb-tag is-builder">option</span></div>
go
func WithVolumeSize(mebibytes uint32) VolumeOption

Set disk volume capacity in MiB. Required when the kind is VolumeKindDisk.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>mebibytes</code><span className="msb-type">uint32</span></div> <div className="msb-param-desc">Disk capacity in MiB.</div> </div> </div>

<span className="msb-recv"></span><span className="msb-hn">WithVolumeQuota()</span>

<div className="msb-tags"><span className="msb-tag is-builder">option</span></div>
go
func WithVolumeQuota(mebibytes uint32) VolumeOption

Set the recorded quota in MiB for directory volumes. Zero means unlimited.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>mebibytes</code><span className="msb-type">uint32</span></div> <div className="msb-param-desc">Quota in MiB; zero means unlimited.</div> </div> </div>

<span className="msb-recv"></span><span className="msb-hn">WithVolumeLabels()</span>

<div className="msb-tags"><span className="msb-tag is-builder">option</span></div>
go
func WithVolumeLabels(labels map[string]string) VolumeOption

Attach key-value labels to the volume. When called repeatedly, the maps merge; later keys overwrite earlier ones.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>labels</code><span className="msb-type">map[string]string</span></div> <div className="msb-param-desc">Metadata labels to attach.</div> </div> </div>

<span className="msb-recv">Mount.</span><span className="msb-hn">Bind()</span>

<div className="msb-tags"><span className="msb-tag is-builder">option</span></div>
go
func (mountFactory) Bind(hostPath string, opts MountOptions) MountConfig

Bind-mount a host directory into the sandbox. Changes in the guest are reflected on the host and vice versa. Returns a MountConfig for use with WithMounts.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>hostPath</code><span className="msb-type">string</span></div> <div className="msb-param-desc">Host directory to bind.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>opts</code><a className="msb-type" href="#mountoptions">MountOptions</a></div> <div className="msb-param-desc">Mount tuning (read-only, noexec, and so on).</div> </div> </div> <Accordion title="Example">
go
"/host": m.Mount.Bind("/var/data", m.MountOptions{Readonly: true})
</Accordion>

<span className="msb-recv">Mount.</span><span className="msb-hn">Named()</span>

<div className="msb-tags"><span className="msb-tag is-builder">option</span></div>
go
func (mountFactory) Named(name string, opts MountOptions) MountConfig

Mount an existing named persistent volume. The volume must already exist (create it with CreateVolume). Returns a MountConfig.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>name</code><span className="msb-type">string</span></div> <div className="msb-param-desc">Name of an existing volume.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>opts</code><a className="msb-type" href="#mountoptions">MountOptions</a></div> <div className="msb-param-desc">Mount tuning.</div> </div> </div> <Accordion title="Example">
go
"/data": m.Mount.Named("my-data", m.MountOptions{})
</Accordion>

<span className="msb-recv">Mount.</span><span className="msb-hn">NamedWith()</span>

<div className="msb-tags"><span className="msb-tag is-builder">option</span></div>
go
func (mountFactory) NamedWith(name string, opts MountOptions, namedOpts NamedVolumeOptions) MountConfig

Mount a named persistent volume with explicit sandbox-time existence behavior. NamedVolumeOptions.Mode accepts "existing" (default), "create", or "ensure-exists". Mode: "create" fails when the named volume already exists. Mode: "ensure-exists" creates the volume if it is missing and reuses a compatible existing volume; it errors when the existing volume has a different kind, quota, or capacity than requested, and never mutates existing volume metadata.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>name</code><span className="msb-type">string</span></div> <div className="msb-param-desc">Volume name.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>opts</code><a className="msb-type" href="#mountoptions">MountOptions</a></div> <div className="msb-param-desc">Mount tuning.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>namedOpts</code><a className="msb-type" href="#namedvolumeoptions">NamedVolumeOptions</a></div> <div className="msb-param-desc">Provisioning mode, kind, size, and quota.</div> </div> </div> <Accordion title="Example">
go
sb, err := m.CreateSandbox(ctx, "worker",
    m.WithImage("python"),
    m.WithMounts(map[string]m.MountConfig{
        "/cache": m.Mount.NamedWith("pip-cache", m.MountOptions{}, m.NamedVolumeOptions{
            Mode: "ensure-exists",
        }),
        "/var/lib/docker": m.Mount.NamedWith("docker-data", m.MountOptions{}, m.NamedVolumeOptions{
            Mode:    "ensure-exists",
            Kind:    "disk",
            SizeMiB: 20 * 1024,
        }),
    }),
)
</Accordion>

<span className="msb-recv">Mount.</span><span className="msb-hn">Tmpfs()</span>

<div className="msb-tags"><span className="msb-tag is-builder">option</span></div>
go
func (mountFactory) Tmpfs(opts TmpfsOptions) MountConfig

Mount an ephemeral in-memory filesystem. Contents are discarded when the sandbox stops. Returns a MountConfig.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>opts</code><a className="msb-type" href="#tmpfsoptions">TmpfsOptions</a></div> <div className="msb-param-desc">Size limit and mount flags.</div> </div> </div> <Accordion title="Example">
go
"/scratch": m.Mount.Tmpfs(m.TmpfsOptions{SizeMiB: 128})
</Accordion>

<span className="msb-recv">Mount.</span><span className="msb-hn">Disk()</span>

<div className="msb-tags"><span className="msb-tag is-builder">option</span></div>
go
func (mountFactory) Disk(hostPath string, opts DiskOptions) MountConfig

Mount a host disk image as a virtio-blk device. Supports raw, qcow2, and vmdk formats. Returns a MountConfig.

<p className="msb-label">Parameters</p> <div className="msb-params"> <div className="msb-param"> <div className="msb-param-key"><code>hostPath</code><span className="msb-type">string</span></div> <div className="msb-param-desc">Host path to the disk image.</div> </div> <div className="msb-param"> <div className="msb-param-key"><code>opts</code><a className="msb-type" href="#diskoptions">DiskOptions</a></div> <div className="msb-param-desc">Format, inner filesystem, and mount flags.</div> </div> </div> <Accordion title="Example">
go
"/img": m.Mount.Disk("./data.qcow2", m.DiskOptions{
    Format:   "qcow2",
    Fstype:   "ext4",
    Readonly: true,
})
</Accordion>

Types


Volume

<div className="msb-tags"><span className="msb-tag is-type">struct</span></div>

A named persistent volume carrying its host-side path. Returned by CreateVolume. Lookups via GetVolume and ListVolumes yield richer VolumeHandle values instead.

<p className="msb-backref">Returned by <a href="#createvolume">CreateVolume()</a></p>
MethodReturnsDescription
Name()stringVolume name
Path()stringHost filesystem path of the data directory
FS()*VolumeFsHost-side filesystem accessor
Remove(ctx)errorDelete this volume (all sandboxes using it must be stopped)

VolumeHandle

<div className="msb-tags"><span className="msb-tag is-type">struct</span></div>

Metadata reference for a named volume. Obtain via GetVolume or ListVolumes.

<p className="msb-backref">Returned by <a href="#getvolume">GetVolume()</a> · <a href="#listvolumes">ListVolumes()</a></p>
MethodReturnsDescription
Name()stringVolume name
Path()stringHost filesystem path of the data directory
Kind()VolumeKindVolume kind: VolumeKindDir or VolumeKindDisk
QuotaMiB()*uint32Quota in MiB, or nil if unlimited
UsedBytes()uint64Current disk usage in bytes
CapacityBytes()*uint64Disk capacity in bytes, or nil
DiskFormat()*stringDisk image format, or nil
DiskFstype()*stringDisk filesystem type, or nil
Labels()map[string]stringMetadata labels
CreatedAt()time.TimeCreation timestamp, zero value if unknown
FS()*VolumeFsHost-side filesystem accessor
Remove(ctx)errorDelete this volume

VolumeFs

<div className="msb-tags"><span className="msb-tag is-type">struct</span></div>

Host-side filesystem operations on a volume's data directory. Obtain via Volume.FS() or VolumeHandle.FS(). All path arguments are relative to the volume root; escaping paths are rejected with ErrPathEscape.

<p className="msb-backref">Returned by <a href="#v-fs">Volume.FS()</a> · <a href="#h-fs">VolumeHandle.FS()</a></p>
MethodReturnsDescription
Root()stringAbsolute host path of the data directory
Read(relPath)([]byte, error)Read file bytes
ReadString(relPath)(string, error)Read file as a UTF-8 string
Write(relPath, data)errorWrite bytes, creating or truncating
WriteString(relPath, content)errorWrite a string
Mkdir(relPath)errorCreate a directory tree
Remove(relPath)errorDelete a file or empty directory
RemoveAll(relPath)errorRecursive delete
Exists(relPath)(bool, error)Check for existence

ErrPathEscape

<div className="msb-tags"><span className="msb-tag is-type">error</span></div>
go
var ErrPathEscape = errors.New("microsandbox: path escapes volume root")

Returned by every VolumeFs method when relPath is absolute, contains a .. sequence that resolves outside the volume root, or otherwise escapes the volume's directory after filepath.Clean.

<p className="msb-backref">Returned by <a href="#volumefs">VolumeFs</a> methods</p>
go
if _, err := vfs.Read("../etc/passwd"); errors.Is(err, m.ErrPathEscape) {
    log.Println("nice try")
}

VolumeConfig

<div className="msb-tags"><span className="msb-tag is-type">struct</span></div>

The config struct populated by VolumeOption functions. Most callers go through CreateVolume(ctx, name, opts...); VolumeConfig is exported for callers that prefer to construct one directly.

<p className="msb-backref">Mutated by <a href="#volumeoption">VolumeOption</a></p>
FieldTypeDescription
QuotaMiBuint32Maximum storage size in MiB (zero = unlimited)
KindVolumeKindVolume kind (VolumeKindDir by default)
SizeMiBuint32Disk capacity in MiB for VolumeKindDisk
Labelsmap[string]stringMetadata labels

VolumeOption

<div className="msb-tags"><span className="msb-tag is-type">type</span></div>
go
type VolumeOption func(*VolumeConfig)

A functional option for CreateVolume. Constructed by the WithVolume* helpers.

<p className="msb-backref">Used by <a href="#createvolume">CreateVolume()</a></p>

VolumeKind

<div className="msb-tags"><span className="msb-tag is-type">type</span></div>
go
type VolumeKind string

Describes the storage backing for a named volume.

<p className="msb-backref">Used by <a href="#volumeconfig">VolumeConfig</a> · <a href="#withvolumekind">WithVolumeKind()</a> · <a href="#h-kind">VolumeHandle.Kind()</a></p>
ConstantValueDescription
VolumeKindDir"dir"Directory-backed named volume
VolumeKindDisk"disk"Raw ext4 disk-backed named volume

MountConfig

<div className="msb-tags"><span className="msb-tag is-type">struct</span></div>

Discriminated mount configuration produced by the Mount factory helpers and passed to WithMounts. Construct it with the factory rather than by hand: the factory sets the internal kind discriminator and enforces the mutually-exclusive flavours. Inspect the flavour via Kind().

<p className="msb-backref">Returned by <a href="#mount-bind">Mount.Bind()</a> · <a href="#mount-named">Mount.Named()</a> · <a href="#mount-namedwith">Mount.NamedWith()</a> · <a href="#mount-tmpfs">Mount.Tmpfs()</a> · <a href="#mount-disk">Mount.Disk()</a></p>
FieldTypeDescription
BindstringHost path for bind mounts
NamedstringVolume name for named mounts
NamedModestringProvisioning mode for named mounts ("existing", "create", "ensure-exists")
NamedKindstringKind for provisioned named mounts ("dir" or "disk")
QuotaMiBuint32Quota in MiB for provisioned directory volumes
TmpfsboolSet for tmpfs mounts
DiskstringHost path for disk images
FormatstringDisk format
FstypestringInner filesystem type
ReadonlyboolWhether the mount is read-only
NoexecboolWhether direct execution from the mount is disabled
NosuidboolWhether setuid/setgid elevation from files on the mount is ignored
NodevboolWhether device files on the mount are ignored
SizeMiBuint32Size limit for tmpfs / capacity for provisioned disk volumes
StatVirtualizationStatVirtualizationPer-mount stat-virtualization policy (bind / named only)
HostPermissionsHostPermissionsPer-mount host-permission propagation policy (bind / named only)
MethodReturnsDescription
Kind()MountKindWhich mount flavour this is

MountKind

<div className="msb-tags"><span className="msb-tag is-type">type</span></div>
go
type MountKind uint8

Discriminates between the four mount flavours. Inspect via mount.Kind().

<p className="msb-backref">Returned by <a href="#mountconfig">MountConfig.Kind()</a></p>
ConstantDescription
MountKindBindHost bind mount
MountKindNamedNamed persistent volume
MountKindTmpfsIn-memory tmpfs
MountKindDiskHost disk image

MountOptions

<div className="msb-tags"><span className="msb-tag is-type">struct</span></div>

Tuning struct for Mount.Bind, Mount.Named, and Mount.NamedWith. StatVirtualization and HostPermissions are virtiofs-only and rejected at build time if combined with a tmpfs or disk-image mount; their zero values preserve the conservative defaults (strict, private).

<p className="msb-backref">Used by <a href="#mount-bind">Mount.Bind()</a> · <a href="#mount-named">Mount.Named()</a> · <a href="#mount-namedwith">Mount.NamedWith()</a></p>
FieldTypeDescription
ReadonlyboolMount as read-only; virtiofs-backed mounts also reject writes in the host filesystem server
NoexecboolPrevent direct execution from the mount
NosuidboolIgnore setuid and setgid privilege elevation from files on the mount
NodevboolIgnore device files on the mount
StatVirtualizationStatVirtualizationPer-mount stat-virtualization policy (virtiofs only)
HostPermissionsHostPermissionsPer-mount host-permission propagation policy (virtiofs only)

NamedVolumeOptions

<div className="msb-tags"><span className="msb-tag is-type">struct</span></div>

Tunes sandbox-time named volume provisioning for Mount.NamedWith.

<p className="msb-backref">Used by <a href="#mount-namedwith">Mount.NamedWith()</a></p>
FieldTypeDescription
Modestring"existing", "create", or "ensure-exists"; empty means "existing"
Kindstring"dir" or "disk"; empty means "dir"
SizeMiBuint32Disk capacity in MiB; required when creating or ensuring a missing disk volume
QuotaMiBuint32Directory volume quota in MiB

TmpfsOptions

<div className="msb-tags"><span className="msb-tag is-type">struct</span></div>

Tuning struct for Mount.Tmpfs.

<p className="msb-backref">Used by <a href="#mount-tmpfs">Mount.Tmpfs()</a></p>
FieldTypeDescription
SizeMiBuint32Maximum size in MiB
ReadonlyboolMount as read-only
NoexecboolPrevent direct execution from the mount
NosuidboolIgnore setuid and setgid privilege elevation from files on the mount
NodevboolIgnore device files on the mount

DiskOptions

<div className="msb-tags"><span className="msb-tag is-type">struct</span></div>

Tuning struct for Mount.Disk.

<p className="msb-backref">Used by <a href="#mount-disk">Mount.Disk()</a></p>
FieldTypeDescription
FormatstringFormat hint ("raw", "qcow2", "vmdk"). Optional; the runtime can usually probe
FstypestringInner filesystem type (e.g. "ext4", "xfs"). Optional; omitted means auto-detect
ReadonlyboolMount as read-only
NoexecboolPrevent direct execution from the mount
NosuidboolIgnore setuid and setgid privilege elevation from files on the mount
NodevboolIgnore device files on the mount