Skip to content

Volumes

This page describes volumes: storage that stays when a sandbox goes. It is for a contributor who must change how the daemon creates, lists, deletes or mounts a volume, or how the model cache works.

The code is in internal/sandbox/volume.go. The create path in internal/sandbox/manager.go calls it. The engine side is in internal/engine/docker.go. Read manager.md first for the records and the lock m.mu.

The files of a sandbox go when the daemon kills the sandbox. A volume is a directory that the engine keeps separate from all containers. A sandbox mounts a volume at a path. What the sandbox writes there stays for the next sandbox that mounts the volume.

The design has three decisions:

  • The engine keeps the volumes. The daemon keeps no list of volumes and no record for a volume. The cost: each list and each lookup is an engine call. The benefit: a lost state file loses no volume.
  • A volume is never a path on the host. The daemon gives the engine the name of a volume, never a directory of the host. A client cannot use a mount to reach a file of the host.
  • Only volumes that boxfork made are visible. The daemon does not list, mount or delete a volume that a different program made.

A volume has no size limit. The engine stores it on the disk of the host.

One boxfork volume is one engine volume with a fixed name and two labels.

Property Value Constant
Engine name boxfork-vol-<name> volumePrefix
Label boxfork.managed=true LabelManaged
Label boxfork.volume=<name> LabelVolume

The prefix keeps the volumes of boxfork apart from the other volumes on the machine. The labels prove that boxfork made the volume. The daemon examines all three properties each time.

Two functions do this check:

  • engineVolume looks up one volume by name. It returns ErrVolumeNotFound if the engine has no such volume. It returns the same error if the label boxfork.managed is not true or the label boxfork.volume is not the name.
  • ownVolumes lists the volumes. It asks the engine for the volumes with the label boxfork.managed=true. Then it does the check again for each volume: the label, a valid name in boxfork.volume, and the engine name boxfork-vol-<name>.

ownVolumes does not trust the filter of the engine. Docker.Volumes also examines the label of each volume that the engine returns.

Type Fields Use
Mount Name, Path, ReadOnly (JSON: name, path, read_only) One volume at one path in one sandbox. It is in the create request, in Sandbox.Volumes and in the state file.
Volume Name, CreatedAt, Sandboxes, DefaultPath (JSON: name, created_at, sandboxes, default_path) The answer of the API for one volume. The daemon calculates it for each request.
engine.Mount Volume, Path, ReadOnly The mount as the engine gets it. Volume is the engine name.
engine.Volume Name, Labels, CreatedAt A volume as the engine reports it.

There is no volRecord. The fields volMu, volPending and volDeleting of Manager are the only volume data in the memory of the daemon.

volumeName is the regular expression ^[a-z0-9][a-z0-9_.-]{0,62}$:

  • lower-case letters, digits, ., _ and -;
  • the first character is a letter or a digit;
  • a maximum of 63 characters.

CreateVolume, DeleteVolume and checkMounts all apply the rule before they call the engine. A name that does not match never becomes part of an engine name or of a URL of the engine API.

checkMounts examines the full set of mounts of one sandbox and returns the set sorted by path. The sort puts a mount in a directory after the mount of that directory. Each failure is an InvalidError.

Rule Detail
A maximum of MaxMounts (16) mounts The default mounts of the host are in the count.
The name matches volumeName
The path is absolute and clean It starts with /, and path.Clean does not change it. Thus no .., no // and no / at the end.
The path does not contain :, ,, a NUL byte or a new line
The path is not / A volume cannot cover the full sandbox.
The path is not at or below /.boxfork, /proc, /sys or /dev noMountUnder. The helper lives in /.boxfork. The kernel owns the other three.
No two mounts have the same path

One sandbox can mount the same volume at two different paths. A path that only starts with the same letters as a forbidden path, for example /devices, is permitted.

ParseMount reads the text form of the command line: name:/path, name:/path:ro or name:/path:rw. It then calls checkMounts for the one mount.

