After you attach a new data disk to an Elastic Compute Service (ECS) instance, you must create and mount one or more file systems on the disk. This topic describes how to partition and format a new data disk on a Linux instance.

Prerequisites

An independent data disk is created and attached to an instance. If you create a data disk together with the instance, skip the attach operation. For more information about how to attach data disks to ECS instances, see Attach a data disk.
Note The attach operation refers to attaching disks to ECS instances in the ECS console, instead of mounting file systems by running the mount command within the operating systems of ECS instances.

Background information

By default, the device names of data disks are assigned by the system based on the following naming conventions:

  • Device names of data disks for I/O optimized instances use the /dev/vd[b-z] format. Examples: /dev/vdb, /dev/vdc, and /dev/vdd.
  • Device names of data disks for non-I/O optimized instances use the /dev/xvd[b-z] format. Examples: /dev/xvdb, /dev/xvdc, and /dev/xvdd.
Data disks support the GUID Partition Table (GPT) and Master Boot Record (MBR) partition formats.
  • GPT: can recognize partitions larger than 2 TiB in size and allows an unlimited number of partitions to be created on each disk.
  • MBR: can recognize partitions up to 2 TiB in size and allows up to four partitions to be created on each disk.

Precautions

When you partition and format a data disk, take note of the following items:
  • Disk partitioning and formatting are high-risk operations. Exercise caution when you partition and format disks. This topic applies only to new data disks.

    If a data disk that you want to format contains data, you must create a snapshot for the disk before you format it. This can prevent data loss. For more information, see Create a snapshot for a disk.

  • In ECS, only data disks can be partitioned, whereas system disks cannot.

    If you use a third-party tool to forcibly partition a system disk, unforeseen risks such as system failures and data loss may occur. You can only extend or create partitions on system disks that have been extended. For more information, see Resize disks online for Linux instances.

Procedure

The following table describes the resources used in the examples of this topic.
Resource Description
Image used by the instance Alibaba Cloud Linux 3.2104 64-bit public image
Data disk
  • Device name of the data disk: /dev/vdb
  • Capacity: 40 GiB

Step 1: Connect to an ECS instance and view the data disk

Connect to an ECS instance and check whether the data disk is attached to the ECS instance.

  1. Connect to the ECS instance.
  2. Run the following command to view the information of the data disk attached to the ECS instance:
    fdisk -l
    A command output similar to the following one is returned. Format disk_fdisk

    If no /dev/vd* device name is displayed, check whether the data disk is attached to the instance. /dev/vd* indicates the new data disk. In this example, /dev/vdb is used. For information about how to attach a data disk in the ECS console, see Attach a data disk.

  3. Create partitions for the data disk.

Step 2: Create GPT partitions for the data disk

Perform the following steps to create GPT partitions that are larger than 2 TiB in size for the data disk.

  1. If Parted and e2fsprogs are not installed, run the following commands to install them.
    • Run the following command to install Parted:
      yum install -y parted
    • Run the following command to install e2fsprogs:
      yum install -y e2fsprogs
    Note The yum command in this step is applicable to Linux distributions such as CentOS. For other Linux distributions, modify the command based on your package management software. For example, run the apt-get install <Package name> command for Debian or Ubuntu.
  2. Use Parted to partition the data disk.
    1. Run the following command to start partitioning the data disk:
      parted /dev/vdb
    2. Run the following command to set the partition format to GPT:
      mklabel gpt
    3. Run the following command to create a primary partition and specify the start and end sectors for the partition:
      mkpart primary 1 100%
    4. Run the following command to check whether the partitions are aligned:
      align-check optimal 1
      A command output similar to the following one is returned:
      1 aligned
      Note If the partitions are not aligned, 1 not aligned is returned. For more information about how to troubleshoot this issue, see Troubleshooting.
    5. Run the following command to view the partition table:
      print
    6. Run the following command to exit Parted:
      quit
    A command output similar to the following one is returned. Partitioning by using Parted
  3. Run the following command to re-read the partition table:
    partprobe
  4. Run the following command to view the new partitions:
    fdisk -lu /dev/vdb
    A command output similar to the following one is returned. If the new partition is created, the information of gpt is displayed. gpt

Step 2: Create MBR partitions for the data disk

Perform the following steps to create MBR partitions that are up to 2 TiB in size for the data disk.

