Guides - Manage Block Storage Volumes with the Linode API
Block Storage is a scalable, high-speed, and fault tolerant storage service used to add additional storage to a Linode Compute Instance.
The Linode API allows you to create, delete, attach, detach, clone, and resize Block Storage Volumes.
Before You Begin
You need a Personal Access Token for the Linode API to complete the steps in this guide. See Manage Personal Access Tokens for more information.
Store the token as a temporary shell variable to simplify repeated requests. Replace <Access Token> in this example with your token:
export TOKEN=<token-string>Create a Block Storage Volume
Create a new Block Storage Volume by making a POST request to the /volumes endpoint. You can also automatically attach the new Volume to an existing Compute Instance by passing the instance’s ID when creating the Volume.
List the Compute Instances on your account:
curl -H "Authorization: Bearer $token" \ https://api.linode.com/v4/linode/instancesChoose a Compute Instance from the returned list and copy its
idandregionvalues.Create a Volume in the same region as the target instance. Use the ID of the target instance and adjust the size (in GB), region, and label to the desired values:
curl -H "Content-Type: application/json" \ -H "Authorization: Bearer $token" \ -X POST -d '{ "label": "my-volume", "region": "us-east", "size": 100, "linode_id": 1234567 }' \ https://api.linode.com/v4/volumesNote The Volume and Compute Instance must be in the same region.Examine the response JSON object and copy the values in the
idandfilesystem_pathfields:1 2 3 4 5 6 7 8 9 10 11{ "linode_id":1234567, "label":"my-volume", "size":100, "updated":"2018-05-07T14:59:48", "created":"2018-05-07T14:59:48", "id":6830, "status":"creating", "region":"us-east", "filesystem_path":"/dev/disk/by-id/scsi-0Linode_Volume_my-volume" }
Query the Volume using the
/volumes/$volume_idendpoint to make sure it was successfully created:curl -H "Authorization: Bearer $token" \ https://api.linode.com/v4/volumes/$volume_idIf the
statusfield in the response isactive, your Volume is ready to use.
Mount the Volume
The API can’t directly mount the new Volume after it is attached. SSH into the Linode and mount it manually:
Create a filesystem on the Volume:
mkfs.ext4 $volume_pathCreate a mountpoint:
mkdir /mnt/my-volumeMount the Volume:
mount $volume_path /mnt/my-volumeTo automatically mount the Volume every time your Compute Instance boots, add the following line to your
/etc/fstabfile:- File: /etc/fstab
1$volume_path /mnt/my-volume defaults 0 2
Attach and Detach the Volume
If you did not specify a Compute Instance when creating the Volume, or would like to attach it to a different instance, use the /attach and /detach endpoints:
Detach the Volume. Replace
$volume_idwith the Volume ID from the previous section:curl -H "Authorization: Bearer $token" \ -X POST \ https://api.linode.com/v4/volumes/$volume_id/detachAttach the Volume to the new target Compute Instance:
curl -H "Authorization: Bearer $token" \ -H "Content-Type: application/json" \ -X POST -d \ '{ "linode_id": $linode-id }' \ https://api.linode.com/v4/volumes/$volume_id/attachNote If a Compute Instance is not running and has more than one configuration profile, include aconfig_idparameter in the POST request to specify which profile to use. If you do not specify a profile, the first profile will be used by default.
Clone a Volume
To copy all of the data in a Block Storage Volume to a new Volume:
curl -H "Authorization: Bearer $token" \
-X POST -d '{
"label": "new-volume"
}' \
https://api.linode.com/v4/volumes/$volume_id/cloneDelete a Volume
Remove a Volume from your account with a DELETE request. If the Volume is attached to a Compute Instance, you will have to detach it before it can be deleted:
curl -H "Authorization: Bearer $token" \
-X DELETE \
https://api.linode.com/v4/volumes/$volume_idResize a Volume
If you need additional space, you can increase the size of a Volume through the API. It is not possible to reduce the size of a Volume.
Pass the desired size (in gigabytes) using the size parameter:
curl -H "Content-Type: application/json" \
-H "Authorization: Bearer $token" \
-X POST -d '{
"size": 200
}' \
https://api.linode.com/v4/volumes/$volume_id/resizeMore Information
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This page was originally published on