Introduction

DigitalOcean’s Block Storage allows you to create and attach additional storage volumes to your DigitalOcean Droplets. Volumes are an independent resource that can easily be moved from one Droplet to another within the same datacenter. Attached volumes function like locally connected storage drives, allowing you to manage your storage with familiar tools and techniques.

In this guide, we will cover the following topics:

  • What is DigitalOcean Block Storage?
  • What are some ways to use Block Storage?
  • How to create and attach volumes to Droplets
  • How to partition and format volumes
  • How to delete and detach volumes
  • Additional interfaces to manage volumes

What is DigitalOcean Block Storage?

DigitalOcean Block Storage is a flexible, convenient way of managing additional storage for your DigitalOcean Droplets. Block Storage is provisioned in units known as volumes. Volumes function as block devices, meaning they appear to the operating system as locally attached storage drives which can be partitioned and formatted according to your needs.

A few things you should know about Block Storage on DigitalOcean:

  • Price:
    • Volumes are charged at a rate of $0.10 per GB per month. For example, if you create a 100GB volume, you would be charged an additional $10 per month.
    • A volume will be charged from the moment it is created until the time of deletion in hourly increments, just like Droplets.
  • Storage media: Like Droplets, storage volumes are backed by SSDs.
  • Available sizes: Storage volumes can be created in sizes ranging from 1GB to 16TB.
  • Region Support: Currently, volumes are available in NYC1, SFO2, FRA1, and SGP1.
  • Additional details:
    • Volumes are region-specific resources. Volumes can be moved freely between Droplets within that region.
    • A volume may only be attached to one Droplet at a time. However, up to five volumes can be attached to a single Droplet.

When Should I Use Block Storage?

Block Storage is a good solution in scenarios where you need more storage space, but do not require the additional processing power or memory that a larger Droplet would provide. Block Storage volumes can be created, destroyed, or expanded easily as your needs change, simplifying initial planning around your storage requirements.

Because DigitalOcean volumes function as generic block devices, they can be useful in a large variety of contexts. A few examples are:

  • 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

Since Block Storage is not a highly specialized resource, it can be used for just about anything that would benefit from additional disk space.

With these points in mind, let’s take a look at how to use DigitalOcean Block Storage.

Creating and Attaching Volumes

The main interface for creating and attaching DigitalOcean volumes is through the Volumes section of a Droplet’s detail page in the DigitalOcean control panel. This menu section will be available for Droplets in regions with volume support (currently only NYC1, SFO2, FRA1, and SGP1).

You can get to a Droplet’s detail page by clicking the Droplet’s name in the main interface:

Droplet name link

If the Droplet is in one of the supported regions, the side menu will have a Volumes item:

Droplet volumes menu item

Click on the Volumes item. You will be taken to an area where you can create and attach a volume to the Droplet:

Droplet volumes without volumes attached

Creating and Attaching a New Volume

To create a new volume, start by clicking on the Attach a volume button:

Add volume prompt in the Droplet menu

You will be given the option to create a new volume or attach an existing volume. The default selection will create a new volume.

Select a name for the new volume which will be used for identification. Afterwards, choose one of the preconfigured size options, or enter your own custom size between 1GB and 16TB:

Create new volume prompt

After you have made your selections, click on the Create button at the bottom.

Your new volume will be created and attached to your Droplet. You can now move on to partitioning and formatting the new volume from within your Droplet, which is covered below.

Attaching an Existing, Unattached Volume

To attach an existing, unattached volume instead of creating a new volume, click on the Attach a volumebutton, and then click on the Unattached volumes tab at the top. You will see a list of all of your available, unattached volumes in that Droplet’s region:

Attach unattached volume prompt

Select the volume you wish to attach to the Droplet. When you are finished, click on the Attach button at the bottom.

The volume you selected will be attached to your Droplet and will be immediately visible to the Droplet’s operating system. To get started, you just need to configure the new storage space. If you already have a filesystem written to the volume, you can mount the filesystem and begin using it. If you have not yet formatted the space, follow the steps below.

