All Products
Search
Document Center

Elastic Compute Service:Initialize a data disk whose size does not exceed 2 TiB on a Linux instance

Last Updated:Dec 04, 2024

After you attach a new data disk to a Linux Elastic Compute Service (ECS) instance, you must initialize the data disk before the disk can be recognized by the operating system and used to store data. If the size of the data disk does not exceed 2 TiB and you want to store and manage data in different categories in different partitions, you can perform the initialization operations described in this topic. The initialization operations include partitioning the data disk and mounting file systems on the disk.

Note

You can perform the initialization operations described in this topic only on empty data disks that are created together with Linux ECS instances or empty data disks that are separately created and attached to ECS instances. For more information, see the Scenarios section of the "Overview" topic.

Prerequisites

A data disk is attached to a Linux ECS instance and is in the In Use state. For more information, see Attach a data disk.

Note

A data disk created together with a Linux ECS instance is already attached to the instance.

Procedure

In the following examples, an ECS instance that runs Alibaba Cloud Linux 3 is used. The commands that you can run to initialize a data disk may vary based on the instance operating system.

Step 1: Connect to the Linux ECS instance and view the data disks

  1. Connect to the Linux ECS instance.

  2. Run the following command to obtain the device names of the data disks:

    sudo fdisk -lu

    The following command output indicates that two disks are attached to the Linux ECS instance:

    • ①: /dev/vda is the system disk, which is created together with the instance and already initialized.

    • ②: /dev/vdb is a data disk, which needs to be initialized.

    image

(Conditionally required) Step 2: Create partitions on the data disk

If you want to individually store, maintain, and manage data in different categories, such as applications, files, and storage data, in different partitions, perform this step to create partitions on the data disk. If you want to use the data disk as a raw disk, you can create a file system for the disk without the need to partition the disk. For more information, see Create a file system on a raw disk.

Important
  • The maximum size of a partition and the maximum number of partitions per disk vary based on the partition format. For more information, see the Partition formats section in the "Overview" topic.

  • If you want to subsequently extend the data disk to a size larger than 2 TiB, partition the disk in the GUID partition table (GPT) format. If you partition the data disk in the Master Boot Record (MBR) format, you cannot extend the disk to a size larger than 2 TiB. If you extend the data disk in the MBR partition format to a size larger than 2 TiB, you may need to repartition and reformat the disk, which causes data loss.

Create a GPT partition

Perform the following steps to create a GPT partition on a data disk whose size does not exceed 2 TiB by using the Parted tool:

  1. Run the following command to install Parted:

    sudo yum install -y parted
    Note

    The yum command used 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 following commands to install Parted on Debian or Ubuntu:

    sudo apt-get update
    sudo apt-get install -y parted
  2. Use Parted to partition the data disk.

    1. Run the following command to partition the data disk. In this example, the /dev/vdb data disk is used.

      sudo parted /dev/vdb

      /dev/vdb is the device name of the data disk. Replace /dev/vdb with the actual device name of your data disk.

    2. On the Parted command line, 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 positions of the partition.

      mkpart <Partition name> <Start position> <End position>
      Important

      The start and end positions of the partitions are specified in units of MiB or GiB (4096B by default). If the units are incorrect, it will affect partition alignment and impact the performance of the disk.

      Specify the <Partition name>, <Start position>, and <End position> values based on your needs.

      • Single partition example: A primary partition named primary is created on the data disk. The start position of the partition is 1 MiB, and 100% of the disk capacity is allocated to the primary partition.

        mkpart primary 1MiB 100%
      • multi-partition example: Two partitions are created on the data disk. The first partition is named primary with the capacity of 20 GiB. The rest of the disk capacity is allocated to the second partition named secondary.

        mkpart primary 1MiB 20GiB
        mkpart secondary 20GiB 100%
    4. (Conditionally required) If you want to create multiple partitions on the data disk, repeat Step c.

    5. Run the following command to check whether the partition is aligned.

      The number of the primary partition is 1. To optimize disk performance, we recommend that you align the partition.

      align-check optimal 1

      If the partition is aligned, the following command output is returned:

      1 aligned
      Note

      If the partition is misaligned, 1 not aligned is returned. For information about how to resolve the misalignment issue, see the FAQ about disk initialization section in this topic.

    6. Run the following command to check the partition table:

      print
    7. Run the following command to exit Parted:

      quit

    The following figure shows the partitioning process.

    image.png

  3. Run the following command to reread the partition table:

    partprobe
  4. Run the following command to view information about the new partition. In this example, the /dev/vdb data disk is used.

    sudo fdisk -lu /dev/vdb

    /dev/vdb is the device name of the data disk. Replace /dev/vdb with the actual device name of your data disk.

    The following command output is returned. If the new partition is created, information about GPT is displayed.

    image.png

Create an MBR partition

