We hope you find this tutorial helpful. In addition to guides like this one, we provide simple cloud infrastructure for developers. Learn more →

How To Increase the Size of a DigitalOcean Block Storage Volume

PostedJuly 15, 2016 15k views DigitalOcean Block Storage Storage

Introduction

DigitalOcean's Block Storage volumes are a flexible way of managing your Droplet's storage needs. By attaching a volume to a Droplet, you can easily increase your server's capacity as your requirements change.

While it's possible to attach multiple volumes to a single Droplet to expand the available capacity of your server, it's also possible to expand the size of an existing volume. This process involves increasing the size of the volume in your DigitalOcean account, followed by some operations on the server itself to expand any partitions and filesystems on the volume.

Can I Shrink My Volumes?

Decreasing the size of a volume is not possible due to the high risk of data loss or corruption. Data written to a volume is not guaranteed to be written sequentially at the start of the device, so when reducing the available space, the integrity of the filesystem and the files within can be damaged.

If you no longer require the present capacity of your volume, the safest way to decrease your volume size is to transfer the contents to a smaller volume. After attaching both volumes to a Droplet, tools like rsync can be used to transfer the files safely between the two volumes. Once you have verified that the transfer of the content to the new volume was successful, the larger volume can be destroyed.

Prerequisites

To complete this guide, you should have a Block Storage volume attached to a Droplet in your account. To learn how to set up a Block Storage volume and attach it to a Droplet, follow our guide on how to use DigitalOcean Block Storage.

After the volume itself is expanded, you will have to log into the Droplet as root or as a user with sudo privileges.

Resize the Volume in the DigitalOcean Control Panel

The first task that needs to be completed is to resize the volume in the DigitalOcean control panel. From the Droplet index page, click on the Droplet's name to get to the detail page:

Droplet index page

In the side menu, click in the Volumes item:

Block Storage Volumes menu item

Find the volume you wish to resize in the Attached volumes list. Click on the More link to bring up additional options. From here, select the Resize Volume option:

Block Storage resize volume menu option

In the interface that follows, select a new size for the volume. Valid selections for the resize feature range from a single GB larger than the current size up to the maximum size for volumes (currently 16TB):

Block Storage resize volume interface

When you are ready, click on the Resize Volume button.

Log into the Server to Adjust the Partitions and Filesystems

In order to use the additional space, you must now expand any partitions and filesystem structures on the volume. Log into your Droplet as either root or a sudo user to get started.

The process of adjusting your filesystem will depend on whether your volume has been partitioned or not.

Resizing an Unpartitioned Volume

If your volume has a filesystem written directly to it, without any partition table, you can expand the filesystem directly.

You can tell if your filesystem is written directly to the volume by passing the /dev/disk/by-id identifier to the lsblk command with the --fs option. The /dev/disk/by-id identifier consists of /dev/disk/by-id/scsi-0DO_Volume_ combined with the name of the volume in question:

Note
Some older versions of lsblk do not have a dedicated --fs option. If --fs is not an available option on your version of lsblk, the same output can be achieved by manually specifying the output with -o NAME,FSTYPE,LABEL,UUID,MOUNTPOINT.

You should see the filesystem type in the FSTYPE column associated with the device itself, not a numbered variant like sda1 which would indicate a partition.

  • sudo lsblk --fs /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01
Output
NAME FSTYPE LABEL UUID MOUNTPOINT sda ext4 18efad39-6b7c-44d3-ba21-b0778911c4ed /mnt/volume-nyc1-01

The resize procedure will depend on the filesystem type. We will cover Ext4 and XFS filesystem types.

Expanding Ext4 Filesystems on an Unpartitioned Volume

If the filesystem on the volume is Ext4, simply pass the volume identifier to the resize2fs command:

  • sudo resize2fs /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01
Output
resize2fs 1.42.13 (17-May-2015) Filesystem at /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01 is mounted on /mnt/volume-nyc1-01; on-line resizing required old_desc_blocks = 25, new_desc_blocks = 32 The filesystem on /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01 is now 131072000 (4k) blocks long.

You can verify that the larger filesystem is available with df:

  • df -h -x tmpfs -x devtmpfs
Output
Filesystem Size Used Avail Use% Mounted on /dev/vda1 20G 1.1G 18G 6% / /dev/sda 197G 60M 187G 1% /mnt/volume-nyc1-01

Expanding XFS Filesystems on an Unpartitioned Volume

If the filesystem on the volume is XFS, pass the mount point where the partition is attached to the xfs_growfs tool:

  • sudo xfs_growfs /mnt/volume-nyc1-01
Output
meta-data=/dev/sda isize=512 agcount=4, agsize=32768000 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=1 spinodes=0 data = bsize=4096 blocks=131072000, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal bsize=4096 blocks=64000, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 131072000 to 157286400

When you are finished, you can check that the new space is available with the df command:

  • df -h -x tmpfs -x devtmpfs
Output
Filesystem Size Used Avail Use% Mounted on /dev/vda1 20G 1.1G 18G 6% / /dev/sda 200G 33M 200G 1% /mnt/volume-nyc1-01

