Before you can store, read, and write data on a new data disk that is attached to an Elastic Compute Service (ECS) instance, you must initialize the data disk. The initialization operations include partitioning the disk, formatting file systems, and mounting file systems. This topic describes how to initialize a data disk whose size does not exceed 2 TiB on a Linux instance.

Prerequisites

A data disk is attached to a Linux ECS instance. For more information, see Attach a data disk.

Background information

The example in this topic uses an ECS instance and a data disk that have the configurations described in the following table. Operations may vary based on actual conditions.
Operating systemResourceDescription
LinuxImage used by the instanceAlibaba Cloud Linux 3.2104 64-bit public image
Data disk
Note For information about how to initialize a disk that is larger than 2 TiB, see Initialize a data disk that is larger than 2 TiB in size.

Procedures

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

  1. Connect to the ECS instance.
  2. Run the following command to obtain the device name of the data disk:
    fdisk -lu
    A command output similar to the following one is returned, which indicates that the instance has two disks: the /dev/vda system disk and the /dev/vdb data disk. Data disk information
  3. Create a partition for the data disk.

Step 2: Create a GPT partition for the data disk

Perform the following operations to create a GPT partition for the data disk whose size does not exceed 2 TiB. A GPT partition can be larger than 2 TiB in size.
Important An MBR partition cannot be larger than 2 TiB in size. If the disk for which a partition is created is larger than 2 TiB or if the disk may be resized to larger than 2 TiB, use the GPT partition format.
  1. Run the following command to install Parted:
    yum install -y parted
    Note The yum command in this step is suitable for 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. In this example, the /dev/vdb data disk is used.
      parted /dev/vdb
      /dev/vdb specifies the device name of the data disk. Replace it with the actual device name.
    2. Run the following command in the Parted interactive command-line prompt to set the partition format to GPT:
      mklabel gpt
    3. Run the following command to create a primary partition and specify the start sector and end sector for the partition.
      In this example, a primary partition named primary is created for the data disk and 100% of the disk capacity is allocated to the primary partition.
      mkpart primary 1 100%
    4. Run the following command to check whether the partition is aligned.
      The partition number of the primary partition is 1. We recommend that you align partitions for better disk performance.
      align-check optimal 1
      If the partition is aligned, a command output similar to the following one is returned:
      1 aligned
      Note If the partition is not aligned, 1 not aligned is returned. For information about how to resolve the issue, see FAQ about initializing disks.
    5. Run the following command to check the partition table:
      print
    6. Run the following command to exit Parted:
      quit
    The following figure shows the partition process by using Parted. 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 partition. In this example, the /dev/vdb data disk is used.
    fdisk -lu /dev/vdb
    /dev/vdb specifies the device name of the data disk. Replace it with the actual device name.
    A command output similar to the following one is returned. If the new partition is created, the information about gpt is displayed. gpt

Step 2: Create an MBR partition for the data disk

Perform the following operations to create an MBR partition for the data disk whose size does not exceed 2 TiB.
Important An MBR partition cannot be larger than 2 TiB in size. If the disk for which a partition is created is larger than 2 TiB or if the disk may be resized to larger than 2 TiB, use the GPT partition format.
  1. Create an MBR partition.
    1. Run the following command to partition the data disk. In this example, the /dev/vdb data disk is used.
      fdisk -u /dev/vdb
      /dev/vdb specifies the device name of the data disk. Replace it with the actual device name.
    2. Enter p in the fdisk interactive command-line prompt to check the partition information of the data disk.
    3. Enter n to create a partition.
    4. Enter p to specify the partition as a primary partition.
      Note When you create a single partition for a data disk, the partition must be a 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 as the partition number.
    6. Enter the number of the start sector and press the Enter key.
      In this example, the Enter key is pressed to use the default value 2048 as the number of the start sector.
    7. Enter the number of the end 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 partition information of the data disk.
    9. Enter w to start partitioning. Then, exit after the disk is partitioned.
    The following figure shows the partition process by using fdisk. Partition creation result
  2. Run the following command to view the new partition. In this example, the /dev/vdb data disk is used.
    fdisk -lu /dev/vdb
    /dev/vdb specifies the device name of the data disk. Replace it with the actual device name.
    A command output similar to the following one is returned. If the new partition is created, the information of /dev/vdb1 is displayed. Partition result

Step 3: Create a file system for the partition