Note If your data disk is larger than 2 TiB in size or may need to be extended to larger than 2 TiB, we recommend that you use the GPT partition format. For more information, see Step 2: Create GPT partitions for the data disk.
  1. Create an MBR partition.
    1. Run the following command to partition the data disk:
      fdisk -u /dev/vdb
    2. Enter p to view the partition information of the data disk.
      In this example, the data disk is not partitioned.
    3. Enter n to create a new partition.
    4. Enter p to set the partition as a primary partition.
      Note When you create a single partition for a data disk, the partition must be the primary partition. If you want to create four or more partitions, enter e (extended) at least once to create at least one extended partition.
    5. Enter the partition number and press the Enter key.
      In this example, only one partition is created. Press the Enter key to use the default value 1.
    6. Enter the number of the first available sector and press the Enter key.
      In this example, press the Enter key to use the default value 2048.
    7. Enter the number of the last sector and press the Enter key.
      In this example, only one partition is created. Press the Enter key to use the default value.
    8. Enter p to view the intended partitions of the data disk.
    9. Enter w to start partitioning, and exit after partitioning is complete.
    A command output similar to the following one is returned. Partition creation result
  2. Run the following command to view the new partition:
    fdisk -lu /dev/vdb
    A command output similar to the following one is returned. If the new partition is created, the information of /dev/vdb1 is displayed. Partitioning result

Step 3: Create a file system for the partition

Create a file system for the new partition. The following commands are applicable to ext4 and XFS file systems. You can run one of the following commands to create the corresponding file system.
Note If the data disk is larger than 16 TiB, you must use the specified version of e2fsprogs to format the data disk. For more information, see Appendix 1: Update e2fsprogs on a Linux instance.
  • Run the following command to create an ext4 file system:
    mkfs -t ext4 /dev/vdb1
  • Run the following command to create an XFS file system:
    mkfs -t xfs /dev/vdb1
In this example, an ext4 file system is created. Create a file system

Step 4: Configure the /etc/fstab file and mount the partition

Write the information of the new partition to /etc/fstab to enable this partition to be automatically mounted on instance startup.

Notice We recommend that you use a universally unique identifier (UUID) to reference the new partition in /etc/fstab. The device names of other disks may change due to operations such as releasing a disk. If you use the device name of the data disk in /etc/fstab, your stored data may be affected if the device name changes.
  1. Run the following command to back up etc/fstab:
    cp /etc/fstab /etc/fstab.bak
  2. Write the information of the new partition to /etc/fstab.
    • If you are a root user, you can run the following command to modify /etc/fstab:
      echo `blkid /dev/vdb1 | awk '{print $2}' | sed 's/\"//g'` /mnt ext4 defaults 0 0 >> /etc/fstab
      Description of the parameters in this command:
      • /dev/vdb1: the data disk partition that has a file system created. Replace it with your actual partition name.
      • /mnt: the directory to which the partition is mounted. Replace it with the actual directory of your partition.
      • ext4: the file system type of the partition. Replace it with the type of the created file system.
      Note The Ubuntu 12.04 operating system does not support barriers. You must run the echo '`blkid /dev/vdb1 | awk '{print $3}' | sed 's/\"//g'` /mnt ext4 barrier=0 0 0' >> /etc/fstab command to write the information of the new partition to /etc/fstab.
    • If you are a common user, you can manually modify /etc/fstab. For more information, see Configure UUIDs in the fstab file to automatically attach data disks.
  3. Run the following command to check the information of the new partition in /etc/fstab:
    cat /etc/fstab
    A command output similar to the following one is returned. Query fstab
  4. Run the following command to mount the file systems configured in the /etc/fstab file:
    mount -a
  5. Run the following command to check the mount result:
    df -h
    A command output similar to the following one is returned. If the file systems are mounted, the information of the new file system is displayed. Query the mount result

Troubleshooting

Issue: When GPT partitions are created, the partitions are not aligned.

Solutions:
  1. We recommend that you run the following commands:
    cat /sys/block/vdb/queue/optimal_io_size
    cat /sys/block/vdb/queue/minimum_io_size
    cat /sys/block/vdb/alignment_offset
    cat /sys/block/vdb/queue/physical_block_size
  2. Calculate the start sector number of the optimal partition format by using the following formula: (<optimal_io_size>+<alignment_offset>) /<physical_block_size>.

    For example, if the start sector number is 1024, you can run the mkpart primary 1024s 100% command to create another primary partition.