How to Create Partitions on DigitalOcean Block Storage Volumes

Partitioning lets you divide a single storage device into smaller units that you can manage independently. You can write multiple filesystems to one device in order to segment space along functional lines.

To use partitions with a volume, first you must create the partition(s) and then format each partition

Create Partitions

The two most common partitioning systems are the traditional master boot record (MBR) format and the more modern GUID Partition Table (GPT). The MBR format has some inherent limitations, especially regarding the number and sizes of partitions that you can create.

If you have no specialized needs, we recommend using GPT partitions. To work with GPT partitions, we recommend parted for non-interactive partitioning and gdisk’s more robust menu-driven interface for interactive partitioning, both of which are often installed by default.

First, write a bare GPT partition table to your volume by passing the device to the parted command with the mklabel gpt subcommand:

sudo parted /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01 mklabel gpt

Next, you can begin writing your partitions with parted. The command has several parts:

  • mkpart creates a new partition table of the specified type, like GPT.
  • -a opt aligns the partitions with the disk’s underlying sectors.
  • primary is an odd parameter. parted works with GPT, but its argument structure still reflects its MBR origins, so you still need to specify that you’re writing “primary” partitions, even though GPT doesn’t use these designations.
  • The starting and ending position of the partition take a variety of units, like GB, sectors, or percentages. parted can only handle absolute positioning by providing a definite start and end point.

Using percentages and passing in the -a opt option lets parted to automatically align the partitions to the underlying sectors, which is important for proper performance.

As an example, this command creates a single partition that spans the entire volume:

sudo parted -a opt /dev/disk/by-id/scsi-example mkpart primary 0% 100%

And this command creates two equally-sized partitions:

sudo parted -a opt /dev/disk/by-id/scsi-example mkpart primary 0% 50%
sudo parted -a opt /dev/disk/by-id/scsi-example mkpart primary 50% 100%

To start, use the volume identifier as an argument to gdisk to scan the device and locate existing structures.

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

This enters you into an interactive prompt:

GPT fdisk (gdisk) version 1.0.1

Partition table scan:
MBR: not present
BSD: not present
APM: not present
GPT: not present

Creating new GPT entries.

Command (? for help):

To write a new partition table to the disk, use the o option:

  
    
o

  

Confirm the operation by entering y at the confirmation prompt:

This option deletes all partitions and creates a new protective MBR.
Proceed? (Y/N):

Next, create partitions by using the n option:

  
    
n

  

gdisk takes you through a series of prompts for the partition number, the first sector, the last sector or size, and the GUID for the partition type:

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

You can press ENTER to accept the suggested default values, which is typically the right choice. For the last sector or size prompt, you can use + to indicate relative sizing. This means that you can pass the partition size directly (instead of calculating the end position, as with parted).

You can display the partitions by using the p option:

  
    
p

  
Disk /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01: 209715200 sectors, 100.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 19252774-25E2-4899-96CD-DCFE3B846DCC
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 209715166
Partitions will be aligned on 2048-sector boundaries
Total free space is 188743613 sectors (90.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
1            2048        20973567   10.0 GiB    8300  Linux filesystem

To write the table to the volume and exit the utility, use the w option, which prompts you to confirm the changes.

  
    
w

  
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): 

When you are certain of your changes, type Y to write the partitions to the volume.

Format the Partitions

After partitioning, you must format the partitions by writing a filesystem to each one. The filesystem is responsible for managing file-level interactions and providing reliable methods of writing and retrieving information on the disk.

There are many different types of Linux-compatible filesystems, like ext4, XFS, Btrfs, and ZFS. Distributions usually promote ext4 or XFS as the default choice for general purpose computing. The tooling for these two filesystems is usually installed by default, which means that formatting and managing these filesystems is straightforward.

Most Linux filesystems are formatted using tools that begin with mkfs. followed by the name of the filesystem in lowercase, like mkfs.ext4 or mkfs.xfs. By default, this will create a filesystem that fills the entire block device passed to it.

Warning

When formatting with mkfs, use the identifier for the partition, not the identifier for the whole volume. Formatting the volume will overwrite the partitions you created.

When using the /dev/disk/by-id identifiers, partitions end in -part#. You can use mkfs’s -L option to label the partition, which gives it a persistent name to use as an alternative to the /dev/disk/by-id identifiers.

To format a partition with an ext4 filesystem, use mkfs.ext4:

sudo mkfs.ext4 /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01-part1
mke2fs 1.42.13 (17-May-2015)
Discarding device blocks: done
Creating filesystem with 2621440 4k blocks and 655360 inodes
Filesystem UUID: 37858ba9-c2f3-4afe-9013-83111111e862
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

To format a partition with an XFS partition, use mkfs.xfs:

sudo mkfs.xfs /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01-part1
meta-data=/dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01-part1 isize=512    agcount=4, agsize=5898175 blks
        =                       sectsz=512   attr=2, projid32bit=1
        =                       crc=1        finobt=1, sparse=0
data     =                       bsize=4096   blocks=23592699, imaxpct=25
        =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=11519, version=2
        =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

Once you’ve formatted each partition, you’re ready to mount and use the volume.