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:Mar 18, 2024

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

In the examples, an ECS instance and a data disk that have the configurations described in the following table are used. Operations may vary based on actual conditions.

Operating system

Resource

Description

Linux

Image used by the ECS instance

Alibaba 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 in size, see Initialize a data disk that is larger than 2 TiB in size.

Procedure

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

  1. Connect to the Linux instance.

  2. Run the following command to obtain the device name of the data disk:

    sudo fdisk -lu

    A command output similar to the following one is returned, which indicates that the instance has two disks: the system disk whose device name is /dev/vda and the new data disk whose device name is /dev/vdb.

    image.png

Step 2: Create a partition on the data disk

Important
  • The size of a disk that uses the master boot record (MBR) partition format is capped at 2 TiB. If your disk is larger than 2 TiB in size or the disk may be resized to larger than 2 TiB, use the GUID partition table (GPT) partition format.

  • Parted can be used to create MBR partitions and GPT partitions. fdisk can be used to create only MBR partitions.

Create a GPT partition

Perform the following steps to create a GPT partition on the data disk that does not exceed 2 TiB in size by using Parted. Disks that use the GPT partition format can be larger than 2 TiB in size.

  1. Run the following command to install Parted:

    sudo 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 partition the data disk. In this example, the /dev/vdb data disk is partitioned.

      sudo parted /dev/vdb

      /dev/vdb is the device name of the data disk. Replace it 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.

      In this example, a primary partition named primary is created on the data disk and 100% of the disk capacity is allocated to the primary partition.

      mkpart primary 1 100%
      Note

      If you want to create multiple partitions on the data disk, repeat this step.

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

      The 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 the FAQ about initializing disks section in this topic.

    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 partitioning process.

    image.png

  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.

    sudo fdisk -lu /dev/vdb

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

    A command output similar to the following one is returned. If the new partition is created, the information about gpt is displayed.

    image.png

Create an MBR partition

Perform the following steps to create an MBR partition on the data disk that does not exceed 2 TiB in size 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 it with the actual device name of your data disk.

      A command output similar to the following one 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.

      A command output similar to the following one is returned.

      image.png

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

      A command output similar to the following one 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

        Up to four partitions can be created on an MBR disk. The partitions can be all primary partitions, or three primary partitions and one extended partition. The extended partition must be divided into logical partitions before it can be used. For more information, see Partition formats.

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

      A command output similar to the following one 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.

      A command output similar to the following one 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, press the Enter key to use the default value 2048 as the start sector number.

      A command output similar to the following one is returned.

      image.png

      Note

      The range of start sector numbers and range of end sector numbers that are available for your selection are automatically displayed. You can specify a start sector number and an end sector number within 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 specifies the end sector number of the partition. Valid values: 2048 to 83886079. Default value: 83886079. In this example, press the Enter key to use the default value 83886079 as the end sector number.

      A command output similar to the following one is returned.

      image.png

      Note

      The range of start sector numbers and range of end sector numbers that are available for your selection are automatically displayed. You can specify a start sector number and an end sector number within 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.

      A command output similar to the following one is returned, which indicates the detailed information about the new partition /dev/vdb1.

      image.png

      Note

      If the preceding partitioning operations are not properly performed, enter q to exit fdisk. The partitioning results are not retained. You can reperform the preceding steps.

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

      A command output similar to the following one is returned, which indicates that the partition is created.

      image.png

      Note

      If you want to create multiple partitions on the data disk, repeat this step.

  2. Run the following command to view 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 it with the actual device name of your data disk.

    A command output similar to the following one is returned. If the new partition is created, the information of /dev/vdb1 is displayed.

    image.png

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 meets your business requirements.

  1. Run the following command to install e2fsprogs:

    sudo 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. 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 it with the actual device name of your data disk.

    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 that you use 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.

    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.

    image.png

    Create an xfs file system

    sudo mkfs -t xfs /dev/vdb1

    /dev/vdb1 is the partition name of the data disk. Replace it with the actual device name of your data disk.

    A command output similar to the following one is returned. In this example, an xfs file system is created for the /dev/vdb1 partition of the data disk.

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

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 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 a 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 the etc/fstab file:

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

    Root users

    Run the following command to modify the /etc/fstab file:

    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 it with the actual partition name.

    • /mnt: the mount point of the partition. Replace it with the actual mount point.

      Important

      You need to specify or create a directory as the mount point based on the actual environment. You can mount the partition to an existing directory, or run the mkdir command to create a directory and then mount the partition to the new directory.

    • ext4: the file system type of the partition. Replace it with the type of the created file system.

    • defaults: the mount parameters of the file system. Configure parameters based your business requirements to ensure the data security and performance of the file system. For more information, see Run the mount commands to mount Ext4 file systems.

    Note
    • The Ubuntu 12.04 operating system does 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 a disk has multiple consecutive partitions, run a command specific to each partition to write partition information to /etc/fstab. For example, for the /dev/vdb data disk that has the /dev/vdb1 and /dev/vdb2 partitions, run the following commands separately:

      echo `blkid /dev/vdb1 | awk '{print $2}' | sed 's/\"//g'` <Mount point of /dev/vdb1> <File system type of /dev/vdb1> defaults 0 0 >> /etc/fstab
      echo `blkid /dev/vdb2 | awk '{print $2}' | sed 's/\"//g'` <Mount point of /dev/vdb2> <File system type of /dev/vdb2> defaults 0 0 >> /etc/fstab

    Common users

    Manually modify the /etc/fstab file. For more information, see Configure UUIDs in the fstab file to automatically mount 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.

    image.png

  4. Run the following command to mount the file system that is configured in the /etc/fstab file. If the file system is correctly configured in /etc/fstab, no error is reported.

    sudo 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.

    image.png

FAQ about initializing disks

What do I do if a GPT partition is created on a disk 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%

    The recommended sector number is calculated based on the following formula: <Recommended sector number> = (<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 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 version of e2fsprogs that you use:

    rpm -qa | grep e2fsprogs

    A command output similar to the following one is returned.

    查询版本

    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 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. 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:

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, 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 business requirements.