Perform the following steps to create an MBR partition on a data disk whose size does not exceed 2 TiB by using fdisk.

  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.

      sudo fdisk -u /dev/vdb

      /dev/vdb is the device name of the data disk. Replace /dev/vdb with the actual device name of your data disk.

      The following command output is returned.

      image.png

    2. On the fdisk command line, enter p and press the Enter key to check the partition information of the data disk.

      The following command output is returned.

      image.png

    3. Enter n and press the Enter key to partition the data disk.

      The following command output is returned.

      image.png

      The data disk has two types of partitions, as shown in the preceding figure.

      • p: primary partition.

      • e: extended partition.

        Note

        For information about partitions, see the Partition formats section of the "Overview" topic.

    4. In this example, only one primary partition is created. Enter p and press the Enter key to create a primary partition.

      The following command output is returned.

      image.png

      Partition number indicates the number of the primary partition, which ranges from 1 to 4.

    5. Enter the partition number and press the Enter key.

      In this example, only one primary partition is created. Press the Enter key to use the default value 1 as the number of the primary partition.

      The following command output is returned.

      image.png

    6. Enter a start sector number and press the Enter key.

      First sector specifies the start sector number of the partition. Valid values: 2048 to 83886079. Default value: 2048. In this example, the Enter key is pressed to use the default value 2048 as the start sector number.

      The following command output is returned.

      image.png

      Note

      The range of the start sector numbers and the range of the end sector numbers that are available for your selection are automatically displayed. You can specify a start sector number and an end sector number included the ranges or use the default values. The start sector number must be smaller than the end sector number.

    7. Enter an end sector number and press the Enter key.

      Last sector indicates the end sector number of the partition. Valid values: 2048 to 83886079. Default value: 83886079. In this example, the Enter key is pressed to use the default value 83886079 as the end sector number.

      The following command output is returned.

      image.png

      Note

      The range of the start sector numbers and the range of the end sector numbers that are available for your selection are automatically displayed. You can specify a start sector number and an end sector number included the ranges or use the default values. The start sector number must be smaller than the end sector number.

    8. Enter p and press the Enter key to view the partition information of the data disk.

      The following command output is returned, which contains the detailed information about the new partition /dev/vdb1.

      image.png

      Note

      If the preceding partitioning operations are not successful, enter q to exit fdisk. The partitioning results are not retained. You can perform the preceding steps again.

    9. Enter w and press the Enter key to write the partitioning results to the partition table.

      The following command output indicates that the partition is created.

      image.png

  2. (Conditionally required) If you want to create multiple partitions on the data disk, repeat Step 1.

  3. Run the following command to view the information of the new partition. In this example, the /dev/vdb data disk is used.

    sudo fdisk -lu /dev/vdb

    /dev/vdb is the device name of the data disk. Replace /dev/vdb with the actual device name of your data disk.

    The following command output is returned. If the new partition is created, information about /dev/vdb1 is displayed.

    image.png

Step 3: Create and mount a file system

You must create and mount file systems on a data disk regardless of whether the disk is partitioned before the disk can be recognized by the operating system of the ECS instance to which the disk is attached and be used as expected. The types of file systems that you can create vary based on the partition size. Create a file system that meets your business requirements.

  1. Run the following command to install e2fsprogs:

    sudo yum install -y e2fsprogs
    Note

    The yum command used 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. Create a file system. In the examples, an Ext4 file system and an XFS file system are created.

    Create an Ext4 file system

    sudo mkfs -t ext4 /dev/vdb1

    /dev/vdb1 is the partition name of the data disk. Replace /dev/vdb1 with an actual partition name.

    Note
    • If an error occurs when you create an Ext4 file system for a 16 TiB data disk, the version of the e2fsprogs package that you use may be earlier than 1.42. Upgrade the version of e2fsprogs to version 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.

    The following command output is returned, which indicates that an Ext4 file system is created for the /dev/vdb1 partition of the data disk.

    image.png

    Create an XFS file system

    sudo mkfs -t xfs /dev/vdb1

    /dev/vdb1 is the partition name of the data disk. Replace /dev/vdb1 with an actual partition name.

    The following command output is returned, which indicates that an XFS file system is created for the /dev/vdb1 partition of the data disk.

    image.png

  3. Run the following command to mount the file system.

    Note

    You can run the mount command with different mount parameters to mount Ext4 file systems based on your business requirements, including data security requirements and performance requirements. For more information, see Mount Ext4 file systems with mount command options.

    sudo mount /dev/vdb1 /mnt

    Take note of the following parameters in the command:

    • /dev/vdb1 is the partition name of the data disk. Replace /dev/vdb1 with an actual partition name.

    • /mnt is the mount point of the partition. Replace /mnt with an existing directory. Alternatively, run the sudo mkdir -p <New directory> command to create a directory, and then replace /mnt with the new directory. Sample command: sudo mkdir -p /data.

  4. Run the following command to check whether the file system is mounted as expected:

    df -Th

    The following command output is returned. If the file system is mounted, information about the new file system is displayed.

    image

Step 4: Configure the disk partition to automatically mount on instance startup

