Volumes
A volume is a disk that belongs to your team, not to a sandbox. Mount it when you create a sandbox, and read or write its files directly from your program, with no sandbox running.
from svm import Sandbox, Volume
volume = Volume.create("docs-data", size_gb=1)try: # Read and write files with no sandbox running. volume.write("/notes/hello.txt", "written from outside\n") print(volume.read_text("/notes/hello.txt"))
# A read-write mount is single-attach: one sandbox at a time. with Sandbox.create(template="base", volume_mounts=[volume.mount("/mnt/data")]) as sandbox: sandbox.commands.run("echo 'written in a sandbox' > /mnt/data/notes/sandbox.txt") print(sandbox.commands.run("ls /mnt/data/notes").stdout)
for entry in volume.list_files("/notes"): print(entry["name"], entry["kind"], entry["size"])finally: volume.delete()import { Sandbox, Volume } from "@svm/sandbox";
const volume = await Volume.create("docs-data", 1); // 1 GBtry { // Read and write without a sandbox. await volume.write("/inputs/hello.txt", "written from outside"); console.log(await volume.readText("/inputs/hello.txt"));
// A read-write mount attaches the volume to one sandbox at a time. const sandbox = await Sandbox.create({ template: "base", volumeMounts: [{ ...volume.mount("/data") }] }); try { await sandbox.commands.run("echo from the sandbox > /data/inputs/reply.txt"); } finally { await sandbox.kill(); }
for (const entry of await volume.listFiles("/inputs")) { console.log(entry.kind, entry.name, entry.size); }} finally { await volume.delete();}volume, err := client.Volumes().Create(ctx, "docs-data", 1, "") // 1 GBif err != nil { return err}defer volume.Delete(ctx)
if _, err := volume.Write(ctx, "/notes/hello.txt", []byte("written from outside")); err != nil { return err}text, err := volume.ReadText(ctx, "/notes/hello.txt")if err != nil { return err}fmt.Println(text)
// A read-write mount is single-attach: one sandbox at a time.sandbox, err := client.CreateSandbox(ctx, svm.CreateSandboxOptions{ Template: "base", VolumeMounts: []map[string]any{volume.Mount("/data", "", false)},})if err != nil { return err}defer sandbox.Kill(ctx)commands, err := sandbox.Commands()if err != nil { return err}if _, err := commands.Run(ctx, "echo from the sandbox > /data/notes/sandbox.txt", svm.RunOptions{}); err != nil { return err}
entries, err := volume.ListFiles(ctx, "/notes")if err != nil { return err}for _, entry := range entries { fmt.Println(entry.Name, entry.Kind, entry.Size)}require "svm"
volume = SVM::Volume.create("datasets", size_gb: 10)begin # Files on a volume can be written and read without any sandbox running. volume.write("/raw/hello.txt", "hello from outside") puts volume.read_text("/raw/hello.txt")
# A read-write mount is single-attach: one sandbox at a time. box = SVM::Sandbox.create(template: "base", volume_mounts: [volume.mount("/data")]) begin box.commands.run("echo written inside > /data/raw/inside.txt") ensure box.kill end
volume.list_files("/raw").each { |entry| puts "#{entry['kind']} #{entry['name']} #{entry['size']}" }ensure volume.deleteendimport dev.svm.sandbox.Client;import dev.svm.sandbox.Sandbox;import dev.svm.sandbox.Volume;import java.util.List;
public class VolumesExample { public static void main(String[] args) { Client client = Client.fromEnvironment(); Volume volume = Volume.create(client, "docs-data", 1); // 1 GB try { volume.write("/config.json", "{\"debug\": true}\n"); // no sandbox needed System.out.print(volume.readText("/config.json"));
Sandbox.CreateOptions options = new Sandbox.CreateOptions(); options.volumeMounts = List.of(volume.mount("/mnt/data")); // read-write: one sandbox at a time options.wait = true; try (Sandbox sandbox = Sandbox.create(client, options)) { sandbox.commands().run("echo from the sandbox > /mnt/data/hello.txt"); }
for (Volume.Entry entry : volume.listFiles()) { System.out.println(entry.name + " " + entry.size); } } finally { volume.delete(); } }}From the CLI:
svm volume create data --size-gb 10svm sandbox create --template base --volume <volume-id>:/datasvm volume put <volume-id> ./notes.txt /notes.txtsvm volume cat <volume-id> /notes.txt