Volumes
A volume is a persistent storage device that keeps data across restarts and even redeployments.
This documents presents a guide where you will create a volume, attach it to a web server instance, and write to it. Then you will detach it, remove that instance, attach it to a new instance, and confirm the data persisted.
Then, it presents some features of volumes.
Volume workflow
You can create an instance with attached volumes in the same call - see the volumes field for the POST /instances endpoint.
In this case, the volume's lifetime is tied to the instance - when you delete the instance, the volume disappears.
Setting up the volume
To start, create the volume with the CLI, with size in MBs and name my-volume:
The command should return the volume's UUID, and you can check the operation worked via:
which should output something like:
The ATTACHED TO field is empty because the platform hasn't attached it to any instance yet.
Populating the volume with local data (optional)
To populate an empty volume with local data, use the legacy CLI volume import command.
For example, assuming the volume's name is my-volume and that the data you want to import are in your my-data directory, you would run:
You should see output like:
Setting up the web server
Use a Flask web server to write to the volume:
Replace the contents of server.pywith:
server.py
On every request, this simple server will write a timestamp to a file on the mounted persistent volume and print out the current contents of the file.
Start the Flask web server, create a
service for it via the publish flag, and mount the my-volume volume at /mnt:
You should see output like:
To confirm that the platform attached the volume, run:
You should see output like:
Testing the server
The Flask server writes the time and date to /mnt/log.txt for each request.
Test it by running curl several times.
Example output:
To test data persistence, first stop the instance, detach the volume, and remove the instance:
The explicit volume detach command is only available in the legacy CLI.
Now start another instance and, like before, mount the same volume:
This should output something like:
Run one final curl to this new address.
You should see the previous contents plus a new entry at the bottom:
Cleaning up
To clean up, first detach the volume from all instances and then remove it:
The explicit volume detach command is only available in the legacy CLI.
Volume templates
A volume template is a volume in the TEMPLATE state.
Once a volume transitions to template state, it's immutable: you can clone it but not write to it or delete it while active clones exist.
The platform creates volume templates automatically when you convert an instance into an instance template: the platform converts all volumes attached to that instance into volume templates as an atomic operation.
Volume templates have their own create, read, update, and delete endpoints at /volumes/templates.
They support delete locks and tags.
Volume cloning
A volume clone is an independent copy of an existing volume.
The clone operation is asynchronous: the platform creates the new volume immediately in a pending state, and the data copy completes in the background.
To clone one or more volumes, you can use the POST /volumes/clone endpoint.
The platform also clones volumes implicitly when you clone an instance or create one from a template. It clones each attached volume and attaches the clone to the new instance.
You can't delete a source volume or template while it has active clones.
Shared volumes
More than one instance can mount the same volume simultaneously as long as all mounts are in read-only mode. Read-only sharing is always permitted regardless of how many other instances have the volume mounted.
A read-write mount is exclusive. You can't add one while any other mount exists on that volume, and a volume can't have two read-write mounts at once.
Specify readonly: true when attaching a volume at instance creation or via the attach endpoint:
POST /instances
Custom filesystems
By default, volumes use a platform-configured filesystem (typically ext4 for block volumes and virtiofs for hosted volumes).
If the platform operator has registered more named filesystems, you can select one by passing its name in the filesystem field when creating a volume:
POST /volumes
Managed volumes
This is an advanced feature that's only available on the self-hosted platform. Quotas aren't yet enforced by the platform for managed volumes, so use them with caution to avoid filling up your host's disk.
A managed volume points to an existing directory on the host rather than a platform-allocated storage file.
Create a managed volume with a host_path field instead of size_mb:
POST /volumes
host_pathmust be an absolute, normalised path to an existing directory on the host (no.,.., or:components).uidandgidset the ownership used when accessing the volume from the guest. Both default to0if not specified.size_mbandtemplateare mutually exclusive withhost_path.- The platform doesn't create, format, resize, or delete the backing directory for managed volumes.
Learn more
- The CLI reference and the legacy CLI reference.
- Unikraft Cloud's REST API reference, in particular the section on volumes.