file

Initialize TiB data disks less than or equal to 2 (Linux)

After a new data disk is attached to the ECS instance, it cannot directly store data. Usually, you need to initialize the data disk for partitioning, file system formatting, and mounting the file system before the system can read or write data. This article describes how to initialize a data disk less than or equal to 2 TiB in a Linux system.

prerequisite

The data disk has been attached to the ECS instance. See Attach data disk

background information

The configuration used in this example is shown below. You need to operate according to the actual environment.

operating system

resources

describe

Linux system

ECS instance image

Public image Alibaba Cloud Linux 3.2104 64 bit

Data disk

explain

If the cloud disk capacity you use is greater than 2 TiB, please refer to Initialize the TiB data disk larger than 2

Operation steps

Step 1: Log in to the ECS instance and view the data disk

  1. Connect ECS instances remotely.

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

     sudo fdisk -lu

    The running results are as follows, indicating that the current ECS instance has two cloud disks, /dev/vda Is the system disk, /dev/vdb It is a new data disk.

     image.png

Step 2: Create partitions for the data disk

important
  • MBR partitions do not support capacities above 2 TiB. If the cloud disk capacity you use is greater than 2 TiB or may be expanded to more than 2 TiB later, please use GPT partition format when partitioning.

  • The Parted tool is applicable to MBR partitions and GPT partitions. The fdisk partitioning tool is only applicable to MBR partitions.

Create GPT partition

Follow the steps below to create GPT partitions for data disks less than or equal to 2 TiB through the Parted partitioning tool. GPT partitions also support capacities above 2 TiB.

  1. Run the following command to install the Parted tool.

     sudo yum install -y parted
    explain

    The yum command in this step is applicable to Linux distributions such as CentOS. For other Linux distributions, please modify the installation command according to the actual package management software. For example, Debian or Ubuntu, use Apt get install<package name>

  2. Use the Parted tool to partition the data disk.

    1. Run the following command to start partitioning. This operation is based on the data disk /dev/vdb Take for example.

       sudo parted /dev/vdb

      Among them, /dev/vdb It is the device name of the data disk. Please replace it according to the actual environment.

    2. Enter the following in the Parted interactive command line to set the GPT partition format.

       mklabel gpt
    3. Enter the following contents to divide a primary partition, and set the start and end positions of the partition.

      This operation takes the partition of a primary partition, the partition name is primary, and 100% capacity is given to the first partition as an example.

       mkpart primary 1 100%
      explain

      If you need to create multiple partitions for a single cloud disk, you need to repeat this step to create other partitions.

    4. Run the following command to check the partition alignment.

      Among them, one It is the corresponding partition number. It is generally recommended that you align partitions to obtain better cloud disk performance.

       align-check optimal 1

      If the partition is aligned, the result is as follows:

       1 aligned
      explain

      If the return is 1 not aligned , indicating that the partition is not aligned. See FAQ for initializing cloud disk

    5. Enter the following to view the partition table.

       print
    6. Enter the following content to exit the Parted tool.

       quit

    The partitioning process is shown below.

     image.png

  3. Run the following command to make the system reread the partition table.

     partprobe
  4. Run the following command to view the new partition information. This operation is based on the data disk /dev/vdb Take for example.

     sudo fdisk -lu /dev/vdb

    Among them, /dev/vdb It is the device name of the data disk. Please replace it according to the actual environment.

    The running results are as follows. If the relevant information about gpt appears, the new partition has been created.

     image.png

Create MBR partition