If you want the disk partition to automatically mount on instance startup, write information about the disk partition to the /etc/fstab file and add the mount information of the disk partition to the /etc/fstab file based on your business requirements.

  1. Run the following command to back up the etc/fstab file:

    sudo cp /etc/fstab /etc/fstab.bak
  2. Write information about the new partition to the /etc/fstab file.

    sudo sh -c "echo `blkid /dev/vdb1 | awk '{print \$2}' | sed 's/\"//g'` /mnt ext4 defaults 0 0 >> /etc/fstab"

    Take note of the following parameters in the command:

    • /dev/vdb1: the partition name of the data disk. Replace /dev/vdb1 with an actual partition name. You can run the df -Th command to obtain the names of partitions.

    • /mnt: the mount point of the partition. Replace /mnt with an actual mount point. You can run the df -Th command to obtain the mount points of partitions.

    • ext4: the file system type of the partition. Replace ext4 with the type of the file system that you created. You can run the df -Th command to obtain the file system types of partitions.

    • defaults: the mount parameter of the file system. Specify mount parameters based on your business requirements. For more information, see fstab.

    Note

    The Ubuntu 12.04 operating system does not support barriers. You must run the sudo sh -c "echo `blkid /dev/vdb1 | awk '{print $2}' | sed 's/\"//g'` /mnt ext4 barrier=0 0 0 >> /etc/fstab" command to write information about the disk partition to the /etc/fstab file.

  3. (Conditionally required) If a data disk has multiple consecutive partitions, run a command specific to each partition to write partition information to the /etc/fstab file.

    For example, for the /dev/vdb data disk that has the /dev/vdb1 and /dev/vdb2 partitions, separately run the following commands:

    sudo sh -c "echo `blkid /dev/vdb1 | awk '{print \$2}' | sed 's/\"//g'` <Mount point of the /dev/vdb1 partition> <File system type of the /dev/vdb1 partition> defaults 0 0 >> /etc/fstab"
    sudo sh -c "echo `blkid /dev/vdb2 | awk '{print \$2}' | sed 's/\"//g'` <Mount point of the /dev/vdb2 partition> <File system type of the /dev/vdb2 partition> defaults 0 0 >> /etc/fstab"
  4. Run the following command to view information about the new partition in the /etc/fstab file:

    cat /etc/fstab

    The following command output is returned, which contains information about the /dev/vdb1 partition.

    image.png

    Note

    If the information about the partition contained in the command output is incorrect, you can delete the incorrect partition information from the /etc/fstab file and add the correct information about the partition to the file. For more information, see Configure UUIDs in the fstab file to automatically attach data disks.

  5. Run the following commands to check whether the partition is automatically mounted:

    sudo umount /dev/vdb1
    sudo mount -a
    mount | grep /mnt

    The following command output is returned. If the file system of the partition is mounted, the mount information of the file system is displayed.

    image

FAQ about disk initialization

What do I do if a GPT partition is created on a disk but is misaligned?

  1. Run the following command to query the parameters of the disk. In this example, the /dev/vdb data disk is used.

    sudo parted /dev/vdb
  2. In the interface of the Parted tool, run the following command to remove the incorrect partition. Run the print command to check the incorrect partition number within the partition table. Take number 1 for example.

    Important

    Before you remove a partition, make sure there is no data inside the partition or you have backed up the data.

    rm 1
  3. Run the following command to re-create a primary partition, with units of MiB. Ensure that the start and end positions of the partitions are specified in units of MiB or GiB.

    mkpart primary <Start position>MiB <End position>MiB

How do I upgrade e2fsprogs on a Linux instance?

To format a 16 TiB data disk that has Ext4 file systems, you must use e2fsprogs whose version is 1.42 or later. If you use e2fsprogs of a version earlier than 1.42, 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 e2fsprogs whose version is 1.42 or later. In this example, e2fsprogs 1.42.8 is installed.

  1. Run the following command to check the version of e2fsprogs that you use:

    rpm -qa | grep e2fsprogs

    The following command output is returned.

    查询版本

    If the version of e2fsprogs 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 upgraded:

    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. If 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 lazy init 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 you format the disk, run the following command to disable the lazy init feature when you format the file system:

sudo 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, an extended period of time may be required to format a disk. For example, a 32 TiB data disk may require 10 minutes to 30 minutes to be formatted. Disable the lazy init feature based on your business requirements.

References

  • If your disk is larger than 2 TiB in size and you want to store and manage data in different categories in different partitions on the disk, initialize the disk. For more information, see the Initialize a data disk whose size exceeds 2 TiB on a Linux instance section of the "Initialize a data disk whose size exceeds 2 TiB" topic.

  • If you want to use a disk as a raw disk without the need to partition the disk, you need to only create a file system for the raw disk when you initialize the disk. For more information, see Create a file system on a raw disk.

  • If a disk does not have sufficient storage space, you can extend the capacity of the disk to increase the storage space. For information about how to extend a disk, see Overview.