Manager.CreateVolume(ctx, name) makes an empty volume. The API route is POST /v1/volumes with the body {"name": "..."}.

  1. It examines the name. A wrong name is an InvalidError.
  2. It takes volMu. Only one creation or deletion runs at a time.
  3. If MaxVolumes is larger than zero, createVolume counts the volumes with ownVolumes. At the limit, it returns a LimitError.
  4. It calls Engine.CreateVolume with the engine name and the two labels.
  5. If the engine reports ErrExists, it returns an ExistsError.
  6. It returns the Volume with the creation time of the engine and an empty list of sandboxes.

Step 5 has two messages. createVolume calls engineVolume to find out whose volume has the name. If the volume is not one of boxfork, the message tells the client to select a different name.

volMu is necessary for two reasons. The limit is a count on the engine, and two creations at the same moment can both be below it. And the engine answers a second create of a name with the first volume, as if it made a new one. Docker.CreateVolume thus looks up the name first, and volMu keeps two creations apart.

Manager.ListVolumes(ctx) returns the volumes sorted by name. The API route is GET /v1/volumes.

  1. It calls ownVolumes. No lock is held during this engine call.
  2. Under m.mu, volumeUsers makes a map from each volume name to the sandboxes that mount it.
  3. For each volume, it leaves out the sandboxes that are hidden.
  4. It sets DefaultPath if the volume is a default mount of the host.

volumeUsers reads the field Volumes of each record in boxes. A suspended sandbox is thus in the list. A sandbox that mounts one volume two times is in the list one time. A waiting sandbox, a copy of a fork in progress and a sandbox in removal are hidden. The list does not name them.

Manager.DeleteVolume(ctx, name) deletes a volume and all its files. The API route is DELETE /v1/volumes/{name}. It refuses while a sandbox mounts the volume. A sandbox that loses its volume gets an empty directory, or cannot wake.

  1. A name that does not match volumeName returns ErrVolumeNotFound.
  2. It takes volMu.
  3. engineVolume makes sure that the volume exists and that boxfork made it.
  4. If the volume is a default mount of the host, it returns an InUseError.
  5. Under m.mu, it reads the users of the volume and volPending for the volume.
  6. If there is a user or a create in progress, it returns an InUseError.
  7. In the same hold of m.mu, it sets volDeleting for the name.
  8. It calls Engine.RemoveVolume.
  9. A deferred function clears volDeleting.

The InUseError of step 6 has three messages:

Case Message
A client can see one or more of the sandboxes It names them and tells the client to kill them.
Only hidden sandboxes mount the volume “is mounted by a sandbox that is being made or removed; try again in a moment”
No sandbox mounts it, but a create is in progress “is being mounted by a sandbox that is starting”

The message names only sandboxes that the client can see and kill.

Step 8 can fail in three ways:

Engine answer Result
ErrNotFound ErrVolumeNotFound. The volume went away between step 3 and step 8.
ErrInUse An InUseError: “still held by a container that is being removed”. No sandbox has the volume, thus the holder is a container on its way out, or a container that a person started by hand.
A different error An error with the text “deleting volume”. The volume stays usable.

To delete a default volume, start the daemon without that --volume flag first.

A volume must exist before a sandbox mounts it. The daemon does not create a volume as a side effect of a create.

flowchart TD
A[Create request with volumes] --> B[withDefaults adds the default mounts]
B --> C[checkMounts: names, paths, count, sort]
C --> D[holdVolumes under m.mu]
D -- a deletion is in progress --> X1[ErrVolumeNotFound]
D --> E[boot: engineMounts looks up each volume]
E -- a volume is absent or not from boxfork --> X2[ErrVolumeNotFound]
E --> F[Engine.Create with the mounts]
F --> G[guest own for the writable mounts]
G --> H[the record with Volumes is saved]
H --> I[releaseVolumes]
  1. Create refuses a request with more than MaxMounts volumes.
  2. withDefaults adds each default mount whose path the request does not use.
  3. checkMounts examines the full set.
  4. Under m.mu, holdVolumes refuses a volume that is in volDeleting. Then it adds 1 to volPending for each mount.
  5. boot runs. See the next section.
  6. Create adds the record, with the sorted mounts in Volumes, and writes the state file.
  7. A deferred function calls releaseVolumes, which subtracts 1 from volPending for each mount.