Preparing Volumes for Use in Linux

After you attach a volume to your Droplet, you must prepare the volume for use as you would with any new disk in Linux. The steps needed will depend on whether you are working with a newly created volume or setting up an existing volume on a new server.

To begin, log into your Droplet with a user with sudo privileges or as root.

Preparing a Newly Created Volume

Newly created volumes require some preparation before the server can begin to utilize the additional space. Volumes must be partitioned, formatted with a filesystem, and mounted. Most of the time, the /etc/fstab file should be modified as well, so that mounting will take place automatically at boot.

While this process can be modified significantly according to your needs and may vary slightly depending on the Linux distribution, the procedure below can be used to get up and running quickly in most cases. Our article on partitioning and formatting DigitalOcean volumes provides more in-depth instructions if needed.

These commands will:

  • create a single GPT partition that spans the entire volume (certain tools expect some type of partition table)
  • format the partition with the Ext4 filesystem
  • create a mount point under the /mnt directory
  • adjust the /etc/fstab file to define a persistent mount
  • mount the filesystem

When running the commands, change the highlighted volume-nyc1-01 references to the name you gave your volume. The /dev/disk/by-id identifiers are used because they are predictable based on the volume name and will always point to the correct volume, even after reboots:

  • sudo parted /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01 mklabel gpt
  • sudo parted -a opt /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01 mkpart primary ext4 0% 100%
  • sudo mkfs.ext4 /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01-part1
  • sudo mkdir -p /mnt/volume-nyc1-01-part1
  • echo ‘/dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01-part1 /mnt/volume-nyc1-01-part1 ext4 defaults,nofail,discard 0 2′ | sudo tee -a /etc/fstab
  • sudo mount -a

When you are finished, skip the next section to test to make sure the volume is working correctly.

Preparing an Existing, Formatted Volume

After attaching an existing volume that has previously been partitioned and formatted, you will need to identify the filesystems available, mount them, and likely adjust the /etc/fstab file for persistence.

The identification process involves:

  • discovering the partitions and filesystems available
  • mapping the partition names to stable identifiers, so that they can be mounted reliably

To discover the partitions and filesystems on a volume, pass the volume identifier to the lsblk command. The /dev/disk/by-id identifier is a combination of the string /dev/disk/by-id/scsi-0DO_Volume_(a static prefix) and the name of your volume. Request specific output fields with the -o option:

  • lsblk -o NAME,FSTYPE,SIZE,TYPE,MOUNTPOINT /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01
Output
NAME   FSTYPE  SIZE TYPE MOUNTPOINT
sda            100G disk 
└─sda1 ext4    100G part

