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.
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.
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
Connect to the Linux ECS instance.
For more information, see Connect to a Linux instance by using a password or key.
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.
(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.
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:
Run the following command to install Parted:
sudo yum install -y parted
NoteThe
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
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 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.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%
(Conditionally required) If you want to create multiple partitions on the data disk, repeat Step c.
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
NoteIf 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.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 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.
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.
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 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 command output is returned.
Enter n and press the
Enter
key to partition the data disk.The following command output is returned.
The data disk has two types of partitions, as shown in the preceding figure.
p
: primary partition.e
: extended partition.NoteFor information about partitions, see the Partition formats section of 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 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 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 command output is returned.
NoteThe 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.
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, theEnter
key is pressed to use the default value83886079
as the end sector number.The following command output is returned.
NoteThe 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.
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
.NoteIf 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.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.
(Conditionally required) If you want to create multiple partitions on the data disk, repeat Step 1.
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.
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.
Run the following command to install e2fsprogs:
sudo yum install -y e2fsprogs
NoteThe 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.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.NoteIf 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.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.Run the following command to mount the file system.
NoteYou 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 thesudo mkdir -p <New directory>
command to create a directory, and then replace /mnt with the new directory. Sample command:sudo mkdir -p /data
.
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.
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.
Run the following command to back up the etc/fstab file:
sudo cp /etc/fstab /etc/fstab.bak
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 thedf -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 thedf -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 thedf -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.
NoteThe 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.(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 "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 "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"
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.NoteIf 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.
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.
FAQ about disk initialization
What do I do if a GPT partition is created on a disk but is misaligned?
How do I upgrade e2fsprogs on a Linux instance?
How do I disable the lazy init feature on a Linux instance?
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.