Create a file system for the new partition. Partition sizes vary based on file system types. Create a file system that suits your needs.

  1. Run the following command to install e2fsprogs:
    yum install -y e2fsprogs
    Note The yum command in this step is suitable for 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. Run the following command to create a file system. The following commands are used to create an ext4 file system and an xfs file system.
    • Create an ext4 file system
      mkfs -t ext4 /dev/vdb1
      /dev/vdb1 specifies the partition name of the data disk. Replace it with the actual partition name.
      Important
      • If an error is reported when you create an ext4 file system for a 16 TiB data disk, the version of the e2fsprogs package may be earlier than 1.42. Upgrade the version of e2fsprogs to 1.42 or later. For more information, see the How do I upgrade e2fsprogs on a Linux instance? section in this topic.
      • The lazy init feature of ext4 file systems affects the I/O performance of data disks. You can disable the lazy init feature of ext4 file systems. For more information, see the How do I disable the lazy init feature on a Linux instance? section in this topic.
    • Create an xfs file system
      mkfs -t xfs /dev/vdb1
      /dev/vdb1 specifies the partition name of the data disk. Replace it with the actual partition name.
    A command output similar to the following one is returned. In this example, an ext4 file system is created for the /dev/vdb1 partition of the data disk. Create a file system

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

If you want the system to mount the data disk on instance startup, write the information of the disk partition to /etc/fstab and configure the disk partition to automatically mount on instance startup.
Important We recommend that you use a universally unique identifier (UUID) to reference the new partition in /etc/fstab. Operations that are performed on a disk, such as release operations, may cause the device names of other disks to change. If you use the device name of the data disk in /etc/fstab, your stored data may be affected if the device name is changed.
  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 partition name of the data disk. Replace it with the actual partition name.
      • /mnt: the mount point of the partition. Replace it with the actual mount point.
      • ext4: the file system type of the partition. Replace it with the file system type of the created file system.
      Note Ubuntu 12.04 operating systems do not support barriers. You must run the echo `blkid /dev/vdb1 | awk '{print $2}' | sed 's/\"//g'` /mnt ext4 barrier=0 0 0 >> /etc/fstab command to write the information of the new partition to /etc/fstab. If your instance does not run an Ubuntu 12.04 operating system, ignore the note.
    • If you are a common user, you can 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 system that is configured in /etc/fstab. If the file system is correctly configured in /etc/fstab, no error is reported.
    mount -a
  5. Run the following command to check whether the file system is mounted as expected:
    df -h
    A command output similar to the following one is returned. If the file system is mounted, the information of the new file system is displayed. Query the mount result

FAQ about initializing disks

What do I do if a GPT partition is created but not aligned?

  1. Run the following commands to query the parameters of the disk:
    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. Run the following command to re-create a primary partition:
    mkpart primary <Recommended sector number>s 100%
    <Recommended sector number>=(<optimal_io_size> + <alignment_offset>)/<physical_block_size>. For example, if the sector number is 1024, you can run the mkpart primary 1024s 100% command to re-create a primary partition.

How do I upgrade e2fsprogs on a Linux instance?

To format a 16 TiB data disk with ext4 file systems, you must use e2fsprogs 1.42 or later. If e2fsprogs of a version earlier than 1.42 is used, the following error message is reported:
mkfs.ext4: Size of device /dev/vdb too big to be expressed in 32 bits using a blocksize of 4096.            

Perform the following steps to install a later version of e2fsprogs. In this example, e2fsprogs 1.42.8 is used.

  1. Run the following command to check the current e2fsprogs version:
    rpm -qa | grep e2fsprogs

    A command output similar to the following one is returned.

    Query the e2fsprogs version

    If the version is earlier than 1.42, perform the following operations to upgrade e2fsprogs:

  2. Run the following command to download e2fsprogs 1.42.8.
    You can visit the e2fsprogs website to obtain the latest software package.
    wget https://www.kernel.org/pub/linux/kernel/people/tytso/e2fsprogs/v1.42.8/e2fsprogs-1.42.8.tar.gz --no-check-certificate
  3. Compile the later version of e2fsprogs.
    1. Run the following command to decompress the software package:
      tar xvzf e2fsprogs-1.42.8.tar.gz
    2. Run the following command to go to the package directory:
      cd e2fsprogs-1.42.8
    3. Run the following command to generate the Makefile file:
      ./configure
    4. Run the following command to compile e2fsprogs:
      make
    5. Run the following command to install e2fsprogs:
      make install
  4. Run the following command to check whether e2fsprogs is updated:
    rpm -qa | grep e2fsprogs

How do I disable the lazy init feature on a Linux instance?

By default, the lazy init feature of ext4 file systems is enabled. When the feature is enabled, the instance initiates a thread to continuously initialize the metadata of ext4 file systems. If you format a data disk when the feature is enabled, the IOPS of the disk may be temporarily affected.

If you want to test the performance of a data disk immediately after the disk is formatted, run the following command to disable the lazy init feature when you format the file system:
mke2fs -O 64bit,has_journal,extents,huge_file,flex_bg,uninit_bg,dir_nlink,extra_isize -E lazy_itable_init=0,lazy_journal_init=0   /dev/vdb1
Note If the lazy init feature is disabled, a long period of time may be required to format a disk. For example, a 32 TiB data disk may require 10 to 30 minutes to be completely formatted. Disable the lazy init feature based on your needs.