Follow the steps below to create an MBR partition for data disks less than or equal to 2 TiB through the fdisk partition tool.

  1. Create an MBR partition.

    1. Run the following command to partition the data disk. This operation is based on the data disk /dev/vdb Take for example.

       sudo fdisk -u /dev/vdb

      Among them, /dev/vdb It is the device name of the data disk. Please replace it according to the actual environment.

      Echo information is shown in the following figure.

       image.png

    2. On the fdisk interactive command line, enter p And press Enter Key to view the partition of the data disk.

      Echo information is shown in the following figure.

       image.png

    3. input n And press Enter Key to start creating a new partition.

      Echo information is shown in the following figure.

       image.png

      Indicates that the cloud disk has two partition types:

      • p Represents the primary partition.

      • e Represents an extended partition.

        explain

        The cloud disk uses the MBR partition form. You can create up to four primary partitions, or three primary partitions and one extended partition. The extended partition cannot be used directly, but needs to be divided into several logical partitions. For more information, see Introduction to partition format

    4. Take creating a primary partition as an example. input p And press Enter Key to start creating a primary partition.

      Echo information is shown in the following figure.

       image.png

      Partition number Indicates the number of the primary partition. You can select 1-4.

    5. Enter the partition number and press Enter Key.

      This example takes creating a primary partition as an example Press Enter key , indicating that the partition number of the primary partition is one

      Echo information is shown in the following figure.

       image.png

    6. Enter the starting sector number and press Enter Key.

      First sector Indicates the starting sector number of the partition free space. You can select 2048-83886079, and the default value is 2048. In this example, directly Press Enter key , indicating that the default start sector number is selected two thousand and forty-eight

      Echo information is shown in the following figure.

       image.png

      explain

      The system will automatically prompt the starting sector number and ending sector number of the available space of the partition. You can customize or use the default value in this range. The starting sector number of the partition must be less than the ending sector number.

    7. Enter the last sector number and press Enter Key.

      Last sector Indicates the end sector number of the partition's available space. You can select 2048-83886079, and the default value is 83886079. In this example, directly Press Enter key , indicating that the default cut-off sector number is selected eighty-three million eight hundred and eighty-six thousand and seventy-nine

      Echo information is shown in the following figure.

       image.png

      explain

      The system will automatically prompt the starting sector number and ending sector number of the available space of the partition. You can customize or use the default value in this range. The starting sector number of the partition must be less than the ending sector number.

    8. input p And press Enter Key to view the partition planning of the data disk.

      Echo information is as shown in the figure below, indicating the new partition /dev/vdb1 Details of.

       image.png

      explain

      If the above partition operation is incorrect, please enter q Exit the fdisk partition tool, and the previous partition results will not be retained. You can re partition according to the above steps.

    9. input w And press Enter Key to write the partition results to the partition table.

      The echo information is shown in the following figure, indicating that the partition creation is completed.

       image.png

      explain

      If you need to create multiple partitions for a single cloud disk, you need to repeat this step to create other partitions.

  2. Run the following command to view the new partition information. This operation is based on the data disk /dev/vdb Take for example.

     sudo fdisk -lu /dev/vdb

    Among them, /dev/vdb It is the device name of the data disk. Please replace it according to the actual environment.

    The running result is shown in the figure below. If /dev/vdb1 Indicates that the new partition has been created.

     image.png

Step 3: Create a file system for the partition

Create a file system on the new partition. Different file systems support different partition sizes. Please create file systems according to actual needs.

  1. Run the following command to install the e2fsprogs tool.

     sudo yum install -y e2fsprogs
    explain

    The yum command in this step is applicable to Linux distributions such as CentOS. For other Linux distributions, please modify the installation command according to the actual package management software. For example, Debian or Ubuntu, use Apt get install<package name>

  2. Run the following command to create a file system. The following example command describes how to create ext4 and xfs file systems.

    Create ext4 file system

     sudo mkfs -t ext4 /dev/vdb1

    Among them, /dev/vdb1 Is the partition name of the data disk. Please replace it according to the actual environment.

    important
    • When the data disk has a capacity of 16 TiB and an error is reported when creating an ext4 file system, you need to check whether the e2fsprogs toolkit is higher than 1.42, and install a higher version of the e2fsprogs toolkit. See How to upgrade the e2fsprogs toolkit?

    • The ext4 file system's lazy init function will affect the I/O performance of the data disk. You can turn off the ext4 file system's lazy init function. See How to turn off the lazy init function of a Linux instance?

    The example information shown in the figure below is represented as data disk partition /dev/vdb1 Create an ext4 file system.

     image.png

    Create xfs file system

     sudo mkfs -t xfs /dev/vdb1

    Among them, /dev/vdb1 Is the partition name of the data disk. Please replace it according to the actual environment.

    The example information shown in the figure below is represented as data disk partition /dev/vdb1 Create an xfs file system.

     image.png

Step 4: Configure Auto attach partition after power on

If you need to set the cloud disk to be automatically attached at power on, you need to set the /etc/fstab Write the new partition information in, and start the boot to automatically mount the partition.

important