If the request and the defaults together are more than MaxMounts, the error message gives the numbers. It says how many mounts the host adds and how many are left for the request.

volPending covers the time between step 4 and step 6. In that time the sandbox is not in boxes, and volumeUsers cannot find it. After step 6 the record itself protects the volume.

engineMounts calls engineVolume for each mount. If one volume is absent, boot fails with ErrVolumeNotFound before it creates a container. The reason is the behavior of an engine. An engine that must mount a volume that it does not have makes an empty volume with that name. It reports no error. The sandbox then finds an empty directory where its data was.

engineMounts returns one engine.Mount for each mount, with the engine name boxfork-vol-<name>.

Docker.Create in internal/engine/docker.go writes each mount into HostConfig.Mounts of the engine API:

Key Value Reason
Type volume Never bind. The source is the name of a volume and cannot be a path of the host.
Source boxfork-vol-<name>
Target The path in the sandbox
ReadOnly From Mount.ReadOnly
VolumeOptions.NoCopy true The engine does not copy the files of the image at that path into the volume. A volume contains only what a sandbox wrote.

See engine.md for the full CreateSpec.

A new volume is an empty directory that belongs to root. An image whose commands run as a different user cannot write to it. After boot started the container and the helper answered, giveVolumes handles this.

giveVolumes collects the paths of the mounts that are not read-only. It runs /.boxfork/guest own <paths> in the container as the user 0. The helper code is in internal/guest/own.go. See also helper.md.

  • The helper reads the owner of /proc/1. That is the user of the first process and of each command.
  • If that user is root, the helper answers KEPT and changes nothing.
  • For each path, the helper changes the owner only if the directory is empty and belongs to root.
  • The helper answers OK <count> or ERR <reason>.

A volume that contains a file keeps its owner. Thus the first user that fills a volume owns it. A sandbox whose image runs as a different user can find that it cannot write there.

A failure of giveVolumes does not fail the create. The daemon writes a warning with the sandbox ID. boot runs giveVolumes each time: at a create, for each copy of a fork, and at a wake.

An image can declare volumes of its own (VOLUME in a Dockerfile). The engine makes a separate volume for each such path and each container. A commit does not contain those files, and they go with the container. Docker.Remove asks the engine to remove them with the container (v=true).

unsaved compares the volume paths of the image with the mounts of the sandbox. A path of the image with no mount at exactly that path is an unsaved path. The record lists the unsaved paths in UnsavedPaths. A sandbox with an unsaved path is never suspended. See suspend.md.

A mount of an boxfork volume at exactly the path of an image volume covers it. The files there are then on the boxfork volume. unsaved compares with path.Clean, thus an image path with / at the end matches. A mount at a directory above the image volume does not cover it.

boxfork serve --volume name:/path[:ro] gives a mount to each new sandbox. The flag can occur more than one time. The values go to Config.DefaultVolumes. The model cache is a default volume with :ro:

Terminal window
boxfork serve --volume models:/models:ro

Each sandbox then finds the same directory /models, read-only. Model files are on the host one time, not one time for each sandbox.

ensureDefaultVolumes runs at the start of New, before New reads the state file.

  1. checkMounts examines the default mounts as it examines the mounts of a sandbox.
  2. For each default, engineVolume looks for the volume.
  3. If the volume exists, the daemon keeps it and does not change its files.
  4. If the engine answers “not found”, createVolume makes the volume.
  5. If the engine fails for a different reason, New returns the error.

The daemon does not start in these cases:

  • A default mount has a wrong name or a wrong path.
  • The engine cannot tell if a default volume exists. A wrong guess of “not there” makes a new, empty volume.
  • The engine name of a default is in use by a volume that boxfork did not make.
  • MaxVolumes does not permit one more volume.