Resizing a Partitioned Volume

If your volume is partitioned, the final partition itself must first be expanded before expanding the filesystem.

Identify the Partitions and Mount Points

To find the partitions associated with your volume, as well as their mount points, pass the /dev/disk/by-id identifier to the lsblk command. The /dev/disk/by-id identifier consists of /dev/disk/by-id/scsi-0DO_Volume_ combined with the name of the volume in question:

  • sudo lsblk /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01
Output
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 200G 0 disk └─sda1 8:1 0 100G 0 part /mnt/volume-nyc1-01-part1

Here, we can see the /dev/sd* names for the volume and its partitions, the partitioning scheme, and the mount points. You can also see that the size of the disk is currently greater than the size of the partitions written to it. This confirms that the volume resize in the control panel was successful.

Unmount the Partitions

Since we will be rewriting some of the partition information on the volume, we should make sure that all of the volume's partitions are unmounted.

First, you may need to find and terminate any processes currently accessing the mounted filesystem. You can do so by passing the mount points you found in the lsblk output to the lsof command:

  • 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 conflicts with processes accessing the mounted filesystem, unmount it with the umount command:

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

Repeat this for all of the mounted partitions on the volume.

Rewrite the Partition Table

To rewrite the partition table, we will use the gdisk tool. This has a few advantages over other options:

  • It allows us to backup our existing table before modifying data. This is extremely helpful in case we make a mistake.
  • It can explicitly move the GPT recovery data to the end of the disk. Other tools can do this too, but sometimes only when the tool notices the issue.
  • It works with partitions of any size. Tools like growpart are very straight forward, but versions shipped with most distributions can only resize partitions up to 2 TB. gdisk offers a more general purpose solution.

To get started, change to a writable directory (so that we can successfully write the partition table backup file) and start gdisk with the volume identifier:

  • cd ~
  • sudo gdisk /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01

Note
At any time during this process, you can type ? to display the help menu of available commands.

Back Up the Current Partition Table

Before you make changes, backup the current partition table with the b command. When prompted, enter the name for the backup file:

  • b
secondary_label Output]
Enter backup filename to save: partition_table.bak

Move the Backup GPT Data Structures

Next, we can move the backup GPT data structures to the end of the volume, which changed when we expanded the resource. Switch to the "experts" menu for a moment:

  • x

Your prompt will change to reflect the new sub-menu. At any time, you can press ? to get the help for this menu, or press m to return to the main menu.

To move the GPT table backup structures to the end of the disk, type:

  • e
Output
Relocating backup data structures to the end of the disk

Now, go back to the main menu by typing:

  • m

The prompt should change back, indicating that you are no longer in the "experts" menu.

Removing and Recreating the Final Partition

In order to utilize the new space on the volume, we will expand the last partition into the free area. This actually involves gathering data about the current partition, removing it, and then recreating it to occupy the additional space.

Start by displaying the current partition table:

  • p
Output
Disk /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01: 419430400 sectors, 200.0 GiB Logical sector size: 512 bytes Disk identifier (GUID): 016F1E0A-83C5-4CF2-A78B-150F689CC45A Partition table holds up to 128 entries First usable sector is 34, last usable sector is 419430366 Partitions will be aligned on 2048-sector boundaries Total free space is 209719229 sectors (100.0 GiB) Number Start (sector) End (sector) Size Code Name 1 2048 209713151 100.0 GiB 8300 primary

