An Introduction to DigitalOcean Block Storage
DigitalOcean's Block Storage allows you to create and attach additional storage volumes to your DigitalOcean Droplets. Volumes are an independent resource that can be moved from one Droplet to another within the same datacenter. Attached volumes function like locally connected storage drives, allowing you to manage them using familiar tools and techniques.
Block Storage is useful when you need more space but do not require the additional processing power or memory that a larger Droplet would provide. Volumes can be created, destroyed, or expanded as your needs change, simplifying initial planning around your storage requirements.
Since Block Storage is not a specialized resource, it can be used for just about anything that would benefit from additional disk space. Examples include:
- As the document root or media uploads directory for a web server
- To house database files for a database server
- As a target location for backups
- As expanded storage for personal file hosting platforms like ownCloud
- As components for building more advanced storage solutions, like RAID arrays
With these points in mind, let's take a look at how to use DigitalOcean Block Storage.
Block Storage Volumes at a Glance
- Price: Volumes are charged at a rate of $0.10 per GB per month. For example, if you created a 100GB volume, you would be charged an additional $10 per month. A volume is billed in hourly increments from the moment it is created until the time of deletion, just like Droplets.
- Storage media: Like Droplets, block storage volumes are backed by SSDs.
- Encryption: Block Storage volumes are encrypted using the LUKS (Linux Unified Key Setup) specification. The entire storage cluster is encrypted, which ensures that snapshots of volumes are also encrypted at rest.
- Redundancy: Block Storage stores data on hardware that is separated from the Droplet and replicated multiple times across different racks, reducing the chances of data loss in the event of hardware failure.
- Available sizes: Storage volumes can be created in sizes ranging from 1GB to 16TB.
Region Support: Currently, volumes are available in NYC1, NYC3, SFO2, FRA1, SGP1, TOR1, BLR1, LON1, and AMS3. Volumes can be moved freely between Droplets within a region and migrated to new regions using snapshots.
- A volume can only be attached to one Droplet at a time. However, up to seven volumes can be attached to a single Droplet.
- Volumes are not included in Droplet backups.
Note: The NYC3 region contains both block storage-capable hardware and legacy hardware that does not yet support attaching block storage volumes. If your Droplet is placed on legacy hardware, you will be unable to attach a volume without migrating to a new server.
To guarantee that your Droplet will be able to use block storage in NYC3, attach a volume during the Droplet creation process. If you don't need it right away, you can detach or destroy it and add a volume later.
Creating and Attaching New Volumes
You can create and attach new volumes in four main contexts:
When Creating New Droplets
You can create and attach a new volume or attach an existing volume at the same time you create a Droplet in the Add Block Storage section. When you choose to add a volume, the selection of regions for the Droplet will be automatically limited to those regions where block storage is available.
From the Create Menu
You can create a new volume and attach it to an existing Droplet at any time from the Create menu.
On Existing Droplets
When you've already created a Droplet, if it is located in a region with block storage support, you can create and manage volumes on its Volumes page
From the Droplets Page
You can manage all the volumes associated with your account by following the Droplets link in the main navigation and selecting the Volumes tab:
No matter how you create a new volume, before it's ready for use, you'll need to format and mount it.
Formatting and Mounting Volumes
Directions to prepare a volume for use are available in its More menu under Config instructions.
The customized directions have three parts that are ready to cut and paste into a terminal from your local machine:
SSH to your Droplet: The SSH command with the Droplet's IP address filled in. Note that if you've disabled root logins or are working with CoreOS, FreeBSD, or RancherOS, you'll need to substitute the appropriate user for
rootin the SSH command.
Format the volume: One time only: Directions for an
ext4filesystem with the SCSI ID of the volume filled in. Generally, this is a one-time-only task; when you format a volume it destroys all existing data.
If you prefer, you can choose to use a different file system. How To Partition and Format DigitalOcean Block Storage Volumes in Linux and How To Partition and Format Storage Devices in Linux can help you. Note that DigitalOcean volumes have better performance and are easier to move and resize if they are not partitioned.
Mount the volume: Directions to create a mount point, mount the volume, and set the volume to automatically mount when you reboot with the SCSI ID of the volume filled in.
Once these steps are complete, the volume will be ready to use.
When resizing or detaching a volume, unmounting it first can protect data integrity by ensuring the volume isn't being actively written to. To unmount a volume:
df to list mount points if you need to:
- sudo df --human-readable --print-type
The volume mount point will look something like:
OutputFilesystem Type Size Used Avail Use% Mounted on . . . /dev/sda ext4 99G 60M 94G 1% /mnt/volume-sfo2-01 . . .
Locate the mount point for your volume and substitute it in the command below to unmount it:
- sudo umount --verbose /mnt/mount_point
If it's busy, the command will provide feedback similar to:
Outputumount: /mnt/mount_point: target is busy (In some cases useful info about processes that use the device is found by lsof(8) or fuser(1).)
If the volume is safe to unmount, it will return:
- umount: /mnt/mount_point unmounted
With the volume unmounted, you can safely proceed to resize or detach it.
One major benefits of volumes is that you can increase their size without powering down the Droplet.
To do this, you will:
- Increase the size in the Control Panel by opening the volume's More menu, choosing Resize, and selecting the new size.
- Log into the Droplet
- Make sure the volume is not being written to by unmounting it or powering down the Droplet.
- If the volume was partitioned, adjust the partition table
- Expand the filesystem on the Droplet.
Note that when a volume is unformatted, you can both increase and decrease its size. When it is formatted, however, you can only increase the size because of the high risk of data loss or corruption. How To Increase the Size of a DigitalOcean Block Storage Volume provides detailed instructions for resizing.
Moving Volumes: Attaching and Detaching
Another benefit of a block storage volume is being able to move it from one Droplet to another. A volume can only be attached to one Droplet at a time, so to move it, you'd first detach it if needed, then attach it to the new Droplet and mount it. This can all be managed from the Volumes page:
An Attach to a Droplet link appears in the Droplet column if a volume isn't attached.
When it is attached, information about the Droplet appears in the Droplet column and a Detach link is available in the More menu.
To attach an unattached volume, click the link. Search and select the Droplet you want to connect to. Note that Droplets in other regions are visible but are greyed out and cannot be selected. Droplets in the same region as the volume are darker and you can choose one of them to attach to.
Once you've attached the volume to a new Droplet, you will need to mount it again. You can revisit the Config instructions and follow the mount directions, but DO NOT format the volume or you will destroy all existing data.
To detach a volume:
- Make sure the volume is not being written to by unmounting it or powering down the Droplet.
- Then, open the volume's More menu and choose Detach.
Once the volume is detached, it can be attached and configured on a new Droplet in the same region.
Moving a Volume to a New Region with Snapshots
Snapshots allow you to make exact copies of volume. They're ideal for on-demand backups or to make a volume available in a different region.
To use the volume in a different region, you can take a snapshot of it and use that snapshot to create a new volume in the desired region. An Introduction to DigitalOcean Snapshots provides more detail on how to take volume snapshots and create new volumes based on them.
Note: Volumes are not included in weekly DigitalOcean backups.
To delete a volume, locate it on the account-level Volumes page or the Volumes page for an individual Droplet, open its More menu, and click Delete. This is permanent and cannot be reversed.
In this tutorial we've given an overview of how to create, format, mount, attach, detach, snapshot, and delete DigitalOcean block storage volumes.
The DigitalOcean API
The DigitalOcean API can also be used for Block Storage management tasks. While using the API is out of scope for this guide, you can learn more about how to control your storage resources programmatically by viewing our API documentation.
doctl: DigitalOcean's Official Command Line Tool
An alternative to using DigitalOcean's API directly is to use
doctl, the DigitalOcean command line client. Block Storage volumes can be created, deleted, and managed with
doctl in much the same way as other DigitalOcean resources. You can find out more in the guide How To Work with DigitalOcean Block Storage Using Doctl.