withDefaults adds each default mount to the mounts of the request. If the request has a mount at the path of a default, the mount of the request wins. withDefaults then leaves out the default.

This rule is the way to fill the model cache. A sandbox that mounts models:/models with no :ro gets the volume writable at the same path. It downloads the model files, and each later sandbox reads them.

The rule also means that the read-only setting is a default and not a protection against a client. Each client that has the API key can mount the same volume writable.

  • DeleteVolume refuses a default volume with an InUseError, also when no sandbox exists.
  • ListVolumes gives the path of the default in default_path.
  • The default mounts are in the count of MaxMounts.
  • The default mounts are part of the setup mark. See “Pools” below.

Kill removes the container. The volume and its files stay. The daemon never deletes a volume by itself.

A snapshot does not contain the files of a mounted volume. The engine commit saves only the files of the container itself. snapshot calls excluded, which joins the unsaved paths and the mount paths and sorts them. The result is the field ExcludedPaths of the snapshot (excluded_paths in the API).

A sandbox that a client creates from a snapshot does not get the mounts of the first sandbox. It gets the mounts of its own create request and the default mounts.

A copy mounts the same volumes as its original, at the same paths and with the same read-only setting. Fork copies rec.Volumes into the create request of each copy and sets volumesFinal.

  • The volumes are the same volumes, not copies. What one sandbox writes there, the others see.
  • volumesFinal stops withDefaults. A copy does not get a default that the host got after the original started. Such a default can cover what the original has at that path.
  • An original with 16 mounts can thus fork, also when the host now has more defaults.

The daemon does not coordinate writes of more than one sandbox to one volume.

A suspended sandbox has no container, thus the engine does not know that the sandbox needs its volumes. The record keeps Volumes, and volumeUsers reads it. DeleteVolume refuses the volume.

At a wake, boot calls engineMounts again. If a person deleted a volume with the tools of the engine, the wake fails. wakeLocked changes ErrVolumeNotFound to a StuckError. The API answers 409 sandbox_stuck. The sandbox stays suspended with its files. It can wake after someone makes a volume with that name, which is empty, or a client can kill it.

A PoolRequest has no volumes. A waiting sandbox mounts the default volumes of the host and no others.

  • take gives no waiting sandbox to a create request that has volumes, or that has volumesFinal. That create starts a new container.
  • A create request with no volumes can get a waiting sandbox. That sandbox has the default mounts.
  • setupOf puts the default mounts into the setup mark. After a restart with different --volume flags, drainPools replaces the waiting sandboxes.

See pools.md.

The mounts of each sandbox are in its record in the state file. New does not look at the volumes of the sandboxes. If the state file is lost, the volumes stay on the engine, and ListVolumes shows them with no sandboxes.

Race Guard
Two clients create the same name at the same moment. volMu. The second create gets an ExistsError.
Many creates go past MaxVolumes together. volMu. Each create counts on the engine while it holds the lock.
A delete comes while a sandbox that mounts the volume starts. volPending is larger than zero. The delete returns an InUseError.
A create comes while the engine deletes the volume. holdVolumes finds the name in volDeleting and returns ErrVolumeNotFound.
A create that failed still counts as a user. The deferred releaseVolumes in Create runs on each return.
A delete that failed leaves volDeleting set. The deferred function in DeleteVolume clears it on each return.
A volume goes away between the check and the mount. engineMounts runs in boot immediately before Engine.Create. The time between them is not zero.

DeleteVolume reads the users and sets volDeleting in one hold of m.mu. holdVolumes reads volDeleting and changes volPending in one hold of m.mu. One of the two always sees the other.