Note the Number, Start (sector), Code, and Name values of the last partition. In the example above, only a single partition is present. We will be removing this partition and creating a new partition with all of the same properties except for its ending location (and consequently, it's size).

Use the d command to remove the partition. If you only have a single partition, it will be removed. If you have multiple partitions, you will be prompted to enter the partition number. Select the last partition:

  • d
Output
Using 1

Next, we can create a new partition. Use the n command to begin the process:

  • n

You will be asked a series of questions about the new partition you wish to create. Use the values from the partition that you just deleted to help you answer these questions.

It is likely that the correct values will be suggested, in which case you can simply press ENTER to accept the default values. For the prompt about the Last sector, accept the default to expand the partition to the end of the disk:

Output
Partition number (1-128, default 1): First sector (34-419430366, default = 2048) or {+-}size{KMGTP}: Last sector (2048-419430366, default = 419430366) or {+-}size{KMGTP}: Current type is 'Linux filesystem' Hex code or GUID (L to show codes, Enter = 8300): Changed type of partition to 'Linux filesystem'

The partition creation process does not prompt for an internal GPT partition name. If this is important to you, you can set the name to its previous value using the c command. Again, you will be prompted for the partition to operate on if you have more than one available:

  • c
Output
Using 1 Enter name: primary

Writing the New Partition Table to the Volume

When you are finished, verify the new partition table you've created. As we stated before, all of the information in the table at the bottom of the output should match the previous table, with the exceptions of the End (sector) and Size columns:

  • p
Output
Disk /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01: 419430400 sectors, 200.0 GiB Logical sector size: 512 bytes Disk identifier (GUID): 016F1E0A-83C5-4CF2-A78B-150F689CC45A Partition table holds up to 128 entries First usable sector is 34, last usable sector is 419430366 Partitions will be aligned on 2048-sector boundaries Total free space is 2014 sectors (1007.0 KiB) Number Start (sector) End (sector) Size Code Name 1 2048 419430366 200.0 GiB 8300 primary

After verifying the information, write the partition data to the volume and exit the tool with the w command. You will be prompted to confirm the procedure. Type Y if you are ready:

  • w
Output
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING PARTITIONS!! Do you want to proceed? (Y/N): Y OK; writing new GUID partition table (GPT) to /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01. The operation has completed successfully.

This should expand the final partition to consume the rest of the available space on the volume.

Expand the Filesystem to Occupy the Available Space

Next, we need to expand the filesystem on the final partition to use the extra available space.

Finding the Partition Identifier and the Filesystem Type

The procedure used to grow the final partition's filesystem depends on the filesystem type. You can find the filesystems of your volume's partitions by passing the volume identifier to the lsblk with the --fs option:

Note
Some older versions of lsblk do not have a dedicated --fs option. If --fs is not an available option on your version of lsblk, the same output can be achieved by manually specifying the output with -o NAME,FSTYPE,LABEL,UUID,MOUNTPOINT.

  • sudo lsblk --fs /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01
Output
NAME FSTYPE LABEL UUID MOUNTPOINT sda └─sda1 ext4 218ac00e-dff5-4529-b3b7-bb3338a50ca9

Find the filesystem type of the last partition on your volume by looking at the FSTYPE column.

You can map the /dev/sd* name for the partition back to it's /dev/disk/by-id identifier by checking the current linking in the /dev/disk/by-id directory:

  • 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

We'll cover how to expand Ext4 and XFS filesystems in this guide.

Expanding Ext4 Filesystems on a Partitioned Volume

The Ext4 resize tool requires you to check the partition for inconsistencies that may have happened during the repartitioning procedure before you can expand it. The e2fsck command can safely check unmounted filesystems.

Pass the last partition on the volume to the e2fsck program. Use the -f option to force the check:

  • sudo e2fsck -f /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01-part1
Output
e2fsck 1.42.13 (17-May-2015) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01-part1: 13/6553600 files (0.0% non-contiguous), 459351/26213888 blocks

After checking the filesystem, you can expand it to fill the partition by passing it to resize2fs:

  • sudo resize2fs /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01-part1
Output
resize2fs 1.42.13 (17-May-2015) Resizing the filesystem on /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01-part1 to 52428539 (4k) blocks. The filesystem on /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01-part1 is now 52428539 (4k) blocks long.

The filesystem should now be expanded. You can now mount it and verify the procedure:

  • mount -a
  • df -h -x tmpfs -x devtmpfs
Output
Filesystem Size Used Avail Use% Mounted on /dev/vda1 20G 1.1G 18G 6% / /dev/sda1 197G 60M 187G 1% /mnt/volume-nyc1-01-part1

Expanding XFS Filesystems on a Partitioned Volume

Although not strictly required, it is a good idea to use the xfs_repair utility to check the filesystem for inconsistencies introduced by repartitioning. Pass in the last partition on the volume:

  • sudo xfs_repair /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01-part1
Output
Phase 1 - find and verify superblock... Phase 2 - using internal log - zero log... - scan filesystem freespace and inode maps... - found root inode chunk Phase 3 - for each AG... - scan and clear agi unlinked lists... - process known inodes and perform inode discovery... - agno = 0 - agno = 1 - agno = 2 - agno = 3 - process newly discovered inodes... Phase 4 - check for duplicate blocks... - setting up duplicate extent list... - check for inodes claiming duplicate blocks... - agno = 0 - agno = 1 - agno = 2 - agno = 3 Phase 5 - rebuild AG headers and trees... - reset superblock... Phase 6 - check inode connectivity... - resetting contents of realtime bitmap and summary inodes - traversing filesystem ... - traversal finished ... - moving disconnected inodes to lost+found ... Phase 7 - verify and correct link counts... done

Once the disk has been checked, mount the filesystem. XFS filesystems must be mounted before they can be expanded:

  • sudo mount -a

Now, the xfs_growfs command can be used to expand the filesystem. The xfs_growfs command takes the mount point as an argument:

  • sudo xfs_growfs /mnt/volume-nyc1-01-part1
Output
meta-data=/dev/sda1 isize=512 agcount=4, agsize=6553472 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=1 spinodes=0 data = bsize=4096 blocks=26213888, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal bsize=4096 blocks=12799, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 26213888 to 52428539

When you are finished, you can check that the new space is available with the df command:

  • df -h -x tmpfs -x devtmpfs
Output
Filesystem Size Used Avail Use% Mounted on /dev/vda1 20G 1.1G 18G 6% / /dev/sdb1 200G 33M 200G 1% /mnt/volume-nyc1-01-part1

Conclusion

Expanding a volume can be used to quickly get more space in your current filesystem. You can grow the size of your volumes as your capacity requirements increase instead of having to guess your future needs at the beginning of your projects.

3 Comments

Creative Commons License