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 |
|
For information about how to initialize a disk whose size exceeds 2 TiB, see Initialize a data disk whose size exceeds 2 TiB.
Procedure
Step 1: Connect to the Linux instance and view the data disk
Connect to the Linux instance.
For more information, see Connect to a Linux instance by using a password or key.
Run the following command to obtain the device name of the data disk:
sudo fdisk -lu
The following sample command output 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
.
Step 2: Create a partition on the data disk
The maximum size of a disk that uses the master boot record (MBR) partition format is 2 TiB. If the size of your disk exceeds 2 TiB 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 whose size does not exceed 2 TiB by using Parted. The size of disks that use the GPT partition format can exceed 2 TiB.
Run the following command to install Parted:
sudo yum install -y parted
NoteThe 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.Use Parted to partition the data disk.
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 /dev/vdb with the actual device name of your data disk.On the Parted command line, run the following command to set the partition format to GPT:
mklabel gpt
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%
NoteIf you want to create multiple partitions on the data disk, repeat this step.
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 partitions.align-check optimal 1
If the partition is aligned, the following sample command output is returned:
1 aligned
NoteIf 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.Run the following command to check the partition table:
print
Run the following command to exit Parted:
quit
The following figure shows the partitioning process.
Run the following command to reread the partition table:
partprobe
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 /dev/vdb with the actual device name of your data disk.The following sample command output is returned. If the new partition is created, the information about gpt is displayed.
Create an MBR partition
Perform the following steps to create an MBR partition on the data disk whose size does not exceed 2 TiB by using fdisk.
Create an MBR partition.
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 sample command output is returned.
On the fdisk command line, enter p and press the
Enter
key to check the partition information of the data disk.The following sample command output is returned.
Enter n and press the
Enter
key to partition the data disk.The following sample command output is returned.
The data disk has two types of partitions, as shown in the preceding figure.
p
: primary partitione
: extended partitionNoteUp 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. Before the extended partition can be used, the partition must be divided into logical partitions. For more information, see the Partition formats section in the "Overview" topic.
In this example, only one primary partition is created. Enter p and press the
Enter
key to create a primary partition.The following sample command output is returned.
Partition number
indicates the number of the primary partition, which ranges from 1 to 4.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 value1
as the number of the primary partition.The following sample command output is returned.
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, theEnter
key is pressed to use the default value2048
as the start sector number.The following sample command output is returned.
NoteThe 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.
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, theEnter
key is pressed to use the default value83886079
as the end sector number.The following sample command output is returned.
NoteThe 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.
Enter p and press the
Enter
key to view the partition information of the data disk.The following sample command output is returned, which indicates the detailed information about the new partition
/dev/vdb1
.NoteIf the preceding partitioning operations are not performed as expected, enter
q
to exit fdisk. The partitioning results are not retained. You can reperform the preceding steps.Enter w and press the
Enter
key to write the partitioning results to the partition table.The following sample command output indicates that the partition is created.
NoteIf you want to create multiple partitions on the data disk, repeat this step.
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 /dev/vdb with the actual device name of your data disk.The following sample command output is returned. If the new partition is created, the information about
/dev/vdb1
is displayed.
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.
Run the following command to install e2fsprogs:
sudo yum install -y e2fsprogs
NoteThe 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.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 the actual device name of your data disk.ImportantIf 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.
The following sample command output is returned. In this example, an ext4 file system is created for the
/dev/vdb1
partition of the data disk.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 the actual device name of your data disk.The following sample command output 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 about the disk partition to /etc/fstab and configure the disk partition to automatically mount on instance startup.
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 a data disk in /etc/fstab, your stored data may be affected if the device name is changed.
Run the following command to back up the etc/fstab file:
sudo cp /etc/fstab /etc/fstab.bak
Write the information about 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 /dev/vdb1 with the actual partition name./mnt
: the mount point of the partition. Replace /mnt with the actual mount point.ImportantSpecify 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 ext4 with the type of the created file system.defaults
: the mount parameters of the file system. Configure parameters based your business requirements to ensure data security and high performance of the file system. For more information, see Run the mount commands to mount Ext4 file systems.
NoteThe 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 about 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, separately run the following commands: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
Regular users
Modify the /etc/fstab file. For more information, see Configure UUIDs in the fstab file to automatically mount data disks.
Run the following command to check the information about the new partition in /etc/fstab:
cat /etc/fstab
The following sample command output is returned.
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
Run the following command to check whether the file system is mounted as expected:
df -h
The following sample command output is returned. If the file system is mounted, the information about the new file system is displayed.
FAQ about disk initialization
What do I do if a GPT partition is created on a disk but not aligned?
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
Run the following command to re-create a primary partition:
mkpart primary <Recommended sector number>s 100%
The recommended sector number is calculated by using 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 themkpart 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 e2fsprogs 1.42 or later. In this example, e2fsprogs 1.42.8 is used.
Run the following command to check the version of e2fsprogs that you use:
rpm -qa | grep e2fsprogs
The following sample command output is returned.
If the version is earlier than 1.42, perform the following operations to upgrade e2fsprogs.
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
Compile the later version of e2fsprogs.
Run the following command to decompress the software package:
tar xvzf e2fsprogs-1.42.8.tar.gz
Run the following command to go to the package directory:
cd e2fsprogs-1.42.8
Run the following command to generate the Makefile file:
./configure
Run the following command to compile e2fsprogs:
make
Run the following command to install e2fsprogs:
make install
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
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 to 30 minutes to be formatted. Disable the lazy init feature based on your business requirements.
References
For information about how to initialize a data disk whose size exceeds 2 TiB on a Linux ECS instance, see Initialize a data disk whose size exceeds 2 TiB.
For information about how to initialize a data disk whose size does not exceed 2 TiB on a Windows instance, see Initialize a data disk whose size does not exceed 2 TiB on a Windows instance.
For information about how to resize a disk, see Overview.