It is recommended that you /etc/fstab The globally unique identifier UUID is used in to reference the new partition. The device name of other cloud disks may change due to operations such as releasing cloud disks /etc/fstab The device name is directly used in. When the device name changes, your storage data may be affected.

  1. Run the following command to backup /etc/fstab File.

     sudo cp /etc/fstab /etc/fstab.bak
  2. stay /etc/fstab Write the new partition information in.

    Root user

    Run the following command to modify /etc/fstab File.

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

    Including:

    • /dev/vdb1 : The partition name of the data disk. You need to replace it according to the actual environment.

    • /mnt : The mount point of the partition. You need to replace it according to the actual environment.

      important

      This directory needs to be specified according to the actual environment. Please specify or create the directory to be mounted correctly. You can mount the partition to the existing directory of the environment, or you can first use the mkdir Command to create a new directory, and then mount it.

    • ext4 : The file system type of the partition. You need to modify it according to the created file system type.

    • defaults : The mount parameters of the file system. Please select the corresponding mount parameters according to the actual needs to meet your requirements for data security and performance of the file system. For more information, see Use the mount command to mount the ext4 file system

    explain
    • The Ubuntu 12.04 system does not support barrier. You need to run echo `blkid /dev/vdb1 | awk '{print $2}' | sed 's/\"//g'` /mnt ext4 barrier=0 0 0 >> /etc/fstab Command. If your operating system is not Ubuntu 12.04, please ignore this instruction.

    • If a single disk has multiple consecutive partitions, the partition information needs to be written separately /etc/fstab Medium. For example, data disk /dev/vdb There are 2 partitions /dev/vdb1 and /dev/vdb2 , you need to execute the following commands respectively:

       Echo ` blkid/dev/vdb1 | awk '{print $2}' | sed's/ "//g '`<mount point of/dev/vdb1><file system type of/dev/vdb1>defaults 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>>/etc/fstab

    Ordinary users

    Manual modification /etc/fstab File. See Configure UUID mode in fstab file to automatically mount data disk

  3. Run the following command to view /etc/fstab New partition information in.

     cat /etc/fstab

    The results are shown below.

     image.png

  4. Run the following command to mount /etc/fstab Configured file system. If there is no error output, it means your /etc/fstab The configuration is correct.

     sudo mount -a
  5. Run the following command to check whether the mount result meets your expectations.

     df -h

    The results are as follows. If the information of creating a new file system appears, the file system is mounted successfully.

     image.png

FAQ for initializing cloud disk

How to solve the problem of misaligned partitions when creating GPT partitions?

  1. Run the following command to view cloud disk parameters.

     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 subdivide a primary partition.

     Mkpart primary<recommended sector value>s 100%

    Among them, <Recommended sector value>=(<optimal_io_size>+<alignment_offset>)/<physical_block_size> For example, if 1024 is the calculated recommended sector value, you can run mkpart primary 1024s 100% Redivide a primary partition.

How to upgrade the e2fsprogs toolkit for Linux instances?

If the data disk capacity is 16 TiB, you need to use the e2fsprogs toolkit version 1.42 and above to complete the ext4 file system format. If the e2fsprogs version is lower than 1.42, the following error message will appear.

 mkfs.ext4: Size of device /dev/vdb too big to be expressed in 32 bits using a blocksize of 4096.

You need to install a higher version of e2fsprogs in the following way, such as 1.42.8 used in this example.

  1. Run the following command to check the current version of e2fsprogs.

     rpm -qa | grep e2fsprogs

    The running results are shown below.

     Query version

    If the current version is lower than 1.42, follow the steps below to install the software.

  2. Run the following command to download e2fsprogs of version 1.42.8.

    You can also visit e2fsprogs Find the latest 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 a higher version of the tool.

    1. Run the following command to decompress the software package.

       tar xvzf e2fsprogs-1.42.8.tar.gz
    2. Run the following command to enter the software package directory.

       cd e2fsprogs-1.42.8
    3. Run the following command to generate a 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 the version is updated successfully.

     rpm -qa | grep e2fsprogs

How to turn off the lazy init function of a Linux instance?

The ext4 file system enables the lazy init function by default. When this function is enabled, the instance will initiate a thread to continuously initialize the metadata of the ext4 file system, thus delaying metadata initialization. Therefore, in the near future after the data disk is formatted, the IOPS performance of the cloud disk will be affected, and the IOPS performance test data will be significantly lower.

If you need to test the performance of the data disk immediately after formatting, run the following command to turn off the lazy_init function when formatting 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
explain

After the lazy init function is turned off, the formatting time will be greatly extended. Formatting 32 TiB data disks may take 10 to 30 minutes. Please choose whether to use the lazy init function according to your own needs.

Related Documents

  • Introduction to this page (1)