Failure Result HTTP answer
A wrong volume name in a create of a volume or of a sandbox InvalidError 400 invalid_request
A wrong mount path, or two mounts at one path InvalidError 400 invalid_request
More than 16 mounts InvalidError 400 invalid_request
The host is at MaxVolumes LimitError 429 limit_reached
The name exists ExistsError 409 already_exists
A sandbox names a volume that does not exist ErrVolumeNotFound from engineMounts. No container stays. 404 volume_not_found
A sandbox names a volume that a different program made ErrVolumeNotFound 404 volume_not_found
A delete of a volume that a sandbox mounts InUseError 409 in_use
A delete of a default volume InUseError 409 in_use
A delete of a volume that a leftover container holds InUseError 409 in_use
The engine cannot list the volumes An error “listing the volumes” or “counting the volumes” 500 internal
The helper cannot give a new volume to the user of the sandbox The create succeeds. The daemon writes a warning. 201
A suspended sandbox whose volume is gone StuckError at each call that wakes 409 sandbox_stuck
A default volume cannot be made at the start New returns the error. The daemon does not start.
  1. The daemon gives the engine only the name of a volume, never a path of the host. The mount type is always volume.
  2. Each engine name of a volume starts with boxfork-vol- and comes from a name that matches volumeName.
  3. The daemon lists, mounts and deletes only a volume with the correct name and both labels.
  4. boot looks up each volume before Engine.Create. The engine never makes a volume silently.
  5. The daemon does not delete a volume while a record in boxes names it or volPending counts it.
  6. The daemon deletes a volume only on a DeleteVolume call from a client.
  7. holdVolumes and releaseVolumes always come in pairs.
  8. The mounts in a record are sorted by path, and no two mounts have the same path.
  9. A copy of a fork mounts exactly the volumes of its original.
  10. No function calls the engine while it holds m.mu. volMu can be held during an engine call.
  11. Take volMu before m.mu, never in the opposite sequence.

Add a field to a mount, for example an option

Section titled “Add a field to a mount, for example an option”
  1. Add the field with a JSON tag to Mount in volume.go.
  2. Examine the field in checkMounts.
  3. Add the field to engine.Mount and send it to the engine in Docker.Create.
  4. Record it in the test engine in enginetest/fake.go, to let a test read it.
  5. If the text form needs it, read it in ParseMount.
  6. Remember that the state file contains Mount. Decide what an old record with no such field means.
  7. Remember that a fork copies the mounts and a wake uses them again.
  8. Add cases to TestVolumeNamesAndMountPathsAreChecked and TestParseMount.
  9. Add a case to TestCreateSendsMountsAsVolumesNeverAsHostPaths in internal/engine/docker_test.go.
  1. Change volumeName or checkMounts.
  2. Make sure that each name stays safe in a URL of the engine API.
  3. Do not permit a path at or below /.boxfork. The helper is there.
  4. Decide what happens to a volume or a record that the old rule permitted.
  5. Change the error message. It is the only documentation that a client sees at that moment.
  6. Add the new cases to TestVolumeNamesAndMountPathsAreChecked.
  1. Examine the name with volumeName first.
  2. Take volMu if the operation creates, deletes or changes a volume.
  3. Use engineVolume to find the volume. Do not call Engine.Volume directly.
  4. If the operation must not run while a sandbox uses the volume, read volumeUsers and volPending under m.mu.
  5. Release m.mu before you call the engine.
  6. Return ErrVolumeNotFound, an InUseError or an ExistsError. The API has an answer for each.
  7. If the engine needs a new call, add it to the Engine interface, to Docker and to the test engine.
  8. Add the route in internal/api/server.go and a test in internal/api/volume_test.go.
  1. Change withDefaults for the rule of each create.
  2. Change ensureDefaultVolumes for the rule at the start.
  3. Keep the default mounts in setupOf. If you do not, a waiting sandbox keeps old mounts after a restart.
  4. Keep volumesFinal for a fork.
  5. Add cases to TestDefaultVolumes and TestDefaultVolumesAtStartup.

The unit tests use the test engine, which keeps volumes in memory. A test can read the mounts that the manager gave to the test engine. The test engine answers guest own itself. It does not change the owner of a real directory.

