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 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 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
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:
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
.
Step 2: Create a partition on the data disk
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.
Run the following command to install Parted:
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.parted /dev/vdb
/dev/vdb
is the device name of the data disk. Replace it 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
. 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
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 of 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 re-read the partition table:
partprobe
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
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 gpt is displayed.
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.
Create an MBR partition.
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
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.
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.
Enter n and press the
Enter
key to partition the data disk.A command output similar to the following one is returned.
The data disk has two types of partitions, as shown in the preceding figure.
p
: primary partition.e
: extended partition.NoteUp 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 Overview.
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.
Partition number
indicates the number of the primary partition, which can range 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.A command output similar to the following one is returned.
Enter a start sector number and press the
Enter
key.First sector
specifies the number of the start sector in the partition. Valid values: 2048 to 83886079. Default value: 2048. In this example, press theEnter
key to use the default value2048
as the start sector number.A command output similar to the following one 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 number of the end sector in the partition. Valid values: 2048 to 83886079. Default value: 83886079. In this example, press theEnter
key to use the default value83886079
as the end sector number.A command output similar to the following one 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.A command output similar to the following one is returned, which indicates the detailed information of the new
/dev/vdb1
partition.NoteIf 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.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.
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.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.
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.
Run the following command to install the e2fsprogs tool:
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
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.ImportantIf 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
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 ext4 file system is created for the
/dev/vdb1
partition of the data disk.
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.
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.
Run the following command to back up the etc/fstab file:
cp /etc/fstab /etc/fstab.bak
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
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. You can mount the partition to an existing directory, or run themkdir
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.
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 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
If you are a common user, you can manually modify /etc/fstab. For more information, see Configure UUIDs in the fstab file to automatically attach data disks.
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.
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.
mount -a
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.
FAQ about initializing disks
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 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 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 a later version of e2fsprogs. In this example, e2fsprogs 1.42.8 is used.
Run the following command to check the current e2fsprogs version:
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.
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:
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, 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.