To map the /dev/sd* naming scheme used in the lsblk output to the /dev/disk/by-id naming scheme used to reliably identify DigitalOcean volumes and partitions, use the file command:

  • file /dev/disk/by-id/*
Output
/dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01:       symbolic link to ../../sda
/dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01-part1: symbolic link to ../../sda1

In this example, the disk consists of a single 100G partition that spans the entire volume. It is formatted with the Ext4 filesystem. Looking at the mapping, we can see that the stable identifier for this Ext4 partition is /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01-part1. With this information, we can mount the filesystem reliably.

Create a mount point to attach the volume:

sudo mkdir /mnt/volume-nyc1-01-part1

Add the entry to the /etc/fstab file with the appropriate options for the filesystem type you discovered:

  • sudo nano /etc/fstab

In this example, the partition we found is using the Ext4 filesystem:

/etc/fstab
  • . . .
  • /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01-part1 /mnt/volume-nyc1-01-part1 ext4 defaults 0 2

Save and close the file when you are finished.

You can mount the volume by typing:

  • sudo mount -a

Continue below to test that the filesystem is mounted correctly.

Testing the Mounted Filesystem

Check that the mount was successful by passing the mount point to findmnt:

  • findmnt /mnt/volume-nyc1-01-part1

You should see output indicating that it is currently mounted:

Output
TARGET                    SOURCE    FSTYPE OPTIONS
/mnt/volume-nyc1-01-part1 /dev/sda1 ext4   rw,relatime,data=ordered

If you list the contents of the directory, you should see the lost+found directory if you are using an Ext4 filesystem:

  • ls /mnt/volume-nyc1-01-part1
Output
lost+found

Double check that the new storage space is writable:

  • echo ‘success’ | sudo tee /mnt/volume-nyc1-01-part1/test_file

Now read the file back:

  • cat /mnt/volume-nyc1-01-part1/test_file
Output
success

If the mount is working as expected, delete the test file:

  • sudo rm /mnt/volume-nyc1-01-part1/test_file

Deleting and Detaching Volumes

To remove a volume from a Droplet, you can either delete or detach the volume.

  • Deleting a volume: permanently destroys the volume and its contents. This action cannot be reversed.
  • Detaching a volume: simply removes the volume from its current Droplet. The volume and all of its contents can later be attached to a different Droplet as needed.

To avoid operating system errors within your Droplet, it is important to ensure that the volume isn’t in use before performing either of these actions.

Ensuring the Volume Is Not In Use

The first step in detaching or deleting a volume is to ensure that the volume is not being actively used within the server. The best way to do this log into your server and unmount the associated filesystems.

You can see if any processes are currently using the mounted filesystem by passing the mount point to lsof:

  • cd ~
  • sudo lsof +f — /mnt/volume-nyc1-01-part1

If the command displays any output, you will have to stop the listed processes before unmounting the filesystem.

Once you have resolved any processes accessing the mounted filesystem, unmount it:

  • sudo umount /mnt/volume-nyc1-01-part1

If you will not be reattaching the volume, adjust the /etc/fstab file to remove any entries referencing the volume:

  • sudo nano /etc/fstab

You may also wish to remove the mount point if you will not be using it again:

  • sudo rmdir /mnt/volume-nyc1-01-part1

From here, you can safely detach or delete the volume from the DigitalOcean control panel.

Deleting a Volume with the Control Panel

To completely destroy a volume and all of its contents, visit the Volumes section of the Droplet’s detail page.

Warning

Deleting a volume is irreversible. Please be sure that you select the correct volume in the steps that follow.
You can get to a Droplet’s detail page by clicking the Droplet’s name in the main interface:

Droplet name link

Click on the Volumes item in the side menu:

Droplet volumes menu item

You will be taken to an area where you can see the volumes currently attached to the Droplet:

Droplet volumes with volumes attached

In the More menu of the volume you wish to delete, select the Delete option:

Droplet delete volume menu item

The volume and its contents will be completely deleted. This action is irreversible.

Detaching a Volume with the Control Panel

To detach an attached volume, visit the Volumes section of the Droplet’s detail page. You can get to a Droplet’s detail page by clicking the Droplet’s name in the main interface:

Droplet name link

Click on the Volumes item in the side menu:

Droplet volumes menu item

You will be taken to an area where you can see the volumes currently attached to the Droplet:

Droplet volumes with volumes attached

In the More menu of the volume you wish to detach, select the Detach option:

Droplet detach volume menu item

You can now attach the volume to a different Droplet as needed.

Resizing Volumes

It is possible to increase the size of an existing volume to give yourself access to additional capacity. This procedure involves resizing the volume itself within the control panel and then adjusting the partitions and filesystems on the volume to utilize the additional space.

Read our guide on Increasing the Size of a Block Storage Volume to learn how to resize an existing volume.

Other Interfaces to Manage Volumes

While the Volumes menu within a Droplet is the primary management interface for volumes, there are other places where volumes can be viewed and managed.

The Volumes Index Page

The Droplets index page contains a link for the Volumes index page along the top:

Volume index page link

The Volumes index page can be used to get an overview of all of the volumes currently associated with your account:

Volume index page

This interface can be used to attach, detach, or delete any of your volumes. This interface is primarily used to provide account-wide management of volume resources.

The “Add a Volume” Option During Droplet Creation

Volumes can also be created or attached to a Droplet using the Add block storage section during the Droplet creation process:

Droplet creation add block storage section

Clicking the Add a volume button will give you the option to create a volume or attach unattached volumes. This lets you make storage decisions while creating your Droplet:

Droplet creation add a volume
Using the volumes feature during creation will filter the available Droplet regions and will automatically assign the volume name.

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 by reading our guide on using doctl to manage DigitalOcean resources.

Conclusion

You should now have a basic understanding of how DigitalOcean Block Storage works and how to create and manage volumes within your account. By leveraging Block Storage, you can now manage storage requirements with greater flexibility.

62 Comments

  • B
  • I
  • UL
  • OL
  • Code
  • Highlight
  • Table
  • Is it possible to resize the volume? Suppose I create 500GB volume, what will be the procedure to increase/decrease the size?

    • @ssrahman It is possible to expand a volume, but not decrease it’s size (due to the potential issue of data loss). The More menu of a volume gives you the ability to expand the volume.

      Once you have expanded the volume in the control panel, you’ll also have to make sure to resize the filesystem written to it to use the extra space. I’ll be updating the article with instructions or a link to the procedure soon.

      In the mean time, the basic steps are to:

      Unmount the volume:

      • cd ~
      • sudo umount /mnt/volume-nyc1-01-part1

      Find the current mapping between static /dev/disk/by-id identifiers and the /dev/sd* identifiers for the volume:

      • file /dev/disk/by-id
      Output
      /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01:       symbolic link to ../../sda
      /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01-part1: symbolic link to ../../sda1
      

      You need to find the volume (not the partition) and the /dev/sd* identifier that it is currently associated with. The growpart tool can only handle /dev/sd* identifiers.

      Pass the /dev/sd* name you find, along with the number of the last partition on the device to the growpart tool:

      • sudo growpart /dev/sda 1

      If you are using Ext4, run a quick check on the partition and then run the resize2fs:

      • sudo e2fsck -f /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01-part1
      • sudo resize2fs /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01-part1

      Now, just remount:

      • sudo mount -a

      You should be able to see your new expanded storage with df:

      • df -h -x tmpfs -x devtmpfs

      Hopefully that helps. We’ll be getting some more complete content up on this soon.

      • @ssrahman Forgot to mention this in the last comment. The growpart command is really simple, but most currently shipped versions have a bug that prevent resizes above 2TB in size. For these situations, you will have to remove the current partition and write a new partition with the same start location, but expanded to the end of the disk.

        Start by unmounting the filesystem:

        • sudo umount /mnt/volume-nyc1-01-part1

        Now, start up parted in interactive mode, by passing in the volume device:

        • sudo parted -a opt /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01

        At various points during this process, you may be asked to grow the GPT table to take into account the new space. You can simply type Fix to do this:

        • Warning: Not all of the space available to /dev/sdb appears to be used, you can fix the GPT to use all of
        • the space (an extra 16567500800 blocks) or continue with the current setting?
        • Fix/Ignore? Fix

        Change the output units to use sectors and then print the current partition table:

        • unit s
        • print

        You will have to look at the last partition in the list if you have more than one. Note the partition number, the start sector, and the name:

        Output
        Model: DO Volume (scsi)
        Disk /dev/sdb: 16777216000s
        Sector size (logical/physical): 512B/512B
        Partition Table: gpt
        Disk Flags: 
        
        Number  Start  End         Size        File system  Name     Flags
         1      2048s  209713151s  209711104s  ext4         primary
        

        Now, delete the partition and create a new partition that lines up at the same start sector, but expands to the end of the available space:

        • rm 1
        • mkpart primary 2048s 100%

        Now, exit back to the command line by typing:

        • quit

        From here, the procedure aligns with the previous comment. Check the filesystem and then expand it. Pass in the specific partition to these tools:

        • sudo e2fsck -f /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01-part1
        • sudo resize2fs /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01-part1

        Now, mount the filesystem again:

        • sudo mount -a

        Your expanded filesystem should be visible with df:

        • df -h -x devtmpfs -x tmpfs

        That should work for larger resizes.