File Content
internal/sandbox/volume_test.go The main tests of volumes
internal/sandbox/review5_test.go giveVolumes, the mounts of a copy, hidden sandboxes in a delete
internal/sandbox/faults_test.go TestAnImageThatKeepsFilesInAVolume
internal/sandbox/pool_test.go TestWaitingSandboxesWithoutTheHostsSharedVolumesAreReplaced
internal/guest/own_test.go The own command of the helper
internal/engine/docker_test.go TestCreateSendsMountsAsVolumesNeverAsHostPaths, TestVolumes
internal/api/volume_test.go TestVolumesOverTheAPI: the routes and the status codes
cmd/boxfork/cli_test.go TestCLIVolumes and the error messages of the command line
sdk/python/tests/test_sdk.py test_volumes, test_an_image_that_keeps_files_in_a_volume

The most important tests:

Test What it proves
TestAVolumeOutlivesItsSandboxes The engine name and labels, the files stay after a kill, the mounts stay after a restart.
TestVolumeNamesAndMountPathsAreChecked Each rule of volumeName and checkMounts.
TestOnlyBoxforksOwnVolumesCanBeMounted engineVolume and ownVolumes refuse the volumes of other programs, also with an engine that ignores the filter.
TestAVolumeInUseIsNotDeleted DeleteVolume refuses while a sandbox, also a paused one, mounts the volume.
TestASuspendedSandboxKeepsItsVolume A suspended sandbox is a user of its volume.
TestAVolumeThatVanishedIsNotQuietlyReplaced engineMounts and the StuckError at a wake.
TestCopiesAndSnapshotsWithVolumes The mounts of a copy and the ExcludedPaths of a snapshot.
TestAnImagesOwnVolumeCoveredByOneOfOurs unsaved and the exact path match.
TestDefaultVolumes, TestDefaultVolumesAtStartup withDefaults and ensureDefaultVolumes.
TestMaxVolumes The limit, also with many creates at the same time.
TestAVolumeIsNotDeletedFromUnderASandboxThatIsStarting volPending.
TestAVolumeBeingDeletedIsNotMounted, TestNoSandboxTakesUpAVolumeWhileTheEngineDeletesIt volDeleting.
TestALeftoverContainerHoldingAVolume The ErrInUse and ErrNotFound answers of Engine.RemoveVolume.
TestWhatAListOfVolumesSays The sort of the list, the sort of the mounts, hidden sandboxes.
TestANewVolumeIsHandedToTheSandbox giveVolumes at a create, at a wake and for a copy, and the warning on a failure.
TestACopyDoesNotTakeUpTheHostsNewerDefaults volumesFinal, and the message for too many mounts.
TestAVolumeHeldByASandboxNobodyCanSee The three messages of the InUseError.
  • A volume has no size limit. One sandbox can fill the disk of the host through a writable volume.
  • The daemon does not delete a volume by itself. A volume stays until a client deletes it.
  • The read-only model cache is a default, not a protection. A client with the API key can mount the same volume writable.
  • More than one sandbox can write to one volume at the same time. The daemon does not lock files.
  • The first user that writes to a volume owns it. A sandbox that runs as a different user can fail to write there.
  • The tools that a model gets through the MCP server and the tool kit cannot make or mount a volume. README.md says this. This page did not examine that code.
  • Each list of volumes and each create with mounts calls the engine for each volume. There is no cache.
  • A client cannot rename a volume, copy a volume or read its size.
  • The unit tests do not use a real engine volume. The test engine keeps volumes in memory.
  • No proof script in scripts/proof tests a volume.
  • README.md says that the model cache example ran with huggingface_hub 2.6.0 on real containers. The repository has no script that repeats it.
  • No unit test changes the owner of a real directory. own_test.go tests the decision of the helper with a prepared system.
  • No test shows what a real engine does with NoCopy, with a mount in a mount, or with v=true at a removal. The tests of docker.go examine only the requests that the daemon sends.
  • No test shows the behavior of volumes on Podman or under gVisor.
  • Nobody ran this code on x86_64, on NVIDIA GPU hardware, or on a real AWS or Google Cloud account.
  • The runs on a real engine were on Docker with gVisor on arm64, in a nested virtual machine, and on Podman.